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/>.
22 #include "directional-element-interface.hh"
24 #include "font-interface.hh"
25 #include "international.hh"
26 #include "output-def.hh"
27 #include "paper-score.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "staff-symbol.hh"
34 MAKE_SCHEME_CALLBACK (Rest, y_offset_callback, 1);
36 Rest::y_offset_callback (SCM smob)
38 Grob *me = Grob::unsmob (smob);
39 int duration_log = scm_to_int (me->get_property ("duration-log"));
40 Real ss = Staff_symbol_referencer::staff_space (me);
42 return scm_from_double (ss * 0.5 * Rest::staff_position_internal (me, duration_log, get_grob_direction (me)));
46 Rest::staff_position_internal (Grob *me, int duration_log, int dir)
51 bool position_override = scm_is_number (me->get_property ("staff-position"));
54 if (position_override)
57 = robust_scm2double (me->get_property ("staff-position"), 0);
60 semibreve rests are positioned one staff line off
62 if (duration_log == 0)
66 trust the client on good positioning;
67 would be tempting to adjust position of rests longer than a quarter
68 to be properly aligned to staff lines,
69 but custom rest shapes may not need that sort of care.
75 Real vpos = dir * robust_scm2int (me->get_property ("voiced-position"), 0);
79 /* Only half notes or longer want alignment with staff lines */
83 We need a staff symbol for actually aligning anything
85 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
89 std::vector<Real> linepos = Staff_symbol::line_positions (staff);
94 std::sort (linepos.begin (), linepos.end ());
96 if (duration_log == 0)
99 lower voice semibreve rests generally hang a line lower
106 make a semibreve rest hang from the next available line,
107 except when there is none.
110 std::vector<Real>::const_iterator it
111 = std::upper_bound (linepos.begin (), linepos.end (), pos);
112 if (it != linepos.end ())
115 pos = linepos.back ();
119 std::vector<Real>::const_iterator it
120 = std::upper_bound (linepos.begin (), linepos.end (), pos);
121 if (it != linepos.begin ())
126 /* Finished for neutral position */
130 /* If we have a voiced position, make sure that it's on the
131 proper side of neutral before using it.
134 Real neutral = staff_position_internal (me, duration_log, 0);
136 if (dir * (pos - neutral) > 0)
139 return neutral + vpos;
142 /* A rest might lie under a beam, in which case it should be cross-staff if
143 the beam is cross-staff because the rest's position depends on the
144 formatting of the beam. */
145 MAKE_SCHEME_CALLBACK (Rest, calc_cross_staff, 1);
147 Rest::calc_cross_staff (SCM smob)
149 Grob *me = Grob::unsmob (smob);
150 Grob *stem = Grob::unsmob (me->get_object ("stem"));
155 return stem->get_property ("cross-staff");
159 make this function easily usable in C++
162 Rest::glyph_name (Grob *me, int durlog, const string &style, bool try_ledgers,
165 bool is_ledgered = false;
166 if (try_ledgers && (durlog == -1 || durlog == 0 || durlog == 1))
168 int const pos = int (Staff_symbol_referencer::get_position (me)
171 half rests need ledger if not lying on a staff line,
172 whole rests need ledger if not hanging from a staff line,
173 breve rests need ledger if neither lying on nor hanging from a staff line
175 if (-1 <= durlog && durlog <= 1)
176 is_ledgered = !Staff_symbol_referencer::on_staff_line (me, pos)
178 && Staff_symbol_referencer::on_staff_line (me, pos + 2));
181 string actual_style (style.c_str ());
183 if ((style == "mensural") || (style == "neomensural"))
187 FIXME: Currently, ancient font does not provide ledgered rests;
188 hence the "o" suffix in the glyph name is bogus. But do we need
189 ledgered rests at all now that we can draw ledger lines with
190 variable width, length and blotdiameter? -- jr
195 There are no 32th/64th/128th mensural/neomensural rests. In
196 these cases, revert back to default style.
202 if (((style == "classical") || (style == "z")) && (durlog != 2))
205 these styles differ from the default in quarter rests only
210 if (style == "default")
213 Some parts of lily still prefer style "default" over "".
214 Correct this here. -- jr
219 return ("rests." + ::to_string (durlog) + (is_ledgered ? "o" : "")
223 MAKE_SCHEME_CALLBACK (Rest, print, 1);
225 Rest::brew_internal_stencil (Grob *me, bool ledgered)
227 SCM durlog_scm = me->get_property ("duration-log");
228 if (!scm_is_number (durlog_scm))
229 return Stencil ().smobbed_copy ();
231 int durlog = scm_to_int (durlog_scm);
233 string style = robust_symbol2string (me->get_property ("style"), "default");
235 Font_metric *fm = Font_interface::get_default_font (me);
236 string font_char = glyph_name (me, durlog, style, ledgered, 0.0);
237 Stencil out = fm->find_by_name (font_char);
239 me->warning (_f ("rest `%s' not found", font_char.c_str ()));
241 return out.smobbed_copy ();
245 translate the rest vertically by amount DY, but only if
246 it doesn't have staff-position set.
249 Rest::translate (Grob *me, int dy)
251 if (!scm_is_number (me->get_property ("staff-position")))
253 me->translate_axis (dy * Staff_symbol_referencer::staff_space (me) / 2.0, Y_AXIS);
254 Grob *p = me->get_parent (Y_AXIS);
255 p->flush_extent_cache (Y_AXIS);
260 Rest::print (SCM smob)
262 return brew_internal_stencil (Grob::unsmob (smob), true);
265 MAKE_SCHEME_CALLBACK (Rest, width, 1);
267 We need the callback. The real stencil has ledgers depending on
268 Y-position. The Y-position is known only after line breaking. */
270 Rest::width (SCM smob)
272 return generic_extent_callback (Grob::unsmob (smob), X_AXIS);
275 MAKE_SCHEME_CALLBACK (Rest, height, 1);
277 Rest::height (SCM smob)
279 return generic_extent_callback (Grob::unsmob (smob), Y_AXIS);
283 We need the callback. The real stencil has ledgers depending on
284 Y-position. The Y-position is known only after line breaking. */
286 Rest::generic_extent_callback (Grob *me, Axis a)
289 Don't want ledgers: ledgers depend on Y position, which depends on
290 rest collision, which depends on stem size which depends on beam
291 slop of opposite note column.
293 consequence: we get too small extents and potential collisions
296 SCM m = brew_internal_stencil (me, a != X_AXIS);
297 return ly_interval2scm (Stencil::unsmob (m)->extent (a));
300 MAKE_SCHEME_CALLBACK (Rest, pure_height, 3);
302 Rest::pure_height (SCM smob,
306 Grob *me = Grob::unsmob (smob);
307 SCM m = brew_internal_stencil (me, false);
308 return ly_interval2scm (Stencil::unsmob (m)->extent (Y_AXIS));
312 "A rest symbol. The property @code{style} can be"
313 " @code{default}, @code{mensural}, @code{neomensural} or"
314 " @code{classical}.",