]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
* lily/rest.cc (y_offset_callback): merge function of 3 callbacks.
[lilypond.git] / lily / rest-collision.cc
1 /*
2   rest-collision.cc -- implement Rest_collision
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "rest-collision.hh"
10
11 #include <cmath>                // ceil.
12 using namespace std;
13
14 #include "warn.hh"
15 #include "note-column.hh"
16 #include "stem.hh"
17 #include "rhythmic-head.hh"
18 #include "output-def.hh"
19 #include "rest.hh"
20 #include "pointer-group-interface.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "duration.hh"
23 #include "directional-element-interface.hh"
24
25 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback, 1);
26 SCM
27 Rest_collision::force_shift_callback (SCM smob)
28 {
29   Grob *them = unsmob_grob (smob);
30   if (Note_column::has_rests (them))
31     {
32       Grob *collision = unsmob_grob (them->get_object ("rest-collision"));
33
34       if (collision)
35         {
36           (void) collision->get_property ("positioning-done");
37         }
38     }
39   return scm_from_double (0.0);
40 }
41
42 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback_rest, 1);
43 SCM
44 Rest_collision::force_shift_callback_rest (SCM rest)
45 {
46   Grob *rest_grob = unsmob_grob (rest);
47   Grob *parent = rest_grob->get_parent (X_AXIS);
48   if (Note_column::has_interface (parent))
49     return force_shift_callback (parent->self_scm ());
50   else
51     return scm_from_double (0.0);
52 }
53
54 void
55 Rest_collision::add_column (Grob *me, Grob *p)
56 {
57   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
58
59   /*
60     only add callback for the rests, since we don't move anything
61     else.
62
63     (not?)
64   */
65   add_offset_callback (p, Rest_collision::force_shift_callback_proc, Y_AXIS);
66   p->set_object ("rest-collision", me->self_scm ());
67
68   Grob *rest = unsmob_grob (p->get_object ("rest"));
69   if (rest)
70     {
71       rest->set_property ("Y-offset" ,
72                           Rest_collision::force_shift_callback_rest_proc);
73     }
74 }
75
76 /*
77   TODO: look at horizontal-shift to determine ordering between rests
78   for more than two voices.
79 */
80 MAKE_SCHEME_CALLBACK(Rest_collision, calc_positioning_done, 1);
81 SCM
82 Rest_collision::calc_positioning_done (SCM smob)
83 {
84   Grob *me = unsmob_grob (smob);
85   extract_grob_set (me, "elements", elts);
86
87   Link_array<Grob> rests;
88   Link_array<Grob> notes;
89
90   for (int i = 0; i < elts.size (); i++)
91     {
92       Grob *e = elts[i];
93       if (unsmob_grob (e->get_object ("rest")))
94         {
95           /*
96             Ignore rests under beam.
97           */
98           Grob *st = unsmob_grob (e->get_object ("stem"));
99           if (st && unsmob_grob (st->get_object ("beam")))
100             continue;
101
102           rests.push (e);
103         }
104       else
105         notes.push (e);
106     }
107
108   /*
109     handle rest-rest and rest-note collisions
110
111     [todo]
112     * decide not to print rest if too crowded?
113     */
114
115   /*
116     no partners to collide with
117   */
118   if (rests.size () + notes.size () < 2)
119     return SCM_UNSPECIFIED;
120
121   Real staff_space = Staff_symbol_referencer::staff_space (me);
122   /*
123     only rests
124   */
125   if (!notes.size ())
126     {
127
128       /*
129         This is incomplete: in case of an uneven number of rests, the
130         center one should be centered on the staff.
131       */
132       Drul_array<Link_array<Grob> > ordered_rests;
133       for (int i = 0; i < rests.size (); i++)
134         {
135           Grob *r = Note_column::get_rest (rests[i]);
136
137           Direction d = get_grob_direction (r);
138           if (d)
139             ordered_rests[d].push (rests[i]);
140           else
141             rests[d]->warning (_ ("can't resolve rest collision: rest direction not set"));
142         }
143
144       Direction d = LEFT;
145       do
146         ordered_rests[d].sort (Note_column::shift_compare);
147       while (flip (&d) != LEFT)
148         ;
149
150       do
151         {
152           if (ordered_rests[d].size () < 1)
153             {
154               if (ordered_rests[-d].size () > 1)
155                 ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
156
157               return SCM_UNSPECIFIED;
158             }
159         }
160       while (flip (&d) != LEFT);
161
162       Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
163       common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
164
165       Real diff
166         = (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP]
167            - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) / staff_space;
168
169       if (diff > 0)
170         {
171           int amount_down = (int) ceil (diff / 2);
172           diff -= amount_down;
173           Note_column::translate_rests (ordered_rests[DOWN].top (),
174                                         -2 * amount_down);
175           if (diff > 0)
176             Note_column::translate_rests (ordered_rests[UP].top (),
177                                           2 * int (ceil (diff)));
178         }
179
180       do
181         {
182           for (int i = ordered_rests[d].size () -1; i-- > 0;)
183             {
184               Real last_y = ordered_rests[d][i + 1]->extent (common, Y_AXIS)[d];
185               Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
186
187               Real diff = d * ((last_y - y) / staff_space);
188               if (diff > 0)
189                 Note_column::translate_rests (ordered_rests[d][i], d * (int) ceil (diff) * 2);
190             }
191         }
192       while (flip (&d) != LEFT);
193     }
194   else
195     {
196       /*
197         Rests and notes.
198       */
199       if (rests.size () > 1)
200         warning (_ ("too many colliding rests"));
201       Grob *rcol = 0;
202       Direction dir = CENTER;
203
204       for (int i = rests.size (); !rcol && i--;)
205         if (Note_column::dir (rests[i]))
206           {
207             dir = Note_column::dir (rests[i]);
208             rcol = rests[i];
209           }
210
211       if (!rcol)
212         return SCM_UNSPECIFIED;
213
214       Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
215
216       Interval restdim = rcol->extent (common, Y_AXIS);
217       if (restdim.is_empty ())
218         return SCM_UNSPECIFIED;
219
220       Real staff_space = Staff_symbol_referencer::staff_space (rcol);
221       Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
222
223       Interval notedim;
224       for (int i = 0; i < notes.size (); i++)
225         notedim.unite (notes[i]->extent (common, Y_AXIS));
226
227       Real dist
228         = minimum_dist + dir * max (notedim[dir] - restdim[-dir], 0.0);
229
230       int stafflines = Staff_symbol_referencer::line_count (me);
231       if (!stafflines)
232         {
233           programming_error ("no staff line count");
234           stafflines = 5;
235         }
236
237       // move discretely by half spaces.
238       int discrete_dist = int (ceil (dist / (0.5 * staff_space)));
239
240       // move by whole spaces inside the staff.
241       if (discrete_dist < stafflines + 1)
242         discrete_dist = int (ceil (discrete_dist / 2.0) * 2.0);
243
244       Note_column::translate_rests (rcol, dir * discrete_dist);
245     }
246   return SCM_UNSPECIFIED;
247 }
248
249 ADD_INTERFACE (Rest_collision, "rest-collision-interface",
250                "Move around ordinary rests (not multi-measure-rests) to avoid "
251                "conflicts.",
252
253                /* properties */
254                "minimum-distance "
255                "positioning-done "
256                "elements");
257