]> git.donarmstrong.com Git - lilypond.git/blob - lily/fingering-column.cc
Web-ja: update introduction
[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 = unsmob<Grob> (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]->parent_relative (common[Y_AXIS], Y_AXIS);
103
104           // Checking only between sequential neighbors, seems good enough
105           if (!intersection(x_ext, prev_x_ext).is_empty())
106             stack_end += d * (y_ext.length() + padding);
107           // minmax() returns whichever is further along in direction d
108           stack_end = minmax(d, stack_end, parent_y);
109
110           shift[i] += 0.5 * (stack_end - y_ext[d] - parent_y);
111
112           prev_x_ext = x_ext;
113         }
114      }
115
116   for (vsize i = 0; i < fingerings.size (); i++)
117     fingerings[i]->translate_axis(shift[i], Y_AXIS);
118 }
119
120 void
121 Fingering_column::do_x_positioning (Grob *me)
122 {
123   extract_grob_set (me, "fingerings", fingerings);
124   if (!fingerings.size ())
125     return;
126
127   Grob *common_x = common_refpoint_of_array (fingerings, me, X_AXIS);
128
129   Real snap = robust_scm2double (me->get_property ("snap-radius"), 0.3);
130   vector<Fingering_and_offset> fos;
131
132   for (vsize i = 0; i < fingerings.size (); i++)
133     fos.push_back (Fingering_and_offset (fingerings[i], fingerings[i]->relative_coordinate (common_x, X_AXIS)));
134
135   vector_sort (fos, fingering_and_offset_less);
136   Direction dir = get_grob_direction (fingerings[0]);
137   if (dir == RIGHT)
138     reverse (fos);
139
140   Real prev = infinity_f * dir;
141   for (vsize i = 0; i < fos.size (); i++)
142     {
143       if ((fabs (fos[i].offset_ - prev) < snap)
144                 && (fabs (fos[i].offset_ - prev) > EPS))
145         fos[i].offset_ = prev;
146
147       prev = fos[i].offset_;
148     }
149
150   for (vsize i = 0; i < fos.size (); i++)
151     fos[i].fingering_->translate_axis (fos[i].offset_ - fos[i].fingering_->relative_coordinate (common_x, X_AXIS), X_AXIS);
152 }
153
154 void
155 Fingering_column::add_fingering (Grob *fc, Grob *f)
156 {
157   Pointer_group_interface::add_grob (fc, ly_symbol2scm ("fingerings"), f);
158   f->set_parent (fc, X_AXIS);
159   f->set_property ("Y-offset", Grob::x_parent_positioning_proc);
160 }
161
162 ADD_INTERFACE (Fingering_column,
163                "Makes sure that fingerings placed laterally"
164                " do not collide and that they are flush if"
165                " necessary.",
166
167                /* properties */
168                "padding "
169                "positioning-done "
170                "snap-radius "
171               );