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