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