Docs: LM 4.4.3: Clarify overriding vertical positioning of spanners
[lilypond/mpolesky.git] / lily / dot-configuration.cc
blob559f83be2e1a2ae9990e4ac636dba5ae1d8316c7
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--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 */
11 #include <cstdio>
12 #include "dot-configuration.hh"
13 #include "dot-formatting-problem.hh"
14 #include "staff-symbol-referencer.hh"
17 int
18 Dot_configuration::badness () const
20 int t = 0;
21 for (Dot_configuration::const_iterator i (begin ());
22 i != end (); i++)
24 int p = i->first;
25 int demerit = sqr (p - i->second.pos_) * 2;
27 int dot_move_dir = sign (p - i->second.pos_);
28 if (i->second.extremal_head_)
30 if (i->second.dir_
31 && dot_move_dir != i->second.dir_)
32 demerit += 3;
33 else if (dot_move_dir != UP)
34 demerit += 2;
36 else if (dot_move_dir != UP)
37 demerit += 1;
39 t += demerit;
42 return t;
45 void
46 Dot_configuration::print () const
48 printf ("dotconf { ");
49 for (Dot_configuration::const_iterator i (begin ());
50 i != end (); i++)
51 printf ("%d, ", i->first);
52 printf ("}\n");
56 Shift K and following (preceding) entries up (down) as necessary to
57 prevent staffline collisions if D is up (down).
59 If K is in CFG, then do nothing.
62 Dot_configuration
63 Dot_configuration::shifted (int k, Direction d) const
65 Dot_configuration new_cfg (*problem_);
66 int offset = 0;
68 if (d > 0)
70 for (Dot_configuration::const_iterator i (begin ());
71 i != end (); i++)
73 int p = i->first;
74 if (p == k)
76 if (Staff_symbol_referencer::on_line (i->second.dot_, p))
77 p += d;
78 else
79 p += 2* d;
81 offset = 2*d;
83 new_cfg[p] = i->second;
85 else
87 if (new_cfg.find (p) == new_cfg.end ())
88 offset = 0;
89 new_cfg[p + offset] = i->second;
93 else
95 Dot_configuration::const_iterator i (end ());
98 i--;
100 int p = i->first;
101 if (p == k)
103 if (Staff_symbol_referencer::on_line (i->second.dot_, p))
104 p += d;
105 else
106 p += 2* d;
108 offset = 2*d;
110 new_cfg[p] = i->second;
112 else
114 if (new_cfg.find (p) == new_cfg.end ())
115 offset = 0;
117 new_cfg[p + offset] = i->second;
120 while (i != begin ());
123 return new_cfg;
127 Remove the collision in CFG either by shifting up or down, whichever
128 is best.
130 void
131 Dot_configuration::remove_collision (int p)
133 bool collide = find (p) != end ();
135 if (collide)
137 Dot_configuration cfg_up = shifted (p, UP);
138 Dot_configuration cfg_down = shifted (p, DOWN);
140 int b_up = cfg_up.badness ();
141 int b_down = cfg_down.badness ();
143 *this = (b_up < b_down) ? cfg_up : cfg_down;
147 Dot_configuration::Dot_configuration (Dot_formatting_problem const &problem)
149 problem_ = &problem;
152 Real
153 Dot_configuration::x_offset () const
155 Real off = 0.0;
156 for (Dot_configuration::const_iterator i (begin ());
157 i != end (); i++)
158 off = max (off, problem_->head_skyline_.height ((*i).first));
160 return off;