2 accidental-placement.cc -- implement Accidental_placement
4 source file of the GNU LilyPond music typesetter
6 (c) 2002 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"
24 Hmm. why not group-extent?
26 MAKE_SCHEME_CALLBACK(Accidental_placement,extent_callback, 2);
28 Accidental_placement::extent_callback(SCM s, SCM axis)
30 Grob * me =unsmob_grob (s);
31 Axis a = Axis (gh_scm2int (axis));
35 SCM w = position_accidentals (me);
39 MAKE_SCHEME_CALLBACK(Accidental_placement,alignment_callback, 2);
41 Accidental_placement::alignment_callback(SCM s, SCM )
43 Grob * me =unsmob_grob (s);
45 Grob * par = me->get_parent (X_AXIS);
46 if (!to_boolean (par->get_grob_property ("alignment-done")))
48 par->set_grob_property ("alignment-done", SCM_BOOL_T);
49 position_accidentals (par);
52 return gh_int2scm (0);
58 Accidental_placement::add_accidental (Grob* me, Grob* a)
60 a->set_parent (me, X_AXIS);
61 a->add_offset_callback (alignment_callback_proc, X_AXIS);
62 SCM cause = a->get_parent (Y_AXIS)->get_grob_property("cause");
64 Music *mcause =unsmob_music (cause);
67 programming_error ("Note head has no music cause!");
71 Pitch *p= unsmob_pitch (mcause->get_mus_property ("pitch"));
73 int n = p->notename_i_;
75 SCM accs = me->get_grob_property ("accidentals");
76 SCM key = gh_int2scm (n);
77 SCM entry = scm_assq (key, accs);
78 if (entry == SCM_BOOL_F)
83 entry = gh_cdr (entry);
85 entry = gh_cons (a->self_scm (), entry);
87 accs = scm_assq_set_x (accs, key, entry);
89 me->set_grob_property ("accidentals", accs);
93 Split into break reminders.
96 Accidental_placement::split_accidentals (Grob * accs,
97 Link_array<Grob> *break_reminder,
98 Link_array<Grob> *real_acc)
100 for (SCM acs =accs->get_grob_property ("accidentals"); gh_pair_p (acs);
102 for (SCM s = gh_cdar (acs); gh_pair_p (s); s = gh_cdr (s))
104 Grob *a = unsmob_grob (gh_car (s));
106 if (unsmob_grob (a->get_grob_property ("tie")))
107 break_reminder->push (a);
114 Accidentals are special, because they appear and disappear after
118 Accidental_placement::get_relevant_accidental_extent (Grob *me,
122 Link_array<Grob> br, ra;
123 Link_array<Grob> *which = 0;
125 Accidental_placement::split_accidentals (me, &br, &ra);
128 if (dynamic_cast<Item*>(left_object)->break_status_dir () == RIGHT)
134 for (int i = 0; i < which->size(); i++)
136 extent.unite (which->elem(i)->extent (item_col, X_AXIS));
139 if (!extent.empty_b())
141 Real p = gh_scm2double (me->get_grob_property ("left-padding"));
150 struct Accidental_placement_entry
152 Array<Skyline_entry> left_skyline_;
153 Array<Skyline_entry> right_skyline_;
154 Interval vertical_extent_;
156 Link_array<Grob> grobs_;
159 Accidental_placement_entry()
166 static Interval all_accidental_vertical_extent;
167 Real ape_priority (Accidental_placement_entry const * a)
169 return a->vertical_extent_[UP];
174 int ape_compare (Accidental_placement_entry *const &a,
175 Accidental_placement_entry *const &b)
177 return sign (ape_priority (a) - ape_priority(b));
180 int ape_rcompare (Accidental_placement_entry *const &a,
181 Accidental_placement_entry *const &b)
183 return -sign (ape_priority (a) - ape_priority(b));
198 stagger_apes (Link_array<Accidental_placement_entry> *apes)
200 Link_array<Accidental_placement_entry> asc = *apes;
203 asc.sort (&ape_compare);
209 while (i < asc.size())
211 Accidental_placement_entry * a = 0;
227 Return: width as SCM interval.
230 This routine computes placements of accidentals. During
231 add_accidental(), accidentals are already grouped by note, so that
232 octaves are placed above each other; they form columns. Then the
233 columns are sorted: the biggest columns go closest to the note.
234 Then the columns are spaced as closely as possible (using skyline
238 TODO: more advanced placement. Typically, the accs should be placed
239 to form a C shape, like this
248 The naturals should be left of the C as well; they should
251 Note that this placement problem looks NP hard, so we just use a
252 simple strategy, not an optimal choice.
257 Accidental_placement::position_accidentals (Grob * me)
259 SCM accs = me->get_grob_property ("accidentals");
262 TODO: there is a bug in this code. If two accs are on the same
263 Y-position, they share an Ape, and will be pritned in overstrike.
265 Link_array<Accidental_placement_entry> apes;
266 for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
268 Accidental_placement_entry *ape = new Accidental_placement_entry;
269 ape->notename_ = gh_scm2int (gh_caar (s));
271 for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
272 ape->grobs_.push (unsmob_grob (gh_car (t)));
278 Grob *common[] = {me, 0};
281 First we must extract *all* pointers. We can only determine
282 extents if we're sure that we've found the right common refpoint
284 Link_array<Grob> note_cols, heads;
285 for (int i= apes.size (); i--;)
287 Accidental_placement_entry * ape = apes[i];
288 for (int j = ape->grobs_.size(); j--;)
290 Grob * a = ape->grobs_[j];
293 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
297 Grob *head = a->get_parent (Y_AXIS);
299 Grob * col = head->get_parent (X_AXIS);
300 if (Note_column::has_interface (col))
301 note_cols.push (col);
308 This is a little kludgy: to get all notes, we look if there are
311 for (int i = note_cols.size() ; i--;)
313 Grob *c = note_cols[i]->get_parent (X_AXIS);
314 if (Note_collision_interface::has_interface (c))
316 Link_array<Grob> gs =
317 Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
319 note_cols.concat (gs);
323 for (int i = note_cols.size() ; i--;)
325 heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
330 heads.default_sort();
332 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
335 for (int i= apes.size (); i--;)
337 Accidental_placement_entry * ape = apes[i];
338 ape->left_skyline_ = empty_skyline (LEFT);
339 ape->right_skyline_ = empty_skyline (RIGHT);
341 for (int j = apes[i]->grobs_.size(); j--;)
343 Grob * a = apes[i]->grobs_[j];
345 Array<Box> boxes = Accidental_interface::accurate_boxes (a, common);
347 ape->extents_.concat (boxes);
348 for (int j = boxes.size(); j--;)
350 insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
351 insert_extent_into_skyline (&ape->right_skyline_ , boxes[j], Y_AXIS, RIGHT);
358 for (int i = apes.size(); i--;)
362 for (int j = apes[i]->extents_.size(); j--;)
364 y.unite (apes[i]->extents_[j][Y_AXIS]);
366 apes[i]->vertical_extent_ = y;
369 all_accidental_vertical_extent = total;
370 stagger_apes (&apes);
372 Accidental_placement_entry * head_ape = new Accidental_placement_entry;
373 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
374 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
375 Array<Box> head_extents;
376 for (int i = heads.size(); i--;)
378 Box b(heads[i]->extent (common[X_AXIS] , X_AXIS),
379 heads[i]->extent (common[Y_AXIS], Y_AXIS));
381 insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
384 head_ape-> left_skyline_ = head_skyline;
385 head_ape->offset_ = 0.0;
387 SCM rs = me->get_grob_property ("right-padding");
388 if (gh_number_p (rs))
389 head_ape->offset_ -= gh_scm2double (rs);
393 SCM spad = me->get_grob_property ("padding");
394 if (gh_number_p (spad))
395 padding = gh_scm2double (spad);
401 There is a bug in this code: the left_skylines should be
402 accumulated, otherwise the b will collide with bb in
409 apes.push (head_ape);
410 for (int i= apes.size () -1 ; i-- > 0;)
412 Accidental_placement_entry *ape = apes[i];
416 d = - skyline_meshing_distance (ape->right_skyline_,
417 apes[j]->left_skyline_);
420 || j + 1 == apes.size())
430 ape->offset_ += apes[j]->offset_ + d;
434 for (int i = apes.size(); i--;)
436 Accidental_placement_entry* ape = apes[i];
437 for (int j = ape->grobs_.size(); j--;)
439 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
444 Interval left_extent, right_extent;
445 Accidental_placement_entry *ape = apes[0];
447 for (int i = ape->extents_.size(); i--;)
448 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
451 for (int i = ape->extents_.size(); i--;)
452 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
454 SCM ls = me->get_grob_property ("left-padding");
455 if (gh_number_p (rs))
456 left_extent[LEFT] -= gh_scm2double (ls);
459 Interval width(left_extent[LEFT], right_extent[RIGHT]);
461 SCM scm_width = ly_interval2scm (width);
462 me->set_extent (scm_width, X_AXIS);
464 for (int i = apes.size(); i--;)
470 ADD_INTERFACE(Accidental_placement,
471 "accidental-placement-interface",
472 "Take care of complex accidental collisions.",
473 "left-padding padding right-padding accidentals alignment-done")