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"
21 MAKE_SCHEME_CALLBACK(Accidental_placement,extent_callback, 2);
23 Accidental_placement::extent_callback(SCM s, SCM axis)
25 Grob * me =unsmob_grob (s);
26 Axis a = Axis (gh_scm2int (axis));
30 SCM w = position_accidentals (me);
34 MAKE_SCHEME_CALLBACK(Accidental_placement,alignment_callback, 2);
36 Accidental_placement::alignment_callback(SCM s, SCM )
38 Grob * me =unsmob_grob (s);
40 Grob * par = me->get_parent (X_AXIS);
41 if (!to_boolean (par->get_grob_property ("alignment-done")))
43 par->set_grob_property ("alignment-done", SCM_BOOL_T);
44 position_accidentals (par);
47 return gh_int2scm (0);
53 Accidental_placement::add_accidental (Grob* me, Grob* a)
55 a->set_parent (me, X_AXIS);
56 a->add_offset_callback (alignment_callback_proc, X_AXIS);
57 SCM cause = a->get_parent (Y_AXIS)->get_grob_property("cause");
59 Music *mcause =unsmob_music (cause);
62 programming_error ("Note head has no music cause!");
66 Pitch *p= unsmob_pitch (mcause->get_mus_property ("pitch"));
68 int n = p->notename_i_;
70 SCM accs = me->get_grob_property ("accidentals");
71 SCM key = gh_int2scm (n);
72 SCM entry = scm_assq (key, accs);
73 if (entry == SCM_BOOL_F)
78 entry = gh_cdr (entry);
80 entry = gh_cons (a->self_scm (), entry);
82 accs = scm_assq_set_x (accs, key, entry);
84 me->set_grob_property ("accidentals", accs);
87 struct Accidental_placement_entry
89 Array<Skyline_entry> left_skyline_;
90 Array<Skyline_entry> right_skyline_;
91 Interval vertical_extent_;
93 Link_array<Grob> grobs_;
96 Accidental_placement_entry()
103 static Interval all_accidental_vertical_extent;
104 Real ape_priority (Accidental_placement_entry const * a)
106 Real c = a->vertical_extent_.center();
109 far from center means we can fold more. Hopefully.
111 Real center_distance =
112 fabs(c - all_accidental_vertical_extent[LEFT]) >?
113 fabs(c - all_accidental_vertical_extent[RIGHT]);
115 return 20 * a->vertical_extent_.length () + center_distance;
120 int ape_compare (Accidental_placement_entry *const &a,
121 Accidental_placement_entry *const &b)
123 return sign (ape_priority (a) - ape_priority(b));
127 Return: width as SCM interval.
130 This routine computes placements of accidentals. During
131 add_accidental(), accidentals are already grouped by note, so that
132 octaves are placed above each other; they form columns. Then the
133 columns are sorted: the biggest columns go closest to the note.
134 Then the columns are spaced as closely as possible (using skyline
138 TODO: more advanced placement. Typically, the accs should be placed
139 to form a C shape, like this
148 The naturals should be left of the C as well; they should
151 TODO: Try to combine Apes before sorting them: this will allow a
154 Note that this placement problem is almost certainly NP hard, so we
155 just use a simple strategy, not an optimal choice.
159 Accidental_placement::position_accidentals (Grob * me)
161 SCM accs = me->get_grob_property ("accidentals");
163 Link_array<Accidental_placement_entry> apes;
164 for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
166 Accidental_placement_entry *ape = new Accidental_placement_entry;
167 ape->notename_ = gh_scm2int (gh_caar (s));
169 for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
170 ape->grobs_.push (unsmob_grob (gh_car (t)));
179 First we must extract *all* pointers. We can only determine
180 extents if we're sure that we've found the right common refpoint
182 Link_array<Grob> note_cols, heads;
183 for (int i= apes.size (); i--;)
185 Accidental_placement_entry * ape = apes[i];
186 for (int j = ape->grobs_.size(); j--;)
188 Grob * a = ape->grobs_[j];
190 commony = commony->common_refpoint (a, Y_AXIS);
191 Grob *head = a->get_parent (Y_AXIS);
193 Grob * col = head->get_parent (X_AXIS);
194 if (Note_column::has_interface (col))
195 note_cols.push (col);
201 for (int i = note_cols.size() ; i--;)
203 heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
208 heads.default_sort();
210 commony = common_refpoint_of_array (heads, commony, Y_AXIS);
213 for (int i= apes.size (); i--;)
215 Accidental_placement_entry * ape = apes[i];
216 ape->left_skyline_ = empty_skyline ( LEFT);
217 ape->right_skyline_ = empty_skyline ( RIGHT);
219 for (int j = apes[i]->grobs_.size(); j--;)
221 Grob * a = apes[i]->grobs_[j];
223 b[X_AXIS] = a->extent (me, X_AXIS);
224 b[Y_AXIS] = a->extent (commony, Y_AXIS);
226 ape->extents_.push (b);
229 TODO: replace the extents of a flat by combination of two
230 bboxes, so that we use the shape of the flat better.
232 insert_extent_into_skyline (&ape->left_skyline_, b, Y_AXIS, LEFT);
233 insert_extent_into_skyline (&ape->right_skyline_ , b,Y_AXIS, RIGHT);
239 for (int i = apes.size(); i--;)
243 for (int j = apes[i]->extents_.size(); j--;)
245 y.unite (apes[i]->extents_[j][Y_AXIS]);
247 apes[i]->vertical_extent_ = y;
250 all_accidental_vertical_extent = total;
251 apes.sort (&ape_compare);
253 Accidental_placement_entry * head_ape = new Accidental_placement_entry;
254 Grob *commonx = common_refpoint_of_array (heads, me, X_AXIS);
255 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
256 Array<Box> head_extents;
257 for (int i = heads.size(); i--;)
259 Box b(heads[i]->extent (commonx, X_AXIS),
260 heads[i]->extent (commony, Y_AXIS));
262 insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
265 head_ape-> left_skyline_ = head_skyline;
266 head_ape->offset_ = 0.0;
268 SCM rs = me->get_grob_property ("right-padding");
269 if (gh_number_p (rs))
270 head_ape->offset_ -= gh_scm2double (rs);
273 apes.push (head_ape);
274 for (int i= apes.size () -1 ; i-- > 0;)
276 Accidental_placement_entry *ape = apes[i];
280 d = - skyline_meshing_distance (ape->right_skyline_,
281 apes[j]->left_skyline_);
284 || j + 1 == apes.size())
294 ape->offset_ += apes[j]->offset_ + d;
298 for (int i = apes.size(); i--;)
300 Accidental_placement_entry* ape = apes[i];
301 for (int j = ape->grobs_.size(); j--;)
303 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
308 Interval left_extent, right_extent;
309 Accidental_placement_entry *ape = apes[0];
311 for (int i = ape->extents_.size(); i--;)
312 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
315 for (int i = ape->extents_.size(); i--;)
316 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
318 SCM ls = me->get_grob_property ("left-padding");
319 if (gh_number_p (rs))
320 left_extent[LEFT] -= gh_scm2double (ls);
323 Interval width(left_extent[LEFT], right_extent[RIGHT]);
325 SCM scm_width = ly_interval2scm (width);
326 me->set_extent (scm_width, X_AXIS);
328 for (int i = apes.size(); i--;)
334 ADD_INTERFACE(Accidental_placement,
335 "accidental-placement-interface",
336 "Take care of complex accidental collisions.",
337 "left-padding right-padding accidentals alignment-done")