]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol.cc
527a5c7baf726b2a0d04d7e4e1c841777ea9fa0f
[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, Item const *head)
139 {
140   // allow the option to override ledger positions via note head grob
141   if (head)
142     {
143       SCM posns = head->get_property ("ledger-positions");
144       if (scm_is_pair (posns))
145         return ly_scm2floatvector (posns);
146     }
147
148   SCM ledger_positions = me->get_property ("ledger-positions");
149   Real ledger_extra = robust_scm2double (me->get_property ("ledger-extra"), 0);
150   vector<Real> line_positions = Staff_symbol::line_positions (me);
151   vector<Real> values;
152
153   if (line_positions.empty ())
154     return values;
155
156   // find the staff line nearest to note position
157   Real nearest_line = line_positions[0];
158   Real line_dist = abs (line_positions[0] - pos);
159   for (vector<Real>::const_iterator i = line_positions.begin (),
160        e = line_positions.end ();
161        i != e;
162        ++i)
163     {
164       if (abs (*i - pos) < line_dist)
165         {
166           nearest_line = *i;
167           line_dist = abs (*i - pos);
168         }
169     }
170
171   if (line_dist < .5)
172     return values;
173
174   Direction dir = (Direction)sign (pos - nearest_line);
175
176   if (scm_is_pair (ledger_positions))
177     // custom ledger line positions
178     {
179       Real min_pos = HUGE_VAL;
180       Real max_pos = -HUGE_VAL;
181       SCM s2;
182
183       // find the extent of the ledger pattern
184       for (SCM s = ledger_positions; scm_is_pair (s); s = scm_cdr (s))
185         {
186           s2 = scm_car (s);
187           if (!scm_is_number (s2))
188             s2 = scm_car (s2);
189           Real current_ledger = scm_to_double (s2);
190           if (current_ledger > max_pos)
191             max_pos = current_ledger;
192           if (current_ledger < min_pos)
193             min_pos = current_ledger;
194         }
195
196       Real cycle = max_pos - min_pos;
197
198       Interval ledger_fill;
199       ledger_fill.add_point (nearest_line + 0.5 * dir);
200       ledger_fill.add_point (pos + 0.5 * dir + ledger_extra * dir);
201
202       // fill the Interval ledger_fill with ledger lines
203       int n = (int) floor ((ledger_fill[DOWN] - min_pos) / cycle);
204       Real current;
205       SCM s = scm_cdr (ledger_positions);
206       if (!scm_is_pair (s) || cycle < 0.1)
207         return values;
208       do
209         {
210           s2 = scm_car (s);
211           if (scm_is_number (s2))
212             {
213               current = scm_to_double (s2) + n * cycle;
214               if (ledger_fill.contains (current))
215                 values.push_back (current);
216             }
217           else
218             // grouped ledger lines, either add all or none
219             {
220               do
221                 {
222                   current = scm_to_double (scm_car (s2)) + n * cycle;
223                   if (ledger_fill.contains (current))
224                     {
225                       s2 = scm_car (s);
226                       do
227                         {
228                           current = scm_to_double (scm_car (s2)) + n * cycle;
229                           values.push_back (current);
230                           s2 = scm_cdr (s2);
231                         }
232                       while (scm_is_pair (s2));
233                     }
234                   else
235                     s2 = scm_cdr (s2);
236                 }
237               while (scm_is_pair (s2));
238             }
239           s = scm_cdr (s);
240           if (!scm_is_pair (s))
241             {
242               s = scm_cdr (ledger_positions);
243               n++;
244             }
245         }
246       while (current <= ledger_fill[UP]);
247     }
248   else
249     // normal ledger lines
250     {
251       int ledger_count = (int) floor ((abs (nearest_line - pos) + ledger_extra) / 2);
252       values.resize (ledger_count);
253       for (int i = 0; i < ledger_count; i++)
254         {
255           values[i] = nearest_line + dir * (ledger_count - i) * 2;
256         }
257     }
258   // remove any ledger lines that would fall on staff lines,
259   // which can happen when ledger-extra > 0
260   vector<Real> final_values;
261   for (vector<Real>::const_iterator i = values.begin (),
262        e = values.end ();
263        i != e;
264        ++i)
265     {
266       if (find (line_positions.begin (), line_positions.end (), *i) == line_positions.end ())
267         {
268           final_values.push_back (*i);
269         }
270     }
271   return final_values;
272 }
273
274 int
275 Staff_symbol::line_count (Grob *me)
276 {
277   SCM line_positions = me->get_property ("line-positions");
278   if (scm_is_pair (line_positions))
279     return scm_ilength (line_positions);
280   else
281     return robust_scm2int (me->get_property ("line-count"), 0);
282 }
283
284 Real
285 Staff_symbol::staff_space (Grob *me)
286 {
287   Real ss = me->layout ()->get_dimension (ly_symbol2scm ("staff-space"));
288
289   return robust_scm2double (me->get_property ("staff-space"), 1.0) * ss;
290 }
291
292 Real
293 Staff_symbol::get_line_thickness (Grob *me)
294 {
295   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
296
297   return robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
298 }
299
300 Real
301 Staff_symbol::get_ledger_line_thickness (Grob *me)
302 {
303   SCM lt_pair = me->get_property ("ledger-line-thickness");
304   Offset z = robust_scm2offset (lt_pair, Offset (1.0, 0.1));
305
306   return z[X_AXIS] * get_line_thickness (me) + z[Y_AXIS] * staff_space (me);
307 }
308
309 MAKE_SCHEME_CALLBACK (Staff_symbol, height, 1);
310 SCM
311 Staff_symbol::height (SCM smob)
312 {
313   Grob *me = unsmob<Grob> (smob);
314   Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
315   t *= robust_scm2double (me->get_property ("thickness"), 1.0);
316
317   SCM line_positions = me->get_property ("line-positions");
318
319   Interval y_ext;
320   Real space = staff_space (me);
321   if (scm_is_pair (line_positions))
322     {
323       for (SCM s = line_positions; scm_is_pair (s);
324            s = scm_cdr (s))
325         y_ext.add_point (scm_to_double (scm_car (s)) * 0.5 * space);
326     }
327   else
328     {
329       int l = Staff_symbol::line_count (me);
330       Real height = (l - 1) * staff_space (me) / 2;
331       y_ext = Interval (-height, height);
332     }
333   y_ext.widen (t / 2);
334   return ly_interval2scm (y_ext);
335 }
336
337 bool
338 Staff_symbol::on_line (Grob *me, int pos, bool allow_ledger)
339 {
340   // standard staff lines (any line count) and standard ledger lines
341   if (!scm_is_pair (me->get_property ("line-positions"))
342       && !scm_is_pair (me->get_property ("ledger-positions")))
343     {
344       int const line_cnt = line_count (me);
345       bool result = abs (pos + line_cnt) % 2 == 1;
346       if (result && !allow_ledger)
347         {
348           result = -line_cnt < pos && pos < line_cnt;
349         }
350       return result;
351     }
352
353   // staff lines (custom or standard)
354   vector<Real> lines = Staff_symbol::line_positions (me);
355   for (vector<Real>::const_iterator i = lines.begin (),
356        e = lines.end ();
357        i != e;
358        ++i)
359     {
360       if (pos == *i)
361         return true;
362     }
363
364   // ledger lines (custom or standard)
365   if (allow_ledger)
366     {
367       vector<Real> ledgers = Staff_symbol::ledger_positions (me, pos);
368       if (ledgers.empty ())
369         return false;
370       for (vector<Real>::const_iterator i = ledgers.begin (),
371            e = ledgers.end ();
372            i != e;
373            ++i)
374         {
375           if (pos == *i)
376             return true;
377         }
378     }
379   return false;
380 }
381
382 Interval
383 Staff_symbol::line_span (Grob *me)
384 {
385   SCM line_positions = me->get_property ("line-positions");
386   Interval iv;
387
388   if (scm_is_pair (line_positions))
389     for (SCM s = line_positions; scm_is_pair (s); s = scm_cdr (s))
390       iv.add_point (scm_to_double (scm_car (s)));
391   else
392     {
393       int count = line_count (me);
394       return Interval (-count + 1, count - 1);
395     }
396
397   return iv;
398 }
399
400 ADD_INTERFACE (Staff_symbol,
401                "This spanner draws the lines of a staff.  A staff symbol"
402                " defines a vertical unit, the @emph{staff space}.  Quantities"
403                " that go by a half staff space are called @emph{positions}."
404                "  The center (i.e., middle line or space) is position@tie{}0."
405                " The length of the symbol may be set by hand through the"
406                " @code{width} property.",
407
408                /* properties */
409                "break-align-symbols "
410                "ledger-extra "
411                "ledger-line-thickness "
412                "ledger-positions "
413                "line-count "
414                "line-positions "
415                "staff-space "
416                "thickness "
417                "width "
418               );