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"
13 #include "rhythmic-head.hh"
14 #include "accidental-interface.hh"
16 #include "note-collision.hh"
17 #include "note-column.hh"
18 #include "pointer-group-interface.hh"
20 #include "stream-event.hh"
25 Accidental_placement::add_accidental (Grob *me, Grob *a)
27 a->set_parent (me, X_AXIS);
28 a->set_property ("X-offset", Grob::x_parent_positioning_proc);
29 SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
31 Stream_event *mcause = unsmob_stream_event (cause);
34 programming_error ("note head has no event cause");
38 Pitch *p = unsmob_pitch (mcause->get_property ("pitch"));
40 int n = p->get_notename ();
42 SCM accs = me->get_object ("accidental-grobs");
43 SCM key = scm_from_int (n);
44 SCM entry = scm_assq (key, accs);
45 if (entry == SCM_BOOL_F)
48 entry = scm_cdr (entry);
50 entry = scm_cons (a->self_scm (), entry);
52 accs = scm_assq_set_x (accs, key, entry);
54 me->set_object ("accidental-grobs", accs);
58 Split into break reminders.
61 Accidental_placement::split_accidentals (Grob *accs,
62 vector<Grob*> *break_reminder,
63 vector<Grob*> *real_acc)
65 for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
67 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
69 Grob *a = unsmob_grob (scm_car (s));
71 if (unsmob_grob (a->get_object ("tie")))
72 break_reminder->push_back (a);
74 real_acc->push_back (a);
79 Accidentals are special, because they appear and disappear after
83 Accidental_placement::get_relevant_accidental_extent (Grob *me,
88 vector<Grob*> *which = 0;
90 Accidental_placement::split_accidentals (me, &br, &ra);
93 if (dynamic_cast<Item *> (left_object)->break_status_dir () == RIGHT)
99 for (vsize i = 0; i < which->size (); i++)
100 extent.unite (which->at (i)->extent (item_col, X_AXIS));
102 if (!extent.is_empty ())
104 Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
111 struct Accidental_placement_entry
113 Skyline left_skyline_;
114 Skyline right_skyline_;
115 Interval vertical_extent_;
116 vector<Box> extents_;
117 vector<Grob*> grobs_;
120 Accidental_placement_entry ()
127 static Interval all_accidental_vertical_extent;
128 Real ape_priority (Accidental_placement_entry const *a)
130 return a->vertical_extent_[UP];
133 int ape_compare (Accidental_placement_entry *const &a,
134 Accidental_placement_entry *const &b)
136 return sign (ape_priority (a) - ape_priority (b));
139 bool ape_less (Accidental_placement_entry *const &a,
140 Accidental_placement_entry *const &b)
142 return ape_priority (a) < ape_priority (b);
145 int ape_rcompare (Accidental_placement_entry *const &a,
146 Accidental_placement_entry *const &b)
148 return -sign (ape_priority (a) - ape_priority (b));
160 stagger_apes (vector<Accidental_placement_entry*> *apes)
162 vector<Accidental_placement_entry*> asc = *apes;
164 vector_sort (asc, &ape_less);
169 for (vsize i = 0; i < asc.size ();)
171 Accidental_placement_entry *a = 0;
188 This routine computes placements of accidentals. During
189 add_accidental (), accidentals are already grouped by note, so that
190 octaves are placed above each other; they form columns. Then the
191 columns are sorted: the biggest columns go closest to the note.
192 Then the columns are spaced as closely as possible (using skyline
196 TODO: more advanced placement. Typically, the accs should be placed
197 to form a C shape, like this
206 The naturals should be left of the C as well; they should
209 Note that this placement problem looks NP hard, so we just use a
210 simple strategy, not an optimal choice.
214 TODO: there should be more space in the following situation
228 MAKE_SCHEME_CALLBACK(Accidental_placement, calc_positioning_done, 1);
230 Accidental_placement::calc_positioning_done (SCM smob)
232 Grob *me = unsmob_grob (smob);
236 SCM accs = me->get_object ("accidental-grobs");
237 if (!scm_is_pair (accs))
241 TODO: there is a bug in this code. If two accs are on the same
242 Y-position, they share an Ape, and will be printed in overstrike.
244 vector<Accidental_placement_entry*> apes;
245 for (SCM s = accs; scm_is_pair (s); s = scm_cdr (s))
247 Accidental_placement_entry *ape = new Accidental_placement_entry;
248 ape->notename_ = scm_to_int (scm_caar (s));
250 for (SCM t = scm_cdar (s); scm_is_pair (t); t = scm_cdr (t))
251 ape->grobs_.push_back (unsmob_grob (scm_car (t)));
253 apes.push_back (ape);
256 Grob *common[] = {me, 0};
259 First we must extract *all* pointers. We can only determine
260 extents if we're sure that we've found the right common refpoint
262 vector<Grob*> note_cols, heads;
263 for (vsize i = apes.size (); i--;)
265 Accidental_placement_entry *ape = apes[i];
266 for (vsize j = ape->grobs_.size (); j--;)
268 Grob *a = ape->grobs_[j];
271 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
275 Grob *head = a->get_parent (Y_AXIS);
277 Grob *col = head->get_parent (X_AXIS);
278 if (Note_column::has_interface (col))
279 note_cols.push_back (col);
281 heads.push_back (head);
286 This is a little kludgy: to get all notes, we look if there are
289 for (vsize i = note_cols.size (); i--;)
291 Grob *c = note_cols[i]->get_parent (X_AXIS);
292 if (Note_collision_interface::has_interface (c))
294 extract_grob_set (c, "elements", gs);
296 concat (note_cols, gs);
300 for (vsize i = note_cols.size (); i--;)
301 concat (heads, extract_grob_array (note_cols[i], "note-heads"));
303 vector_sort (heads, less<Grob*> ());
305 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
307 for (vsize i = apes.size (); i--;)
309 Accidental_placement_entry *ape = apes[i];
311 for (vsize j = apes[i]->grobs_.size (); j--;)
313 Grob *a = apes[i]->grobs_[j];
314 vector<Box> boxes = Accidental_interface::accurate_boxes (a, common);
316 ape->extents_.insert (ape->extents_.end (), boxes.begin (), boxes.end ());
318 ape->left_skyline_ = Skyline (ape->extents_, Y_AXIS, LEFT);
319 ape->right_skyline_ = Skyline (ape->extents_, Y_AXIS, RIGHT);
323 for (vsize i = apes.size (); i--;)
327 for (vsize j = apes[i]->extents_.size (); j--;)
328 y.unite (apes[i]->extents_[j][Y_AXIS]);
329 apes[i]->vertical_extent_ = y;
332 all_accidental_vertical_extent = total;
333 stagger_apes (&apes);
335 Accidental_placement_entry *head_ape = new Accidental_placement_entry;
336 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
338 vector<Box> head_extents;
339 for (vsize i = heads.size (); i--;)
340 head_extents.push_back (Box (heads[i]->extent (common[X_AXIS], X_AXIS),
341 heads[i]->extent (common[Y_AXIS], Y_AXIS)));
343 vector<Grob *> stems;
344 for (vsize i = 0; i < heads.size (); i++)
346 if (Grob *s = Rhythmic_head::get_stem (heads[i]))
350 vector_sort (stems, less<Grob*> ());
352 for (vsize i = 0; i < stems.size (); i ++)
354 int very_large = INT_MAX;
356 head_extents.push_back (Box (heads[i]->extent (common[X_AXIS], X_AXIS),
357 heads[i]->pure_height (common[Y_AXIS], 0, very_large)));
360 head_ape->left_skyline_ = Skyline (head_extents, Y_AXIS, LEFT);
361 head_ape->offset_ = 0.0;
363 Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
365 Skyline left_skyline = head_ape->left_skyline_;
366 left_skyline.raise (-robust_scm2double (me->get_property ("right-padding"), 0))
369 Add accs entries right-to-left.
371 for (vsize i = apes.size (); i-- > 0;)
373 Real offset = -apes[i]->right_skyline_.distance (left_skyline);
375 offset = (i < apes.size () - 1) ? apes[i + 1]->offset_ : 0.0;
379 apes[i]->offset_ = offset;
381 Skyline new_left_skyline = apes[i]->left_skyline_;
382 new_left_skyline.raise (apes[i]->offset_);
383 new_left_skyline.merge (left_skyline);
384 left_skyline = new_left_skyline;
387 for (vsize i = apes.size (); i--;)
389 Accidental_placement_entry *ape = apes[i];
390 for (vsize j = ape->grobs_.size (); j--;)
391 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
394 Interval left_extent, right_extent;
395 Accidental_placement_entry *ape = apes[0];
397 for (vsize i = ape->extents_.size (); i--;)
398 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
401 for (vsize i = ape->extents_.size (); i--;)
402 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
404 left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
405 Interval width (left_extent[LEFT], right_extent[RIGHT]);
407 SCM scm_width = ly_interval2scm (width);
408 me->flush_extent_cache (X_AXIS);
409 me->set_property ("X-extent", scm_width);
411 for (vsize i = apes.size (); i--;)
417 ADD_INTERFACE (Accidental_placement,
418 "Resolve accidental collisions.",