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