]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-configuration.cc
584273df8b40ccf93ba85d44d9903bafc0894a1f
[lilypond.git] / lily / dot-configuration.cc
1 /*
2   dot-implement.cc -- declare Dot_configuration
3
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.
7
8   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 */
10
11 #include "dot-configuration.hh"
12 #include "dot-formatting-problem.hh"
13 #include "staff-symbol-referencer.hh"
14
15
16 int
17 Dot_configuration::badness () const
18 {
19   int t = 0;
20   for (Dot_configuration::const_iterator i (begin ());
21        i != end (); i++)
22     {
23       int p = i->first;
24       int demerit = sqr (p - i->second.pos_) * 2;
25
26       int dot_move_dir = sign (p - i->second.pos_);
27       if (i->second.extremal_head_)
28         {
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;
34         }
35       else if (dot_move_dir != UP)
36         demerit += 1;
37
38       t += demerit;
39     }
40
41   return t;
42 }
43
44 void
45 Dot_configuration::print () const
46 {
47   printf ("dotconf { ");
48   for (Dot_configuration::const_iterator i (begin ());
49        i != end (); i++)
50     printf ("%d, ", i->first);
51   printf ("}\n");
52 }
53
54 /*
55   Shift K and following (preceding) entries up (down) as necessary to
56   prevent staffline collisions if D is up (down).
57
58   If K is in CFG, then do nothing.
59 */
60
61 Dot_configuration
62 Dot_configuration::shifted (int k, Direction d) const
63 {
64   Dot_configuration new_cfg (*problem_);
65   int offset = 0;
66
67   if (d > 0)
68     {
69       for (Dot_configuration::const_iterator i (begin ());
70            i != end (); i++)
71         {
72           int p = i->first;
73           if (p == k)
74             {
75               if (Staff_symbol_referencer::on_line (i->second.dot_, p))
76                 p += d;
77               else
78                 p += 2* d;
79
80               offset = 2*d;
81
82               new_cfg[p] = i->second;
83             }
84           else
85             {
86               if (new_cfg.find (p) == new_cfg.end ())
87                 offset = 0;
88               new_cfg[p + offset] = i->second;
89             }
90         }
91     }
92   else
93     {
94       Dot_configuration::const_iterator i (end ());
95       do
96         {
97           i--;
98
99           int p = i->first;
100           if (p == k)
101             {
102               if (Staff_symbol_referencer::on_line (i->second.dot_, p))
103                 p += d;
104               else
105                 p += 2* d;
106
107               offset = 2*d;
108
109               new_cfg[p] = i->second;
110             }
111           else
112             {
113               if (new_cfg.find (p) == new_cfg.end ())
114                 offset = 0;
115
116               new_cfg[p + offset] = i->second;
117             }
118         }
119       while (i != begin ());
120     }
121
122   return new_cfg;
123 }
124
125 /*
126   Remove the collision in CFG either by shifting up or down, whichever
127   is best.
128 */
129 void
130 Dot_configuration::remove_collision (int p)
131 {
132   bool collide = find (p) != end ();
133
134   if (collide)
135     {
136       Dot_configuration cfg_up = shifted (p, UP);
137       Dot_configuration cfg_down = shifted (p, DOWN);
138
139       int b_up = cfg_up.badness ();
140       int b_down = cfg_down.badness ();
141
142       *this = (b_up < b_down) ? cfg_up : cfg_down;
143     }
144 }
145
146 Dot_configuration::Dot_configuration (Dot_formatting_problem const &problem)
147 {
148   problem_ = &problem;
149 }
150
151 Real
152 Dot_configuration::x_offset () const
153 {
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));
158
159   return off;
160 }