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