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"
31 MAKE_SCHEME_CALLBACK (Staff_symbol, print, 1);
34 Staff_symbol::print (SCM smob)
36 Grob *me = unsmob<Grob> (smob);
37 Spanner *sp = dynamic_cast<Spanner *> (me);
39 = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
41 Interval span_points (0, 0);
44 For raggedright without ragged staves, simply set width to the linewidth.
46 (ok -- lousy UI, since width is in staff spaces)
50 Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
51 t *= robust_scm2double (me->get_property ("thickness"), 1.0);
53 for (LEFT_and_RIGHT (d))
55 SCM width_scm = me->get_property ("width");
56 if (d == RIGHT && scm_is_number (width_scm))
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.
63 span_points[RIGHT] = scm_to_double (width_scm);
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.
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];
83 span_points[d] -= d * t / 2;
88 vector<Real> line_positions = Staff_symbol::line_positions (me);
91 = Lookup::horizontal_line (span_points
92 - me->relative_coordinate (common, X_AXIS),
95 Real space = staff_space (me);
96 for (vector<Real>::const_iterator i = line_positions.begin (),
97 e = line_positions.end ();
102 b.translate_axis (*i * 0.5 * space, Y_AXIS);
105 return m.smobbed_copy ();
109 Staff_symbol::line_positions (Grob *me)
111 SCM line_positions = me->get_property ("line-positions");
112 if (scm_is_pair (line_positions))
114 int line_count = scm_ilength (line_positions);
115 vector<Real> values (line_count);
117 for (SCM s = line_positions; scm_is_pair (s);
120 values[i++] = scm_to_double (scm_car (s));
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++)
131 values[i] = height - i * 2;
138 Staff_symbol::ledger_positions (Grob *me, int pos, Item const *head)
140 // allow override of ledger positions via note head grob...
143 SCM posns = head->get_property ("ledger-positions");
144 if (scm_is_pair (posns))
145 return ly_scm2floatvector (posns);
148 // ...or via custom ledger positions function
149 SCM lp_function = me->get_property ("ledger-positions-function");
150 if (scm_is_pair (lp_function))
152 SCM func = scm_eval (lp_function, scm_interaction_environment ());
153 if (ly_is_procedure (func))
154 return ly_scm2floatvector (scm_call_2 (func,
156 scm_from_int (pos)));
159 SCM ledger_positions = me->get_property ("ledger-positions");
160 Real ledger_extra = robust_scm2double (me->get_property ("ledger-extra"), 0);
161 vector<Real> line_positions = Staff_symbol::line_positions (me);
164 if (line_positions.empty ())
167 // find the staff line nearest to note position
168 Real nearest_line = line_positions[0];
169 Real line_dist = abs (line_positions[0] - pos);
170 for (vector<Real>::const_iterator i = line_positions.begin (),
171 e = line_positions.end ();
175 if (abs (*i - pos) < line_dist)
178 line_dist = abs (*i - pos);
185 Direction dir = (Direction)sign (pos - nearest_line);
187 if (scm_is_pair (ledger_positions))
188 // custom ledger positions via StaffSymbol.ledger-positions
190 Real min_pos = HUGE_VAL;
191 Real max_pos = -HUGE_VAL;
194 // find the extent of the ledger pattern
195 for (SCM s = ledger_positions; scm_is_pair (s); s = scm_cdr (s))
198 if (!scm_is_number (s2))
200 Real current_ledger = scm_to_double (s2);
201 if (current_ledger > max_pos)
202 max_pos = current_ledger;
203 if (current_ledger < min_pos)
204 min_pos = current_ledger;
207 Real cycle = max_pos - min_pos;
209 Interval ledger_fill;
210 ledger_fill.add_point (nearest_line + 0.5 * dir);
211 ledger_fill.add_point (pos + 0.5 * dir + ledger_extra * dir);
213 // fill the Interval ledger_fill with ledger lines
214 int n = (int) floor ((ledger_fill[DOWN] - min_pos) / cycle);
216 SCM s = scm_cdr (ledger_positions);
217 if (!scm_is_pair (s) || cycle < 0.1)
222 if (scm_is_number (s2))
224 current = scm_to_double (s2) + n * cycle;
225 if (ledger_fill.contains (current))
226 values.push_back (current);
229 // grouped ledger lines, either add all or none
233 current = scm_to_double (scm_car (s2)) + n * cycle;
234 if (ledger_fill.contains (current))
239 current = scm_to_double (scm_car (s2)) + n * cycle;
240 values.push_back (current);
243 while (scm_is_pair (s2));
248 while (scm_is_pair (s2));
251 if (!scm_is_pair (s))
253 s = scm_cdr (ledger_positions);
257 while (current <= ledger_fill[UP]);
260 // normal ledger lines
262 int ledger_count = (int) floor ((abs (nearest_line - pos) + ledger_extra) / 2);
263 values.resize (ledger_count);
264 for (int i = 0; i < ledger_count; i++)
266 values[i] = nearest_line + dir * (ledger_count - i) * 2;
269 // remove any ledger lines that would fall on staff lines,
270 // which can happen when ledger-extra > 0
271 vector<Real> final_values;
272 for (vector<Real>::const_iterator i = values.begin (),
277 if (find (line_positions.begin (), line_positions.end (), *i) == line_positions.end ())
279 final_values.push_back (*i);
286 Staff_symbol::line_count (Grob *me)
288 SCM line_positions = me->get_property ("line-positions");
289 if (scm_is_pair (line_positions))
290 return scm_ilength (line_positions);
292 return robust_scm2int (me->get_property ("line-count"), 0);
296 Staff_symbol::staff_space (Grob *me)
298 Real ss = me->layout ()->get_dimension (ly_symbol2scm ("staff-space"));
300 return robust_scm2double (me->get_property ("staff-space"), 1.0) * ss;
304 Staff_symbol::get_line_thickness (Grob *me)
306 Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
308 return robust_scm2double (me->get_property ("thickness"), 1.0) * lt;
312 Staff_symbol::get_ledger_line_thickness (Grob *me)
314 SCM lt_pair = me->get_property ("ledger-line-thickness");
315 Offset z = robust_scm2offset (lt_pair, Offset (1.0, 0.1));
317 return z[X_AXIS] * get_line_thickness (me) + z[Y_AXIS] * staff_space (me);
320 MAKE_SCHEME_CALLBACK (Staff_symbol, height, 1);
322 Staff_symbol::height (SCM smob)
324 Grob *me = unsmob<Grob> (smob);
325 Real t = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
326 t *= robust_scm2double (me->get_property ("thickness"), 1.0);
328 SCM line_positions = me->get_property ("line-positions");
331 Real space = staff_space (me);
332 if (scm_is_pair (line_positions))
334 for (SCM s = line_positions; scm_is_pair (s);
336 y_ext.add_point (scm_to_double (scm_car (s)) * 0.5 * space);
340 int l = Staff_symbol::line_count (me);
341 Real height = (l - 1) * staff_space (me) / 2;
342 y_ext = Interval (-height, height);
345 return ly_interval2scm (y_ext);
349 Staff_symbol::on_line (Grob *me, int pos, bool allow_ledger)
351 // standard staff lines (any line count) and standard ledger lines
352 if (!scm_is_pair (me->get_property ("line-positions"))
353 && !scm_is_pair (me->get_property ("ledger-positions")))
355 int const line_cnt = line_count (me);
356 bool result = abs (pos + line_cnt) % 2 == 1;
357 if (result && !allow_ledger)
359 result = -line_cnt < pos && pos < line_cnt;
364 // staff lines (custom or standard)
365 vector<Real> lines = Staff_symbol::line_positions (me);
366 for (vector<Real>::const_iterator i = lines.begin (),
375 // ledger lines (custom or standard)
378 vector<Real> ledgers = Staff_symbol::ledger_positions (me, pos);
379 if (ledgers.empty ())
381 for (vector<Real>::const_iterator i = ledgers.begin (),
394 Staff_symbol::line_span (Grob *me)
396 SCM line_positions = me->get_property ("line-positions");
399 if (scm_is_pair (line_positions))
400 for (SCM s = line_positions; scm_is_pair (s); s = scm_cdr (s))
401 iv.add_point (scm_to_double (scm_car (s)));
404 int count = line_count (me);
405 return Interval (-count + 1, count - 1);
411 ADD_INTERFACE (Staff_symbol,
412 "This spanner draws the lines of a staff. A staff symbol"
413 " defines a vertical unit, the @emph{staff space}. Quantities"
414 " that go by a half staff space are called @emph{positions}."
415 " The center (i.e., middle line or space) is position@tie{}0."
416 " The length of the symbol may be set by hand through the"
417 " @code{width} property.",
420 "break-align-symbols "
422 "ledger-line-thickness "
424 "ledger-positions-function "