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