]> git.donarmstrong.com Git - lilypond.git/blob - lily/dot-column.cc
84ace8228f545fee8d9a5eeedb502cb3729a5e49
[lilypond.git] / lily / dot-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <cstdio>
21 #include <cmath>
22 #include <map>
23 #include <set>
24
25 using namespace std;
26
27 #include "axis-group-interface.hh"
28 #include "directional-element-interface.hh"
29 #include "dot-column.hh"
30 #include "dot-configuration.hh"
31 #include "dot-formatting-problem.hh"
32 #include "dots.hh"
33 #include "grob.hh"
34 #include "note-head.hh"
35 #include "pointer-group-interface.hh"
36 #include "rest.hh"
37 #include "rhythmic-head.hh"
38 #include "side-position-interface.hh"
39 #include "staff-symbol-referencer.hh"
40 #include "stem.hh"
41
42 using std::set;
43 using std::vector;
44
45 MAKE_SCHEME_CALLBACK (Dot_column, calc_positioning_done, 1);
46 SCM
47 Dot_column::calc_positioning_done (SCM smob)
48 {
49   Grob *me = unsmob<Grob> (smob);
50
51   /*
52     Trigger note collision resolution first, since that may kill off
53     dots when merging.
54   */
55   if (Grob *collision = unsmob<Grob> (me->get_object ("note-collision")))
56     (void) collision->get_property ("positioning-done");
57
58   me->set_property ("positioning-done", SCM_BOOL_T);
59
60   vector<Grob *> dots
61     = extract_grob_array (me, "dots");
62
63   vector<Grob *> parent_stems;
64   Real ss = 0;
65
66   Grob *commonx = me;
67   for (vsize i = 0; i < dots.size (); i++)
68     {
69       Grob *n = dots[i]->get_parent (Y_AXIS);
70       commonx = n->common_refpoint (commonx, X_AXIS);
71
72       if (Grob *stem = unsmob<Grob> (n->get_object ("stem")))
73         {
74           commonx = stem->common_refpoint (commonx, X_AXIS);
75
76           if (Stem::first_head (stem) == n)
77             parent_stems.push_back (stem);
78         }
79     }
80
81   vector<Box> boxes;
82   set<Grob *> stems;
83
84   extract_grob_set (me, "side-support-elements", support);
85
86   Interval base_x;
87   for (vsize i = 0; i < parent_stems.size (); i++)
88     base_x.unite (Stem::first_head (parent_stems[i])->extent (commonx, X_AXIS));
89
90   for (vsize i = 0; i < support.size (); i++)
91     {
92       Grob *s = support[i];
93       if (!ss)
94         ss = Staff_symbol_referencer::staff_space (s);
95
96       /* can't inspect Y extent of rest.
97
98          Rest collisions should wait after line breaking.
99       */
100       Interval y;
101       if (has_interface<Rest> (s))
102         {
103           base_x.unite (s->extent (commonx, X_AXIS));
104           continue;
105         }
106       else if (has_interface<Stem> (s))
107         {
108           Real y1 = Stem::head_positions (s)[-get_grob_direction (s)];
109           Real y2 = y1 + get_grob_direction (s) * 7;
110
111           y.add_point (y1);
112           y.add_point (y2);
113
114           stems.insert (s);
115         }
116       else if (has_interface<Note_head> (s))
117         y = Interval (-1.1, 1.1);
118       else
119         {
120           programming_error ("unknown grob in dot col support");
121           continue;
122         }
123
124       y += Staff_symbol_referencer::get_position (s);
125
126       Box b (s->extent (commonx, X_AXIS), y);
127       boxes.push_back (b);
128
129       if (Grob *stem = unsmob<Grob> (s->get_object ("stem")))
130         stems.insert (stem);
131     }
132
133   for (set<Grob *>::const_iterator i (stems.begin ());
134        i != stems.end (); i++)
135     {
136       Grob *stem = (*i);
137       Grob *flag = Stem::flag (stem);
138       if (flag)
139         {
140           Grob *commony = stem->common_refpoint (flag, Y_AXIS);
141           Interval y = flag->extent (commony, Y_AXIS) * (2 / ss);
142           Interval x = flag->extent (commonx, X_AXIS);
143
144           boxes.push_back (Box (x, y));
145         }
146     }
147
148   /*
149     The use of pure_position_less and pure_get_rounded_position below
150     are due to the fact that this callback is called before line breaking
151     occurs.  Because dots' actual Y posiitons may be linked to that of
152     beams (dots are attached to rests, which are shifted to avoid beams),
153     we instead must use their pure Y positions.
154   */
155   vector_sort (dots, pure_position_less);
156
157   SCM chord_dots_limit = me->get_property ("chord-dots-limit");
158   if (scm_is_number (chord_dots_limit))
159     {
160       // Sort dots by stem, then check for dots above the limit for each stem
161       vector <vector <Grob *> > dots_each_stem (parent_stems.size ());
162       for (vsize i = 0; i < dots.size (); i++)
163         if (Grob *stem = unsmob<Grob> (dots[i]->get_parent (Y_AXIS)
164                                       -> get_object ("stem")))
165           for (vsize j = 0; j < parent_stems.size (); j++)
166             if (stem == parent_stems[j])
167               {
168                 dots_each_stem[j].push_back (dots[i]);
169                 break;
170               }
171       for (vsize j = 0; j < parent_stems.size (); j++)
172         {
173           Interval chord = Stem::head_positions (parent_stems[j]);
174           int total_room = ((int) chord.length () + 2
175                             + scm_to_int (chord_dots_limit)) / 2;
176           int total_dots = dots_each_stem[j].size ();
177           // remove excessive dots from the ends of the stem
178           for (int first_dot = 0; total_dots > total_room; total_dots--)
179             if (0 == (total_dots - total_room) % 2)
180               dots_each_stem[j][first_dot++]->suicide ();
181             else
182               dots_each_stem[j][first_dot + total_dots - 1]->suicide ();
183         }
184     }
185
186   for (vsize i = dots.size (); i--;)
187     {
188       if (!dots[i]->is_live ())
189         dots.erase (dots.begin () + i);
190       else
191         // Undo any fake translations that were done in add_head.
192         dots[i]->translate_axis (-dots[i]->relative_coordinate (me, X_AXIS), X_AXIS);
193     }
194
195   Dot_formatting_problem problem (boxes, base_x);
196
197   Dot_configuration cfg (problem);
198   for (vsize i = 0; i < dots.size (); i++)
199     {
200       Dot_position dp;
201       dp.dot_ = dots[i];
202
203       Grob *note = dots[i]->get_parent (Y_AXIS);
204       if (note)
205         {
206           if (has_interface<Note_head> (note))
207             dp.dir_ = to_dir (dp.dot_->get_property ("direction"));
208
209           dp.x_extent_ = note->extent (commonx, X_AXIS);
210         }
211
212       int p = Staff_symbol_referencer::pure_get_rounded_position (dp.dot_);
213
214       /* icky, since this should go via a Staff_symbol_referencer
215          offset callback but adding a dot overwrites Y-offset. */
216       p += (int) robust_scm2double (dp.dot_->get_property ("staff-position"), 0.0);
217       dp.pos_ = p;
218
219       cfg.remove_collision (p);
220       cfg[p] = dp;
221       if (Staff_symbol_referencer::on_line (dp.dot_, p)
222           && !scm_is_eq (dp.dot_->get_property ("style"),
223                          ly_symbol2scm ("kievan")))
224         cfg.remove_collision (p);
225     }
226
227   problem.register_configuration (cfg);
228
229   for (Dot_configuration::const_iterator i (cfg.begin ());
230        i != cfg.end (); i++)
231     {
232       /*
233         Junkme?
234        */
235       Staff_symbol_referencer::pure_set_position (i->second.dot_, i->first);
236     }
237
238   me->translate_axis (cfg.x_offset () - me->relative_coordinate (commonx, X_AXIS),
239                       X_AXIS);
240   return SCM_BOOL_T;
241 }
242
243 void
244 Dot_column::add_head (Grob *me, Grob *head)
245 {
246   Grob *d = unsmob<Grob> (head->get_object ("dot"));
247   if (d)
248     {
249       Side_position_interface::add_support (me, head);
250
251       Pointer_group_interface::add_grob (me, ly_symbol2scm ("dots"), d);
252       d->set_property ("Y-offset", Grob::x_parent_positioning_proc);
253       // Dot formatting requests the Y-offset, which for rests may
254       // trigger post-linebreak callbacks.  On the other hand, we need the
255       // correct X-offset of the dots for horizontal collision avoidance.
256       // The translation here is undone in calc_positioning_done, where we
257       // do the X-offset properly.
258       if (has_interface<Rest> (head))
259         d->translate_axis (head->extent (head, X_AXIS).length (), X_AXIS);
260       else
261         d->set_property ("X-offset", Grob::x_parent_positioning_proc);
262       Axis_group_interface::add_element (me, d);
263     }
264 }
265
266 ADD_INTERFACE (Dot_column,
267                "Group dot objects so they form a column, and position"
268                " dots so they do not clash with staff lines.",
269
270                /* properties */
271                "chord-dots-limit "
272                "dots "
273                "positioning-done "
274                "direction "
275                "note-collision "
276               );
277