]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-placement.cc
Start to break Accidental_placement::calc_positioning_done into small pieces.
[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--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9
10 #include "accidental-placement.hh"
11
12 #include "item.hh"
13 #include "rhythmic-head.hh"
14 #include "accidental-interface.hh"
15 #include "music.hh"
16 #include "note-collision.hh"
17 #include "note-column.hh"
18 #include "pointer-group-interface.hh"
19 #include "skyline.hh"
20 #include "stream-event.hh"
21 #include "warn.hh"
22
23
24 void
25 Accidental_placement::add_accidental (Grob *me, Grob *a)
26 {
27   a->set_parent (me, X_AXIS);
28   a->set_property ("X-offset", Grob::x_parent_positioning_proc);
29   SCM cause = a->get_parent (Y_AXIS)->get_property ("cause");
30
31   Stream_event *mcause = unsmob_stream_event (cause);
32   if (!mcause)
33     {
34       programming_error ("note head has no event cause");
35       return;
36     }
37
38   Pitch *p = unsmob_pitch (mcause->get_property ("pitch"));
39
40   int n = p->get_notename ();
41
42   SCM accs = me->get_object ("accidental-grobs");
43   SCM key = scm_from_int (n);
44   SCM entry = scm_assq (key, accs);
45   if (entry == SCM_BOOL_F)
46     entry = SCM_EOL;
47   else
48     entry = scm_cdr (entry);
49
50   entry = scm_cons (a->self_scm (), entry);
51
52   accs = scm_assq_set_x (accs, key, entry);
53
54   me->set_object ("accidental-grobs", accs);
55 }
56
57 /*
58   Split into break reminders.
59 */
60 void
61 Accidental_placement::split_accidentals (Grob *accs,
62                                          vector<Grob*> *break_reminder,
63                                          vector<Grob*> *real_acc)
64 {
65   for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
66        acs = scm_cdr (acs))
67     for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
68       {
69         Grob *a = unsmob_grob (scm_car (s));
70
71         if (unsmob_grob (a->get_object ("tie"))
72             && !to_boolean (a->get_property ("forced")))
73           break_reminder->push_back (a);
74         else
75           real_acc->push_back (a);
76       }
77 }
78
79 vector<Grob*>
80 Accidental_placement::get_relevant_accidentals (vector<Grob*> const &elts, Grob *left)
81 {
82   vector<Grob*> br;
83   vector<Grob*> ra;
84   vector<Grob*> ret;
85   bool right = dynamic_cast<Item *> (left)->break_status_dir () == RIGHT;
86
87   for (vsize i = 0; i < elts.size (); i++)
88     {
89       split_accidentals (elts[i], &br, &ra);
90       
91       ret.insert (ret.end (), ra.begin (), ra.end ());
92
93       if (right)
94         ret.insert (ret.end (), br.begin (), br.end ());
95     }
96   return ret;
97 }
98
99 struct Accidental_placement_entry
100 {
101   Skyline left_skyline_;
102   Skyline right_skyline_;
103   Interval vertical_extent_;
104   vector<Box> extents_;
105   vector<Grob*> grobs_;
106   Real offset_;
107   int notename_;
108   Accidental_placement_entry ()
109   {
110     offset_ = 0.0;
111     notename_ = -1;
112   }
113 };
114
115 static Interval all_accidental_vertical_extent;
116 Real ape_priority (Accidental_placement_entry const *a)
117 {
118   return a->vertical_extent_[UP];
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 bool ape_less (Accidental_placement_entry *const &a,
128                Accidental_placement_entry *const &b)
129 {
130   return ape_priority (a) < ape_priority (b);
131 }
132
133 int ape_rcompare (Accidental_placement_entry *const &a,
134                   Accidental_placement_entry *const &b)
135 {
136   return -sign (ape_priority (a) - ape_priority (b));
137 }
138
139 /*
140   TODO: should favor
141
142   b
143   b
144
145   placement
146 */
147 void
148 stagger_apes (vector<Accidental_placement_entry*> *apes)
149 {
150   vector<Accidental_placement_entry*> asc = *apes;
151
152   vector_sort (asc, &ape_less);
153
154   apes->clear ();
155
156   int parity = 1;
157   for (vsize i = 0; i < asc.size ();)
158     {
159       Accidental_placement_entry *a = 0;
160       if (parity)
161         {
162           a = asc.back ();
163           asc.pop_back ();
164         }
165       else
166         a = asc[i++];
167
168       apes->push_back (a);
169       parity = !parity;
170     }
171
172   reverse (*apes);
173 }
174
175 static void
176 set_ape_skylines (Accidental_placement_entry *ape,
177                   Grob **common)
178 {
179   for (vsize j = ape->grobs_.size (); j--;)
180     {
181       Grob *a = ape->grobs_[j];
182       vector<Box> boxes = Accidental_interface::accurate_boxes (a, common);
183       ape->extents_.insert (ape->extents_.end (), boxes.begin (), boxes.end ());
184     }
185   ape->left_skyline_ = Skyline (ape->extents_, 0, Y_AXIS, LEFT);
186   ape->right_skyline_ = Skyline (ape->extents_, 0, Y_AXIS, RIGHT);
187 }
188
189 static vector<Grob*>
190 extract_heads_and_stems (vector<Accidental_placement_entry*> const &apes)
191 {
192   vector<Grob*> note_cols;
193   vector<Grob*> ret;
194
195   for (vsize i = apes.size (); i--;)
196     {
197       Accidental_placement_entry *ape = apes[i];
198       for (vsize j = ape->grobs_.size (); j--;)
199         {
200           Grob *acc = ape->grobs_[j];
201           Grob *head = acc->get_parent (Y_AXIS);
202           Grob *col = head->get_parent (X_AXIS);
203
204           if (Note_column::has_interface (col))
205             note_cols.push_back (col);
206           else
207             ret.push_back (head);
208         }
209     }
210
211   /*
212     This is a little kludgy: in case there are note columns without
213     accidentals, we get them from the Note_collision objects.
214   */
215   for (vsize i = note_cols.size (); i--;)
216     {
217       Grob *c = note_cols[i]->get_parent (X_AXIS);
218       if (Note_collision_interface::has_interface (c))
219         {
220           extract_grob_set (c, "elements", columns);
221           concat (note_cols, columns);
222         }
223     }
224
225   /* Now that we have all of the columns, grab all of the note-heads */
226   for (vsize i = note_cols.size (); i--;)
227     concat (ret, extract_grob_array (note_cols[i], "note-heads"));
228
229   /* Now that we have all of the heads, grab all of the stems */
230   for (vsize i = ret.size (); i--;)
231     if (Grob *s = Rhythmic_head::get_stem (ret[i]))
232       ret.push_back (s);
233
234
235   vector_sort (ret, less<Grob*> ());
236   uniq (ret);
237   return ret;
238 }
239
240 static Grob*
241 common_refpoint_of_accidentals (vector<Accidental_placement_entry*> const &apes, Axis a)
242 {
243   Grob *ret = 0;
244
245   for (vsize i = apes.size (); i--;)
246     for (vsize j = apes[i]->grobs_.size (); j--;)
247       {
248         if (!ret)
249           ret = apes[i]->grobs_[j];
250         else
251           ret = ret->common_refpoint (apes[i]->grobs_[j], a);
252       }
253
254   return ret;
255 }
256
257
258 /*
259   This routine computes placements of accidentals. During
260   add_accidental (), accidentals are already grouped by note, so that
261   octaves are placed above each other; they form columns. Then the
262   columns are sorted: the biggest columns go closest to the note.
263   Then the columns are spaced as closely as possible (using skyline
264   spacing).
265
266
267   TODO: more advanced placement. Typically, the accs should be placed
268   to form a C shape, like this
269
270
271   ##
272   b b
273   # #
274   b
275   b b
276
277   The naturals should be left of the C as well; they should
278   be separate accs.
279
280   Note that this placement problem looks NP hard, so we just use a
281   simple strategy, not an optimal choice.
282 */
283
284 /*
285   TODO: there should be more space in the following situation
286
287
288   Natural + downstem
289
290   *
291   *  |_
292   *  | |    X
293   *  |_|   |
294   *    |   |
295   *
296
297 */
298
299 MAKE_SCHEME_CALLBACK (Accidental_placement, calc_positioning_done, 1);
300 SCM
301 Accidental_placement::calc_positioning_done (SCM smob)
302 {
303   Grob *me = unsmob_grob (smob);
304   if (!me->is_live ())
305     return SCM_BOOL_T;
306
307   me->set_property ("positioning-done", SCM_BOOL_T);
308   
309   SCM accs = me->get_object ("accidental-grobs");
310   if (!scm_is_pair (accs))
311     return SCM_BOOL_T;
312
313   vector<Accidental_placement_entry*> apes;
314   for (SCM s = accs; scm_is_pair (s); s = scm_cdr (s))
315     {
316       Accidental_placement_entry *ape = new Accidental_placement_entry;
317       ape->notename_ = scm_to_int (scm_caar (s));
318
319       for (SCM t = scm_cdar (s); scm_is_pair (t); t = scm_cdr (t))
320         ape->grobs_.push_back (unsmob_grob (scm_car (t)));
321
322       apes.push_back (ape);
323     }
324
325   Grob *common[] = {me, 0};
326
327   vector<Grob*> heads_and_stems = extract_heads_and_stems (apes);
328
329   common[Y_AXIS] = common_refpoint_of_accidentals (apes, Y_AXIS);
330   common[Y_AXIS] = common_refpoint_of_array (heads_and_stems, common[Y_AXIS], Y_AXIS);
331   common[X_AXIS] = common_refpoint_of_array (heads_and_stems, me, X_AXIS);
332
333   for (vsize i = apes.size (); i--;)
334     set_ape_skylines (apes[i], common);
335
336   Interval total;
337   for (vsize i = apes.size (); i--;)
338     {
339       Interval y;
340
341       for (vsize j = apes[i]->extents_.size (); j--;)
342         y.unite (apes[i]->extents_[j][Y_AXIS]);
343       apes[i]->vertical_extent_ = y;
344       total.unite (y);
345     }
346   all_accidental_vertical_extent = total;
347   stagger_apes (&apes);
348
349   Accidental_placement_entry *head_ape = new Accidental_placement_entry;
350   
351   vector<Box> head_extents;
352   for (vsize i = heads_and_stems.size (); i--;)
353     head_extents.push_back (Box (heads_and_stems[i]->extent (common[X_AXIS], X_AXIS),
354                                  heads_and_stems[i]->pure_height (common[Y_AXIS], 0, INT_MAX)));
355
356   head_ape->left_skyline_ = Skyline (head_extents, 0, Y_AXIS, LEFT);
357   head_ape->offset_ = 0.0;
358
359   Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
360
361   Skyline left_skyline = head_ape->left_skyline_;
362   left_skyline.raise (-robust_scm2double (me->get_property ("right-padding"), 0));
363   
364   /*
365     Add accs entries right-to-left.
366   */
367   for (vsize i = apes.size (); i-- > 0;)
368     {
369       Real offset = -apes[i]->right_skyline_.distance (left_skyline);
370       if (isinf (offset))
371         offset = (i + 1 < apes.size ()) ? apes[i + 1]->offset_ : 0.0;
372       else
373         offset -= padding;
374
375       apes[i]->offset_ = offset;
376
377       Skyline new_left_skyline = apes[i]->left_skyline_;
378       new_left_skyline.raise (apes[i]->offset_);
379       new_left_skyline.merge (left_skyline);
380       left_skyline = new_left_skyline;
381     }
382
383   for (vsize i = apes.size (); i--;)
384     {
385       Accidental_placement_entry *ape = apes[i];
386       for (vsize j = ape->grobs_.size (); j--;)
387         ape->grobs_[j]->translate_axis (ape->offset_, X_AXIS);
388     }
389
390   Interval left_extent, right_extent;
391   Accidental_placement_entry *ape = apes[0];
392
393   for (vsize i = ape->extents_.size (); i--;)
394     left_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
395
396   ape = apes.back ();
397   for (vsize i = ape->extents_.size (); i--;)
398     right_extent.unite (ape->offset_ + ape->extents_[i][X_AXIS]);
399
400   left_extent[LEFT] -= robust_scm2double (me->get_property ("left-padding"), 0);
401   Interval width (left_extent[LEFT], right_extent[RIGHT]);
402
403   SCM scm_width = ly_interval2scm (width);
404   me->flush_extent_cache (X_AXIS);
405   me->set_property ("X-extent", scm_width);
406
407   junk_pointers (apes);
408
409   delete head_ape;
410   
411   return SCM_BOOL_T;
412 }
413
414 ADD_INTERFACE (Accidental_placement,
415                "Resolve accidental collisions.",
416
417                /* properties */
418                "accidental-grobs "
419                "direction "
420                "left-padding "
421                "padding "
422                "positioning-done "
423                "right-padding "
424                "script-priority "
425                )