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