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