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