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