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