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"
22 MAKE_SCHEME_CALLBACK(Accidental_placement,extent_callback, 2);
24 Accidental_placement::extent_callback(SCM s, SCM axis)
26 Grob * me =unsmob_grob (s);
27 Axis a = Axis (gh_scm2int (axis));
31 SCM w = position_accidentals (me);
35 MAKE_SCHEME_CALLBACK(Accidental_placement,alignment_callback, 2);
37 Accidental_placement::alignment_callback(SCM s, SCM )
39 Grob * me =unsmob_grob (s);
41 Grob * par = me->get_parent (X_AXIS);
42 if (!to_boolean (par->get_grob_property ("alignment-done")))
44 par->set_grob_property ("alignment-done", SCM_BOOL_T);
45 position_accidentals (par);
48 return gh_int2scm (0);
54 Accidental_placement::add_accidental (Grob* me, Grob* a)
56 a->set_parent (me, X_AXIS);
57 a->add_offset_callback (alignment_callback_proc, X_AXIS);
58 SCM cause = a->get_parent (Y_AXIS)->get_grob_property("cause");
60 Music *mcause =unsmob_music (cause);
63 programming_error ("Note head has no music cause!");
67 Pitch *p= unsmob_pitch (mcause->get_mus_property ("pitch"));
69 int n = p->notename_i_;
71 SCM accs = me->get_grob_property ("accidentals");
72 SCM key = gh_int2scm (n);
73 SCM entry = scm_assq (key, accs);
74 if (entry == SCM_BOOL_F)
79 entry = gh_cdr (entry);
81 entry = gh_cons (a->self_scm (), entry);
83 accs = scm_assq_set_x (accs, key, entry);
85 me->set_grob_property ("accidentals", accs);
88 struct Accidental_placement_entry
90 Array<Skyline_entry> left_skyline_;
91 Array<Skyline_entry> right_skyline_;
92 Interval vertical_extent_;
94 Link_array<Grob> grobs_;
97 Accidental_placement_entry()
104 static Interval all_accidental_vertical_extent;
105 Real ape_priority (Accidental_placement_entry const * a)
107 return a->vertical_extent_[UP];
112 int ape_compare (Accidental_placement_entry *const &a,
113 Accidental_placement_entry *const &b)
115 return sign (ape_priority (a) - ape_priority(b));
118 int ape_rcompare (Accidental_placement_entry *const &a,
119 Accidental_placement_entry *const &b)
121 return -sign (ape_priority (a) - ape_priority(b));
136 stagger_apes (Link_array<Accidental_placement_entry> *apes)
138 Link_array<Accidental_placement_entry> asc = *apes;
141 asc.sort (&ape_compare);
147 while (i < asc.size())
149 Accidental_placement_entry * a = 0;
165 Return: width as SCM interval.
168 This routine computes placements of accidentals. During
169 add_accidental(), accidentals are already grouped by note, so that
170 octaves are placed above each other; they form columns. Then the
171 columns are sorted: the biggest columns go closest to the note.
172 Then the columns are spaced as closely as possible (using skyline
176 TODO: more advanced placement. Typically, the accs should be placed
177 to form a C shape, like this
186 The naturals should be left of the C as well; they should
189 TODO: Try to combine Apes before sorting them: this will allow a
192 Note that this placement problem is almost certainly NP hard, so we
193 just use a simple strategy, not an optimal choice.
197 Accidental_placement::position_accidentals (Grob * me)
199 SCM accs = me->get_grob_property ("accidentals");
201 Link_array<Accidental_placement_entry> apes;
202 for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
204 Accidental_placement_entry *ape = new Accidental_placement_entry;
205 ape->notename_ = gh_scm2int (gh_caar (s));
207 for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
208 ape->grobs_.push (unsmob_grob (gh_car (t)));
217 First we must extract *all* pointers. We can only determine
218 extents if we're sure that we've found the right common refpoint
220 Link_array<Grob> note_cols, heads;
221 for (int i= apes.size (); i--;)
223 Accidental_placement_entry * ape = apes[i];
224 for (int j = ape->grobs_.size(); j--;)
226 Grob * a = ape->grobs_[j];
228 commony = commony->common_refpoint (a, Y_AXIS);
229 Grob *head = a->get_parent (Y_AXIS);
231 Grob * col = head->get_parent (X_AXIS);
232 if (Note_column::has_interface (col))
233 note_cols.push (col);
240 This is a little kludgy: to get all notes, we look if there are
243 for (int i = note_cols.size() ; i--;)
245 Grob *c = note_cols[i]->get_parent (X_AXIS);
246 if (Note_collision_interface::has_interface (c))
248 Link_array<Grob> gs =
249 Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
251 note_cols.concat (gs);
255 for (int i = note_cols.size() ; i--;)
257 heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
262 heads.default_sort();
264 commony = common_refpoint_of_array (heads, commony, Y_AXIS);
267 for (int i= apes.size (); i--;)
269 Accidental_placement_entry * ape = apes[i];
270 ape->left_skyline_ = empty_skyline (LEFT);
271 ape->right_skyline_ = empty_skyline (RIGHT);
273 for (int j = apes[i]->grobs_.size(); j--;)
275 Grob * a = apes[i]->grobs_[j];
277 b[X_AXIS] = a->extent (me, X_AXIS);
278 b[Y_AXIS] = a->extent (commony, Y_AXIS);
280 ape->extents_.push (b);
283 TODO: replace the extents of a flat by combination of two
284 bboxes, so that we use the shape of the flat better.
286 insert_extent_into_skyline (&ape->left_skyline_, b, Y_AXIS, LEFT);
287 insert_extent_into_skyline (&ape->right_skyline_ , b,Y_AXIS, RIGHT);
293 for (int i = apes.size(); i--;)
297 for (int j = apes[i]->extents_.size(); j--;)
299 y.unite (apes[i]->extents_[j][Y_AXIS]);
301 apes[i]->vertical_extent_ = y;
304 all_accidental_vertical_extent = total;
305 stagger_apes (&apes);
307 Accidental_placement_entry * head_ape = new Accidental_placement_entry;
308 Grob *commonx = common_refpoint_of_array (heads, me, X_AXIS);
309 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
310 Array<Box> head_extents;
311 for (int i = heads.size(); i--;)
313 Box b(heads[i]->extent (commonx, X_AXIS),
314 heads[i]->extent (commony, Y_AXIS));
316 insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
319 head_ape-> left_skyline_ = head_skyline;
320 head_ape->offset_ = 0.0;
322 SCM rs = me->get_grob_property ("right-padding");
323 if (gh_number_p (rs))
324 head_ape->offset_ -= gh_scm2double (rs);
328 SCM spad = me->get_grob_property ("padding");
329 if (gh_number_p (spad))
330 padding = gh_scm2double (spad);
332 apes.push (head_ape);
333 for (int i= apes.size () -1 ; i-- > 0;)
335 Accidental_placement_entry *ape = apes[i];
339 d = - skyline_meshing_distance (ape->right_skyline_,
340 apes[j]->left_skyline_);
343 || j + 1 == apes.size())
353 ape->offset_ += apes[j]->offset_ + d;
357 for (int i = apes.size(); i--;)
359 Accidental_placement_entry* ape = apes[i];
360 for (int j = ape->grobs_.size(); j--;)
362 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
367 Interval left_extent, right_extent;
368 Accidental_placement_entry *ape = apes[0];
370 for (int i = ape->extents_.size(); i--;)
371 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
374 for (int i = ape->extents_.size(); i--;)
375 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
377 SCM ls = me->get_grob_property ("left-padding");
378 if (gh_number_p (rs))
379 left_extent[LEFT] -= gh_scm2double (ls);
382 Interval width(left_extent[LEFT], right_extent[RIGHT]);
384 SCM scm_width = ly_interval2scm (width);
385 me->set_extent (scm_width, X_AXIS);
387 for (int i = apes.size(); i--;)
393 ADD_INTERFACE(Accidental_placement,
394 "accidental-placement-interface",
395 "Take care of complex accidental collisions.",
396 "left-padding padding right-padding accidentals alignment-done")