]> git.donarmstrong.com Git - lilypond.git/blob - lily/fingering-column.cc
unsmob_pitch -> Pitch::unsmob and related
[lilypond.git] / lily / fingering-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2012 Mike Solomon <mike@mikesolomon.org>
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 "directional-element-interface.hh"
21 #include "grob.hh"
22 #include "fingering-column.hh"
23 #include "pointer-group-interface.hh"
24 #include "staff-symbol-referencer.hh"
25 #include "item.hh"
26 #include "paper-column.hh"
27
28 #define EPS 1e-5
29
30 struct Fingering_and_offset
31 {
32   Grob *fingering_;
33   Real offset_;
34   Fingering_and_offset (Grob *fingering, Real offset);
35 };
36
37 Fingering_and_offset::Fingering_and_offset (Grob *fingering, Real offset) :
38   fingering_ (fingering), offset_ (offset)
39 {
40 }
41
42 bool
43 fingering_and_offset_less (Fingering_and_offset fo0, Fingering_and_offset fo1)
44 {
45   return fo0.offset_ < fo1.offset_;
46 }
47
48 MAKE_SCHEME_CALLBACK (Fingering_column, calc_positioning_done, 1);
49 SCM
50 Fingering_column::calc_positioning_done (SCM smob)
51 {
52   Grob *me = Grob::unsmob (smob);
53   if (!me->is_live ())
54     return SCM_BOOL_T;
55
56   me->set_property ("positioning-done", SCM_BOOL_T);
57
58   do_y_positioning (me);
59   do_x_positioning (me);
60
61   return SCM_BOOL_T;
62 }
63
64 void
65 Fingering_column::do_y_positioning (Grob *me)
66 {
67   extract_grob_set (me, "fingerings", const_fingerings);
68
69   if (const_fingerings.size () < 2)
70     {
71       me->programming_error ("This FingeringColumn should have never been created.");
72       return;
73     }
74
75   vector<Grob *> fingerings;
76   for (vsize i = 0; i < const_fingerings.size (); i++)
77     fingerings.push_back (const_fingerings[i]);
78
79
80   Grob *common[2] = {common_refpoint_of_array (fingerings, me, X_AXIS),
81                      common_refpoint_of_array (fingerings, me, Y_AXIS)};
82
83   Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
84
85   // order the fingerings from bottom to top
86   vector_sort (fingerings, pure_position_less);
87
88  vector<Real> shift(fingerings.size());
89
90   // Try stacking the fingerings top-to-bottom, and then bottom-to-top.
91   // Use the average of the resulting stacked locations as the final positions
92   for (UP_and_DOWN (d))
93      {
94       Real stack_end = -d * infinity_f;
95       Interval prev_x_ext;
96       for (vsize i = (d == UP)? 0 : fingerings.size() - 1;
97            i < fingerings.size ();
98            i += d)
99         {
100           Interval x_ext = fingerings[i]->extent(common[X_AXIS], X_AXIS);
101           Interval y_ext = fingerings[i]->extent(fingerings[i], Y_AXIS);
102           Real parent_y = fingerings[i]->get_parent(Y_AXIS)
103                          ->relative_coordinate(common[Y_AXIS], Y_AXIS);
104
105           // Checking only between sequential neighbors, seems good enough
106           if (!intersection(x_ext, prev_x_ext).is_empty())
107             stack_end += d * (y_ext.length() + padding);
108           // minmax() returns whichever is further along in direction d
109           stack_end = minmax(d, stack_end, parent_y);
110
111           shift[i] += 0.5 * (stack_end - y_ext[d] - parent_y);
112
113           prev_x_ext = x_ext;
114         }
115      }
116
117   for (vsize i = 0; i < fingerings.size (); i++)
118     fingerings[i]->translate_axis(shift[i], Y_AXIS);
119 }
120
121 void
122 Fingering_column::do_x_positioning (Grob *me)
123 {
124   extract_grob_set (me, "fingerings", fingerings);
125   if (!fingerings.size ())
126     return;
127
128   Grob *common_x = common_refpoint_of_array (fingerings, me, X_AXIS);
129
130   Real snap = robust_scm2double (me->get_property ("snap-radius"), 0.3);
131   vector<Fingering_and_offset> fos;
132
133   for (vsize i = 0; i < fingerings.size (); i++)
134     fos.push_back (Fingering_and_offset (fingerings[i], fingerings[i]->relative_coordinate (common_x, X_AXIS)));
135
136   vector_sort (fos, fingering_and_offset_less);
137   Direction dir = get_grob_direction (fingerings[0]);
138   if (dir == RIGHT)
139     reverse (fos);
140
141   Real prev = infinity_f * dir;
142   for (vsize i = 0; i < fos.size (); i++)
143     {
144       if ((fabs (fos[i].offset_ - prev) < snap)
145                 && (fabs (fos[i].offset_ - prev) > EPS))
146         fos[i].offset_ = prev;
147
148       prev = fos[i].offset_;
149     }
150
151   for (vsize i = 0; i < fos.size (); i++)
152     fos[i].fingering_->translate_axis (fos[i].offset_ - fos[i].fingering_->relative_coordinate (common_x, X_AXIS), X_AXIS);
153 }
154
155 void
156 Fingering_column::add_fingering (Grob *fc, Grob *f)
157 {
158   Pointer_group_interface::add_grob (fc, ly_symbol2scm ("fingerings"), f);
159   f->set_parent (fc, X_AXIS);
160   f->set_property ("Y-offset", Grob::x_parent_positioning_proc);
161 }
162
163 ADD_INTERFACE (Fingering_column,
164                "Makes sure that fingerings placed laterally"
165                " do not collide and that they are flush if"
166                " necessary.",
167
168                /* properties */
169                "padding "
170                "positioning-done "
171                "snap-radius "
172               );