]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-placement.cc
* scm/define-music-types.scm (music-descriptions): remove
[lilypond.git] / lily / accidental-placement.cc
1 /*
2   accidental-placement.cc -- implement Accidental_placement
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9
10 #include "accidental-placement.hh"
11
12 #include "accidental-interface.hh"
13 #include "music.hh"
14 #include "note-collision.hh"
15 #include "note-column.hh"
16 #include "pitch.hh"
17 #include "pointer-group-interface.hh"
18 #include "skyline.hh"
19 #include "stream-event.hh"
20 #include "warn.hh"
21
22
23 void
24 Accidental_placement::add_accidental (Grob *me, Grob *a)
25 {
26   a->set_parent (me, X_AXIS);
27   a->set_property ("X-offset", Grob::x_parent_positioning_proc);
28   SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
29
30   Stream_event *mcause = unsmob_stream_event (cause);
31   if (!mcause)
32     {
33       programming_error ("note head has no event cause");
34       return;
35     }
36
37   Pitch *p = unsmob_pitch (mcause->get_property ("pitch"));
38
39   int n = p->get_notename ();
40
41   SCM accs = me->get_object ("accidental-grobs");
42   SCM key = scm_from_int (n);
43   SCM entry = scm_assq (key, accs);
44   if (entry == SCM_BOOL_F)
45     entry = SCM_EOL;
46   else
47     entry = scm_cdr (entry);
48
49   entry = scm_cons (a->self_scm (), entry);
50
51   accs = scm_assq_set_x (accs, key, entry);
52
53   me->set_object ("accidental-grobs", accs);
54 }
55
56 /*
57   Split into break reminders.
58 */
59 void
60 Accidental_placement::split_accidentals (Grob *accs,
61                                          vector<Grob*> *break_reminder,
62                                          vector<Grob*> *real_acc)
63 {
64   for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
65        acs = scm_cdr (acs))
66     for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
67       {
68         Grob *a = unsmob_grob (scm_car (s));
69
70         if (unsmob_grob (a->get_object ("tie")))
71           break_reminder->push_back (a);
72         else
73           real_acc->push_back (a);
74       }
75 }
76
77 /*
78   Accidentals are special, because they appear and disappear after
79   ties at will.
80 */
81 Interval
82 Accidental_placement::get_relevant_accidental_extent (Grob *me,
83                                                       Item *item_col,
84                                                       Grob *left_object)
85 {
86   vector<Grob*> br, ra;
87   vector<Grob*> *which = 0;
88
89   Accidental_placement::split_accidentals (me, &br, &ra);
90   concat (br, ra);
91
92   if (dynamic_cast<Item *> (left_object)->break_status_dir () == RIGHT)
93     which = &br;
94   else
95     which = &ra;
96
97   Interval extent;
98   for (vsize i = 0; i < which->size (); i++)
99     extent.unite (which->at (i)->extent (item_col, X_AXIS));
100
101   if (!extent.is_empty ())
102     {
103       Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
104       extent[LEFT] -= p;
105     }
106
107   return extent;
108 }
109
110 struct Accidental_placement_entry
111 {
112   vector<Skyline_entry> left_skyline_;
113   vector<Skyline_entry> right_skyline_;
114   Interval vertical_extent_;
115   vector<Box> extents_;
116   vector<Grob*> grobs_;
117   Real offset_;
118   int notename_;
119   Accidental_placement_entry ()
120   {
121     offset_ = 0.0;
122     notename_ = -1;
123   }
124 };
125
126 static Interval all_accidental_vertical_extent;
127 Real ape_priority (Accidental_placement_entry const *a)
128 {
129   return a->vertical_extent_[UP];
130 }
131
132 int ape_compare (Accidental_placement_entry *const &a,
133                  Accidental_placement_entry *const &b)
134 {
135   return sign (ape_priority (a) - ape_priority (b));
136 }
137
138 bool ape_less (Accidental_placement_entry *const &a,
139                Accidental_placement_entry *const &b)
140 {
141   return ape_priority (a) < ape_priority (b);
142 }
143
144 int ape_rcompare (Accidental_placement_entry *const &a,
145                   Accidental_placement_entry *const &b)
146 {
147   return -sign (ape_priority (a) - ape_priority (b));
148 }
149
150 /*
151   TODO: should favor
152
153   b
154   b
155
156   placement
157 */
158 void
159 stagger_apes (vector<Accidental_placement_entry*> *apes)
160 {
161   vector<Accidental_placement_entry*> asc = *apes;
162
163   vector_sort (asc, &ape_less);
164
165   apes->clear ();
166
167   int parity = 1;
168   for (vsize i = 0; i < asc.size ();)
169     {
170       Accidental_placement_entry *a = 0;
171       if (parity)
172         {
173           a = asc.back ();
174           asc.pop_back ();
175         }
176       else
177         a = asc[i++];
178
179       apes->push_back (a);
180       parity = !parity;
181     }
182
183   reverse (*apes);
184 }
185
186 /*
187   This routine computes placements of accidentals. During
188   add_accidental (), accidentals are already grouped by note, so that
189   octaves are placed above each other; they form columns. Then the
190   columns are sorted: the biggest columns go closest to the note.
191   Then the columns are spaced as closely as possible (using skyline
192   spacing).
193
194
195   TODO: more advanced placement. Typically, the accs should be placed
196   to form a C shape, like this
197
198
199   ##
200   b b
201   # #
202   b
203   b b
204
205   The naturals should be left of the C as well; they should
206   be separate accs.
207
208   Note that this placement problem looks NP hard, so we just use a
209   simple strategy, not an optimal choice.
210 */
211
212 /*
213   TODO: there should be more space in the following situation
214
215
216   Natural + downstem
217
218   *
219   *  |_
220   *  | |    X
221   *  |_|   |
222   *    |   |
223   *
224
225 */
226
227 MAKE_SCHEME_CALLBACK(Accidental_placement, calc_positioning_done, 1);
228 SCM
229 Accidental_placement::calc_positioning_done (SCM smob)
230 {
231   Grob *me = unsmob_grob (smob);
232   if (!me->is_live ())
233     return SCM_BOOL_T;
234
235   SCM accs = me->get_object ("accidental-grobs");
236   if (!scm_is_pair (accs))
237     return SCM_BOOL_T;
238
239   /*
240     TODO: there is a bug in this code. If two accs are on the same
241     Y-position, they share an Ape, and will be printed in overstrike.
242   */
243   vector<Accidental_placement_entry*> apes;
244   for (SCM s = accs; scm_is_pair (s); s = scm_cdr (s))
245     {
246       Accidental_placement_entry *ape = new Accidental_placement_entry;
247       ape->notename_ = scm_to_int (scm_caar (s));
248
249       for (SCM t = scm_cdar (s); scm_is_pair (t); t = scm_cdr (t))
250         ape->grobs_.push_back (unsmob_grob (scm_car (t)));
251
252       apes.push_back (ape);
253     }
254
255   Grob *common[] = {me, 0};
256
257   /*
258     First we must extract *all* pointers. We can only determine
259     extents if we're sure that we've found the right common refpoint
260   */
261   vector<Grob*> note_cols, heads;
262   for (vsize i = apes.size (); i--;)
263     {
264       Accidental_placement_entry *ape = apes[i];
265       for (vsize j = ape->grobs_.size (); j--;)
266         {
267           Grob *a = ape->grobs_[j];
268
269           if (common[Y_AXIS])
270             common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
271           else
272             common[Y_AXIS] = a;
273
274           Grob *head = a->get_parent (Y_AXIS);
275
276           Grob *col = head->get_parent (X_AXIS);
277           if (Note_column::has_interface (col))
278             note_cols.push_back (col);
279           else
280             heads.push_back (head);
281         }
282     }
283
284   /*
285     This is a little kludgy: to get all notes, we look if there are
286     collisions as well.
287   */
288   for (vsize i = note_cols.size (); i--;)
289     {
290       Grob *c = note_cols[i]->get_parent (X_AXIS);
291       if (Note_collision_interface::has_interface (c))
292         {
293           extract_grob_set (c, "elements", gs);
294
295           concat (note_cols, gs);
296         }
297     }
298
299   for (vsize i = note_cols.size (); i--;)
300     concat (heads, extract_grob_array (note_cols[i], "note-heads"));
301
302   vector_sort (heads, less<Grob*> ());
303   uniq (heads);
304   common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
305
306   for (vsize i = apes.size (); i--;)
307     {
308       Accidental_placement_entry *ape = apes[i];
309       ape->left_skyline_ = empty_skyline (LEFT);
310       ape->right_skyline_ = empty_skyline (RIGHT);
311
312       for (vsize j = apes[i]->grobs_.size (); j--;)
313         {
314           Grob *a = apes[i]->grobs_[j];
315
316           vector<Box> boxes = Accidental_interface::accurate_boxes (a, common);
317
318           ape->extents_.insert (ape->extents_.end (), boxes.begin (), boxes.end ());
319           for (vsize j = boxes.size (); j--;)
320             {
321               insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
322               insert_extent_into_skyline (&ape->right_skyline_, boxes[j], Y_AXIS, RIGHT);
323             }
324         }
325     }
326
327   Interval total;
328   for (vsize i = apes.size (); i--;)
329     {
330       Interval y;
331
332       for (vsize j = apes[i]->extents_.size (); j--;)
333         y.unite (apes[i]->extents_[j][Y_AXIS]);
334       apes[i]->vertical_extent_ = y;
335       total.unite (y);
336     }
337   all_accidental_vertical_extent = total;
338   stagger_apes (&apes);
339
340   Accidental_placement_entry *head_ape = new Accidental_placement_entry;
341   common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
342   vector<Skyline_entry> head_skyline (empty_skyline (LEFT));
343   vector<Box> head_extents;
344   for (vsize i = heads.size (); i--;)
345     {
346       Box b (heads[i]->extent (common[X_AXIS], X_AXIS),
347              heads[i]->extent (common[Y_AXIS], Y_AXIS));
348
349       insert_extent_into_skyline (&head_skyline, b, Y_AXIS, LEFT);
350     }
351
352   head_ape->left_skyline_ = head_skyline;
353   head_ape->offset_ = 0.0;
354
355   Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
356
357   vector<Skyline_entry> left_skyline = head_ape->left_skyline_;
358   heighten_skyline (&left_skyline,
359                     -robust_scm2double (me->get_property ("right-padding"), 0));
360   /*
361     Add accs entries right-to-left.
362   */
363   for (vsize i = apes.size (); i-- > 0;)
364     {
365       Real offset
366         = -skyline_meshing_distance (apes[i]->right_skyline_, left_skyline);
367       if (isinf (offset))
368         offset = (i < apes.size () - 1) ? apes[i + 1]->offset_ : 0.0;
369       else
370         offset -= padding;
371
372       apes[i]->offset_ = offset;
373
374       vector<Skyline_entry> new_left_skyline = apes[i]->left_skyline_;
375       heighten_skyline (&new_left_skyline, apes[i]->offset_);
376       merge_skyline (&new_left_skyline, left_skyline, LEFT);
377       left_skyline = new_left_skyline;
378     }
379
380   for (vsize i = apes.size (); i--;)
381     {
382       Accidental_placement_entry *ape = apes[i];
383       for (vsize j = ape->grobs_.size (); j--;)
384         ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
385     }
386
387   Interval left_extent, right_extent;
388   Accidental_placement_entry *ape = apes[0];
389
390   for (vsize i = ape->extents_.size (); i--;)
391     left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
392
393   ape = apes.back ();
394   for (vsize i = ape->extents_.size (); i--;)
395     right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
396
397   left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
398   Interval width (left_extent[LEFT], right_extent[RIGHT]);
399
400   SCM scm_width = ly_interval2scm (width);
401   me->flush_extent_cache (X_AXIS);
402   me->set_property ("X-extent", scm_width);
403
404   for (vsize i = apes.size (); i--;)
405     delete apes[i];
406
407   return SCM_BOOL_T;
408 }
409
410 ADD_INTERFACE (Accidental_placement,
411                "accidental-placement-interface",
412                "Resolve accidental collisions.",
413
414                /* properties */
415                "accidental-grobs "
416                "left-padding "
417                "padding "
418                "positioning-done "
419                "right-padding ")