]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-placement.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[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           break_reminder->push_back (a);
73         else
74           real_acc->push_back (a);
75       }
76 }
77
78 vector<Grob*>
79 Accidental_placement::get_break_reminder_accidentals (vector<Grob*> const &elts, Grob *left)
80 {
81   vector<Grob*> br;
82   vector<Grob*> ra;
83   vector<Grob*> ret;
84
85   if (dynamic_cast<Item *> (left)->break_status_dir () != RIGHT)
86     return vector<Grob*> ();
87
88   for (vsize i = 0; i < elts.size (); i++)
89     {
90       split_accidentals (elts[i], &br, &ra);
91       ret.insert (ret.end (), br.begin (), br.end ());
92     }
93   return ret;
94 }
95
96 /*
97   Accidentals are special, because they appear and disappear after
98   ties at will.
99 */
100 Interval
101 Accidental_placement::get_relevant_accidental_extent (Grob *me,
102                                                       Item *item_col,
103                                                       Grob *left_object)
104 {
105   vector<Grob*> br, ra;
106   vector<Grob*> *which = 0;
107
108   Accidental_placement::split_accidentals (me, &br, &ra);
109   concat (br, ra);
110
111   if (dynamic_cast<Item *> (left_object)->break_status_dir () == RIGHT)
112     which = &br;
113   else
114     which = &ra;
115
116   Interval extent;
117   for (vsize i = 0; i < which->size (); i++)
118     extent.unite (which->at (i)->extent (item_col, X_AXIS));
119
120   if (!extent.is_empty ())
121     {
122       Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
123       extent[LEFT] -= p;
124     }
125
126   return extent;
127 }
128
129 struct Accidental_placement_entry
130 {
131   Skyline left_skyline_;
132   Skyline right_skyline_;
133   Interval vertical_extent_;
134   vector<Box> extents_;
135   vector<Grob*> grobs_;
136   Real offset_;
137   int notename_;
138   Accidental_placement_entry ()
139   {
140     offset_ = 0.0;
141     notename_ = -1;
142   }
143 };
144
145 static Interval all_accidental_vertical_extent;
146 Real ape_priority (Accidental_placement_entry const *a)
147 {
148   return a->vertical_extent_[UP];
149 }
150
151 int ape_compare (Accidental_placement_entry *const &a,
152                  Accidental_placement_entry *const &b)
153 {
154   return sign (ape_priority (a) - ape_priority (b));
155 }
156
157 bool ape_less (Accidental_placement_entry *const &a,
158                Accidental_placement_entry *const &b)
159 {
160   return ape_priority (a) < ape_priority (b);
161 }
162
163 int ape_rcompare (Accidental_placement_entry *const &a,
164                   Accidental_placement_entry *const &b)
165 {
166   return -sign (ape_priority (a) - ape_priority (b));
167 }
168
169 /*
170   TODO: should favor
171
172   b
173   b
174
175   placement
176 */
177 void
178 stagger_apes (vector<Accidental_placement_entry*> *apes)
179 {
180   vector<Accidental_placement_entry*> asc = *apes;
181
182   vector_sort (asc, &ape_less);
183
184   apes->clear ();
185
186   int parity = 1;
187   for (vsize i = 0; i < asc.size ();)
188     {
189       Accidental_placement_entry *a = 0;
190       if (parity)
191         {
192           a = asc.back ();
193           asc.pop_back ();
194         }
195       else
196         a = asc[i++];
197
198       apes->push_back (a);
199       parity = !parity;
200     }
201
202   reverse (*apes);
203 }
204
205 /*
206   This routine computes placements of accidentals. During
207   add_accidental (), accidentals are already grouped by note, so that
208   octaves are placed above each other; they form columns. Then the
209   columns are sorted: the biggest columns go closest to the note.
210   Then the columns are spaced as closely as possible (using skyline
211   spacing).
212
213
214   TODO: more advanced placement. Typically, the accs should be placed
215   to form a C shape, like this
216
217
218   ##
219   b b
220   # #
221   b
222   b b
223
224   The naturals should be left of the C as well; they should
225   be separate accs.
226
227   Note that this placement problem looks NP hard, so we just use a
228   simple strategy, not an optimal choice.
229 */
230
231 /*
232   TODO: there should be more space in the following situation
233
234
235   Natural + downstem
236
237   *
238   *  |_
239   *  | |    X
240   *  |_|   |
241   *    |   |
242   *
243
244 */
245
246 MAKE_SCHEME_CALLBACK (Accidental_placement, calc_positioning_done, 1);
247 SCM
248 Accidental_placement::calc_positioning_done (SCM smob)
249 {
250   Grob *me = unsmob_grob (smob);
251   if (!me->is_live ())
252     return SCM_BOOL_T;
253
254   me->set_property ("positioning-done", SCM_BOOL_T);
255   
256   SCM accs = me->get_object ("accidental-grobs");
257   if (!scm_is_pair (accs))
258     return SCM_BOOL_T;
259
260   /*
261     TODO: there is a bug in this code. If two accs are on the same
262     Y-position, they share an Ape, and will be printed in overstrike.
263   */
264   vector<Accidental_placement_entry*> apes;
265   for (SCM s = accs; scm_is_pair (s); s = scm_cdr (s))
266     {
267       Accidental_placement_entry *ape = new Accidental_placement_entry;
268       ape->notename_ = scm_to_int (scm_caar (s));
269
270       for (SCM t = scm_cdar (s); scm_is_pair (t); t = scm_cdr (t))
271         ape->grobs_.push_back (unsmob_grob (scm_car (t)));
272
273       apes.push_back (ape);
274     }
275
276   Grob *common[] = {me, 0};
277
278   /*
279     First we must extract *all* pointers. We can only determine
280     extents if we're sure that we've found the right common refpoint
281   */
282   vector<Grob*> note_cols, heads;
283   for (vsize i = apes.size (); i--;)
284     {
285       Accidental_placement_entry *ape = apes[i];
286       for (vsize j = ape->grobs_.size (); j--;)
287         {
288           Grob *a = ape->grobs_[j];
289
290           if (common[Y_AXIS])
291             common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
292           else
293             common[Y_AXIS] = a;
294
295           Grob *head = a->get_parent (Y_AXIS);
296
297           Grob *col = head->get_parent (X_AXIS);
298           if (Note_column::has_interface (col))
299             note_cols.push_back (col);
300           else
301             heads.push_back (head);
302         }
303     }
304
305   /*
306     This is a little kludgy: to get all notes, we look if there are
307     collisions as well.
308   */
309   for (vsize i = note_cols.size (); i--;)
310     {
311       Grob *c = note_cols[i]->get_parent (X_AXIS);
312       if (Note_collision_interface::has_interface (c))
313         {
314           extract_grob_set (c, "elements", gs);
315
316           concat (note_cols, gs);
317         }
318     }
319
320   for (vsize i = note_cols.size (); i--;)
321     concat (heads, extract_grob_array (note_cols[i], "note-heads"));
322
323   vector_sort (heads, less<Grob*> ());
324   uniq (heads);
325
326   vector<Grob *> stems;
327   for (vsize i = 0; i < heads.size  (); i++)
328     {
329       if (Grob *s = Rhythmic_head::get_stem (heads[i]))
330         stems.push_back (s);
331     }
332   
333   vector_sort (stems, less<Grob*> ());
334   uniq (stems);
335
336   common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
337   common[Y_AXIS] = common_refpoint_of_array (stems, common[Y_AXIS], Y_AXIS);
338
339   for (vsize i = 0; i < heads.size  (); i++)
340     {
341       if (Grob *s = Rhythmic_head::get_stem (heads[i]))
342         {
343           stems.push_back (s);
344           common[Y_AXIS] = s->common_refpoint (common[Y_AXIS], Y_AXIS);
345         }
346     }
347
348   vector_sort (stems, less<Grob*> ());
349   uniq (stems);
350   
351
352   for (vsize i = apes.size (); i--;)
353     {
354       Accidental_placement_entry *ape = apes[i];
355
356       for (vsize j = apes[i]->grobs_.size (); j--;)
357         {
358           Grob *a = apes[i]->grobs_[j];
359           vector<Box> boxes = Accidental_interface::accurate_boxes (a, common);
360
361           ape->extents_.insert (ape->extents_.end (), boxes.begin (), boxes.end ());
362         }
363       ape->left_skyline_ = Skyline (ape->extents_, 0, Y_AXIS, LEFT);
364       ape->right_skyline_ = Skyline (ape->extents_, 0, Y_AXIS, RIGHT);
365     }
366
367   Interval total;
368   for (vsize i = apes.size (); i--;)
369     {
370       Interval y;
371
372       for (vsize j = apes[i]->extents_.size (); j--;)
373         y.unite (apes[i]->extents_[j][Y_AXIS]);
374       apes[i]->vertical_extent_ = y;
375       total.unite (y);
376     }
377   all_accidental_vertical_extent = total;
378   stagger_apes (&apes);
379
380   Accidental_placement_entry *head_ape = new Accidental_placement_entry;
381   common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);
382   
383   vector<Box> head_extents;
384   for (vsize i = heads.size (); i--;)
385     head_extents.push_back (Box (heads[i]->extent (common[X_AXIS], X_AXIS),
386                                  heads[i]->extent (common[Y_AXIS], Y_AXIS)));
387
388   for (vsize i = 0; i < stems.size (); i ++)
389     {
390       int very_large = INT_MAX;
391       
392       head_extents.push_back (Box (stems[i]->extent (common[X_AXIS], X_AXIS),
393                                    stems[i]->pure_height (common[Y_AXIS], 0, very_large)));
394     }
395
396   head_ape->left_skyline_ = Skyline (head_extents, 0, Y_AXIS, LEFT);
397   head_ape->offset_ = 0.0;
398
399   Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
400
401   Skyline left_skyline = head_ape->left_skyline_;
402   left_skyline.raise (-robust_scm2double (me->get_property ("right-padding"), 0));
403   
404   /*
405     Add accs entries right-to-left.
406   */
407   for (vsize i = apes.size (); i-- > 0;)
408     {
409       Real offset = -apes[i]->right_skyline_.distance (left_skyline);
410       if (isinf (offset))
411         offset = (i + 1 < apes.size ()) ? apes[i + 1]->offset_ : 0.0;
412       else
413         offset -= padding;
414
415       apes[i]->offset_ = offset;
416
417       Skyline new_left_skyline = apes[i]->left_skyline_;
418       new_left_skyline.raise (apes[i]->offset_);
419       new_left_skyline.merge (left_skyline);
420       left_skyline = new_left_skyline;
421     }
422
423   for (vsize i = apes.size (); i--;)
424     {
425       Accidental_placement_entry *ape = apes[i];
426       for (vsize j = ape->grobs_.size (); j--;)
427         ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
428     }
429
430   Interval left_extent, right_extent;
431   Accidental_placement_entry *ape = apes[0];
432
433   for (vsize i = ape->extents_.size (); i--;)
434     left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
435
436   ape = apes.back ();
437   for (vsize i = ape->extents_.size (); i--;)
438     right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
439
440   left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
441   Interval width (left_extent[LEFT], right_extent[RIGHT]);
442
443   SCM scm_width = ly_interval2scm (width);
444   me->flush_extent_cache (X_AXIS);
445   me->set_property ("X-extent", scm_width);
446
447   junk_pointers (apes);
448
449   delete head_ape;
450   
451   return SCM_BOOL_T;
452 }
453
454 ADD_INTERFACE (Accidental_placement,
455                "Resolve accidental collisions.",
456
457                /* properties */
458                "accidental-grobs "
459                "left-padding "
460                "padding "
461                "positioning-done "
462                "right-padding "
463                "script-priority "
464                )