2 rest-collision.cc -- implement Rest_collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 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 Score_element *them = unsmob_element (element_smob);
27 Axis a = (Axis) gh_scm2int (axis);
30 Score_element * rc = unsmob_element (them->get_elt_property ("rest-collision"));
35 Done: destruct pointers, so we do the shift only once.
37 SCM elts = rc->get_elt_property ("elements");
38 rc->set_elt_property ("elements", SCM_EOL);
43 return gh_double2scm (0.0);
47 Rest_collision::add_column (Score_element*me,Score_element *p)
49 me->add_dependency (p);
50 Pointer_group_interface::add_element (me, "elements", p);
52 p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
53 p->set_elt_property ("rest-collision", me->self_scm ());
58 Combination of dot-count and duration-log.
61 head_characteristic (Score_element * col)
63 Score_element * s = unsmob_element (col->get_elt_property ("rest"));
68 return gh_cons (s->get_elt_property ("duration-log"),
69 gh_int2scm (Rhythmic_head::dot_count (s)));
73 TODO: fixme, fucks up if called twice on the same set of rests.
76 Rest_collision::do_shift (Score_element *me, SCM elts)
79 ugh. -> score elt type
81 Link_array<Score_element> rests;
82 Link_array<Score_element> notes;
84 for (SCM s = elts; gh_pair_p (s); s = gh_cdr (s))
86 Score_element * e = unsmob_element (gh_car (s));
87 if (e && unsmob_element (e->get_elt_property ("rest")))
95 handle rest-rest and rest-note collisions
98 * decide not to print rest if too crowded?
100 * ignore rests under beams.
103 // no rests to collide
105 return SCM_UNSPECIFIED;
107 // no partners to collide with
108 if (rests.size() + notes.size () < 2)
109 return SCM_UNSPECIFIED;
111 // meisjes met meisjes
114 SCM characteristic = head_characteristic (rests[0]);
116 for (; i < rests.size (); i++)
118 if (!gh_equal_p (head_characteristic (rests[i]), characteristic))
123 If all durations are the same, we'll check if there are more
124 rests than maximum-rest-count.
125 Otherwise (different durations), we'll try to display them all
126 (urg: all 3 of them, currently).
129 SCM s = me->get_elt_property ("maximum-rest-count");
130 if (i == rests.size ()
131 && gh_number_p (s) && gh_scm2int (s) < rests.size ())
133 display_count = gh_scm2int (s);
134 for (; i > display_count; i--)
136 Score_element* r = unsmob_element (rests[i-1]->get_elt_property ("rest"));
139 rests[i-1]->suicide ();
143 display_count = rests.size ();
146 Ugh. Should have minimum dist.
148 Ugh. What do we do if we have three different rests?
151 int dy = display_count > 2 ? 6 : 4; // FIXME Should get dims from table.
152 if (display_count > 1)
154 Direction d0 = Note_column::dir (rests[0]);
155 Direction d1 = Note_column::dir (rests[1]);
167 Note_column::translate_rests (rests[0],d0 *dy);
168 Note_column::translate_rests (rests[1], d1 *dy);
171 // meisjes met jongetjes
174 if (rests.size () > 1)
176 warning (_("too many colliding rests"));
178 if (notes.size () > 1)
180 warning (_("too many notes for rest collision"));
182 Score_element * rcol = rests[0];
184 // try to be opposite of noteheads.
185 Direction dir = - Note_column::dir (notes[0]);
187 Interval restdim = Note_column::rest_dim (rcol);
188 if (restdim.empty_b ())
189 return SCM_UNSPECIFIED;
192 Real staff_space = me->paper_l()->get_var ("staffspace");
195 staff_space = rcol->rests[0]->staff_space ();
197 Real minimum_dist = gh_scm2double (me->get_elt_property ("minimum-distance")) * staff_space;
200 assumption: ref points are the same.
203 for (int i = 0; i < notes.size(); i++)
205 notedim.unite (notes[i]->extent (notes[i],Y_AXIS)); // refp?
208 Interval inter (notedim);
209 inter.intersect (restdim);
212 minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0;
216 //int stafflines = 5; // rcol->rests[0]->line_count;
217 int stafflines = Staff_symbol_referencer::line_count (me);
219 stafflines = stafflines != 0 ? stafflines : 5;
221 // move discretely by half spaces.
222 int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
224 // move by whole spaces inside the staff.
225 if (discrete_dist < stafflines+1)
226 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
228 Note_column::translate_rests (rcol,dir * discrete_dist);
230 return SCM_UNSPECIFIED;
234 Rest_collision::set_interface (Score_element*me)
236 me->set_extent_callback (SCM_EOL, X_AXIS);
237 me->set_extent_callback (SCM_EOL, Y_AXIS);