2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "staff-symbol.hh"
23 #include "dimensions.hh"
24 #include "output-def.hh"
25 #include "paper-column.hh"
28 #include "staff-symbol-referencer.hh"
33 MAKE_SCHEME_CALLBACK (Staff_symbol, print, 1);
36 Staff_symbol::print (SCM smob)
38 Grob *me = unsmob<Grob> (smob);
39 Spanner *sp = dynamic_cast<Spanner *> (me);
41 = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
43 Interval span_points (0, 0);
46 For raggedright without ragged staves, simply set width to the linewidth.
48 (ok -- lousy UI, since width is in staff spaces)
52 Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
53 t *= robust_scm2double (me->get_property ("thickness"), 1.0);
55 for (LEFT_and_RIGHT (d))
57 SCM width_scm = me->get_property ("width");
58 if (d == RIGHT && scm_is_number (width_scm))
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.
65 span_points[RIGHT] = scm_to_double (width_scm);
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.
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];
85 span_points[d] -= d * t / 2;
90 vector<Real> line_positions = Staff_symbol::line_positions (me);
93 = Lookup::horizontal_line (span_points
94 - me->relative_coordinate (common, X_AXIS),
97 Real space = staff_space (me);
98 for (vector<Real>::const_iterator i = line_positions.begin (),
99 e = line_positions.end ();
104 b.translate_axis (*i * 0.5 * space, Y_AXIS);
107 return m.smobbed_copy ();
111 Staff_symbol::line_positions (Grob *me)
113 SCM line_positions = me->get_property ("line-positions");
114 if (scm_is_pair (line_positions))
116 int line_count = scm_ilength (line_positions);
117 vector<Real> values (line_count);
119 for (SCM s = line_positions; scm_is_pair (s);
122 values[i++] = scm_to_double (scm_car (s));
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++)
133 values[i] = height - i * 2;
140 Staff_symbol::ledger_positions (Grob *me, int pos)
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);
147 if (line_positions.empty ())
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 ();
158 if (abs (*i - pos) < line_dist)
161 line_dist = abs (*i - pos);
168 Direction dir = (Direction)sign (pos - nearest_line);
170 if (scm_is_pair (ledger_positions))
171 // custom ledger line positions
173 Real min_pos = HUGE_VAL;
174 Real max_pos = -HUGE_VAL;
177 // find the extent of the ledger pattern
178 for (SCM s = ledger_positions; scm_is_pair (s); s = scm_cdr (s))
181 if (!scm_is_number (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;
190 Real cycle = max_pos - min_pos;
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);
196 // fill the Interval ledger_fill with ledger lines
197 int n = (int) floor ((ledger_fill[DOWN] - min_pos) / cycle);
199 SCM s = scm_cdr (ledger_positions);
200 if (!scm_is_pair (s) || cycle < 0.1)
205 if (scm_is_number (s2))
207 current = scm_to_double (s2) + n * cycle;
208 if (ledger_fill.contains (current))
209 values.push_back (current);
212 // grouped ledger lines, either add all or none
216 current = scm_to_double (scm_car (s2)) + n * cycle;
217 if (ledger_fill.contains (current))
222 current = scm_to_double (scm_car (s2)) + n * cycle;
223 values.push_back (current);
226 while (scm_is_pair (s2));
231 while (scm_is_pair (s2));
234 if (!scm_is_pair (s))
236 s = scm_cdr (ledger_positions);
240 while (current <= ledger_fill[UP]);
243 // normal ledger lines
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++)
249 values[i] = nearest_line + dir * (ledger_count - i) * 2;
256 Staff_symbol::line_count (Grob *me)
258 SCM line_positions = me->get_property ("line-positions");
259 if (scm_is_pair (line_positions))
260 return scm_ilength (line_positions);
262 return robust_scm2int (me->get_property ("line-count"), 0);
266 Staff_symbol::staff_space (Grob *me)
268 Real ss = me->layout ()->get_dimension (ly_symbol2scm ("staff-space"));
270 return robust_scm2double (me->get_property ("staff-space"), 1.0) * ss;
274 Staff_symbol::get_line_thickness (Grob *me)
276 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
278 return robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
282 Staff_symbol::get_ledger_line_thickness (Grob *me)
284 SCM lt_pair = me->get_property ("ledger-line-thickness");
285 Offset z = robust_scm2offset (lt_pair, Offset (1.0, 0.1));
287 return z[X_AXIS] * get_line_thickness (me) + z[Y_AXIS] * staff_space (me);
290 MAKE_SCHEME_CALLBACK (Staff_symbol, height, 1);
292 Staff_symbol::height (SCM smob)
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);
298 SCM line_positions = me->get_property ("line-positions");
301 Real space = staff_space (me);
302 if (scm_is_pair (line_positions))
304 for (SCM s = line_positions; scm_is_pair (s);
306 y_ext.add_point (scm_to_double (scm_car (s)) * 0.5 * space);
310 int l = Staff_symbol::line_count (me);
311 Real height = (l - 1) * staff_space (me) / 2;
312 y_ext = Interval (-height, height);
315 return ly_interval2scm (y_ext);
319 Staff_symbol::on_line (Grob *me, int pos, bool allow_ledger)
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")))
325 int const line_cnt = line_count (me);
326 bool result = abs (pos + line_cnt) % 2 == 1;
327 if (result && !allow_ledger)
329 result = -line_cnt < pos && pos < line_cnt;
334 // staff lines (custom or standard)
335 vector<Real> lines = Staff_symbol::line_positions (me);
336 for (vector<Real>::const_iterator i = lines.begin (),
345 // ledger lines (custom or standard)
348 vector<Real> ledgers = Staff_symbol::ledger_positions (me, pos);
349 if (ledgers.empty ())
351 for (vector<Real>::const_iterator i = ledgers.begin (),
364 Staff_symbol::line_span (Grob *me)
366 SCM line_positions = me->get_property ("line-positions");
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)));
374 int count = line_count (me);
375 return Interval (-count + 1, count - 1);
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.",
390 "break-align-symbols "
392 "ledger-line-thickness "