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>
10 #include "accidental-placement.hh"
12 #include "accidental-interface.hh"
14 #include "note-collision.hh"
15 #include "note-column.hh"
17 #include "pointer-group-interface.hh"
19 #include "stream-event.hh"
24 Accidental_placement::add_accidental (Grob *me, Grob *a)
26 a->set_parent (me, X_AXIS);
27 a->set_property ("X-offset", Grob::x_parent_positioning_proc);
28 SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
30 Stream_event *mcause = unsmob_stream_event (cause);
33 programming_error ("note head has no event cause");
37 Pitch *p = unsmob_pitch (mcause->get_property ("pitch"));
39 int n = p->get_notename ();
41 SCM accs = me->get_object ("accidental-grobs");
42 SCM key = scm_from_int (n);
43 SCM entry = scm_assq (key, accs);
44 if (entry == SCM_BOOL_F)
47 entry = scm_cdr (entry);
49 entry = scm_cons (a->self_scm (), entry);
51 accs = scm_assq_set_x (accs, key, entry);
53 me->set_object ("accidental-grobs", accs);
57 Split into break reminders.
60 Accidental_placement::split_accidentals (Grob *accs,
61 vector<Grob*> *break_reminder,
62 vector<Grob*> *real_acc)
64 for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
66 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
68 Grob *a = unsmob_grob (scm_car (s));
70 if (unsmob_grob (a->get_object ("tie")))
71 break_reminder->push_back (a);
73 real_acc->push_back (a);
78 Accidentals are special, because they appear and disappear after
82 Accidental_placement::get_relevant_accidental_extent (Grob *me,
87 vector<Grob*> *which = 0;
89 Accidental_placement::split_accidentals (me, &br, &ra);
92 if (dynamic_cast<Item *> (left_object)->break_status_dir () == RIGHT)
98 for (vsize i = 0; i < which->size (); i++)
99 extent.unite (which->at (i)->extent (item_col, X_AXIS));
101 if (!extent.is_empty ())
103 Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
110 struct Accidental_placement_entry
112 vector<Skyline_entry> left_skyline_;
113 vector<Skyline_entry> right_skyline_;
114 Interval vertical_extent_;
115 vector<Box> extents_;
116 vector<Grob*> grobs_;
119 Accidental_placement_entry ()
126 static Interval all_accidental_vertical_extent;
127 Real ape_priority (Accidental_placement_entry const *a)
129 return a->vertical_extent_[UP];
132 int ape_compare (Accidental_placement_entry *const &a,
133 Accidental_placement_entry *const &b)
135 return sign (ape_priority (a) - ape_priority (b));
138 int ape_rcompare (Accidental_placement_entry *const &a,
139 Accidental_placement_entry *const &b)
141 return -sign (ape_priority (a) - ape_priority (b));
153 stagger_apes (vector<Accidental_placement_entry*> *apes)
155 vector<Accidental_placement_entry*> asc = *apes;
157 vector_sort (asc, &ape_compare);
162 for (vsize i = 0; i < asc.size ();)
164 Accidental_placement_entry *a = 0;
181 This routine computes placements of accidentals. During
182 add_accidental (), accidentals are already grouped by note, so that
183 octaves are placed above each other; they form columns. Then the
184 columns are sorted: the biggest columns go closest to the note.
185 Then the columns are spaced as closely as possible (using skyline
189 TODO: more advanced placement. Typically, the accs should be placed
190 to form a C shape, like this
199 The naturals should be left of the C as well; they should
202 Note that this placement problem looks NP hard, so we just use a
203 simple strategy, not an optimal choice.
207 TODO: there should be more space in the following situation
221 MAKE_SCHEME_CALLBACK(Accidental_placement, calc_positioning_done, 1);
223 Accidental_placement::calc_positioning_done (SCM smob)
225 Grob *me = unsmob_grob (smob);
229 SCM accs = me->get_object ("accidental-grobs");
230 if (!scm_is_pair (accs))
234 TODO: there is a bug in this code. If two accs are on the same
235 Y-position, they share an Ape, and will be printed in overstrike.
237 vector<Accidental_placement_entry*> apes;
238 for (SCM s = accs; scm_is_pair (s); s = scm_cdr (s))
240 Accidental_placement_entry *ape = new Accidental_placement_entry;
241 ape->notename_ = scm_to_int (scm_caar (s));
243 for (SCM t = scm_cdar (s); scm_is_pair (t); t = scm_cdr (t))
244 ape->grobs_.push_back (unsmob_grob (scm_car (t)));
246 apes.push_back (ape);
249 Grob *common[] = {me, 0};
252 First we must extract *all* pointers. We can only determine
253 extents if we're sure that we've found the right common refpoint
255 vector<Grob*> note_cols, heads;
256 for (vsize i = apes.size (); i--;)
258 Accidental_placement_entry *ape = apes[i];
259 for (vsize j = ape->grobs_.size (); j--;)
261 Grob *a = ape->grobs_[j];
264 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
268 Grob *head = a->get_parent (Y_AXIS);
270 Grob *col = head->get_parent (X_AXIS);
271 if (Note_column::has_interface (col))
272 note_cols.push_back (col);
274 heads.push_back (head);
279 This is a little kludgy: to get all notes, we look if there are
282 for (vsize i = note_cols.size (); i--;)
284 Grob *c = note_cols[i]->get_parent (X_AXIS);
285 if (Note_collision_interface::has_interface (c))
287 extract_grob_set (c, "elements", gs);
289 concat (note_cols, gs);
293 for (vsize i = note_cols.size (); i--;)
294 concat (heads, extract_grob_array (note_cols[i], "note-heads"));
296 vector_sort (heads, default_compare);
298 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
300 for (vsize i = apes.size (); i--;)
302 Accidental_placement_entry *ape = apes[i];
303 ape->left_skyline_ = empty_skyline (LEFT);
304 ape->right_skyline_ = empty_skyline (RIGHT);
306 for (vsize j = apes[i]->grobs_.size (); j--;)
308 Grob *a = apes[i]->grobs_[j];
310 vector<Box> boxes = Accidental_interface::accurate_boxes (a, common);
312 ape->extents_.insert (ape->extents_.end (), boxes.begin (), boxes.end ());
313 for (vsize j = boxes.size (); j--;)
315 insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
316 insert_extent_into_skyline (&ape->right_skyline_, boxes[j], Y_AXIS, RIGHT);
322 for (vsize i = apes.size (); i--;)
326 for (vsize j = apes[i]->extents_.size (); j--;)
327 y.unite (apes[i]->extents_[j][Y_AXIS]);
328 apes[i]->vertical_extent_ = y;
331 all_accidental_vertical_extent = total;
332 stagger_apes (&apes);
334 Accidental_placement_entry *head_ape = new Accidental_placement_entry;
335 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
336 vector<Skyline_entry> head_skyline (empty_skyline (LEFT));
337 vector<Box> head_extents;
338 for (vsize i = heads.size (); i--;)
340 Box b (heads[i]->extent (common[X_AXIS], X_AXIS),
341 heads[i]->extent (common[Y_AXIS], Y_AXIS));
343 insert_extent_into_skyline (&head_skyline, b, Y_AXIS, LEFT);
346 head_ape->left_skyline_ = head_skyline;
347 head_ape->offset_ = 0.0;
349 Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
351 vector<Skyline_entry> left_skyline = head_ape->left_skyline_;
352 heighten_skyline (&left_skyline,
353 -robust_scm2double (me->get_property ("right-padding"), 0));
355 Add accs entries right-to-left.
357 for (vsize i = apes.size (); i-- > 0;)
360 = -skyline_meshing_distance (apes[i]->right_skyline_, left_skyline);
362 offset = (i < apes.size () - 1) ? apes[i + 1]->offset_ : 0.0;
366 apes[i]->offset_ = offset;
368 vector<Skyline_entry> new_left_skyline = apes[i]->left_skyline_;
369 heighten_skyline (&new_left_skyline, apes[i]->offset_);
370 merge_skyline (&new_left_skyline, left_skyline, LEFT);
371 left_skyline = new_left_skyline;
374 for (vsize i = apes.size (); i--;)
376 Accidental_placement_entry *ape = apes[i];
377 for (vsize j = ape->grobs_.size (); j--;)
378 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
381 Interval left_extent, right_extent;
382 Accidental_placement_entry *ape = apes[0];
384 for (vsize i = ape->extents_.size (); i--;)
385 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
388 for (vsize i = ape->extents_.size (); i--;)
389 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
391 left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
392 Interval width (left_extent[LEFT], right_extent[RIGHT]);
394 SCM scm_width = ly_interval2scm (width);
395 me->flush_extent_cache (X_AXIS);
396 me->set_property ("X-extent", scm_width);
398 for (vsize i = apes.size (); i--;)
404 ADD_INTERFACE (Accidental_placement,
405 "accidental-placement-interface",
406 "Resolve accidental collisions.",