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 Real c = a->vertical_extent_.center();
110 far from center means we can fold more. Hopefully.
112 Real center_distance =
113 fabs(c - all_accidental_vertical_extent[LEFT]) >?
114 fabs(c - all_accidental_vertical_extent[RIGHT]);
116 return 20 * a->vertical_extent_.length () + center_distance;
121 int ape_compare (Accidental_placement_entry *const &a,
122 Accidental_placement_entry *const &b)
124 return sign (ape_priority (a) - ape_priority(b));
128 Return: width as SCM interval.
131 This routine computes placements of accidentals. During
132 add_accidental(), accidentals are already grouped by note, so that
133 octaves are placed above each other; they form columns. Then the
134 columns are sorted: the biggest columns go closest to the note.
135 Then the columns are spaced as closely as possible (using skyline
139 TODO: more advanced placement. Typically, the accs should be placed
140 to form a C shape, like this
149 The naturals should be left of the C as well; they should
152 TODO: Try to combine Apes before sorting them: this will allow a
155 Note that this placement problem is almost certainly NP hard, so we
156 just use a simple strategy, not an optimal choice.
160 Accidental_placement::position_accidentals (Grob * me)
162 SCM accs = me->get_grob_property ("accidentals");
164 Link_array<Accidental_placement_entry> apes;
165 for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
167 Accidental_placement_entry *ape = new Accidental_placement_entry;
168 ape->notename_ = gh_scm2int (gh_caar (s));
170 for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
171 ape->grobs_.push (unsmob_grob (gh_car (t)));
180 First we must extract *all* pointers. We can only determine
181 extents if we're sure that we've found the right common refpoint
183 Link_array<Grob> note_cols, heads;
184 for (int i= apes.size (); i--;)
186 Accidental_placement_entry * ape = apes[i];
187 for (int j = ape->grobs_.size(); j--;)
189 Grob * a = ape->grobs_[j];
191 commony = commony->common_refpoint (a, Y_AXIS);
192 Grob *head = a->get_parent (Y_AXIS);
194 Grob * col = head->get_parent (X_AXIS);
195 if (Note_column::has_interface (col))
196 note_cols.push (col);
203 This is a little kludgy: to get all notes, we look if there are
206 for (int i = note_cols.size() ; i--;)
208 Grob *c = note_cols[i]->get_parent (X_AXIS);
209 if (Note_collision_interface::has_interface (c))
211 Link_array<Grob> gs =
212 Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
214 note_cols.concat (gs);
218 for (int i = note_cols.size() ; i--;)
220 heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
225 heads.default_sort();
227 commony = common_refpoint_of_array (heads, commony, Y_AXIS);
230 for (int i= apes.size (); i--;)
232 Accidental_placement_entry * ape = apes[i];
233 ape->left_skyline_ = empty_skyline (LEFT);
234 ape->right_skyline_ = empty_skyline (RIGHT);
236 for (int j = apes[i]->grobs_.size(); j--;)
238 Grob * a = apes[i]->grobs_[j];
240 b[X_AXIS] = a->extent (me, X_AXIS);
241 b[Y_AXIS] = a->extent (commony, Y_AXIS);
243 ape->extents_.push (b);
246 TODO: replace the extents of a flat by combination of two
247 bboxes, so that we use the shape of the flat better.
249 insert_extent_into_skyline (&ape->left_skyline_, b, Y_AXIS, LEFT);
250 insert_extent_into_skyline (&ape->right_skyline_ , b,Y_AXIS, RIGHT);
256 for (int i = apes.size(); i--;)
260 for (int j = apes[i]->extents_.size(); j--;)
262 y.unite (apes[i]->extents_[j][Y_AXIS]);
264 apes[i]->vertical_extent_ = y;
267 all_accidental_vertical_extent = total;
268 apes.sort (&ape_compare);
270 Accidental_placement_entry * head_ape = new Accidental_placement_entry;
271 Grob *commonx = common_refpoint_of_array (heads, me, X_AXIS);
272 Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
273 Array<Box> head_extents;
274 for (int i = heads.size(); i--;)
276 Box b(heads[i]->extent (commonx, X_AXIS),
277 heads[i]->extent (commony, Y_AXIS));
279 insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
282 head_ape-> left_skyline_ = head_skyline;
283 head_ape->offset_ = 0.0;
285 SCM rs = me->get_grob_property ("right-padding");
286 if (gh_number_p (rs))
287 head_ape->offset_ -= gh_scm2double (rs);
291 SCM spad = me->get_grob_property ("padding");
292 if (gh_number_p (spad))
293 padding = gh_scm2double (spad);
295 apes.push (head_ape);
296 for (int i= apes.size () -1 ; i-- > 0;)
298 Accidental_placement_entry *ape = apes[i];
302 d = - skyline_meshing_distance (ape->right_skyline_,
303 apes[j]->left_skyline_);
306 || j + 1 == apes.size())
316 ape->offset_ += apes[j]->offset_ + d;
320 for (int i = apes.size(); i--;)
322 Accidental_placement_entry* ape = apes[i];
323 for (int j = ape->grobs_.size(); j--;)
325 ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
330 Interval left_extent, right_extent;
331 Accidental_placement_entry *ape = apes[0];
333 for (int i = ape->extents_.size(); i--;)
334 left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
337 for (int i = ape->extents_.size(); i--;)
338 right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
340 SCM ls = me->get_grob_property ("left-padding");
341 if (gh_number_p (rs))
342 left_extent[LEFT] -= gh_scm2double (ls);
345 Interval width(left_extent[LEFT], right_extent[RIGHT]);
347 SCM scm_width = ly_interval2scm (width);
348 me->set_extent (scm_width, X_AXIS);
350 for (int i = apes.size(); i--;)
356 ADD_INTERFACE(Accidental_placement,
357 "accidental-placement-interface",
358 "Take care of complex accidental collisions.",
359 "left-padding padding right-padding accidentals alignment-done")