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