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"
19 #include "pointer-group-interface.hh"
21 #include "stream-event.hh"
26 Accidental_placement::add_accidental (Grob *me, Grob *a)
28 a->set_parent (me, X_AXIS);
29 a->set_property ("X-offset", Grob::x_parent_positioning_proc);
30 SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
32 Stream_event *mcause = unsmob_stream_event (cause);
35 programming_error ("note head has no event cause");
39 Pitch *p = unsmob_pitch (mcause->get_property ("pitch"));
41 int n = p->get_notename ();
43 SCM accs = me->get_object ("accidental-grobs");
44 SCM key = scm_from_int (n);
45 SCM entry = scm_assq (key, accs);
46 if (entry == SCM_BOOL_F)
49 entry = scm_cdr (entry);
51 entry = scm_cons (a->self_scm (), entry);
53 accs = scm_assq_set_x (accs, key, entry);
55 me->set_object ("accidental-grobs", accs);
59 Split into break reminders.
62 Accidental_placement::split_accidentals (Grob *accs,
63 vector<Grob*> *break_reminder,
64 vector<Grob*> *real_acc)
66 for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
68 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
70 Grob *a = unsmob_grob (scm_car (s));
72 if (unsmob_grob (a->get_object ("tie")))
73 break_reminder->push_back (a);
75 real_acc->push_back (a);
80 Accidentals are special, because they appear and disappear after
84 Accidental_placement::get_relevant_accidental_extent (Grob *me,
89 vector<Grob*> *which = 0;
91 Accidental_placement::split_accidentals (me, &br, &ra);
94 if (dynamic_cast<Item *> (left_object)->break_status_dir () == RIGHT)
100 for (vsize i = 0; i < which->size (); i++)
101 extent.unite (which->at (i)->extent (item_col, X_AXIS));
103 if (!extent.is_empty ())
105 Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
112 struct Accidental_placement_entry
114 Skyline left_skyline_;
115 Skyline right_skyline_;
116 Interval vertical_extent_;
117 vector<Box> extents_;
118 vector<Grob*> grobs_;
121 Accidental_placement_entry ()
128 static Interval all_accidental_vertical_extent;
129 Real ape_priority (Accidental_placement_entry const *a)
131 return a->vertical_extent_[UP];
134 int ape_compare (Accidental_placement_entry *const &a,
135 Accidental_placement_entry *const &b)
137 return sign (ape_priority (a) - ape_priority (b));
140 bool ape_less (Accidental_placement_entry *const &a,
141 Accidental_placement_entry *const &b)
143 return ape_priority (a) < ape_priority (b);
146 int ape_rcompare (Accidental_placement_entry *const &a,
147 Accidental_placement_entry *const &b)
149 return -sign (ape_priority (a) - ape_priority (b));
161 stagger_apes (vector<Accidental_placement_entry*> *apes)
163 vector<Accidental_placement_entry*> asc = *apes;
165 vector_sort (asc, &ape_less);
170 for (vsize i = 0; i < asc.size ();)
172 Accidental_placement_entry *a = 0;
189 This routine computes placements of accidentals. During
190 add_accidental (), accidentals are already grouped by note, so that
191 octaves are placed above each other; they form columns. Then the
192 columns are sorted: the biggest columns go closest to the note.
193 Then the columns are spaced as closely as possible (using skyline
197 TODO: more advanced placement. Typically, the accs should be placed
198 to form a C shape, like this
207 The naturals should be left of the C as well; they should
210 Note that this placement problem looks NP hard, so we just use a
211 simple strategy, not an optimal choice.
215 TODO: there should be more space in the following situation
229 MAKE_SCHEME_CALLBACK(Accidental_placement, calc_positioning_done, 1);
231 Accidental_placement::calc_positioning_done (SCM smob)
233 Grob *me = unsmob_grob (smob);
237 SCM accs = me->get_object ("accidental-grobs");
238 if (!scm_is_pair (accs))
242 TODO: there is a bug in this code. If two accs are on the same
243 Y-position, they share an Ape, and will be printed in overstrike.
245 vector<Accidental_placement_entry*> apes;
246 for (SCM s = accs; scm_is_pair (s); s = scm_cdr (s))
248 Accidental_placement_entry *ape = new Accidental_placement_entry;
249 ape->notename_ = scm_to_int (scm_caar (s));
251 for (SCM t = scm_cdar (s); scm_is_pair (t); t = scm_cdr (t))
252 ape->grobs_.push_back (unsmob_grob (scm_car (t)));
254 apes.push_back (ape);
257 Grob *common[] = {me, 0};
260 First we must extract *all* pointers. We can only determine
261 extents if we're sure that we've found the right common refpoint
263 vector<Grob*> note_cols, heads;
264 for (vsize i = apes.size (); i--;)
266 Accidental_placement_entry *ape = apes[i];
267 for (vsize j = ape->grobs_.size (); j--;)
269 Grob *a = ape->grobs_[j];
272 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
276 Grob *head = a->get_parent (Y_AXIS);
278 Grob *col = head->get_parent (X_AXIS);
279 if (Note_column::has_interface (col))
280 note_cols.push_back (col);
282 heads.push_back (head);
287 This is a little kludgy: to get all notes, we look if there are
290 for (vsize i = note_cols.size (); i--;)
292 Grob *c = note_cols[i]->get_parent (X_AXIS);
293 if (Note_collision_interface::has_interface (c))
295 extract_grob_set (c, "elements", gs);
297 concat (note_cols, gs);
301 for (vsize i = note_cols.size (); i--;)
302 concat (heads, extract_grob_array (note_cols[i], "note-heads"));
304 vector_sort (heads, less<Grob*> ());
306 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
308 for (vsize i = apes.size (); i--;)
310 Accidental_placement_entry *ape = apes[i];
312 for (vsize j = apes[i]->grobs_.size (); j--;)
314 Grob *a = apes[i]->grobs_[j];
315 vector<Box> boxes = Accidental_interface::accurate_boxes (a, common);
317 ape->extents_.insert (ape->extents_.end (), boxes.begin (), boxes.end ());
319 ape->left_skyline_ = Skyline (ape->extents_, Y_AXIS, LEFT);
320 ape->right_skyline_ = Skyline (ape->extents_, Y_AXIS, RIGHT);
324 for (vsize i = apes.size (); i--;)
328 for (vsize j = apes[i]->extents_.size (); j--;)
329 y.unite (apes[i]->extents_[j][Y_AXIS]);
330 apes[i]->vertical_extent_ = y;
333 all_accidental_vertical_extent = total;
334 stagger_apes (&apes);
336 Accidental_placement_entry *head_ape = new Accidental_placement_entry;
337 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
339 vector<Box> head_extents;
340 for (vsize i = heads.size (); i--;)
341 head_extents.push_back (Box (heads[i]->extent (common[X_AXIS], X_AXIS),
342 heads[i]->extent (common[Y_AXIS], Y_AXIS)));
344 vector<Grob *> stems;
345 for (vsize i = 0; i < heads.size (); i++)
347 if (Grob *s = Rhythmic_head::get_stem (heads[i]))
351 vector_sort (stems, less<Grob*> ());
353 for (vsize i = 0; i < stems.size (); i ++)
355 int very_large = INT_MAX;
357 head_extents.push_back (Box (heads[i]->extent (common[X_AXIS], X_AXIS),
358 heads[i]->pure_height (common[Y_AXIS], 0, very_large)));
361 head_ape->left_skyline_ = Skyline (head_extents, Y_AXIS, LEFT);
362 head_ape->offset_ = 0.0;
364 Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
366 Skyline left_skyline = head_ape->left_skyline_;
367 left_skyline.raise (-robust_scm2double (me->get_property ("right-padding"), 0))
370 Add accs entries right-to-left.
372 for (vsize i = apes.size (); i-- > 0;)
374 Real offset = -apes[i]->right_skyline_.distance (left_skyline);
376 offset = (i < apes.size () - 1) ? apes[i + 1]->offset_ : 0.0;
380 apes[i]->offset_ = offset;
382 Skyline new_left_skyline = apes[i]->left_skyline_;
383 new_left_skyline.raise (apes[i]->offset_);
384 new_left_skyline.merge (left_skyline);
385 left_skyline = new_left_skyline;
388 for (vsize i = apes.size (); i--;)
390 Accidental_placement_entry *ape = apes[i];
391 for (vsize j = ape->grobs_.size (); j--;)
392 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
395 Interval left_extent, right_extent;
396 Accidental_placement_entry *ape = apes[0];
398 for (vsize i = ape->extents_.size (); i--;)
399 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
402 for (vsize i = ape->extents_.size (); i--;)
403 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
405 left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
406 Interval width (left_extent[LEFT], right_extent[RIGHT]);
408 SCM scm_width = ly_interval2scm (width);
409 me->flush_extent_cache (X_AXIS);
410 me->set_property ("X-extent", scm_width);
412 for (vsize i = apes.size (); i--;)
418 ADD_INTERFACE (Accidental_placement,
419 "Resolve accidental collisions.",