]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-placement.cc
(internal_brew_molecule): make left protusion
[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--2003 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 ("alignment-done")))
31     {
32       par->set_grob_property ("alignment-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.empty_b())
123     {
124       Real p = gh_scm2double (me->get_grob_property ("left-padding"));
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 SCM
238 Accidental_placement::position_accidentals (Grob * me)
239 {
240   if (!me->live ())
241     return SCM_UNSPECIFIED;
242   
243   SCM accs = me->get_grob_property ("accidental-grobs");
244
245   /*
246     TODO: there is a bug in this code. If two accs are on the same
247     Y-position, they share an Ape, and will be printed in overstrike.
248    */
249   Link_array<Accidental_placement_entry> apes;
250   for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
251     {
252       Accidental_placement_entry *ape = new Accidental_placement_entry;
253       ape->notename_ = gh_scm2int (gh_caar (s));
254       
255       for (SCM t = gh_cdar (s); gh_pair_p (t); t =gh_cdr (t))
256         ape->grobs_.push (unsmob_grob (gh_car (t)));
257
258       apes.push (ape);
259     }
260
261
262   Grob *common[] = {me, 0};
263
264   /*
265     First we must extract *all* pointers. We can only determine
266     extents if we're sure that we've found the right common refpoint
267    */
268   Link_array<Grob> note_cols, heads;
269   for (int i= apes.size (); i--;)
270     { 
271       Accidental_placement_entry * ape = apes[i];
272       for (int j = ape->grobs_.size(); j--;)
273         {
274           Grob * a = ape->grobs_[j];
275
276           if (common[Y_AXIS])
277             common[Y_AXIS] = common[Y_AXIS]->common_refpoint (a, Y_AXIS);
278           else
279             common[Y_AXIS] = a;
280           
281           Grob *head = a->get_parent (Y_AXIS);
282
283           Grob * col = head->get_parent (X_AXIS);
284           if (Note_column::has_interface (col))
285             note_cols.push (col);
286           else
287             heads.push (head);
288         }
289     }
290
291   /*
292     This is a little kludgy: to get all notes, we look if there are
293     collisions as well.
294    */
295   for (int i = note_cols.size() ; i--;)
296     {
297       Grob *c = note_cols[i]->get_parent (X_AXIS);
298       if (Note_collision_interface::has_interface (c))
299         {
300           Link_array<Grob> gs =
301             Pointer_group_interface__extract_grobs (c, (Grob*)0, "elements");
302       
303           note_cols.concat (gs);
304         }
305     }
306   
307   for (int i = note_cols.size() ; i--;)
308     {
309       heads.concat (Pointer_group_interface__extract_grobs (note_cols[i],
310                                                             (Grob*)0,
311                                                             "note-heads"));
312       
313     }
314   heads.default_sort();
315   heads.uniq();
316   common[Y_AXIS] = common_refpoint_of_array (heads, common[Y_AXIS], Y_AXIS);
317
318   
319   for (int i= apes.size (); i--;)
320     {
321       Accidental_placement_entry * ape = apes[i];
322       ape->left_skyline_ = empty_skyline (LEFT);
323       ape->right_skyline_ = empty_skyline (RIGHT);
324    
325       for (int j = apes[i]->grobs_.size(); j--;)
326         {
327           Grob * a = apes[i]->grobs_[j];
328
329           Array<Box> boxes = Accidental_interface::accurate_boxes (a, common);
330           
331           ape->extents_.concat (boxes);
332           for (int j  = boxes.size(); j--;)
333             {
334               insert_extent_into_skyline (&ape->left_skyline_, boxes[j], Y_AXIS, LEFT);
335               insert_extent_into_skyline (&ape->right_skyline_ , boxes[j], Y_AXIS, RIGHT);
336             }
337         }
338     }
339
340
341   Interval total;
342   for (int i = apes.size(); i--;)
343     {
344       Interval y ;
345       
346       for (int j = apes[i]->extents_.size(); j--;)
347         {
348           y.unite (apes[i]->extents_[j][Y_AXIS]);
349         }
350       apes[i]->vertical_extent_ = y;
351       total.unite (y);
352     }
353   all_accidental_vertical_extent = total;
354   stagger_apes (&apes);
355
356   Accidental_placement_entry * head_ape = new Accidental_placement_entry;
357   common[X_AXIS] = common_refpoint_of_array (heads, common[X_AXIS], X_AXIS);  
358   Array<Skyline_entry> head_skyline (empty_skyline (LEFT));
359   Array<Box> head_extents;
360   for (int i = heads.size(); i--;)
361     {
362       Box b(heads[i]->extent (common[X_AXIS] , X_AXIS),
363             heads[i]->extent (common[Y_AXIS], Y_AXIS));
364
365       insert_extent_into_skyline (&head_skyline, b , Y_AXIS, LEFT);
366     }
367
368   head_ape-> left_skyline_ = head_skyline;
369   head_ape->offset_ = 0.0;
370
371   SCM rs = me->get_grob_property ("right-padding");
372   if (gh_number_p (rs))
373     head_ape->offset_ -= gh_scm2double (rs);
374
375   
376   Real padding = 0.2;
377   SCM spad = me->get_grob_property ("padding");
378   if (gh_number_p (spad))
379     padding = gh_scm2double (spad);
380
381   Array<Skyline_entry> left_skyline = head_ape->left_skyline_;
382   /*
383     Add accs entries right-to-left.
384    */
385   for (int i= apes.size (); i-- > 0;)
386     {
387       Real offset =
388         -skyline_meshing_distance (apes[i]->right_skyline_, left_skyline);
389       if (isinf (offset))
390         offset = (i < apes.size ()) ? apes[i+1]->offset_ : 0.0;
391       else
392         offset -= padding;
393
394       apes[i]->offset_ = offset;
395
396       Array<Skyline_entry> new_left_skyline = apes[i]->left_skyline_;
397       heighten_skyline (&new_left_skyline, apes[i]->offset_);
398       merge_skyline (&new_left_skyline, left_skyline, LEFT);
399       left_skyline = new_left_skyline;
400     }      
401
402   for (int i = apes.size(); i--;)
403     {
404       Accidental_placement_entry* ape = apes[i];
405       for (int j  = ape->grobs_.size(); j--;)
406         {
407           ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
408         }
409     }
410
411   
412   Interval left_extent, right_extent;
413   Accidental_placement_entry *ape = apes[0];
414
415   for (int i = ape->extents_.size(); i--;)
416     left_extent.unite (ape->offset_ +  ape->extents_[i][X_AXIS]);
417
418   ape = apes.top();
419   for (int i = ape->extents_.size(); i--;)
420     right_extent.unite (ape->offset_  + ape->extents_[i][X_AXIS]);
421
422   SCM ls = me->get_grob_property ("left-padding");
423   if (gh_number_p (rs))
424     left_extent[LEFT] -= gh_scm2double (ls);
425
426   
427   Interval width(left_extent[LEFT], right_extent[RIGHT]);
428
429   SCM scm_width = ly_interval2scm (width);
430   me->set_extent (scm_width, X_AXIS);
431   
432   for (int i = apes.size(); i--;)
433     delete apes[i];
434
435   return SCM_UNSPECIFIED;
436 }
437
438 ADD_INTERFACE(Accidental_placement,
439               "accidental-placement-interface",
440               "Take care of complex accidental collisions.",
441               "left-padding padding right-padding accidental-grobs alignment-done")