]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-placement.cc
* lily/accidental-placement.cc (stagger_apes): try to arrange accs
[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 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
22 MAKE_SCHEME_CALLBACK(Accidental_placement,extent_callback, 2);
23 SCM
24 Accidental_placement::extent_callback(SCM s, SCM axis)
25 {
26   Grob * me =unsmob_grob (s);
27   Axis a = Axis (gh_scm2int (axis));
28
29   assert (a == X_AXIS);
30
31   SCM w = position_accidentals (me);
32   return w;
33 }
34
35 MAKE_SCHEME_CALLBACK(Accidental_placement,alignment_callback, 2);
36 SCM
37 Accidental_placement::alignment_callback(SCM s, SCM )
38 {
39   Grob * me =unsmob_grob (s);
40
41   Grob * par = me->get_parent (X_AXIS);
42   if (!to_boolean (par->get_grob_property ("alignment-done")))
43     {
44       par->set_grob_property ("alignment-done", SCM_BOOL_T);
45       position_accidentals (par);
46     }
47
48   return gh_int2scm (0);
49 }
50
51
52
53 void
54 Accidental_placement::add_accidental (Grob* me, Grob* a)
55 {
56   a->set_parent (me, X_AXIS);
57   a->add_offset_callback (alignment_callback_proc, X_AXIS);
58   SCM cause = a->get_parent (Y_AXIS)->get_grob_property("cause");
59
60   Music *mcause =unsmob_music (cause); 
61   if (!mcause)
62     {
63       programming_error ("Note head has no music cause!");
64       return; 
65     }
66
67   Pitch *p= unsmob_pitch (mcause->get_mus_property ("pitch"));
68
69   int n = p->notename_i_;
70
71   SCM accs = me->get_grob_property ("accidentals");
72   SCM key = gh_int2scm (n);
73   SCM entry = scm_assq (key, accs);
74   if (entry == SCM_BOOL_F)
75     {
76       entry = SCM_EOL;
77     }
78   else
79     entry = gh_cdr (entry);
80
81   entry = gh_cons (a->self_scm (), entry);
82
83   accs = scm_assq_set_x (accs,  key, entry);
84
85   me->set_grob_property ("accidentals", accs);
86 }
87
88 struct Accidental_placement_entry
89 {
90   Array<Skyline_entry> left_skyline_;
91   Array<Skyline_entry> right_skyline_;
92   Interval vertical_extent_;
93   Array<Box> extents_;
94   Link_array<Grob> grobs_;
95   Real offset_; 
96   int notename_;
97   Accidental_placement_entry()
98   {
99     offset_ =0.0;
100     notename_ = -1;
101   }
102 };
103
104 static Interval all_accidental_vertical_extent;
105 Real ape_priority (Accidental_placement_entry const * a)
106 {
107   return a->vertical_extent_[UP];
108 }
109
110   
111
112 int ape_compare (Accidental_placement_entry *const &a,
113                  Accidental_placement_entry *const &b)
114 {
115   return sign (ape_priority (a) - ape_priority(b));
116 }
117
118 int ape_rcompare (Accidental_placement_entry *const &a,
119                  Accidental_placement_entry *const &b)
120 {
121   return -sign (ape_priority (a) - ape_priority(b));
122 }
123
124
125 /*
126
127 TODO: should favor
128
129   b
130  b
131
132 placement
133  
134 */
135 void
136 stagger_apes (Link_array<Accidental_placement_entry> *apes)
137 {
138   Link_array<Accidental_placement_entry> asc = *apes;
139
140
141   asc.sort (&ape_compare);
142
143   apes->clear();
144
145   int i =0;
146   int parity = 1;
147   while (i < asc.size())
148     {
149       Accidental_placement_entry * a = 0;      
150       if (parity)
151         a = asc.pop();
152       else
153         a = asc[i++];
154
155       apes->push (a);
156       parity = !parity;
157     }
158
159   apes->reverse();
160 }
161
162   
163
164 /*
165   Return: width as SCM interval.
166
167
168   This routine computes placements of accidentals. During
169   add_accidental(), accidentals are already grouped by note, so that
170   octaves are placed above each other; they form columns. Then the
171   columns are sorted: the biggest columns go closest to the note.
172   Then the columns are spaced as closely as possible (using skyline
173   spacing).
174   
175   
176   TODO: more advanced placement. Typically, the accs should be placed
177   to form a C shape, like this
178
179   
180            ##
181         b b
182        # #
183         b
184           b b
185
186    The naturals should be left of the C as well; they should
187    be separate accs.
188
189    TODO: Try to combine Apes before sorting them: this will allow a
190    better placement.
191
192    Note that this placement problem is almost certainly NP hard, so we
193    just use a simple strategy, not an optimal choice.
194 */
195
196 SCM
197 Accidental_placement::position_accidentals (Grob * me)
198 {
199   SCM accs = me->get_grob_property ("accidentals");
200
201   Link_array<Accidental_placement_entry> apes;
202   for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
203     {
204       Accidental_placement_entry *ape = new Accidental_placement_entry;
205       ape->notename_ = gh_scm2int (gh_caar (s));
206       
207       for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
208         ape->grobs_.push (unsmob_grob (gh_car (t)));
209
210       apes.push (ape);
211     }
212
213
214   Grob *commony =0 ;
215
216   /*
217     First we must extract *all* pointers. We can only determine
218     extents if we're sure that we've found the right common refpoint
219    */
220   Link_array<Grob> note_cols, heads;
221   for (int i= apes.size (); i--;)
222     { 
223       Accidental_placement_entry * ape = apes[i];
224       for (int j = ape->grobs_.size(); j--;)
225         {
226           Grob * a = ape->grobs_[j];
227           
228           commony = commony->common_refpoint (a, Y_AXIS);
229           Grob *head = a->get_parent (Y_AXIS);
230
231           Grob * col = head->get_parent (X_AXIS);
232           if (Note_column::has_interface (col))
233             note_cols.push (col);
234           else
235             heads.push (head);
236         }
237     }
238
239   /*
240     This is a little kludgy: to get all notes, we look if there are
241     collisions as well.
242    */
243   for (int i = note_cols.size() ; i--;)
244     {
245       Grob *c = note_cols[i]->get_parent (X_AXIS);
246       if (Note_collision_interface::has_interface (c))
247         {
248           Link_array<Grob> gs =
249             Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
250       
251           note_cols.concat (gs);
252         }
253     }
254   
255   for (int i = note_cols.size() ; i--;)
256     {
257       heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
258                                                             (Grob*)0,
259                                                             "note-heads"));
260       
261     }
262   heads.default_sort();
263   heads.uniq();
264   commony = common_refpoint_of_array (heads, commony, Y_AXIS);
265
266   
267   for (int i= apes.size (); i--;)
268     {
269       Accidental_placement_entry * ape = apes[i];
270       ape->left_skyline_ = empty_skyline (LEFT);
271       ape->right_skyline_ = empty_skyline (RIGHT);
272    
273       for (int j = apes[i]->grobs_.size(); j--;)
274         {
275           Grob * a = apes[i]->grobs_[j];
276           Box b;
277           b[X_AXIS] = a->extent (me, X_AXIS);
278           b[Y_AXIS] = a->extent (commony, Y_AXIS);
279
280           ape->extents_.push (b);
281           
282           /*
283             TODO: replace the extents of a flat by combination of two
284             bboxes, so that we use the shape of the flat better.
285           */
286           insert_extent_into_skyline (&ape->left_skyline_, b, Y_AXIS, LEFT);
287           insert_extent_into_skyline (&ape->right_skyline_ , b,Y_AXIS, RIGHT);
288         }
289     }
290
291
292   Interval total;
293   for (int i = apes.size(); i--;)
294     {
295       Interval y ;
296       
297       for (int j = apes[i]->extents_.size(); j--;)
298         {
299           y.unite (apes[i]->extents_[j][Y_AXIS]);
300         }
301       apes[i]->vertical_extent_ = y;
302       total.unite (y);
303     }
304   all_accidental_vertical_extent = total;
305   stagger_apes (&apes);
306
307   Accidental_placement_entry * head_ape = new Accidental_placement_entry;
308   Grob *commonx = common_refpoint_of_array (heads, me, X_AXIS);  
309   Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
310   Array<Box> head_extents;
311   for (int i = heads.size(); i--;)
312     {
313       Box b(heads[i]->extent (commonx, X_AXIS),
314             heads[i]->extent (commony, Y_AXIS));
315
316       insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
317     }
318
319   head_ape-> left_skyline_ = head_skyline;
320   head_ape->offset_ = 0.0;
321
322   SCM rs = me->get_grob_property ("right-padding");
323   if (gh_number_p (rs))
324     head_ape->offset_ -= gh_scm2double (rs);
325
326   
327   Real padding = 0.2;
328   SCM spad = me->get_grob_property ("padding");
329   if (gh_number_p (spad))
330     padding = gh_scm2double (spad);
331   
332   apes.push (head_ape);
333   for (int i= apes.size () -1 ; i-- > 0;)
334     {
335       Accidental_placement_entry *ape = apes[i];
336       Real d = 0.0;
337       int j = i+1;
338       do {
339         d = - skyline_meshing_distance (ape->right_skyline_,
340                                         apes[j]->left_skyline_);
341
342         if (!isinf(d)
343             || j + 1 == apes.size())
344           break;
345         
346         j = j+ 1;
347       } while (1);
348
349       if (isinf(d))
350         d = 0.0;
351       
352       d -= padding;
353       ape->offset_ += apes[j]->offset_ + d;
354     }
355
356   apes.pop();
357   for (int i = apes.size(); i--;)
358     {
359       Accidental_placement_entry* ape = apes[i];
360       for (int j  = ape->grobs_.size(); j--;)
361         {
362           ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
363         }
364     }
365
366   
367   Interval left_extent, right_extent;
368   Accidental_placement_entry *ape = apes[0];
369
370   for (int i = ape->extents_.size(); i--;)
371     left_extent.unite (ape->offset_ +  ape->extents_[i][X_AXIS]);
372
373   ape = apes.top();
374   for (int i = ape->extents_.size(); i--;)
375     right_extent.unite (ape->offset_  + ape->extents_[i][X_AXIS]);
376
377   SCM ls = me->get_grob_property ("left-padding");
378   if (gh_number_p (rs))
379     left_extent[LEFT] -= gh_scm2double (ls);
380
381   
382   Interval width(left_extent[LEFT], right_extent[RIGHT]);
383
384   SCM scm_width = ly_interval2scm (width);
385   me->set_extent (scm_width, X_AXIS);
386   
387   for (int i = apes.size(); i--;)
388     delete apes[i];
389
390   return scm_width;
391 }
392
393 ADD_INTERFACE(Accidental_placement,
394               "accidental-placement-interface",
395               "Take care of complex accidental collisions.",
396               "left-padding padding right-padding accidentals alignment-done")