2 rest-collision.cc -- implement Rest_collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 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 "output-def.hh"
18 #include "group-interface.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "duration.hh"
21 #include "directional-element-interface.hh"
23 MAKE_SCHEME_CALLBACK (Rest_collision
,force_shift_callback
,2);
25 Rest_collision::force_shift_callback (SCM element_smob
, SCM axis
)
27 Grob
*them
= unsmob_grob (element_smob
);
28 Axis a
= (Axis
) ly_scm2int (axis
);
31 Grob
* rc
= unsmob_grob (them
->get_property ("rest-collision"));
33 if (rc
&& !to_boolean (rc
->get_property ("positioning-done")))
35 rc
->set_property ("positioning-done", SCM_BOOL_T
);
40 return scm_make_real (0.0);
46 Rest_collision::add_column (Grob
*me
,Grob
*p
)
48 me
->add_dependency (p
);
49 Pointer_group_interface::add_grob (me
, ly_symbol2scm ("elements"), p
);
52 only add callback for the rests, since we don't move anything
57 p
->add_offset_callback (Rest_collision::force_shift_callback_proc
, Y_AXIS
);
58 p
->set_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_property ("rest"));
73 return scm_cons (s
->get_property ("duration-log"),
74 scm_int2num (Rhythmic_head::dot_count (s
)));
78 TODO: look at horizontal-shift to determine ordering between rests
79 for more than two voices.
83 Rest_collision::do_shift (Grob
*me
)
85 SCM elts
= me
->get_property ("elements");
87 Link_array
<Grob
> rests
;
88 Link_array
<Grob
> notes
;
90 for (SCM s
= elts
; ly_c_pair_p (s
); s
= ly_cdr (s
))
92 Grob
* e
= unsmob_grob (ly_car (s
));
93 if (unsmob_grob (e
->get_property ("rest")))
96 Ignore rests under beam.
98 Grob
* st
= unsmob_grob (e
->get_property ("stem"));
99 if (st
&& unsmob_grob (st
->get_property ("beam")))
110 handle rest-rest and rest-note collisions
113 * decide not to print rest if too crowded?
117 no partners to collide with
119 if (rests
.size () + notes
.size () < 2)
120 return SCM_UNSPECIFIED
;
123 Real staff_space
= Staff_symbol_referencer::staff_space (me
);
131 This is incomplete: in case of an uneven number of rests, the
132 center one should be centered on the staff.
134 Drul_array
< Link_array
<Grob
> > ordered_rests
;
135 for (int i
= 0; i
< rests
.size (); i
++)
137 Grob
* r
= Note_column::get_rest (rests
[i
]);
139 Direction d
= get_grob_direction (r
);
142 ordered_rests
[d
].push (rests
[i
]);
145 rests
[d
]->warning (_("rest direction not set. Cannot resolve collision."));
150 ordered_rests
[d
].sort (Note_column::shift_compare
);
151 } while (flip (&d
) != LEFT
);
154 if (ordered_rests
[d
].size () < 1)
156 if (ordered_rests
[-d
].size() > 1)
157 ordered_rests
[-d
][0]->warning (_("Too many colliding rests."));
159 return SCM_UNSPECIFIED
;
161 } while (flip (&d
) != LEFT
);
163 Grob
*common
= common_refpoint_of_array (ordered_rests
[DOWN
], me
, Y_AXIS
);
164 common
= common_refpoint_of_array (ordered_rests
[UP
], common
, Y_AXIS
);
167 (ordered_rests
[DOWN
].top ()->extent (common
, Y_AXIS
)[UP
]
168 - ordered_rests
[UP
].top ()->extent (common
, Y_AXIS
)[DOWN
]) /staff_space
;
172 int amount_down
= (int) ceil (diff
/ 2);
174 Note_column::translate_rests (ordered_rests
[DOWN
].top (),
177 Note_column::translate_rests (ordered_rests
[UP
].top (),
178 2 * int (ceil (diff
)));
182 for (int i
= ordered_rests
[d
].size () -1; i
-- > 0;)
184 Real last_y
= ordered_rests
[d
][i
+1]->extent (common
, Y_AXIS
)[d
];
185 Real y
= ordered_rests
[d
][i
]->extent (common
, Y_AXIS
)[-d
];
187 Real diff
= d
* ((last_y
- y
) /staff_space
);
189 Note_column::translate_rests (ordered_rests
[d
][i
],d
* (int) ceil (diff
) * 2);
191 } while (flip (&d
) != LEFT
);
198 if (rests
.size () > 1)
200 warning (_ ("too many colliding rests"));
203 Direction dir
= CENTER
;
205 for (int i
= rests
.size (); !rcol
&& i
--;)
206 if (Note_column::dir (rests
[i
]))
208 dir
= Note_column::dir (rests
[i
]);
213 return SCM_UNSPECIFIED
;
215 Grob
*common
= common_refpoint_of_array (notes
, rcol
, Y_AXIS
);
217 Interval restdim
= rcol
->extent (common
, Y_AXIS
);
218 if (restdim
.is_empty ())
219 return SCM_UNSPECIFIED
;
221 Real staff_space
= Staff_symbol_referencer::staff_space (rcol
);
222 Real minimum_dist
= robust_scm2double (me
->get_property ("minimum-distance"), 1.0) * staff_space
;
225 for (int i
= 0; i
< notes
.size (); i
++)
227 notedim
.unite (notes
[i
]->extent (common
, Y_AXIS
));
231 minimum_dist
+ dir
* (notedim
[dir
] - restdim
[-dir
]) >? 0;
233 int stafflines
= Staff_symbol_referencer::line_count (me
);
236 programming_error ("No staff line count ? ");
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 ADD_INTERFACE (Rest_collision
,"rest-collision-interface",
254 "Move around ordinary rests (not multi-measure-rests) to avoid "
256 "minimum-distance positioning-done elements");