2 accidental-placement.cc -- implement Accidental_placement
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "accidental-placement.hh"
16 #include "note-column.hh"
17 #include "pointer-group-interface.hh"
18 #include "note-collision.hh"
19 #include "accidental-interface.hh"
23 Accidental_placement::add_accidental (Grob *me, Grob *a)
25 a->set_parent (me, X_AXIS);
26 a->set_property ("X-offset", Grob::x_parent_positioning_proc);
27 SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
29 Music *mcause = unsmob_music (cause);
32 programming_error ("note head has no music cause");
36 Pitch *p = unsmob_pitch (mcause->get_property ("pitch"));
38 int n = p->get_notename ();
40 SCM accs = me->get_object ("accidental-grobs");
41 SCM key = scm_from_int (n);
42 SCM entry = scm_assq (key, accs);
43 if (entry == SCM_BOOL_F)
46 entry = scm_cdr (entry);
48 entry = scm_cons (a->self_scm (), entry);
50 accs = scm_assq_set_x (accs, key, entry);
52 me->set_object ("accidental-grobs", accs);
56 Split into break reminders.
59 Accidental_placement::split_accidentals (Grob *accs,
60 Link_array<Grob> *break_reminder,
61 Link_array<Grob> *real_acc)
63 for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
65 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
67 Grob *a = unsmob_grob (scm_car (s));
69 if (unsmob_grob (a->get_object ("tie")))
70 break_reminder->push_back (a);
72 real_acc->push_back (a);
77 Accidentals are special, because they appear and disappear after
81 Accidental_placement::get_relevant_accidental_extent (Grob *me,
85 Link_array<Grob> br, ra;
86 Link_array<Grob> *which = 0;
88 Accidental_placement::split_accidentals (me, &br, &ra);
91 if (dynamic_cast<Item *> (left_object)->break_status_dir () == RIGHT)
97 for (vsize i = 0; i < which->size (); i++)
98 extent.unite (which->at (i)->extent (item_col, X_AXIS));
100 if (!extent.is_empty ())
102 Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
109 struct Accidental_placement_entry
111 std::vector<Skyline_entry> left_skyline_;
112 std::vector<Skyline_entry> right_skyline_;
113 Interval vertical_extent_;
114 std::vector<Box> extents_;
115 Link_array<Grob> grobs_;
118 Accidental_placement_entry ()
125 static Interval all_accidental_vertical_extent;
126 Real ape_priority (Accidental_placement_entry const *a)
128 return a->vertical_extent_[UP];
131 int ape_compare (Accidental_placement_entry *const &a,
132 Accidental_placement_entry *const &b)
134 return sign (ape_priority (a) - ape_priority (b));
137 int ape_rcompare (Accidental_placement_entry *const &a,
138 Accidental_placement_entry *const &b)
140 return -sign (ape_priority (a) - ape_priority (b));
152 stagger_apes (Link_array<Accidental_placement_entry> *apes)
154 Link_array<Accidental_placement_entry> asc = *apes;
156 asc.sort (&ape_compare);
161 for (vsize i = 0; i < asc.size ();)
163 Accidental_placement_entry *a = 0;
177 This routine computes placements of accidentals. During
178 add_accidental (), accidentals are already grouped by note, so that
179 octaves are placed above each other; they form columns. Then the
180 columns are sorted: the biggest columns go closest to the note.
181 Then the columns are spaced as closely as possible (using skyline
185 TODO: more advanced placement. Typically, the accs should be placed
186 to form a C shape, like this
195 The naturals should be left of the C as well; they should
198 Note that this placement problem looks NP hard, so we just use a
199 simple strategy, not an optimal choice.
203 TODO: there should be more space in the following situation
217 MAKE_SCHEME_CALLBACK(Accidental_placement, calc_positioning_done, 1);
219 Accidental_placement::calc_positioning_done (SCM smob)
221 Grob *me = unsmob_grob (smob);
225 SCM accs = me->get_object ("accidental-grobs");
226 if (!scm_is_pair (accs))
230 TODO: there is a bug in this code. If two accs are on the same
231 Y-position, they share an Ape, and will be printed in overstrike.
233 Link_array<Accidental_placement_entry> apes;
234 for (SCM s = accs; scm_is_pair (s); s = scm_cdr (s))
236 Accidental_placement_entry *ape = new Accidental_placement_entry;
237 ape->notename_ = scm_to_int (scm_caar (s));
239 for (SCM t = scm_cdar (s); scm_is_pair (t); t = scm_cdr (t))
240 ape->grobs_.push_back (unsmob_grob (scm_car (t)));
242 apes.push_back (ape);
245 Grob *common[] = {me, 0};
248 First we must extract *all* pointers. We can only determine
249 extents if we're sure that we've found the right common refpoint
251 Link_array<Grob> note_cols, heads;
252 for (vsize i = apes.size (); i--;)
254 Accidental_placement_entry *ape = apes[i];
255 for (vsize j = ape->grobs_.size (); j--;)
257 Grob *a = ape->grobs_[j];
260 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
264 Grob *head = a->get_parent (Y_AXIS);
266 Grob *col = head->get_parent (X_AXIS);
267 if (Note_column::has_interface (col))
268 note_cols.push_back (col);
270 heads.push_back (head);
275 This is a little kludgy: to get all notes, we look if there are
278 for (vsize i = note_cols.size (); i--;)
280 Grob *c = note_cols[i]->get_parent (X_AXIS);
281 if (Note_collision_interface::has_interface (c))
283 extract_grob_set (c, "elements", gs);
285 note_cols.concat (gs);
289 for (vsize i = note_cols.size (); i--;)
290 heads.concat (extract_grob_array (note_cols[i], "note-heads"));
292 heads.default_sort ();
294 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
296 for (vsize i = apes.size (); i--;)
298 Accidental_placement_entry *ape = apes[i];
299 ape->left_skyline_ = empty_skyline (LEFT);
300 ape->right_skyline_ = empty_skyline (RIGHT);
302 for (vsize j = apes[i]->grobs_.size (); j--;)
304 Grob *a = apes[i]->grobs_[j];
306 std::vector<Box> boxes = Accidental_interface::accurate_boxes (a, common);
308 ape->extents_.concat (boxes);
309 for (vsize j = boxes.size (); j--;)
311 insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
312 insert_extent_into_skyline (&ape->right_skyline_, boxes[j], Y_AXIS, RIGHT);
318 for (vsize i = apes.size (); i--;)
322 for (vsize j = apes[i]->extents_.size (); j--;)
323 y.unite (apes[i]->extents_[j][Y_AXIS]);
324 apes[i]->vertical_extent_ = y;
327 all_accidental_vertical_extent = total;
328 stagger_apes (&apes);
330 Accidental_placement_entry *head_ape = new Accidental_placement_entry;
331 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
332 std::vector<Skyline_entry> head_skyline (empty_skyline (LEFT));
333 std::vector<Box> head_extents;
334 for (vsize i = heads.size (); i--;)
336 Box b (heads[i]->extent (common[X_AXIS], X_AXIS),
337 heads[i]->extent (common[Y_AXIS], Y_AXIS));
339 insert_extent_into_skyline (&head_skyline, b, Y_AXIS, LEFT);
342 head_ape->left_skyline_ = head_skyline;
343 head_ape->offset_ = 0.0;
345 Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
347 std::vector<Skyline_entry> left_skyline = head_ape->left_skyline_;
348 heighten_skyline (&left_skyline,
349 -robust_scm2double (me->get_property ("right-padding"), 0));
351 Add accs entries right-to-left.
353 for (vsize i = apes.size (); i-- > 0;)
356 = -skyline_meshing_distance (apes[i]->right_skyline_, left_skyline);
358 offset = (i < apes.size () - 1) ? apes[i + 1]->offset_ : 0.0;
362 apes[i]->offset_ = offset;
364 std::vector<Skyline_entry> new_left_skyline = apes[i]->left_skyline_;
365 heighten_skyline (&new_left_skyline, apes[i]->offset_);
366 merge_skyline (&new_left_skyline, left_skyline, LEFT);
367 left_skyline = new_left_skyline;
370 for (vsize i = apes.size (); i--;)
372 Accidental_placement_entry *ape = apes[i];
373 for (vsize j = ape->grobs_.size (); j--;)
374 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
377 Interval left_extent, right_extent;
378 Accidental_placement_entry *ape = apes[0];
380 for (vsize i = ape->extents_.size (); i--;)
381 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
384 for (vsize i = ape->extents_.size (); i--;)
385 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
387 left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
388 Interval width (left_extent[LEFT], right_extent[RIGHT]);
390 SCM scm_width = ly_interval2scm (width);
391 me->flush_extent_cache (X_AXIS);
392 me->set_property ("X-extent", scm_width);
394 for (vsize i = apes.size (); i--;)
400 ADD_INTERFACE (Accidental_placement,
401 "accidental-placement-interface",
402 "Resolve accidental collisions.",