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"
23 MAKE_SCHEME_CALLBACK(Accidental_placement,extent_callback, 2);
25 Accidental_placement::extent_callback(SCM s, SCM axis)
27 Grob * me =unsmob_grob (s);
28 Axis a = Axis (gh_scm2int (axis));
32 SCM w = position_accidentals (me);
36 MAKE_SCHEME_CALLBACK(Accidental_placement,alignment_callback, 2);
38 Accidental_placement::alignment_callback(SCM s, SCM )
40 Grob * me =unsmob_grob (s);
42 Grob * par = me->get_parent (X_AXIS);
43 if (!to_boolean (par->get_grob_property ("alignment-done")))
45 par->set_grob_property ("alignment-done", SCM_BOOL_T);
46 position_accidentals (par);
49 return gh_int2scm (0);
55 Accidental_placement::add_accidental (Grob* me, Grob* a)
57 a->set_parent (me, X_AXIS);
58 a->add_offset_callback (alignment_callback_proc, X_AXIS);
59 SCM cause = a->get_parent (Y_AXIS)->get_grob_property("cause");
61 Music *mcause =unsmob_music (cause);
64 programming_error ("Note head has no music cause!");
68 Pitch *p= unsmob_pitch (mcause->get_mus_property ("pitch"));
70 int n = p->notename_i_;
72 SCM accs = me->get_grob_property ("accidentals");
73 SCM key = gh_int2scm (n);
74 SCM entry = scm_assq (key, accs);
75 if (entry == SCM_BOOL_F)
80 entry = gh_cdr (entry);
82 entry = gh_cons (a->self_scm (), entry);
84 accs = scm_assq_set_x (accs, key, entry);
86 me->set_grob_property ("accidentals", accs);
89 struct Accidental_placement_entry
91 Array<Skyline_entry> left_skyline_;
92 Array<Skyline_entry> right_skyline_;
93 Interval vertical_extent_;
95 Link_array<Grob> grobs_;
98 Accidental_placement_entry()
105 static Interval all_accidental_vertical_extent;
106 Real ape_priority (Accidental_placement_entry const * a)
108 return a->vertical_extent_[UP];
113 int ape_compare (Accidental_placement_entry *const &a,
114 Accidental_placement_entry *const &b)
116 return sign (ape_priority (a) - ape_priority(b));
119 int ape_rcompare (Accidental_placement_entry *const &a,
120 Accidental_placement_entry *const &b)
122 return -sign (ape_priority (a) - ape_priority(b));
137 stagger_apes (Link_array<Accidental_placement_entry> *apes)
139 Link_array<Accidental_placement_entry> asc = *apes;
142 asc.sort (&ape_compare);
148 while (i < asc.size())
150 Accidental_placement_entry * a = 0;
166 Return: width as SCM interval.
169 This routine computes placements of accidentals. During
170 add_accidental(), accidentals are already grouped by note, so that
171 octaves are placed above each other; they form columns. Then the
172 columns are sorted: the biggest columns go closest to the note.
173 Then the columns are spaced as closely as possible (using skyline
177 TODO: more advanced placement. Typically, the accs should be placed
178 to form a C shape, like this
187 The naturals should be left of the C as well; they should
190 Note that this placement problem looks NP hard, so we just use a
191 simple strategy, not an optimal choice.
196 Accidental_placement::position_accidentals (Grob * me)
198 SCM accs = me->get_grob_property ("accidentals");
201 TODO: there is a bug in this code. If two accs are on the same
202 Y-position, they share an Ape, and will be pritned in overstrike.
204 Link_array<Accidental_placement_entry> apes;
205 for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
207 Accidental_placement_entry *ape = new Accidental_placement_entry;
208 ape->notename_ = gh_scm2int (gh_caar (s));
210 for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
211 ape->grobs_.push (unsmob_grob (gh_car (t)));
217 Grob *common[] = {me, 0};
220 First we must extract *all* pointers. We can only determine
221 extents if we're sure that we've found the right common refpoint
223 Link_array<Grob> note_cols, heads;
224 for (int i= apes.size (); i--;)
226 Accidental_placement_entry * ape = apes[i];
227 for (int j = ape->grobs_.size(); j--;)
229 Grob * a = ape->grobs_[j];
232 common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
236 Grob *head = a->get_parent (Y_AXIS);
238 Grob * col = head->get_parent (X_AXIS);
239 if (Note_column::has_interface (col))
240 note_cols.push (col);
247 This is a little kludgy: to get all notes, we look if there are
250 for (int i = note_cols.size() ; i--;)
252 Grob *c = note_cols[i]->get_parent (X_AXIS);
253 if (Note_collision_interface::has_interface (c))
255 Link_array<Grob> gs =
256 Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
258 note_cols.concat (gs);
262 for (int i = note_cols.size() ; i--;)
264 heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
269 heads.default_sort();
271 common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
274 for (int i= apes.size (); i--;)
276 Accidental_placement_entry * ape = apes[i];
277 ape->left_skyline_ = empty_skyline (LEFT);
278 ape->right_skyline_ = empty_skyline (RIGHT);
280 for (int j = apes[i]->grobs_.size(); j--;)
282 Grob * a = apes[i]->grobs_[j];
284 Array<Box> boxes = Accidental_interface::accurate_boxes (a, common);
286 ape->extents_.concat (boxes);
287 for (int j = boxes.size(); j--;)
289 insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
290 insert_extent_into_skyline (&ape->right_skyline_ , boxes[j], Y_AXIS, RIGHT);
297 for (int i = apes.size(); i--;)
301 for (int j = apes[i]->extents_.size(); j--;)
303 y.unite (apes[i]->extents_[j][Y_AXIS]);
305 apes[i]->vertical_extent_ = y;
308 all_accidental_vertical_extent = total;
309 stagger_apes (&apes);
311 Accidental_placement_entry * head_ape = new Accidental_placement_entry;
312 common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
313 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
314 Array<Box> head_extents;
315 for (int i = heads.size(); i--;)
317 Box b(heads[i]->extent (common[X_AXIS] , X_AXIS),
318 heads[i]->extent (common[Y_AXIS], Y_AXIS));
320 insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
323 head_ape-> left_skyline_ = head_skyline;
324 head_ape->offset_ = 0.0;
326 SCM rs = me->get_grob_property ("right-padding");
327 if (gh_number_p (rs))
328 head_ape->offset_ -= gh_scm2double (rs);
332 SCM spad = me->get_grob_property ("padding");
333 if (gh_number_p (spad))
334 padding = gh_scm2double (spad);
340 There is a bug in this code: the left_skylines should be
341 accumulated, otherwise the b will collide with bb in
348 apes.push (head_ape);
349 for (int i= apes.size () -1 ; i-- > 0;)
351 Accidental_placement_entry *ape = apes[i];
355 d = - skyline_meshing_distance (ape->right_skyline_,
356 apes[j]->left_skyline_);
359 || j + 1 == apes.size())
369 ape->offset_ += apes[j]->offset_ + d;
373 for (int i = apes.size(); i--;)
375 Accidental_placement_entry* ape = apes[i];
376 for (int j = ape->grobs_.size(); j--;)
378 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
383 Interval left_extent, right_extent;
384 Accidental_placement_entry *ape = apes[0];
386 for (int i = ape->extents_.size(); i--;)
387 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
390 for (int i = ape->extents_.size(); i--;)
391 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
393 SCM ls = me->get_grob_property ("left-padding");
394 if (gh_number_p (rs))
395 left_extent[LEFT] -= gh_scm2double (ls);
398 Interval width(left_extent[LEFT], right_extent[RIGHT]);
400 SCM scm_width = ly_interval2scm (width);
401 me->set_extent (scm_width, X_AXIS);
403 for (int i = apes.size(); i--;)
409 ADD_INTERFACE(Accidental_placement,
410 "accidental-placement-interface",
411 "Take care of complex accidental collisions.",
412 "left-padding padding right-padding accidentals alignment-done")