2 rest-collision.cc -- implement Rest_collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include <math.h> // ceil.
12 #include "rest-collision.hh"
13 #include "note-column.hh"
15 #include "rhythmic-head.hh"
16 #include "paper-def.hh"
18 #include "group-interface.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "duration.hh"
22 MAKE_SCHEME_CALLBACK (Rest_collision,force_shift_callback,2);
24 Rest_collision::force_shift_callback (SCM element_smob, SCM axis)
26 Grob *them = unsmob_grob (element_smob);
27 Axis a = (Axis) gh_scm2int (axis);
30 Grob * rc = unsmob_grob (them->get_grob_property ("rest-collision"));
35 Done: destruct pointers, so we do the shift only once.
37 SCM elts = rc->get_grob_property ("elements");
38 rc->set_grob_property ("elements", SCM_EOL);
43 return gh_double2scm (0.0);
47 Rest_collision::add_column (Grob*me,Grob *p)
49 me->add_dependency (p);
50 Pointer_group_interface::add_element (me, "elements", p);
53 only add callback for the rests, since we don't move anything else.
57 p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
58 p->set_grob_property ("rest-collision", me->self_scm ());
63 Combination of dot-count and duration-log.
66 head_characteristic (Grob * col)
68 Grob * s = unsmob_grob (col->get_grob_property ("rest"));
73 return gh_cons (s->get_grob_property ("duration-log"),
74 gh_int2scm (Rhythmic_head::dot_count (s)));
78 TODO: fixme, fucks up if called twice on the same set of rests.
80 TODO: look at horizontal-shift to determine ordering between rests
81 for more than two voices.
85 Rest_collision::do_shift (Grob *me, SCM elts)
88 ugh. -> score elt type
90 Link_array<Grob> rests;
91 Link_array<Grob> notes;
93 for (SCM s = elts; gh_pair_p (s); s = ly_cdr (s))
96 Grob * e = unsmob_grob (ly_car (s));
103 commony= commony->common_refpoint (e, Y_AXIS);
105 if (unsmob_grob (e->get_grob_property ("rest")))
113 handle rest-rest and rest-note collisions
116 * decide not to print rest if too crowded?
118 * ignore rests under beams.
121 // no rests to collide
123 return SCM_UNSPECIFIED;
125 // no partners to collide with
126 if (rests.size () + notes.size () < 2)
127 return SCM_UNSPECIFIED;
129 // meisjes met meisjes
132 SCM characteristic = head_characteristic (rests[0]);
134 for (; i < rests.size (); i++)
136 if (!gh_equal_p (head_characteristic (rests[i]), characteristic))
141 If all durations are the same, we'll check if there are more
142 rests than maximum-rest-count.
143 Otherwise (different durations), we'll try to display them all
144 (urg: all 3 of them, currently).
147 SCM s = me->get_grob_property ("maximum-rest-count");
148 if (i == rests.size ()
149 && gh_number_p (s) && gh_scm2int (s) < rests.size ())
151 display_count = gh_scm2int (s);
152 for (; i > display_count; i--)
154 Grob* r = unsmob_grob (rests[i-1]->get_grob_property ("rest"));
157 rests[i-1]->suicide ();
161 display_count = rests.size ();
164 Ugh. Should have minimum dist.
166 Ugh. What do we do if we have three different rests?
169 int dy = display_count > 2 ? 6 : 4; // FIXME Should get dims from table.
170 if (display_count > 1)
172 Direction d0 = Note_column::dir (rests[0]);
173 Direction d1 = Note_column::dir (rests[1]);
185 Note_column::translate_rests (rests[0],d0 *dy);
186 Note_column::translate_rests (rests[1], d1 *dy);
189 // meisjes met jongetjes
192 if (rests.size () > 1)
194 warning (_ ("too many colliding rests"));
196 if (notes.size () > 1)
198 warning (_ ("too many notes for rest collision"));
200 Grob * rcol = rests[0];
202 // try to be opposite of noteheads.
203 Direction dir = - Note_column::dir (notes[0]);
205 Grob * r = unsmob_grob (rcol->get_grob_property ("rest"));
206 Interval restdim = r->extent (r, Y_AXIS); // ??
208 if (restdim.empty_b ())
209 return SCM_UNSPECIFIED;
211 // FIXME: staff ref'd?
212 Real staff_space = 1.0;
214 Real minimum_dist = gh_scm2double (me->get_grob_property ("minimum-distance")) * staff_space;
217 assumption: ref points are the same.
220 for (int i = 0; i < notes.size (); i++)
222 Grob * stem = Note_column::stem_l (notes[i]);
223 Grob * head = Stem::first_head (stem);
224 notedim.unite (head->extent (commony, Y_AXIS));
227 Interval inter (notedim);
228 inter.intersect (restdim);
231 minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0;
235 //int stafflines = 5; // rcol->rests[0]->line_count;
236 int stafflines = Staff_symbol_referencer::line_count (me);
238 stafflines = stafflines != 0 ? stafflines : 5;
240 // move discretely by half spaces.
241 int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
243 // move by whole spaces inside the staff.
244 if (discrete_dist < stafflines+1)
245 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
247 Note_column::translate_rests (rcol,dir * discrete_dist);
249 return SCM_UNSPECIFIED;
253 Rest_collision::set_interface (Grob*me)
255 me->set_extent_callback (SCM_EOL, X_AXIS);
256 me->set_extent_callback (SCM_EOL, Y_AXIS);