]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol.cc
Merge branch 'lilypond/translation'
[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 "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   int line_count = line_positions.size ();
84
85   Stencil line
86     = Lookup::horizontal_line (span_points
87                                - me->relative_coordinate (common, X_AXIS),
88                                t);
89
90   Real space = staff_space (me);
91   for (int i = 0; i < line_count; i++)
92     {
93       Stencil b (line);
94       b.translate_axis (line_positions[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   int line_count = line_positions.size ();
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 (int i = 1; i < line_count; i++)
146     {
147       if (abs (line_positions[i] - pos) < line_dist)
148         {
149           nearest_line = line_positions[i];
150           line_dist = abs (line_positions[i] - pos);
151         }
152     }
153
154   if (line_dist < .5)
155     return values;
156
157   Direction dir = (Direction)sign (pos - nearest_line);
158
159   if (scm_is_pair (ledger_positions))
160     // custom ledger line positions
161     {
162       Real min_pos = HUGE_VAL;
163       Real max_pos = -HUGE_VAL;
164       SCM s2;
165
166       // find the extent of the ledger pattern
167       for (SCM s = ledger_positions; scm_is_pair (s); s = scm_cdr (s))
168         {
169           s2 = scm_car (s);
170           if (!scm_is_number (s2))
171             s2 = scm_car (s2);
172           Real current_ledger = scm_to_double (s2);
173           if (current_ledger > max_pos)
174             max_pos = current_ledger;
175           if (current_ledger < min_pos)
176             min_pos = current_ledger;
177         }
178
179       Real cycle = max_pos - min_pos;
180
181       Interval ledger_fill;
182       ledger_fill.add_point (nearest_line + 0.5 * dir);
183       ledger_fill.add_point (pos + 0.5 * dir + ledger_extra * dir);
184
185       // fill the Interval ledger_fill with ledger lines
186       int n = (int) floor ((ledger_fill[DOWN] - min_pos) / cycle);
187       Real current;
188       SCM s = scm_cdr (ledger_positions);
189       if (!scm_is_pair (s) || cycle < 0.1)
190         return values;
191       do
192         {
193           s2 = scm_car (s);
194           if (scm_is_number (s2))
195             {
196               current = scm_to_double (s2) + n * cycle;
197               if (ledger_fill.contains (current))
198                 values.push_back (current);
199             }
200           else
201             // grouped ledger lines, either add all or none
202             {
203               do
204                 {
205                   current = scm_to_double (scm_car (s2)) + n * cycle;
206                   if (ledger_fill.contains (current))
207                     {
208                       s2 = scm_car (s);
209                       do
210                         {
211                           current = scm_to_double (scm_car (s2)) + n * cycle;
212                           values.push_back (current);
213                           s2 = scm_cdr (s2);
214                         }
215                       while (scm_is_pair (s2));
216                     }
217                   else
218                     s2 = scm_cdr (s2);
219                 }
220               while (scm_is_pair (s2));
221             }
222           s = scm_cdr (s);
223           if (!scm_is_pair (s))
224             {
225               s = scm_cdr (ledger_positions);
226               n++;
227             }
228         }
229       while (current <= ledger_fill[UP]);
230     }
231   else
232     // normal ledger lines
233     {
234       int ledger_count = (int) floor ((abs (nearest_line - pos) + ledger_extra) / 2);
235       values.resize (ledger_count);
236       for (int i = 0; i < ledger_count; i++)
237         {
238           values[i] = nearest_line + dir * (ledger_count - i) * 2;
239         }
240     }
241   return values;
242 }
243
244 int
245 Staff_symbol::line_count (Grob *me)
246 {
247   SCM line_positions = me->get_property ("line-positions");
248   if (scm_is_pair (line_positions))
249     return scm_ilength (line_positions);
250   else
251     return robust_scm2int (me->get_property ("line-count"), 0);
252 }
253
254 Real
255 Staff_symbol::staff_space (Grob *me)
256 {
257   return robust_scm2double (me->get_property ("staff-space"), 1.0);
258 }
259
260 Real
261 Staff_symbol::get_line_thickness (Grob *me)
262 {
263   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
264
265   return robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
266 }
267
268 Real
269 Staff_symbol::get_ledger_line_thickness (Grob *me)
270 {
271   SCM lt_pair = me->get_property ("ledger-line-thickness");
272   Offset z = robust_scm2offset (lt_pair, Offset (1.0, 0.1));
273
274   return z[X_AXIS] * get_line_thickness (me) + z[Y_AXIS] * staff_space (me);
275 }
276
277 MAKE_SCHEME_CALLBACK (Staff_symbol, height, 1);
278 SCM
279 Staff_symbol::height (SCM smob)
280 {
281   Grob *me = unsmob_grob (smob);
282   Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
283   t *= robust_scm2double (me->get_property ("thickness"), 1.0);
284
285   SCM line_positions = me->get_property ("line-positions");
286
287   Interval y_ext;
288   Real space = staff_space (me);
289   if (scm_is_pair (line_positions))
290     {
291       for (SCM s = line_positions; scm_is_pair (s);
292            s = scm_cdr (s))
293         y_ext.add_point (scm_to_double (scm_car (s)) * 0.5 * space);
294     }
295   else
296     {
297       int l = Staff_symbol::line_count (me);
298       Real height = (l - 1) * staff_space (me) / 2;
299       y_ext = Interval (-height, height);
300     }
301   y_ext.widen (t / 2);
302   return ly_interval2scm (y_ext);
303 }
304
305 bool
306 Staff_symbol::on_line (Grob *me, int pos)
307 {
308   SCM line_positions = me->get_property ("line-positions");
309   if (scm_is_pair (line_positions))
310     {
311       Real min_line = HUGE_VAL;
312       Real max_line = -HUGE_VAL;
313       for (SCM s = line_positions; scm_is_pair (s); s = scm_cdr (s))
314         {
315           Real current_line = scm_to_double (scm_car (s));
316           if (pos == current_line)
317             return true;
318           if (current_line > max_line)
319             max_line = current_line;
320           if (current_line < min_line)
321             min_line = current_line;
322
323         }
324       if (pos < min_line)
325         return (( (int) (rint (pos - min_line)) % 2) == 0);
326       if (pos > max_line)
327         return (( (int) (rint (pos - max_line)) % 2) == 0);
328
329       return false;
330     }
331   else
332     return ((abs (pos + line_count (me)) % 2) == 1);
333 }
334
335 Interval
336 Staff_symbol::line_span (Grob *me)
337 {
338   SCM line_positions = me->get_property ("line-positions");
339   Interval iv;
340
341   if (scm_is_pair (line_positions))
342     for (SCM s = line_positions; scm_is_pair (s); s = scm_cdr (s))
343       iv.add_point (scm_to_double (scm_car (s)));
344   else
345     {
346       int count = line_count (me);
347       return Interval (-count + 1, count - 1);
348     }
349
350   return iv;
351 }
352
353 ADD_INTERFACE (Staff_symbol,
354                "This spanner draws the lines of a staff.  A staff symbol"
355                " defines a vertical unit, the @emph{staff space}.  Quantities"
356                " that go by a half staff space are called @emph{positions}."
357                "  The center (i.e., middle line or space) is position@tie{}0."
358                " The length of the symbol may be set by hand through the"
359                " @code{width} property.",
360
361                /* properties */
362                "ledger-extra "
363                "ledger-line-thickness "
364                "ledger-positions "
365                "line-count "
366                "line-positions "
367                "staff-space "
368                "thickness "
369                "width "
370               );