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