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