2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2012 Mike Solomon <mike@mikesolomon.org>
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.
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.
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/>.
20 #include "directional-element-interface.hh"
22 #include "fingering-column.hh"
23 #include "pointer-group-interface.hh"
24 #include "staff-symbol-referencer.hh"
26 #include "paper-column.hh"
30 struct Fingering_and_offset
34 Fingering_and_offset (Grob *fingering, Real offset);
37 Fingering_and_offset::Fingering_and_offset (Grob *fingering, Real offset) :
38 fingering_ (fingering), offset_ (offset)
43 fingering_and_offset_less (Fingering_and_offset fo0, Fingering_and_offset fo1)
45 return fo0.offset_ < fo1.offset_;
48 MAKE_SCHEME_CALLBACK (Fingering_column, calc_positioning_done, 1);
50 Fingering_column::calc_positioning_done (SCM smob)
52 Grob *me = unsmob_grob (smob);
56 me->set_property ("positioning-done", SCM_BOOL_T);
58 do_y_positioning (me);
59 do_x_positioning (me);
65 Fingering_column::do_y_positioning (Grob *me)
67 extract_grob_set (me, "fingerings", const_fingerings);
69 if (const_fingerings.size () < 2)
71 me->programming_error ("This FingeringColumn should have never been created.");
75 vector<Grob *> fingerings;
76 for (vsize i = 0; i < const_fingerings.size (); i++)
77 fingerings.push_back (const_fingerings[i]);
80 Grob *common[2] = {common_refpoint_of_array (fingerings, me, X_AXIS),
81 common_refpoint_of_array (fingerings, me, Y_AXIS)};
83 Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
85 // order the fingerings from bottom to top
86 vector_sort (fingerings, pure_position_less);
88 vector<Real> shift(fingerings.size());
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
94 Real stack_end = -d * infinity_f;
96 for (vsize i = (d == UP)? 0 : fingerings.size() - 1;
97 i < fingerings.size ();
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);
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);
111 shift[i] += 0.5 * (stack_end - y_ext[d] - parent_y);
117 for (vsize i = 0; i < fingerings.size (); i++)
118 fingerings[i]->translate_axis(shift[i], Y_AXIS);
122 Fingering_column::do_x_positioning (Grob *me)
124 extract_grob_set (me, "fingerings", fingerings);
125 if (!fingerings.size ())
128 Grob *common_x = common_refpoint_of_array (fingerings, me, X_AXIS);
130 Real snap = robust_scm2double (me->get_property ("snap-radius"), 0.3);
131 vector<Fingering_and_offset> fos;
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)));
136 vector_sort (fos, fingering_and_offset_less);
137 Direction dir = get_grob_direction (fingerings[0]);
141 Real prev = infinity_f * dir;
142 for (vsize i = 0; i < fos.size (); i++)
144 if ((fabs (fos[i].offset_ - prev) < snap)
145 && (fabs (fos[i].offset_ - prev) > EPS))
146 fos[i].offset_ = prev;
148 prev = fos[i].offset_;
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);
156 Fingering_column::add_fingering (Grob *fc, Grob *f)
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);
163 ADD_INTERFACE (Fingering_column,
164 "Makes sure that fingerings placed laterally"
165 " do not collide and that they are flush if"