]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol.cc
Adds checks for ledger_positions; issue 1972
[lilypond.git] / lily / staff-symbol.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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 "staff-symbol.hh"
21
22 #include "lookup.hh"
23 #include "dimensions.hh"
24 #include "output-def.hh"
25 #include "warn.hh"
26 #include "item.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "spanner.hh"
29
30 MAKE_SCHEME_CALLBACK (Staff_symbol, print, 1);
31
32 SCM
33 Staff_symbol::print (SCM smob)
34 {
35   Grob *me = unsmob_grob (smob);
36   Spanner *sp = dynamic_cast<Spanner *> (me);
37   Grob *common
38     = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
39
40   Interval span_points (0, 0);
41
42   /*
43     For raggedright without ragged staves, simply set width to the linewidth.
44
45     (ok -- lousy UI, since width is in staff spaces)
46
47     --hwn.
48   */
49   Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
50   t *= robust_scm2double (me->get_property ("thickness"), 1.0);
51
52   Direction d = LEFT;
53   do
54     {
55       SCM width_scm = me->get_property ("width");
56       if (d == RIGHT && scm_is_number (width_scm))
57         {
58           /*
59             don't multiply by Staff_symbol_referencer::staff_space (me),
60             since that would make aligning staff symbols of different sizes to
61             one right margin hell.
62           */
63           span_points[RIGHT] = scm_to_double (width_scm);
64         }
65       else
66         {
67           Item *x = sp->get_bound (d);
68
69           span_points[d] = x->relative_coordinate (common, X_AXIS);
70           if (!x->break_status_dir ()
71               && !x->extent (x, X_AXIS).is_empty ())
72             span_points[d] += x->extent (x, X_AXIS)[d];
73         }
74
75       span_points[d] -= d * t / 2;
76     }
77   while (flip (&d) != LEFT);
78
79   Stencil m;
80
81   vector<Real> line_positions = Staff_symbol::line_positions (me);
82   int line_count = line_positions.size ();
83
84   Stencil line
85     = Lookup::horizontal_line (span_points
86                                - me->relative_coordinate (common, X_AXIS),
87                                t);
88
89   Real space = staff_space (me);
90   for (int i = 0; i < line_count; i++)
91     {
92       Stencil b (line);
93       b.translate_axis (line_positions[i] * 0.5 * space, Y_AXIS);
94       m.add_stencil (b);
95     }
96   return m.smobbed_copy ();
97 }
98
99 vector<Real>
100 Staff_symbol::line_positions (Grob *me)
101 {
102   SCM line_positions = me->get_property ("line-positions");
103   if (scm_is_pair (line_positions))
104     {
105       int line_count = scm_ilength (line_positions);
106       vector<Real> values (line_count);
107       int i = 0;
108       for (SCM s = line_positions; scm_is_pair (s);
109            s = scm_cdr (s))
110         {
111           values[i++] = scm_to_double (scm_car (s));
112         }
113       return values;
114     }
115   else
116     {
117       int line_count = Staff_symbol::line_count (me);
118       Real height = line_count - 1;
119       vector<Real> values (line_count);
120       for (int i = 0; i < line_count; i++)
121         {
122           values[i] = height - i * 2;
123         }
124       return values;
125     }
126 }
127
128 vector<Real>
129 Staff_symbol::ledger_positions (Grob *me, int pos)
130 {
131   SCM ledger_positions = me->get_property ("ledger-positions");
132   Real ledger_extra = robust_scm2double (me->get_property ("ledger-extra"), 0);
133   vector<Real> line_positions = Staff_symbol::line_positions (me);
134   vector<Real> values;
135
136   if (line_positions.empty ())
137     return values;
138
139   int line_count = line_positions.size ();
140
141   // find the staff line nearest to note position
142   Real nearest_line = line_positions[0];
143   Real line_dist = abs (line_positions[0] - pos);
144   for (int i = 1; i < line_count; i++)
145     {
146       if (abs (line_positions[i] - pos) < line_dist)
147         {
148           nearest_line = line_positions[i];
149           line_dist = abs (line_positions[i] - pos);
150         }
151     }
152
153   if (line_dist < .5)
154     return values;
155
156   Direction dir = (Direction)sign (pos - nearest_line);
157
158   if (scm_is_pair (ledger_positions))
159     // custom ledger line positions
160     {
161       Real min_pos = HUGE_VAL;
162       Real max_pos = -HUGE_VAL;
163       SCM s2;
164
165       // find the extent of the ledger pattern
166       for (SCM s = ledger_positions; scm_is_pair (s); s = scm_cdr (s))
167         {
168           s2 = scm_car (s);
169           if (!scm_is_number (s2))
170             s2 = scm_car (s2);
171           Real current_ledger = scm_to_double (s2);
172           if (current_ledger > max_pos)
173             max_pos = current_ledger;
174           if (current_ledger < min_pos)
175             min_pos = current_ledger;
176         }
177
178       Real cycle = max_pos - min_pos;
179
180       Interval ledger_fill;
181       ledger_fill.add_point (nearest_line + 0.5 * dir);
182       ledger_fill.add_point (pos + 0.5 * dir + ledger_extra * dir);
183
184       // fill the Interval ledger_fill with ledger lines
185       int n = (int) floor ((ledger_fill[DOWN] - min_pos) / cycle);
186       Real current;
187       SCM s = scm_cdr (ledger_positions);
188       if (!scm_is_pair (s) || cycle < 0.1)
189         return values;
190       do
191         {
192           s2 = scm_car (s);
193           if (scm_is_number (s2))
194             {
195               current = scm_to_double (s2) + n * cycle;
196               if (ledger_fill.contains (current))
197                 values.push_back (current);
198             }
199           else
200             // grouped ledger lines, either add all or none
201             {
202               do
203                 {
204                   current = scm_to_double (scm_car (s2)) + n * cycle;
205                   if (ledger_fill.contains (current))
206                     {
207                       s2 = scm_car (s);
208                       do
209                         {
210                           current = scm_to_double (scm_car (s2)) + n * cycle;
211                           values.push_back (current);
212                           s2 = scm_cdr (s2);
213                         }
214                       while (scm_is_pair (s2));
215                     }
216                   else
217                     s2 = scm_cdr (s2);
218                 }
219               while (scm_is_pair (s2));
220             }
221           s = scm_cdr (s);
222           if (!scm_is_pair (s))
223             {
224               s = scm_cdr (ledger_positions);
225               n++;
226             }
227         }
228       while (current <= ledger_fill[UP]);
229     }
230   else
231     // normal ledger lines
232     {
233       int ledger_count = (int) floor ((abs (nearest_line - pos) + ledger_extra) / 2);
234       values.resize (ledger_count);
235       for (int i = 0; i < ledger_count; i++)
236         {
237           values[i] = nearest_line + dir * (ledger_count - i) * 2;
238         }
239     }
240   return values;
241 }
242
243 int
244 Staff_symbol::line_count (Grob *me)
245 {
246   SCM line_positions = me->get_property ("line-positions");
247   if (scm_is_pair (line_positions))
248     return scm_ilength (line_positions);
249   else
250     return robust_scm2int (me->get_property ("line-count"), 0);
251 }
252
253 Real
254 Staff_symbol::staff_space (Grob *me)
255 {
256   return robust_scm2double (me->get_property ("staff-space"), 1.0);
257 }
258
259 Real
260 Staff_symbol::get_line_thickness (Grob *me)
261 {
262   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
263
264   return robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
265 }
266
267 Real
268 Staff_symbol::get_ledger_line_thickness (Grob *me)
269 {
270   SCM lt_pair = me->get_property ("ledger-line-thickness");
271   Offset z = robust_scm2offset (lt_pair, Offset (1.0, 0.1));
272
273   return z[X_AXIS] * get_line_thickness (me) + z[Y_AXIS] * staff_space (me);
274 }
275
276 MAKE_SCHEME_CALLBACK (Staff_symbol, height, 1);
277 SCM
278 Staff_symbol::height (SCM smob)
279 {
280   Grob *me = unsmob_grob (smob);
281   Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
282   t *= robust_scm2double (me->get_property ("thickness"), 1.0);
283
284   SCM line_positions = me->get_property ("line-positions");
285
286   Interval y_ext;
287   Real space = staff_space (me);
288   if (scm_is_pair (line_positions))
289     {
290       for (SCM s = line_positions; scm_is_pair (s);
291            s = scm_cdr (s))
292         y_ext.add_point (scm_to_double (scm_car (s)) * 0.5 * space);
293     }
294   else
295     {
296       int l = Staff_symbol::line_count (me);
297       Real height = (l - 1) * staff_space (me) / 2;
298       y_ext = Interval (-height, height);
299     }
300   y_ext.widen (t / 2);
301   return ly_interval2scm (y_ext);
302 }
303
304 bool
305 Staff_symbol::on_line (Grob *me, int pos)
306 {
307   SCM line_positions = me->get_property ("line-positions");
308   if (scm_is_pair (line_positions))
309     {
310       Real min_line = HUGE_VAL;
311       Real max_line = -HUGE_VAL;
312       for (SCM s = line_positions; scm_is_pair (s); s = scm_cdr (s))
313         {
314           Real current_line = scm_to_double (scm_car (s));
315           if (pos == current_line)
316             return true;
317           if (current_line > max_line)
318             max_line = current_line;
319           if (current_line < min_line)
320             min_line = current_line;
321
322         }
323       if (pos < min_line)
324         return (( (int) (rint (pos - min_line)) % 2) == 0);
325       if (pos > max_line)
326         return (( (int) (rint (pos - max_line)) % 2) == 0);
327
328       return false;
329     }
330   else
331     return ((abs (pos + line_count (me)) % 2) == 1);
332 }
333
334 Interval
335 Staff_symbol::line_span (Grob *me)
336 {
337   SCM line_positions = me->get_property ("line-positions");
338   Interval iv;
339
340   if (scm_is_pair (line_positions))
341     for (SCM s = line_positions; scm_is_pair (s); s = scm_cdr (s))
342       iv.add_point (scm_to_double (scm_car (s)));
343   else
344     {
345       int count = line_count (me);
346       return Interval (-count + 1, count - 1);
347     }
348
349   return iv;
350 }
351
352 ADD_INTERFACE (Staff_symbol,
353                "This spanner draws the lines of a staff.  A staff symbol"
354                " defines a vertical unit, the @emph{staff space}.  Quantities"
355                " that go by a half staff space are called @emph{positions}."
356                "  The center (i.e., middle line or space) is position@tie{}0."
357                " The length of the symbol may be set by hand through the"
358                " @code{width} property.",
359
360                /* properties */
361                "ledger-extra "
362                "ledger-line-thickness "
363                "ledger-positions "
364                "line-count "
365                "line-positions "
366                "staff-space "
367                "thickness "
368                "width "
369               );