2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 Jan Nieuwenhuizen <janneke@gnu.org>
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 "multi-measure-rest.hh"
22 #include "font-interface.hh"
26 #include "output-def.hh"
27 #include "paper-column.hh" // urg
28 #include "percent-repeat-item.hh"
30 #include "separation-item.hh"
32 #include "staff-symbol-referencer.hh"
34 #include "text-interface.hh"
38 Multi_measure_rest::bar_width (Spanner *me)
40 SCM spacing_pair = me->get_property ("spacing-pair");
45 Item *col = me->get_bound (d)->get_column ();
47 = (scm_is_pair (spacing_pair)
48 ? index_get_cell (spacing_pair, d)
49 : ly_symbol2scm ("staff-bar"));
50 Interval coldim = Paper_column::break_align_width (col, align_sym);
54 while (flip (&d) != LEFT);
59 MAKE_SCHEME_CALLBACK (Multi_measure_rest, percent, 1);
61 Multi_measure_rest::percent (SCM smob)
63 Grob *me = unsmob_grob (smob);
64 Spanner *sp = dynamic_cast<Spanner *> (me);
66 Stencil r = Percent_repeat_item_interface::x_percent (me, 1);
67 r.translate_axis (-r.extent (X_AXIS).center (), X_AXIS);
71 Grob *common_x = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT),
73 Interval sp_iv = bar_width (sp);
76 Real rx = sp->get_bound (LEFT)->relative_coordinate (common_x, X_AXIS);
78 we gotta stay clear of sp_iv, so move a bit to the right if
81 x_off += max (sp_iv[LEFT] - rx, 0.0);
86 x_off += sp_iv.length () / 2;
88 r.translate_axis (x_off, X_AXIS);
90 return r.smobbed_copy ();
93 MAKE_SCHEME_CALLBACK (Multi_measure_rest, print, 1);
95 Multi_measure_rest::print (SCM smob)
97 Grob *me = unsmob_grob (smob);
98 Spanner *sp = dynamic_cast<Spanner *> (me);
100 Interval sp_iv = bar_width (sp);
101 Real space = sp_iv.length ();
103 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
105 we gotta stay clear of sp_iv, so move a bit to the right if
108 Real x_off = max (sp_iv[LEFT] - rx, 0.0);
111 mol.add_stencil (symbol_stencil (me, space));
114 SCM m (me->get_property ("measure-count"));
115 if (scm_is_number (m))
116 measures = scm_to_int (m);
118 mol.translate_axis (x_off, X_AXIS);
119 return mol.smobbed_copy ();
123 measure_duration_log (Grob *me)
125 SCM sml = dynamic_cast<Spanner *> (me)->get_bound (LEFT)
126 ->get_property ("measure-length");
127 bool round = to_boolean (me->get_property ("round-to-longer-rest"));
128 Rational ml = (unsmob_moment (sml)) ? unsmob_moment (sml)->main_part_ : Rational (1);
130 double duration_log = -log_2 (ml.Rational::to_double ());
131 int measure_duration_log = int (ceil (duration_log));
132 if (round && duration_log - measure_duration_log < 0)
133 measure_duration_log--;
135 SCM duration_logs_list = me->get_property ("usable-duration-logs");
136 int closest_list_elt = -15; // -15 is out of range.
138 for (int i = 0; i < scm_to_int (scm_length (duration_logs_list)); i++)
140 int list_elt = scm_to_int (scm_list_ref (duration_logs_list, scm_from_int (i)));
141 int shortest_distance = abs (measure_duration_log - closest_list_elt);
142 int distance = abs (measure_duration_log - list_elt);
143 if (distance < shortest_distance)
144 closest_list_elt = list_elt;
147 return closest_list_elt;
151 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
154 SCM m (me->get_property ("measure-count"));
155 if (scm_is_number (m))
156 measures = scm_to_int (m);
160 SCM limit = me->get_property ("expand-limit");
161 if (measures > scm_to_int (limit))
164 Stencil s = big_rest (me, (1.0 - 2 * padding) * space);
165 s.translate_axis (padding * space, X_AXIS);
169 Real staff_space = Staff_symbol_referencer::staff_space (me);
171 Font_metric *musfont = Font_interface::get_default_font (me);
172 int mdl = measure_duration_log (me);
176 Stencil s = musfont->find_by_name (Rest::glyph_name (me, mdl, "", true));
177 if (mdl == 0 && Staff_symbol_referencer::get_position (me) == 0.0)
178 s.translate_axis (staff_space, Y_AXIS);
180 s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
184 return church_rest (me, musfont, measures, space);
188 WIDTH can also be 0 to determine the minimum size of the object.
191 Multi_measure_rest::big_rest (Grob *me, Real width)
193 Real thick_thick = robust_scm2double (me->get_property ("thick-thickness"), 1.0);
194 Real hair_thick = robust_scm2double (me->get_property ("hair-thickness"), .1);
196 Real ss = Staff_symbol_referencer::staff_space (me);
197 Real slt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
198 Real y = slt * thick_thick / 2 * ss;
199 Real ythick = hair_thick * slt * ss;
200 Box b (Interval (0.0, max (0.0, (width - 2 * ythick))), Interval (-y, y));
202 Real blot = width ? (.8 * min (y, ythick)) : 0.0;
204 Stencil m = Lookup::round_filled_box (b, blot);
205 Stencil yb = Lookup::round_filled_box (Box (Interval (-0.5, 0.5) * ythick, Interval (-ss, ss)), blot);
207 m.add_at_edge (X_AXIS, RIGHT, yb, 0);
208 m.add_at_edge (X_AXIS, LEFT, yb, 0);
210 m.align_to (X_AXIS, LEFT);
219 Multi_measure_rest::church_rest (Grob *me, Font_metric *musfont, int measures,
226 Real symbols_width = 0.0;
227 SCM duration_logs_list = me->get_property ("usable-duration-logs");
228 int longest_church_rest = 10; // 10 is out of range.
229 for (int i = 0; i < scm_to_int (scm_length (duration_logs_list)); i++)
231 longest_church_rest = min (longest_church_rest,
232 scm_to_int (scm_list_ref (duration_logs_list,
239 int i = longest_church_rest - 1;
241 int mdl = measure_duration_log (me);
246 length = int (pow (2.0, -i));
249 !(l >= length && mdl >= longest_church_rest - i));
254 Stencil r (musfont->find_by_name ("rests." + to_string (k)));
257 Real staff_space = Staff_symbol_referencer::staff_space (me);
258 r.translate_axis (staff_space, Y_AXIS);
260 symbols_width += r.extent (X_AXIS).length ();
261 mols = scm_cons (r.smobbed_copy (), mols);
265 /* Make outer padding this much bigger. */
266 Real outer_padding_factor = 1.5;
267 Real inner_padding = (space - symbols_width)
268 / (2 * outer_padding_factor + (count - 1));
269 if (inner_padding < 0)
273 for (SCM s = mols; scm_is_pair (s); s = scm_cdr (s))
274 mol.add_at_edge (X_AXIS, LEFT, *unsmob_stencil (scm_car (s)),
276 mol.align_to (X_AXIS, LEFT);
277 mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
283 Multi_measure_rest::add_column (Grob *me, Item *c)
285 add_bound_item (dynamic_cast<Spanner *> (me), c);
289 Multi_measure_rest::calculate_spacing_rods (Grob *me, Real length)
291 Spanner *sp = dynamic_cast<Spanner *> (me);
292 if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
294 programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
298 Item *li = sp->get_bound (LEFT)->get_column ();
299 Item *ri = sp->get_bound (RIGHT)->get_column ();
300 Item *lb = li->find_prebroken_piece (RIGHT);
301 Item *rb = ri->find_prebroken_piece (LEFT);
303 Item *combinations[4][2] = {{li, ri},
308 for (int i = 0; i < 4; i++)
310 Item *li = combinations[i][0];
311 Item *ri = combinations[i][1];
317 rod.item_drul_[LEFT] = li;
318 rod.item_drul_[RIGHT] = ri;
320 rod.distance_ = Paper_column::minimum_distance (li, ri)
322 + 2 * robust_scm2double (me->get_property ("bound-padding"), 1.0);
324 Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0);
325 rod.distance_ = max (rod.distance_, minlen);
330 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods, 1);
332 Multi_measure_rest::set_spacing_rods (SCM smob)
334 Grob *me = unsmob_grob (smob);
335 Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
336 calculate_spacing_rods (me, sym_width);
338 return SCM_UNSPECIFIED;
341 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_text_rods, 1);
343 Multi_measure_rest::set_text_rods (SCM smob)
345 Grob *me = unsmob_grob (smob);
346 Stencil *stil = me->get_stencil ();
349 Real len = (stil && !stil->extent (X_AXIS).is_empty ())
350 ? stil->extent (X_AXIS).length ()
352 calculate_spacing_rods (me, len);
354 return SCM_UNSPECIFIED;
357 ADD_INTERFACE (Multi_measure_rest,
358 "A rest that spans a whole number of measures.",
366 "round-to-longer-rest "
369 "usable-duration-logs "