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