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 gi (me);
53 p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
54 p->set_elt_property ("rest-collision", me->self_scm ());
59 Combination of dot-count and duration-log.
62 head_characteristic (Score_element * col)
64 Score_element * s = unsmob_element (col->get_elt_property ("rest"));
69 return gh_cons (s->get_elt_property ("duration-log"),
70 gh_int2scm (Rhythmic_head::dot_count (s)));
74 TODO: fixme, fucks up if called twice on the same set of rests.
77 Rest_collision::do_shift (Score_element *me, SCM elts)
80 ugh. -> score elt type
82 Link_array<Score_element> rests;
83 Link_array<Score_element> notes;
85 for (SCM s = elts; gh_pair_p (s); s = gh_cdr (s))
87 Score_element * e = unsmob_element (gh_car (s));
88 if (e && unsmob_element (e->get_elt_property ("rest")))
96 handle rest-rest and rest-note collisions
99 * decide not to print rest if too crowded?
101 * ignore rests under beams.
104 // no rests to collide
106 return SCM_UNSPECIFIED;
108 // no partners to collide with
109 if (rests.size() + notes.size () < 2)
110 return SCM_UNSPECIFIED;
112 // meisjes met meisjes
115 SCM characteristic = head_characteristic (rests[0]);
117 for (; i < rests.size (); i++)
119 if (!gh_equal_p (head_characteristic (rests[i]), characteristic))
124 If all durations are the same, we'll check if there are more
125 rests than maximum-rest-count.
126 Otherwise (different durations), we'll try to display them all
127 (urg: all 3 of them, currently).
130 SCM s = me->get_elt_property ("maximum-rest-count");
131 if (i == rests.size ()
132 && gh_number_p (s) && gh_scm2int (s) < rests.size ())
134 display_count = gh_scm2int (s);
135 for (; i > display_count; i--)
137 Score_element* r = unsmob_element (rests[i-1]->get_elt_property ("rest"));
140 rests[i-1]->suicide ();
144 display_count = rests.size ();
147 Ugh. Should have minimum dist.
149 Ugh. What do we do if we have three different rests?
152 int dy = display_count > 2 ? 6 : 4; // FIXME Should get dims from table.
153 if (display_count > 1)
155 Direction d0 = Note_column::dir (rests[0]);
156 Direction d1 = Note_column::dir (rests[1]);
168 Note_column::translate_rests (rests[0],d0 *dy);
169 Note_column::translate_rests (rests[1], d1 *dy);
172 // meisjes met jongetjes
175 if (rests.size () > 1)
177 warning (_("too many colliding rests"));
179 if (notes.size () > 1)
181 warning (_("too many notes for rest collision"));
183 Score_element * rcol = rests[0];
185 // try to be opposite of noteheads.
186 Direction dir = - Note_column::dir (notes[0]);
188 Interval restdim = Note_column::rest_dim (rcol);
189 if (restdim.empty_b ())
190 return SCM_UNSPECIFIED;
193 Real staff_space = me->paper_l()->get_var ("staffspace");
196 staff_space = rcol->rests[0]->staff_space ();
198 Real minimum_dist = gh_scm2double (me->get_elt_property ("minimum-distance")) * staff_space;
201 assumption: ref points are the same.
204 for (int i = 0; i < notes.size(); i++)
206 notedim.unite (notes[i]->extent (Y_AXIS));
209 Interval inter (notedim);
210 inter.intersect (restdim);
213 minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0;
217 //int stafflines = 5; // rcol->rests[0]->line_count;
218 int stafflines = Staff_symbol_referencer::line_count (me);
220 stafflines = stafflines != 0 ? stafflines : 5;
222 // move discretely by half spaces.
223 int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
225 // move by whole spaces inside the staff.
226 if (discrete_dist < stafflines+1)
227 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
229 Note_column::translate_rests (rcol,dir * discrete_dist);
231 return SCM_UNSPECIFIED;
235 Rest_collision::set_interface (Score_element*me)
237 me->set_extent_callback (SCM_EOL, X_AXIS);
238 me->set_extent_callback (SCM_EOL, Y_AXIS);