]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-placement.cc
* lily/side-position-interface.cc (general_side_position):
[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   Real c = a->vertical_extent_.center();
108
109   /*
110     far from center means we can fold more. Hopefully.
111    */
112   Real center_distance =
113     fabs(c - all_accidental_vertical_extent[LEFT]) >?
114     fabs(c - all_accidental_vertical_extent[RIGHT]);
115   
116   return 20 * a->vertical_extent_.length  ()  + center_distance;
117 }
118
119   
120
121 int ape_compare (Accidental_placement_entry *const &a,
122                  Accidental_placement_entry *const &b)
123 {
124   return sign (ape_priority (a) - ape_priority(b));
125 }
126
127 /*
128   Return: width as SCM interval.
129
130
131   This routine computes placements of accidentals. During
132   add_accidental(), accidentals are already grouped by note, so that
133   octaves are placed above each other; they form columns. Then the
134   columns are sorted: the biggest columns go closest to the note.
135   Then the columns are spaced as closely as possible (using skyline
136   spacing).
137   
138   
139   TODO: more advanced placement. Typically, the accs should be placed
140   to form a C shape, like this
141
142   
143            ##
144         b b
145        # #
146         b
147           b b
148
149    The naturals should be left of the C as well; they should
150    be separate accs.
151
152    TODO: Try to combine Apes before sorting them: this will allow a
153    better placement.
154
155    Note that this placement problem is almost certainly NP hard, so we
156    just use a simple strategy, not an optimal choice.
157 */
158
159 SCM
160 Accidental_placement::position_accidentals (Grob * me)
161 {
162   SCM accs = me->get_grob_property ("accidentals");
163
164   Link_array<Accidental_placement_entry> apes;
165   for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
166     {
167       Accidental_placement_entry *ape = new Accidental_placement_entry;
168       ape->notename_ = gh_scm2int (gh_caar (s));
169       
170       for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
171         ape->grobs_.push (unsmob_grob (gh_car (t)));
172
173       apes.push (ape);
174     }
175
176
177   Grob *commony =0 ;
178
179   /*
180     First we must extract *all* pointers. We can only determine
181     extents if we're sure that we've found the right common refpoint
182    */
183   Link_array<Grob> note_cols, heads;
184   for (int i= apes.size (); i--;)
185     { 
186       Accidental_placement_entry * ape = apes[i];
187       for (int j = ape->grobs_.size(); j--;)
188         {
189           Grob * a = ape->grobs_[j];
190           
191           commony = commony->common_refpoint (a, Y_AXIS);
192           Grob *head = a->get_parent (Y_AXIS);
193
194           Grob * col = head->get_parent (X_AXIS);
195           if (Note_column::has_interface (col))
196             note_cols.push (col);
197           else
198             heads.push (head);
199         }
200     }
201
202   /*
203     This is a little kludgy: to get all notes, we look if there are
204     collisions as well.
205    */
206   for (int i = note_cols.size() ; i--;)
207     {
208       Grob *c = note_cols[i]->get_parent (X_AXIS);
209       if (Note_collision_interface::has_interface (c))
210         {
211           Link_array<Grob> gs =
212             Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
213       
214           note_cols.concat (gs);
215         }
216     }
217   
218   for (int i = note_cols.size() ; i--;)
219     {
220       heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
221                                                             (Grob*)0,
222                                                             "note-heads"));
223       
224     }
225   heads.default_sort();
226   heads.uniq();
227   commony = common_refpoint_of_array (heads, commony, Y_AXIS);
228
229   
230   for (int i= apes.size (); i--;)
231     {
232       Accidental_placement_entry * ape = apes[i];
233       ape->left_skyline_ = empty_skyline (LEFT);
234       ape->right_skyline_ = empty_skyline (RIGHT);
235    
236       for (int j = apes[i]->grobs_.size(); j--;)
237         {
238           Grob * a = apes[i]->grobs_[j];
239           Box b;
240           b[X_AXIS] = a->extent (me, X_AXIS);
241           b[Y_AXIS] = a->extent (commony, Y_AXIS);
242
243           ape->extents_.push (b);
244           
245           /*
246             TODO: replace the extents of a flat by combination of two
247             bboxes, so that we use the shape of the flat better.
248           */
249           insert_extent_into_skyline (&ape->left_skyline_, b, Y_AXIS, LEFT);
250           insert_extent_into_skyline (&ape->right_skyline_ , b,Y_AXIS, RIGHT);
251         }
252     }
253
254
255   Interval total;
256   for (int i = apes.size(); i--;)
257     {
258       Interval y ;
259       
260       for (int j = apes[i]->extents_.size(); j--;)
261         {
262           y.unite (apes[i]->extents_[j][Y_AXIS]);
263         }
264       apes[i]->vertical_extent_ = y;
265       total.unite (y);
266     }
267   all_accidental_vertical_extent = total;
268   apes.sort (&ape_compare);  
269
270   Accidental_placement_entry * head_ape = new Accidental_placement_entry;
271   Grob *commonx = common_refpoint_of_array (heads, me, X_AXIS);  
272   Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
273   Array<Box> head_extents;
274   for (int i = heads.size(); i--;)
275     {
276       Box b(heads[i]->extent (commonx, X_AXIS),
277             heads[i]->extent (commony, Y_AXIS));
278
279       insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
280     }
281
282   head_ape-> left_skyline_ = head_skyline;
283   head_ape->offset_ = 0.0;
284
285   SCM rs = me->get_grob_property ("right-padding");
286   if (gh_number_p (rs))
287     head_ape->offset_ -= gh_scm2double (rs);
288
289   
290   Real padding = 0.2;
291   SCM spad = me->get_grob_property ("padding");
292   if (gh_number_p (spad))
293     padding = gh_scm2double (spad);
294   
295   apes.push (head_ape);
296   for (int i= apes.size () -1 ; i-- > 0;)
297     {
298       Accidental_placement_entry *ape = apes[i];
299       Real d = 0.0;
300       int j = i+1;
301       do {
302         d = - skyline_meshing_distance (ape->right_skyline_,
303                                         apes[j]->left_skyline_);
304
305         if (!isinf(d)
306             || j + 1 == apes.size())
307           break;
308         
309         j = j+ 1;
310       } while (1);
311
312       if (isinf(d))
313         d = 0.0;
314       
315       d -= padding;
316       ape->offset_ += apes[j]->offset_ + d;
317     }
318
319   apes.pop();
320   for (int i = apes.size(); i--;)
321     {
322       Accidental_placement_entry* ape = apes[i];
323       for (int j  = ape->grobs_.size(); j--;)
324         {
325           ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
326         }
327     }
328
329   
330   Interval left_extent, right_extent;
331   Accidental_placement_entry *ape = apes[0];
332
333   for (int i = ape->extents_.size(); i--;)
334     left_extent.unite (ape->offset_ +  ape->extents_[i][X_AXIS]);
335
336   ape = apes.top();
337   for (int i = ape->extents_.size(); i--;)
338     right_extent.unite (ape->offset_  + ape->extents_[i][X_AXIS]);
339
340   SCM ls = me->get_grob_property ("left-padding");
341   if (gh_number_p (rs))
342     left_extent[LEFT] -= gh_scm2double (ls);
343
344   
345   Interval width(left_extent[LEFT], right_extent[RIGHT]);
346
347   SCM scm_width = ly_interval2scm (width);
348   me->set_extent (scm_width, X_AXIS);
349   
350   for (int i = apes.size(); i--;)
351     delete apes[i];
352
353   return scm_width;
354 }
355
356 ADD_INTERFACE(Accidental_placement,
357               "accidental-placement-interface",
358               "Take care of complex accidental collisions.",
359               "left-padding padding right-padding accidentals alignment-done")