2 accidental-placement.cc -- implement Accidental_placement
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include <libc-extension.hh>
17 #include "accidental-placement.hh"
18 #include "note-column.hh"
19 #include "group-interface.hh"
20 #include "note-collision.hh"
21 #include "accidental-interface.hh"
23 MAKE_SCHEME_CALLBACK(Accidental_placement,alignment_callback, 2);
25 Accidental_placement::alignment_callback(SCM s, SCM )
27 Grob * me =unsmob_grob (s);
29 Grob * par = me->get_parent (X_AXIS);
30 if (!to_boolean (par->get_grob_property ("positioning-done")))
32 par->set_grob_property ("positioning-done", SCM_BOOL_T);
33 position_accidentals (par);
36 return scm_int2num (0);
41 Accidental_placement::add_accidental (Grob* me, Grob* a)
43 a->set_parent (me, X_AXIS);
44 a->add_offset_callback (alignment_callback_proc, X_AXIS);
45 SCM cause = a->get_parent (Y_AXIS)->get_grob_property("cause");
47 Music *mcause =unsmob_music (cause);
50 programming_error ("Note head has no music cause!");
54 Pitch *p= unsmob_pitch (mcause->get_mus_property ("pitch"));
56 int n = p->get_notename ();
58 SCM accs = me->get_grob_property ("accidental-grobs");
59 SCM key = scm_int2num (n);
60 SCM entry = scm_assq (key, accs);
61 if (entry == SCM_BOOL_F)
66 entry = gh_cdr (entry);
68 entry = gh_cons (a->self_scm (), entry);
70 accs = scm_assq_set_x (accs, key, entry);
72 me->set_grob_property ("accidental-grobs", accs);
76 Split into break reminders.
79 Accidental_placement::split_accidentals (Grob * accs,
80 Link_array<Grob> *break_reminder,
81 Link_array<Grob> *real_acc)
83 for (SCM acs =accs->get_grob_property ("accidental-grobs"); gh_pair_p (acs);
85 for (SCM s = gh_cdar (acs); gh_pair_p (s); s = gh_cdr (s))
87 Grob *a = unsmob_grob (gh_car (s));
89 if (unsmob_grob (a->get_grob_property ("tie")))
90 break_reminder->push (a);
97 Accidentals are special, because they appear and disappear after
101 Accidental_placement::get_relevant_accidental_extent (Grob *me,
105 Link_array<Grob> br, ra;
106 Link_array<Grob> *which = 0;
108 Accidental_placement::split_accidentals (me, &br, &ra);
111 if (dynamic_cast<Item*>(left_object)->break_status_dir () == RIGHT)
117 for (int i = 0; i < which->size(); i++)
119 extent.unite (which->elem(i)->extent (item_col, X_AXIS));
122 if (!extent.is_empty ())
124 Real p = gh_scm2double (me->get_grob_property ("left-padding"));
133 struct Accidental_placement_entry
135 Array<Skyline_entry> left_skyline_;
136 Array<Skyline_entry> right_skyline_;
137 Interval vertical_extent_;
139 Link_array<Grob> grobs_;
142 Accidental_placement_entry()
149 static Interval all_accidental_vertical_extent;
150 Real ape_priority (Accidental_placement_entry const * a)
152 return a->vertical_extent_[UP];
157 int ape_compare (Accidental_placement_entry *const &a,
158 Accidental_placement_entry *const &b)
160 return sign (ape_priority (a) - ape_priority(b));
163 int ape_rcompare (Accidental_placement_entry *const &a,
164 Accidental_placement_entry *const &b)
166 return -sign (ape_priority (a) - ape_priority(b));
181 stagger_apes (Link_array<Accidental_placement_entry> *apes)
183 Link_array<Accidental_placement_entry> asc = *apes;
186 asc.sort (&ape_compare);
192 while (i < asc.size())
194 Accidental_placement_entry * a = 0;
211 This routine computes placements of accidentals. During
212 add_accidental(), accidentals are already grouped by note, so that
213 octaves are placed above each other; they form columns. Then the
214 columns are sorted: the biggest columns go closest to the note.
215 Then the columns are spaced as closely as possible (using skyline
219 TODO: more advanced placement. Typically, the accs should be placed
220 to form a C shape, like this
229 The naturals should be left of the C as well; they should
232 Note that this placement problem looks NP hard, so we just use a
233 simple strategy, not an optimal choice.
238 TODO: there should be more space in the following situation
250 Accidental_placement::position_accidentals (Grob * me)
253 return SCM_UNSPECIFIED;
255 SCM accs = me->get_grob_property ("accidental-grobs");
258 TODO: there is a bug in this code. If two accs are on the same
259 Y-position, they share an Ape, and will be printed in overstrike.
261 Link_array<Accidental_placement_entry> apes;
262 for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
264 Accidental_placement_entry *ape = new Accidental_placement_entry;
265 ape->notename_ = gh_scm2int (gh_caar (s));
267 for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
268 ape->grobs_.push (unsmob_grob (gh_car (t)));
274 Grob *common[] = {me, 0};
277 First we must extract *all* pointers. We can only determine
278 extents if we're sure that we've found the right common refpoint
280 Link_array<Grob> note_cols, heads;
281 for (int i= apes.size (); i--;)
283 Accidental_placement_entry * ape = apes[i];
284 for (int j = ape->grobs_.size(); j--;)
286 Grob * a = ape->grobs_[j];
289 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
293 Grob *head = a->get_parent (Y_AXIS);
295 Grob * col = head->get_parent (X_AXIS);
296 if (Note_column::has_interface (col))
297 note_cols.push (col);
304 This is a little kludgy: to get all notes, we look if there are
307 for (int i = note_cols.size() ; i--;)
309 Grob *c = note_cols[i]->get_parent (X_AXIS);
310 if (Note_collision_interface::has_interface (c))
312 Link_array<Grob> gs =
313 Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
315 note_cols.concat (gs);
319 for (int i = note_cols.size() ; i--;)
321 heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
326 heads.default_sort();
328 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
331 for (int i= apes.size (); i--;)
333 Accidental_placement_entry * ape = apes[i];
334 ape->left_skyline_ = empty_skyline (LEFT);
335 ape->right_skyline_ = empty_skyline (RIGHT);
337 for (int j = apes[i]->grobs_.size(); j--;)
339 Grob * a = apes[i]->grobs_[j];
341 Array<Box> boxes = Accidental_interface::accurate_boxes (a, common);
343 ape->extents_.concat (boxes);
344 for (int j = boxes.size(); j--;)
346 insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
347 insert_extent_into_skyline (&ape->right_skyline_ , boxes[j], Y_AXIS, RIGHT);
354 for (int i = apes.size(); i--;)
358 for (int j = apes[i]->extents_.size(); j--;)
360 y.unite (apes[i]->extents_[j][Y_AXIS]);
362 apes[i]->vertical_extent_ = y;
365 all_accidental_vertical_extent = total;
366 stagger_apes (&apes);
368 Accidental_placement_entry * head_ape = new Accidental_placement_entry;
369 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
370 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
371 Array<Box> head_extents;
372 for (int i = heads.size(); i--;)
374 Box b(heads[i]->extent (common[X_AXIS] , X_AXIS),
375 heads[i]->extent (common[Y_AXIS], Y_AXIS));
377 insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
380 head_ape-> left_skyline_ = head_skyline;
381 head_ape->offset_ = 0.0;
383 SCM rs = me->get_grob_property ("right-padding");
384 if (gh_number_p (rs))
385 head_ape->offset_ -= gh_scm2double (rs);
389 SCM spad = me->get_grob_property ("padding");
390 if (gh_number_p (spad))
391 padding = gh_scm2double (spad);
393 Array<Skyline_entry> left_skyline = head_ape->left_skyline_;
395 Add accs entries right-to-left.
397 for (int i= apes.size (); i-- > 0;)
400 -skyline_meshing_distance (apes[i]->right_skyline_, left_skyline);
402 offset = (i < apes.size () - 1) ? apes[i+1]->offset_ : 0.0;
406 apes[i]->offset_ = offset;
408 Array<Skyline_entry> new_left_skyline = apes[i]->left_skyline_;
409 heighten_skyline (&new_left_skyline, apes[i]->offset_);
410 merge_skyline (&new_left_skyline, left_skyline, LEFT);
411 left_skyline = new_left_skyline;
414 for (int i = apes.size(); i--;)
416 Accidental_placement_entry* ape = apes[i];
417 for (int j = ape->grobs_.size(); j--;)
419 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
424 Interval left_extent, right_extent;
425 Accidental_placement_entry *ape = apes[0];
427 for (int i = ape->extents_.size(); i--;)
428 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
431 for (int i = ape->extents_.size(); i--;)
432 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
434 SCM ls = me->get_grob_property ("left-padding");
435 if (gh_number_p (rs))
436 left_extent[LEFT] -= gh_scm2double (ls);
439 Interval width(left_extent[LEFT], right_extent[RIGHT]);
441 SCM scm_width = ly_interval2scm (width);
442 me->set_extent (scm_width, X_AXIS);
444 for (int i = apes.size(); i--;)
447 return SCM_UNSPECIFIED;
450 ADD_INTERFACE(Accidental_placement,
451 "accidental-placement-interface",
452 "Take care of complex accidental collisions.",
453 "left-padding padding right-padding accidental-grobs positioning-done")