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;
83 Score_element * commony = 0;
84 for (SCM s = elts; gh_pair_p (s); s = gh_cdr (s))
87 Score_element * e = unsmob_element (gh_car (s));
94 commony= commony->common_refpoint (e, Y_AXIS);
96 if (unsmob_element (e->get_elt_property ("rest")))
104 handle rest-rest and rest-note collisions
107 * decide not to print rest if too crowded?
109 * ignore rests under beams.
112 // no rests to collide
114 return SCM_UNSPECIFIED;
116 // no partners to collide with
117 if (rests.size() + notes.size () < 2)
118 return SCM_UNSPECIFIED;
120 // meisjes met meisjes
123 SCM characteristic = head_characteristic (rests[0]);
125 for (; i < rests.size (); i++)
127 if (!gh_equal_p (head_characteristic (rests[i]), characteristic))
132 If all durations are the same, we'll check if there are more
133 rests than maximum-rest-count.
134 Otherwise (different durations), we'll try to display them all
135 (urg: all 3 of them, currently).
138 SCM s = me->get_elt_property ("maximum-rest-count");
139 if (i == rests.size ()
140 && gh_number_p (s) && gh_scm2int (s) < rests.size ())
142 display_count = gh_scm2int (s);
143 for (; i > display_count; i--)
145 Score_element* r = unsmob_element (rests[i-1]->get_elt_property ("rest"));
148 rests[i-1]->suicide ();
152 display_count = rests.size ();
155 Ugh. Should have minimum dist.
157 Ugh. What do we do if we have three different rests?
160 int dy = display_count > 2 ? 6 : 4; // FIXME Should get dims from table.
161 if (display_count > 1)
163 Direction d0 = Note_column::dir (rests[0]);
164 Direction d1 = Note_column::dir (rests[1]);
176 Note_column::translate_rests (rests[0],d0 *dy);
177 Note_column::translate_rests (rests[1], d1 *dy);
180 // meisjes met jongetjes
183 if (rests.size () > 1)
185 warning (_("too many colliding rests"));
187 if (notes.size () > 1)
189 warning (_("too many notes for rest collision"));
191 Score_element * rcol = rests[0];
193 // try to be opposite of noteheads.
194 Direction dir = - Note_column::dir (notes[0]);
196 Score_element * r = unsmob_element (rcol->get_elt_property ("rest"));
197 Interval restdim = r->extent (r, Y_AXIS); // ??
199 if (restdim.empty_b ())
200 return SCM_UNSPECIFIED;
202 // FIXME: staff ref'd?
203 Real staff_space = 1.0;
205 Real minimum_dist = gh_scm2double (me->get_elt_property ("minimum-distance")) * staff_space;
208 assumption: ref points are the same.
211 for (int i = 0; i < notes.size(); i++)
213 Score_element * stem = Note_column::stem_l (notes[i]);
214 Score_element * head = Stem::first_head (stem);
215 notedim.unite (head->extent (commony, Y_AXIS));
218 Interval inter (notedim);
219 inter.intersect (restdim);
222 minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0;
226 //int stafflines = 5; // rcol->rests[0]->line_count;
227 int stafflines = Staff_symbol_referencer::line_count (me);
229 stafflines = stafflines != 0 ? stafflines : 5;
231 // move discretely by half spaces.
232 int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
234 // move by whole spaces inside the staff.
235 if (discrete_dist < stafflines+1)
236 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
238 Note_column::translate_rests (rcol,dir * discrete_dist);
240 return SCM_UNSPECIFIED;
244 Rest_collision::set_interface (Score_element*me)
246 me->set_extent_callback (SCM_EOL, X_AXIS);
247 me->set_extent_callback (SCM_EOL, Y_AXIS);