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"
20 MAKE_SCHEME_CALLBACK(Accidental_placement,extent_callback, 2);
22 Accidental_placement::extent_callback(SCM s, SCM axis)
24 Grob * me =unsmob_grob (s);
25 Axis a = Axis (gh_scm2int (axis));
29 SCM w = position_accidentals (me);
33 MAKE_SCHEME_CALLBACK(Accidental_placement,alignment_callback, 2);
35 Accidental_placement::alignment_callback(SCM s, SCM )
37 Grob * me =unsmob_grob (s);
39 Grob * par = me->get_parent (X_AXIS);
40 if (!to_boolean (par->get_grob_property ("alignment-done")))
42 par->set_grob_property ("alignment-done", SCM_BOOL_T);
43 position_accidentals (par);
46 return gh_int2scm (0);
52 Accidental_placement::add_accidental (Grob* me, Grob* a)
54 a->set_parent (me, X_AXIS);
55 a->add_offset_callback (alignment_callback_proc, X_AXIS);
56 SCM cause = a->get_parent (Y_AXIS)->get_grob_property("cause");
58 Music *mcause =unsmob_music (cause);
61 programming_error ("Note head has no music cause!");
65 Pitch *p= unsmob_pitch (mcause->get_mus_property ("pitch"));
67 int n = p->notename_i_;
69 SCM accs = me->get_grob_property ("accidentals");
70 SCM key = gh_int2scm (n);
71 SCM entry = scm_assq (key, accs);
72 if (entry == SCM_BOOL_F)
77 entry = gh_cdr (entry);
79 entry = gh_cons (a->self_scm (), entry);
81 accs = scm_assq_set_x (accs, key, entry);
83 me->set_grob_property ("accidentals", accs);
86 struct Accidental_placement_entry
88 Array<Skyline_entry> left_skyline_;
89 Array<Skyline_entry> right_skyline_;
90 Interval vertical_extent_;
92 Link_array<Grob> grobs_;
95 Accidental_placement_entry()
103 int ape_compare (Accidental_placement_entry *const &a,
104 Accidental_placement_entry *const &b)
106 return sign (a->vertical_extent_.length () - b->vertical_extent_.length());
110 Return: width as SCM interval.
113 This routine computes placements of accidentals. During
114 add_accidental(), accidentals are already grouped by note, so that
115 octaves are placed above each other; they form columns. Then the
116 columns are sorted: the biggest columns go closest to the note.
117 Then the columns are spaced as closely as possible (using skyline
121 TODO: more advanced placement. Typically, the accs should be placed
122 to form a C shape, like this
131 The naturals should be left of the C as well; they should
134 TODO: Try to combine Apes before sorting them: this will allow a
137 Note that this placement problem is almost certainly NP hard, so we
138 just use a simple strategy, not an optimal choice.
142 Accidental_placement::position_accidentals (Grob * me)
144 SCM accs = me->get_grob_property ("accidentals");
146 Link_array<Accidental_placement_entry> apes;
147 for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
149 Accidental_placement_entry *ape = new Accidental_placement_entry;
150 ape->notename_ = gh_scm2int (gh_caar (s));
152 for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
153 ape->grobs_.push (unsmob_grob (gh_car (t)));
160 for (int i= apes.size (); i--;)
161 commony = common_refpoint_of_array (apes[i]->grobs_, commony, Y_AXIS);
163 Link_array<Grob> heads;
164 for (int i= apes.size (); i--;)
166 Accidental_placement_entry * ape = apes[i];
167 ape->left_skyline_ = empty_skyline ( LEFT);
168 ape->right_skyline_ = empty_skyline ( RIGHT);
170 for (int j = apes[i]->grobs_.size(); j--;)
172 Grob * a = apes[i]->grobs_[j];
174 b[X_AXIS] = a->extent (me, X_AXIS);
175 b[Y_AXIS] = a->extent (commony, Y_AXIS);
177 Grob *head = a->get_parent (Y_AXIS);
179 commony = commony->common_refpoint (head, Y_AXIS);
181 ape->extents_.push (b);
185 TODO: replace the extents of a flat by combination of two
186 bboxes, so that we use the shape of the flat better.
188 insert_extent_into_skyline (&ape->left_skyline_, b, Y_AXIS, LEFT);
189 insert_extent_into_skyline (&ape->right_skyline_ , b,Y_AXIS, RIGHT);
193 for (int i = apes.size(); i--;)
197 for (int j = apes[i]->extents_.size(); j--;)
199 y.unite (apes[i]->extents_[j][Y_AXIS]);
201 apes[i]->vertical_extent_ = y;
204 apes.sort (&ape_compare);
206 Accidental_placement_entry * head_ape = new Accidental_placement_entry;
207 Grob *commonx = common_refpoint_of_array (heads, me, X_AXIS);
208 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
209 Array<Box> head_extents;
210 for (int i = heads.size(); i--;)
212 Box b(heads[i]->extent (commonx, X_AXIS),
213 heads[i]->extent (commony, Y_AXIS));
215 insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
218 head_ape-> left_skyline_ = head_skyline;
219 head_ape->offset_ = 0.0;
221 SCM rs = me->get_grob_property ("right-padding");
222 if (gh_number_p (rs))
223 head_ape->offset_ -= gh_scm2double (rs);
226 apes.push (head_ape);
227 for (int i= apes.size () -1 ; i-- > 0;)
229 Accidental_placement_entry *ape = apes[i];
232 confusing naming: left_skyline is a skyline pointing to the
233 left. It is on the right of the curent entry.
238 Array<Skyline_entry> const *right_sky =
240 ? &apes[j]->left_skyline_
243 d = - skyline_meshing_distance (ape->right_skyline_,
247 || j + 1 == apes.size())
257 ape->offset_ += apes[j]->offset_ + d;
261 for (int i = apes.size(); i--;)
263 Accidental_placement_entry* ape = apes[i];
264 for (int j = ape->grobs_.size(); j--;)
266 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
271 Interval left_extent, right_extent;
272 Accidental_placement_entry *ape = apes[0];
274 for (int i = ape->extents_.size(); i--;)
275 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
278 for (int i = ape->extents_.size(); i--;)
279 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
281 SCM ls = me->get_grob_property ("left-padding");
282 if (gh_number_p (rs))
283 left_extent[LEFT] -= gh_scm2double (ls);
286 Interval width(left_extent[LEFT], right_extent[RIGHT]);
288 SCM scm_width = ly_interval2scm (width);
289 me->set_extent (scm_width, X_AXIS);
291 for (int i = apes.size(); i--;)
297 ADD_INTERFACE(Accidental_placement,
298 "accidental-placement-interface",
299 "Take care of complex accidental collisions.",
300 "left-padding right-padding accidentals alignment-done")