2 accidental-placement.cc -- implement Accidental_placement
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "accidental-placement.hh"
12 #include "libc-extension.hh" // isinf
17 #include "note-column.hh"
18 #include "group-interface.hh"
19 #include "note-collision.hh"
20 #include "accidental-interface.hh"
22 MAKE_SCHEME_CALLBACK (Accidental_placement, alignment_callback, 2);
24 Accidental_placement::alignment_callback (SCM s, SCM)
26 Grob *me = unsmob_grob (s);
28 Grob *par = me->get_parent (X_AXIS);
29 if (!to_boolean (par->get_property ("positioning-done")))
31 par->set_property ("positioning-done", SCM_BOOL_T);
32 position_accidentals (par);
35 return scm_int2num (0);
39 Accidental_placement::add_accidental (Grob *me, Grob *a)
41 a->set_parent (me, X_AXIS);
42 a->add_offset_callback (alignment_callback_proc, X_AXIS);
43 SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
45 Music *mcause = unsmob_music (cause);
48 programming_error ("note head has no music cause");
52 Pitch *p = unsmob_pitch (mcause->get_property ("pitch"));
54 int n = p->get_notename ();
56 SCM accs = me->get_property ("accidental-grobs");
57 SCM key = scm_int2num (n);
58 SCM entry = scm_assq (key, accs);
59 if (entry == SCM_BOOL_F)
64 entry = scm_cdr (entry);
66 entry = scm_cons (a->self_scm (), entry);
68 accs = scm_assq_set_x (accs, key, entry);
70 me->set_property ("accidental-grobs", accs);
74 Split into break reminders.
77 Accidental_placement::split_accidentals (Grob *accs,
78 Link_array<Grob> *break_reminder,
79 Link_array<Grob> *real_acc)
81 for (SCM acs = accs->get_property ("accidental-grobs"); scm_is_pair (acs);
83 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
85 Grob *a = unsmob_grob (scm_car (s));
87 if (unsmob_grob (a->get_property ("tie")))
88 break_reminder->push (a);
95 Accidentals are special, because they appear and disappear after
99 Accidental_placement::get_relevant_accidental_extent (Grob *me,
103 Link_array<Grob> br, ra;
104 Link_array<Grob> *which = 0;
106 Accidental_placement::split_accidentals (me, &br, &ra);
109 if (dynamic_cast<Item *> (left_object)->break_status_dir () == RIGHT)
115 for (int i = 0; i < which->size (); i++)
117 extent.unite (which->elem (i)->extent (item_col, X_AXIS));
120 if (!extent.is_empty ())
122 Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
129 struct Accidental_placement_entry
131 Array<Skyline_entry> left_skyline_;
132 Array<Skyline_entry> right_skyline_;
133 Interval vertical_extent_;
135 Link_array<Grob> grobs_;
138 Accidental_placement_entry ()
145 static Interval all_accidental_vertical_extent;
146 Real ape_priority (Accidental_placement_entry const *a)
148 return a->vertical_extent_[UP];
151 int ape_compare (Accidental_placement_entry *const &a,
152 Accidental_placement_entry *const &b)
154 return sign (ape_priority (a) - ape_priority (b));
157 int ape_rcompare (Accidental_placement_entry *const &a,
158 Accidental_placement_entry *const &b)
160 return -sign (ape_priority (a) - ape_priority (b));
172 stagger_apes (Link_array<Accidental_placement_entry> *apes)
174 Link_array<Accidental_placement_entry> asc = *apes;
176 asc.sort (&ape_compare);
182 while (i < asc.size ())
184 Accidental_placement_entry *a = 0;
198 This routine computes placements of accidentals. During
199 add_accidental (), accidentals are already grouped by note, so that
200 octaves are placed above each other; they form columns. Then the
201 columns are sorted: the biggest columns go closest to the note.
202 Then the columns are spaced as closely as possible (using skyline
206 TODO: more advanced placement. Typically, the accs should be placed
207 to form a C shape, like this
216 The naturals should be left of the C as well; they should
219 Note that this placement problem looks NP hard, so we just use a
220 simple strategy, not an optimal choice.
224 TODO: there should be more space in the following situation
235 Accidental_placement::position_accidentals (Grob *me)
238 return SCM_UNSPECIFIED;
240 SCM accs = me->get_property ("accidental-grobs");
241 if (!scm_is_pair (accs))
242 return SCM_UNSPECIFIED;
245 TODO: there is a bug in this code. If two accs are on the same
246 Y-position, they share an Ape, and will be printed in overstrike.
248 Link_array<Accidental_placement_entry> apes;
249 for (SCM s = accs; scm_is_pair (s); s = scm_cdr (s))
251 Accidental_placement_entry *ape = new Accidental_placement_entry;
252 ape->notename_ = scm_to_int (scm_caar (s));
254 for (SCM t = scm_cdar (s); scm_is_pair (t); t = scm_cdr (t))
255 ape->grobs_.push (unsmob_grob (scm_car (t)));
260 Grob *common[] = {me, 0};
263 First we must extract *all* pointers. We can only determine
264 extents if we're sure that we've found the right common refpoint
266 Link_array<Grob> note_cols, heads;
267 for (int i = apes.size (); i--;)
269 Accidental_placement_entry *ape = apes[i];
270 for (int j = ape->grobs_.size (); j--;)
272 Grob *a = ape->grobs_[j];
275 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
279 Grob *head = a->get_parent (Y_AXIS);
281 Grob *col = head->get_parent (X_AXIS);
282 if (Note_column::has_interface (col))
283 note_cols.push (col);
290 This is a little kludgy: to get all notes, we look if there are
293 for (int i = note_cols.size (); i--;)
295 Grob *c = note_cols[i]->get_parent (X_AXIS);
296 if (Note_collision_interface::has_interface (c))
299 = extract_grob_array (c, ly_symbol2scm ("elements"));
301 note_cols.concat (gs);
305 for (int i = note_cols.size (); i--;)
307 heads.concat (extract_grob_array (note_cols[i], ly_symbol2scm ("note-heads")));
309 heads.default_sort ();
311 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
313 for (int i = apes.size (); i--;)
315 Accidental_placement_entry *ape = apes[i];
316 ape->left_skyline_ = empty_skyline (LEFT);
317 ape->right_skyline_ = empty_skyline (RIGHT);
319 for (int j = apes[i]->grobs_.size (); j--;)
321 Grob *a = apes[i]->grobs_[j];
323 Array<Box> boxes = Accidental_interface::accurate_boxes (a, common);
325 ape->extents_.concat (boxes);
326 for (int j = boxes.size (); j--;)
328 insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
329 insert_extent_into_skyline (&ape->right_skyline_, boxes[j], Y_AXIS, RIGHT);
335 for (int i = apes.size (); i--;)
339 for (int j = apes[i]->extents_.size (); j--;)
341 y.unite (apes[i]->extents_[j][Y_AXIS]);
343 apes[i]->vertical_extent_ = y;
346 all_accidental_vertical_extent = total;
347 stagger_apes (&apes);
349 Accidental_placement_entry *head_ape = new Accidental_placement_entry;
350 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
351 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
352 Array<Box> head_extents;
353 for (int i = heads.size (); i--;)
355 Box b (heads[i]->extent (common[X_AXIS], X_AXIS),
356 heads[i]->extent (common[Y_AXIS], Y_AXIS));
358 insert_extent_into_skyline (&head_skyline, b, Y_AXIS, LEFT);
361 head_ape->left_skyline_ = head_skyline;
362 head_ape->offset_ = 0.0;
364 Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
366 Array<Skyline_entry> left_skyline = head_ape->left_skyline_;
367 heighten_skyline (&left_skyline,
368 -robust_scm2double (me->get_property ("right-padding"), 0));
370 Add accs entries right-to-left.
372 for (int i = apes.size (); i-- > 0;)
375 = -skyline_meshing_distance (apes[i]->right_skyline_, left_skyline);
377 offset = (i < apes.size () - 1) ? apes[i + 1]->offset_ : 0.0;
381 apes[i]->offset_ = offset;
383 Array<Skyline_entry> new_left_skyline = apes[i]->left_skyline_;
384 heighten_skyline (&new_left_skyline, apes[i]->offset_);
385 merge_skyline (&new_left_skyline, left_skyline, LEFT);
386 left_skyline = new_left_skyline;
389 for (int i = apes.size (); i--;)
391 Accidental_placement_entry *ape = apes[i];
392 for (int j = ape->grobs_.size (); j--;)
394 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
398 Interval left_extent, right_extent;
399 Accidental_placement_entry *ape = apes[0];
401 for (int i = ape->extents_.size (); i--;)
402 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
405 for (int i = ape->extents_.size (); i--;)
406 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
408 left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
409 Interval width (left_extent[LEFT], right_extent[RIGHT]);
411 SCM scm_width = ly_interval2scm (width);
412 me->set_extent (scm_width, X_AXIS);
414 for (int i = apes.size (); i--;)
417 return SCM_UNSPECIFIED;
420 ADD_INTERFACE (Accidental_placement,
421 "accidental-placement-interface",
422 "Resolve accidental collisions.",
423 "left-padding padding right-padding accidental-grobs positioning-done")