]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-placement.cc
*** empty log message ***
[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
10
11 #include "accidental-placement.hh"
12 #include "skyline.hh"
13 #include "music.hh"
14 #include "pitch.hh"
15 #include "warn.hh"
16 #include "note-column.hh"
17 #include "pointer-group-interface.hh"
18 #include "note-collision.hh"
19 #include "accidental-interface.hh"
20
21 MAKE_SCHEME_CALLBACK (Accidental_placement, alignment_callback, 2);
22 SCM
23 Accidental_placement::alignment_callback (SCM s, SCM)
24 {
25   Grob *me = unsmob_grob (s);
26
27   Grob *par = me->get_parent (X_AXIS);
28   if (!to_boolean (par->get_property ("positioning-done")))
29     {
30       par->set_property ("positioning-done", SCM_BOOL_T);
31       position_accidentals (par);
32     }
33
34   return scm_from_int (0);
35 }
36
37 void
38 Accidental_placement::add_accidental (Grob *me, Grob *a)
39 {
40   a->set_parent (me, X_AXIS);
41   a->add_offset_callback (alignment_callback_proc, X_AXIS);
42   SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
43
44   Music *mcause = unsmob_music (cause);
45   if (!mcause)
46     {
47       programming_error ("note head has no music cause");
48       return;
49     }
50
51   Pitch *p = unsmob_pitch (mcause->get_property ("pitch"));
52
53   int n = p->get_notename ();
54
55   SCM accs = me->get_object ("accidental-grobs");
56   SCM key = scm_from_int (n);
57   SCM entry = scm_assq (key, accs);
58   if (entry == SCM_BOOL_F)
59     entry = SCM_EOL;
60   else
61     entry = scm_cdr (entry);
62
63   entry = scm_cons (a->self_scm (), entry);
64
65   accs = scm_assq_set_x (accs, key, entry);
66
67   me->set_object ("accidental-grobs", accs);
68 }
69
70 /*
71   Split into break reminders.
72 */
73 void
74 Accidental_placement::split_accidentals (Grob *accs,
75                                          Link_array<Grob> *break_reminder,
76                                          Link_array<Grob> *real_acc)
77 {
78   for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
79        acs = scm_cdr (acs))
80     for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
81       {
82         Grob *a = unsmob_grob (scm_car (s));
83
84         if (unsmob_grob (a->get_object ("tie")))
85           break_reminder->push (a);
86         else
87           real_acc->push (a);
88       }
89 }
90
91 /*
92   Accidentals are special, because they appear and disappear after
93   ties at will.
94 */
95 Interval
96 Accidental_placement::get_relevant_accidental_extent (Grob *me,
97                                                       Item *item_col,
98                                                       Grob *left_object)
99 {
100   Link_array<Grob> br, ra;
101   Link_array<Grob> *which = 0;
102
103   Accidental_placement::split_accidentals (me, &br, &ra);
104   br.concat (ra);
105
106   if (dynamic_cast<Item *> (left_object)->break_status_dir () == RIGHT)
107     which = &br;
108   else
109     which = &ra;
110
111   Interval extent;
112   for (int i = 0; i < which->size (); i++)
113     extent.unite (which->elem (i)->extent (item_col, X_AXIS));
114
115   if (!extent.is_empty ())
116     {
117       Real p = robust_scm2double (me->get_property ("left-padding"), 0.2);
118       extent[LEFT] -= p;
119     }
120
121   return extent;
122 }
123
124 struct Accidental_placement_entry
125 {
126   Array<Skyline_entry> left_skyline_;
127   Array<Skyline_entry> right_skyline_;
128   Interval vertical_extent_;
129   Array<Box> extents_;
130   Link_array<Grob> grobs_;
131   Real offset_;
132   int notename_;
133   Accidental_placement_entry ()
134   {
135     offset_ = 0.0;
136     notename_ = -1;
137   }
138 };
139
140 static Interval all_accidental_vertical_extent;
141 Real ape_priority (Accidental_placement_entry const *a)
142 {
143   return a->vertical_extent_[UP];
144 }
145
146 int ape_compare (Accidental_placement_entry *const &a,
147                  Accidental_placement_entry *const &b)
148 {
149   return sign (ape_priority (a) - ape_priority (b));
150 }
151
152 int ape_rcompare (Accidental_placement_entry *const &a,
153                   Accidental_placement_entry *const &b)
154 {
155   return -sign (ape_priority (a) - ape_priority (b));
156 }
157
158 /*
159   TODO: should favor
160
161   b
162   b
163
164   placement
165 */
166 void
167 stagger_apes (Link_array<Accidental_placement_entry> *apes)
168 {
169   Link_array<Accidental_placement_entry> asc = *apes;
170
171   asc.sort (&ape_compare);
172
173   apes->clear ();
174
175   int i = 0;
176   int parity = 1;
177   while (i < asc.size ())
178     {
179       Accidental_placement_entry *a = 0;
180       if (parity)
181         a = asc.pop ();
182       else
183         a = asc[i++];
184
185       apes->push (a);
186       parity = !parity;
187     }
188
189   apes->reverse ();
190 }
191
192 /*
193   This routine computes placements of accidentals. During
194   add_accidental (), accidentals are already grouped by note, so that
195   octaves are placed above each other; they form columns. Then the
196   columns are sorted: the biggest columns go closest to the note.
197   Then the columns are spaced as closely as possible (using skyline
198   spacing).
199
200
201   TODO: more advanced placement. Typically, the accs should be placed
202   to form a C shape, like this
203
204
205   ##
206   b b
207   # #
208   b
209   b b
210
211   The naturals should be left of the C as well; they should
212   be separate accs.
213
214   Note that this placement problem looks NP hard, so we just use a
215   simple strategy, not an optimal choice.
216 */
217
218 /*
219   TODO: there should be more space in the following situation
220
221
222   Natural + downstem
223
224   |_
225   | |    X
226   |_|   |
227   |   |
228 */
229 SCM
230 Accidental_placement::position_accidentals (Grob *me)
231 {
232   if (!me->is_live ())
233     return SCM_UNSPECIFIED;
234
235   SCM accs = me->get_object ("accidental-grobs");
236   if (!scm_is_pair (accs))
237     return SCM_UNSPECIFIED;
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   Link_array<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 (unsmob_grob (scm_car (t)));
251
252       apes.push (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   Link_array<Grob> note_cols, heads;
262   for (int i = apes.size (); i--;)
263     {
264       Accidental_placement_entry *ape = apes[i];
265       for (int 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 (col);
279           else
280             heads.push (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 (int 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           note_cols.concat (gs);
296         }
297     }
298
299   for (int i = note_cols.size (); i--;)
300     heads.concat (extract_grob_array (note_cols[i], "note-heads"));
301
302   heads.default_sort ();
303   heads.uniq ();
304   common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
305
306   for (int 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 (int j = apes[i]->grobs_.size (); j--;)
313         {
314           Grob *a = apes[i]->grobs_[j];
315
316           Array<Box> boxes = Accidental_interface::accurate_boxes (a, common);
317
318           ape->extents_.concat (boxes);
319           for (int 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 (int i = apes.size (); i--;)
329     {
330       Interval y;
331
332       for (int 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   Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
343   Array<Box> head_extents;
344   for (int 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   Array<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 (int 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       Array<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 (int i = apes.size (); i--;)
381     {
382       Accidental_placement_entry *ape = apes[i];
383       for (int 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 (int i = ape->extents_.size (); i--;)
391     left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
392
393   ape = apes.top ();
394   for (int 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->set_extent (scm_width, X_AXIS);
402
403   for (int i = apes.size (); i--;)
404     delete apes[i];
405
406   return SCM_UNSPECIFIED;
407 }
408
409 ADD_INTERFACE (Accidental_placement,
410                "accidental-placement-interface",
411                "Resolve accidental collisions.",
412                "left-padding padding right-padding accidental-grobs positioning-done")