2 accidental-placement.cc -- implement Accidental_placement
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
10 #include "accidental-placement.hh"
14 #include "libc-extension.hh" // isinf
19 #include "note-column.hh"
20 #include "group-interface.hh"
21 #include "note-collision.hh"
22 #include "accidental-interface.hh"
24 MAKE_SCHEME_CALLBACK (Accidental_placement,alignment_callback, 2);
26 Accidental_placement::alignment_callback (SCM s, SCM )
28 Grob * me = unsmob_grob (s);
30 Grob * par = me->get_parent (X_AXIS);
31 if (!to_boolean (par->get_property ("positioning-done")))
33 par->set_property ("positioning-done", SCM_BOOL_T);
34 position_accidentals (par);
37 return scm_int2num (0);
42 Accidental_placement::add_accidental (Grob* me, Grob* a)
44 a->set_parent (me, X_AXIS);
45 a->add_offset_callback (alignment_callback_proc, X_AXIS);
46 SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
48 Music *mcause = unsmob_music (cause);
51 programming_error ("Note head has no music cause!");
55 Pitch *p= unsmob_pitch (mcause->get_property ("pitch"));
57 int n = p->get_notename ();
59 SCM accs = me->get_property ("accidental-grobs");
60 SCM key = scm_int2num (n);
61 SCM entry = scm_assq (key, accs);
62 if (entry == SCM_BOOL_F)
67 entry = scm_cdr (entry);
69 entry = scm_cons (a->self_scm (), entry);
71 accs = scm_assq_set_x (accs, key, entry);
73 me->set_property ("accidental-grobs", accs);
77 Split into break reminders.
80 Accidental_placement::split_accidentals (Grob * accs,
81 Link_array<Grob> *break_reminder,
82 Link_array<Grob> *real_acc)
84 for (SCM acs =accs->get_property ("accidental-grobs"); scm_is_pair (acs);
86 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
88 Grob *a = unsmob_grob (scm_car (s));
90 if (unsmob_grob (a->get_property ("tie")))
91 break_reminder->push (a);
98 Accidentals are special, because they appear and disappear after
102 Accidental_placement::get_relevant_accidental_extent (Grob *me,
106 Link_array<Grob> br, ra;
107 Link_array<Grob> *which = 0;
109 Accidental_placement::split_accidentals (me, &br, &ra);
112 if (dynamic_cast<Item*>(left_object)->break_status_dir () == RIGHT)
118 for (int i = 0; i < which->size (); i++)
120 extent.unite (which->elem (i)->extent (item_col, X_AXIS));
123 if (!extent.is_empty ())
125 Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
134 struct Accidental_placement_entry
136 Array<Skyline_entry> left_skyline_;
137 Array<Skyline_entry> right_skyline_;
138 Interval vertical_extent_;
140 Link_array<Grob> grobs_;
143 Accidental_placement_entry ()
150 static Interval all_accidental_vertical_extent;
151 Real ape_priority (Accidental_placement_entry const * a)
153 return a->vertical_extent_[UP];
158 int ape_compare (Accidental_placement_entry *const &a,
159 Accidental_placement_entry *const &b)
161 return sign (ape_priority (a) - ape_priority (b));
164 int ape_rcompare (Accidental_placement_entry *const &a,
165 Accidental_placement_entry *const &b)
167 return -sign (ape_priority (a) - ape_priority (b));
182 stagger_apes (Link_array<Accidental_placement_entry> *apes)
184 Link_array<Accidental_placement_entry> asc = *apes;
187 asc.sort (&ape_compare);
193 while (i < asc.size ())
195 Accidental_placement_entry * a = 0;
212 This routine computes placements of accidentals. During
213 add_accidental (), accidentals are already grouped by note, so that
214 octaves are placed above each other; they form columns. Then the
215 columns are sorted: the biggest columns go closest to the note.
216 Then the columns are spaced as closely as possible (using skyline
220 TODO: more advanced placement. Typically, the accs should be placed
221 to form a C shape, like this
230 The naturals should be left of the C as well; they should
233 Note that this placement problem looks NP hard, so we just use a
234 simple strategy, not an optimal choice.
239 TODO: there should be more space in the following situation
251 Accidental_placement::position_accidentals (Grob * me)
254 return SCM_UNSPECIFIED;
256 SCM accs = me->get_property ("accidental-grobs");
259 TODO: there is a bug in this code. If two accs are on the same
260 Y-position, they share an Ape, and will be printed in overstrike.
262 Link_array<Accidental_placement_entry> apes;
263 for (SCM s = accs; scm_is_pair (s); s =scm_cdr (s))
265 Accidental_placement_entry *ape = new Accidental_placement_entry;
266 ape->notename_ = scm_to_int (scm_caar (s));
268 for (SCM t = scm_cdar (s); scm_is_pair (t); t =scm_cdr (t))
269 ape->grobs_.push (unsmob_grob (scm_car (t)));
274 Grob *common[] = {me, 0};
277 First we must extract *all* pointers. We can only determine
278 extents if we're sure that we've found the right common refpoint
280 Link_array<Grob> note_cols, heads;
281 for (int i= apes.size (); i--;)
283 Accidental_placement_entry * ape = apes[i];
284 for (int j = ape->grobs_.size (); j--;)
286 Grob * a = ape->grobs_[j];
289 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
293 Grob *head = a->get_parent (Y_AXIS);
295 Grob * col = head->get_parent (X_AXIS);
296 if (Note_column::has_interface (col))
297 note_cols.push (col);
304 This is a little kludgy: to get all notes, we look if there are
307 for (int i = note_cols.size () ; i--;)
309 Grob *c = note_cols[i]->get_parent (X_AXIS);
310 if (Note_collision_interface::has_interface (c))
312 Link_array<Grob> gs =
313 Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
315 note_cols.concat (gs);
319 for (int i = note_cols.size () ; i--;)
321 heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
326 heads.default_sort ();
328 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
331 for (int i= apes.size (); i--;)
333 Accidental_placement_entry * ape = apes[i];
334 ape->left_skyline_ = empty_skyline (LEFT);
335 ape->right_skyline_ = empty_skyline (RIGHT);
337 for (int j = apes[i]->grobs_.size (); j--;)
339 Grob * a = apes[i]->grobs_[j];
341 Array<Box> boxes = Accidental_interface::accurate_boxes (a, common);
343 ape->extents_.concat (boxes);
344 for (int j = boxes.size (); j--;)
346 insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
347 insert_extent_into_skyline (&ape->right_skyline_ , boxes[j], Y_AXIS, RIGHT);
354 for (int i = apes.size (); i--;)
358 for (int j = apes[i]->extents_.size (); j--;)
360 y.unite (apes[i]->extents_[j][Y_AXIS]);
362 apes[i]->vertical_extent_ = y;
365 all_accidental_vertical_extent = total;
366 stagger_apes (&apes);
368 Accidental_placement_entry * head_ape = new Accidental_placement_entry;
369 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
370 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
371 Array<Box> head_extents;
372 for (int i = heads.size (); i--;)
374 Box b (heads[i]->extent (common[X_AXIS] , X_AXIS),
375 heads[i]->extent (common[Y_AXIS], Y_AXIS));
377 insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
380 head_ape-> left_skyline_ = head_skyline;
381 head_ape->offset_ = 0.0;
383 Real padding = robust_scm2double (me->get_property ("padding"),0.2);
385 Array<Skyline_entry> left_skyline = head_ape->left_skyline_;
386 heighten_skyline (&left_skyline,
387 -robust_scm2double (me->get_property ("right-padding"), 0));
389 Add accs entries right-to-left.
391 for (int i= apes.size (); i-- > 0;)
394 -skyline_meshing_distance (apes[i]->right_skyline_, left_skyline);
396 offset = (i < apes.size () - 1) ? apes[i+1]->offset_ : 0.0;
400 apes[i]->offset_ = offset;
402 Array<Skyline_entry> new_left_skyline = apes[i]->left_skyline_;
403 heighten_skyline (&new_left_skyline, apes[i]->offset_);
404 merge_skyline (&new_left_skyline, left_skyline, LEFT);
405 left_skyline = new_left_skyline;
408 for (int i = apes.size (); i--;)
410 Accidental_placement_entry* ape = apes[i];
411 for (int j = ape->grobs_.size (); j--;)
413 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
418 Interval left_extent, right_extent;
419 Accidental_placement_entry *ape = apes[0];
421 for (int i = ape->extents_.size (); i--;)
422 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
425 for (int i = ape->extents_.size (); i--;)
426 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
429 left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
430 Interval width (left_extent[LEFT], right_extent[RIGHT]);
432 SCM scm_width = ly_interval2scm (width);
433 me->set_extent (scm_width, X_AXIS);
435 for (int i = apes.size (); i--;)
438 return SCM_UNSPECIFIED;
441 ADD_INTERFACE (Accidental_placement,
442 "accidental-placement-interface",
443 "Resolve accidental collisions.",
444 "left-padding padding right-padding accidental-grobs positioning-done")