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