Allow for nested contexts of any depth.
[lilypond.git] / lily / dot-configuration.cc
blobc4cf852d1a7a154d254b4d315745eeafbf5aae15
1 /*
2 dot-implement.cc -- declare Dot_configuration
4 Source file of the GNU LilyPond music typesetter. Distributed under
5 terms of the GNU General Public License. LilyPond comes with NO
6 WARRANTY.
8 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 */
11 #include "dot-configuration.hh"
12 #include "dot-formatting-problem.hh"
13 #include "staff-symbol-referencer.hh"
16 int
17 Dot_configuration::badness () const
19 int t = 0;
20 for (Dot_configuration::const_iterator i (begin ());
21 i != end (); i++)
23 int p = i->first;
24 int demerit = sqr (p - i->second.pos_) * 2;
26 int dot_move_dir = sign (p - i->second.pos_);
27 if (i->second.extremal_head_)
29 if (i->second.dir_
30 && dot_move_dir != i->second.dir_)
31 demerit += 3;
32 else if (dot_move_dir != UP)
33 demerit += 2;
35 else if (dot_move_dir != UP)
36 demerit += 1;
38 t += demerit;
41 return t;
44 void
45 Dot_configuration::print () const
47 printf ("dotconf { ");
48 for (Dot_configuration::const_iterator i (begin ());
49 i != end (); i++)
50 printf ("%d, ", i->first);
51 printf ("} \n");
55 Shift K and following (preceding) entries up (down) as necessary to
56 prevent staffline collisions if D is up (down).
58 If K is in CFG, then do nothing.
61 Dot_configuration
62 Dot_configuration::shifted (int k, Direction d) const
64 Dot_configuration new_cfg (*problem_);
65 int offset = 0;
67 if (d > 0)
69 for (Dot_configuration::const_iterator i (begin ());
70 i != end (); i++)
72 int p = i->first;
73 if (p == k)
75 if (Staff_symbol_referencer::on_line (i->second.dot_, p))
76 p += d;
77 else
78 p += 2* d;
80 offset = 2*d;
82 new_cfg[p] = i->second;
84 else
86 if (new_cfg.find (p) == new_cfg.end ())
87 offset = 0;
88 new_cfg[p + offset] = i->second;
92 else
94 Dot_configuration::const_iterator i (end ());
97 i--;
99 int p = i->first;
100 if (p == k)
102 if (Staff_symbol_referencer::on_line (i->second.dot_, p))
103 p += d;
104 else
105 p += 2* d;
107 offset = 2*d;
109 new_cfg[p] = i->second;
111 else
113 if (new_cfg.find (p) == new_cfg.end ())
114 offset = 0;
116 new_cfg[p + offset] = i->second;
119 while (i != begin ());
122 return new_cfg;
126 Remove the collision in CFG either by shifting up or down, whichever
127 is best.
129 void
130 Dot_configuration::remove_collision (int p)
132 bool collide = find (p) != end ();
134 if (collide)
136 Dot_configuration cfg_up = shifted (p, UP);
137 Dot_configuration cfg_down = shifted (p, DOWN);
139 int b_up = cfg_up.badness ();
140 int b_down = cfg_down.badness ();
142 *this = (b_up < b_down) ? cfg_up : cfg_down;
146 Dot_configuration::Dot_configuration (Dot_formatting_problem const &problem)
148 problem_ = &problem;
151 Real
152 Dot_configuration::x_offset () const
154 Real off = 0.0;
155 for (Dot_configuration::const_iterator i (begin ());
156 i != end (); i++)
157 off = max (off, problem_->head_skyline_.height ((*i).first));
159 return off;