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"
23 #include "international.hh"
27 #include "output-def.hh"
28 #include "paper-column.hh" // urg
29 #include "percent-repeat-item.hh"
31 #include "separation-item.hh"
33 #include "staff-symbol-referencer.hh"
35 #include "text-interface.hh"
39 Multi_measure_rest::bar_width (Spanner *me)
41 SCM spacing_pair = me->get_property ("spacing-pair");
46 Item *col = me->get_bound (d)->get_column ();
48 = (scm_is_pair (spacing_pair)
49 ? index_get_cell (spacing_pair, d)
50 : ly_symbol2scm ("staff-bar"));
51 Interval coldim = Paper_column::break_align_width (col, align_sym);
55 while (flip (&d) != LEFT);
60 MAKE_SCHEME_CALLBACK (Multi_measure_rest, percent, 1);
62 Multi_measure_rest::percent (SCM smob)
64 Grob *me = unsmob_grob (smob);
65 Spanner *sp = dynamic_cast<Spanner *> (me);
67 Stencil r = Percent_repeat_item_interface::x_percent (me, 1);
68 r.translate_axis (-r.extent (X_AXIS).center (), X_AXIS);
72 Grob *common_x = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT),
74 Interval sp_iv = bar_width (sp);
77 Real rx = sp->get_bound (LEFT)->relative_coordinate (common_x, X_AXIS);
79 we gotta stay clear of sp_iv, so move a bit to the right if
82 x_off += max (sp_iv[LEFT] - rx, 0.0);
87 x_off += sp_iv.length () / 2;
89 r.translate_axis (x_off, X_AXIS);
91 return r.smobbed_copy ();
94 MAKE_SCHEME_CALLBACK (Multi_measure_rest, print, 1);
96 Multi_measure_rest::print (SCM smob)
98 Grob *me = unsmob_grob (smob);
99 Spanner *sp = dynamic_cast<Spanner *> (me);
101 Interval sp_iv = bar_width (sp);
102 Real space = sp_iv.length ();
104 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
106 we gotta stay clear of sp_iv, so move a bit to the right if
109 Real x_off = max (sp_iv[LEFT] - rx, 0.0);
112 mol.add_stencil (symbol_stencil (me, space));
114 mol.translate_axis (x_off, X_AXIS);
115 return mol.smobbed_copy ();
119 calc_closest_duration_log (Grob *me, double duration, bool force_round_up)
121 bool round_up = force_round_up
122 || to_boolean (me->get_property ("round-up-to-longer-rest"));
123 int closest_usable_duration_log;
125 // Out of range initial values.
127 closest_usable_duration_log = -15; // high value
129 closest_usable_duration_log = 15; // low value
130 int minimum_usable_duration_log = -15;
131 int maximum_usable_duration_log = 15;
133 SCM duration_logs_list = me->get_property ("usable-duration-logs");
134 if (to_boolean (scm_null_p (duration_logs_list))
135 || !to_boolean (scm_list_p (duration_logs_list)))
137 warning (_ ("usable-duration-logs must be a non-empty list. Falling back to whole rests."));
138 closest_usable_duration_log = 0;
142 for (SCM s = duration_logs_list; scm_is_pair (s); s = scm_cdr (s))
144 int dur_log = scm_to_int (scm_car (s));
145 if (dur_log > minimum_usable_duration_log)
146 minimum_usable_duration_log = dur_log;
147 if (dur_log < maximum_usable_duration_log)
148 maximum_usable_duration_log = dur_log;
149 double dur = pow (2.0, -dur_log);
152 if (duration <= dur && dur_log > closest_usable_duration_log)
153 closest_usable_duration_log = dur_log;
157 if (duration >= dur && dur_log < closest_usable_duration_log)
158 closest_usable_duration_log = dur_log;
163 if (closest_usable_duration_log == 15)
164 closest_usable_duration_log = minimum_usable_duration_log;
165 if (closest_usable_duration_log == -15)
166 closest_usable_duration_log = maximum_usable_duration_log;
168 return closest_usable_duration_log;
172 calc_measure_duration_log (Grob *me)
174 SCM sml = dynamic_cast<Spanner *> (me)->get_bound (LEFT)
175 ->get_property ("measure-length");
176 Rational ml = (unsmob_moment (sml)) ? unsmob_moment (sml)->main_part_
178 double measure_duration = ml.Rational::to_double ();
179 bool force_round_up = to_boolean (scm_list_p (scm_member (scm_cons (scm_from_int64 (ml.numerator ()),
180 scm_from_int64 (ml.denominator ())),
181 me->get_property ("round-up-exceptions"))));
182 return calc_closest_duration_log (me, measure_duration, force_round_up);
186 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
188 int measure_count = 0;
189 SCM m (me->get_property ("measure-count"));
190 if (scm_is_number (m))
191 measure_count = scm_to_int (m);
192 if (measure_count <= 0)
195 SCM limit = me->get_property ("expand-limit");
196 if (measure_count > scm_to_int (limit))
199 Stencil s = big_rest (me, (1.0 - 2 * padding) * space);
200 s.translate_axis (padding * space, X_AXIS);
204 Real staff_space = Staff_symbol_referencer::staff_space (me);
206 Font_metric *musfont = Font_interface::get_default_font (me);
207 int mdl = calc_measure_duration_log (me);
209 if (measure_count == 1)
211 Stencil s = musfont->find_by_name (Rest::glyph_name (me, mdl, "", true));
212 if (mdl == 0 && me->get_property ("staff-position") == SCM_EOL)
213 s.translate_axis (staff_space, Y_AXIS);
215 s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
219 return church_rest (me, musfont, measure_count, space);
223 WIDTH can also be 0 to determine the minimum size of the object.
226 Multi_measure_rest::big_rest (Grob *me, Real width)
228 Real thick_thick = robust_scm2double (me->get_property ("thick-thickness"), 1.0);
229 Real hair_thick = robust_scm2double (me->get_property ("hair-thickness"), .1);
231 Real ss = Staff_symbol_referencer::staff_space (me);
232 Real slt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
233 Real y = slt * thick_thick / 2 * ss;
234 Real ythick = hair_thick * slt * ss;
235 Box b (Interval (0.0, max (0.0, (width - 2 * ythick))), Interval (-y, y));
237 Real blot = width ? (.8 * min (y, ythick)) : 0.0;
239 Stencil m = Lookup::round_filled_box (b, blot);
240 Stencil yb = Lookup::round_filled_box (Box (Interval (-0.5, 0.5) * ythick, Interval (-ss, ss)), blot);
242 m.add_at_edge (X_AXIS, RIGHT, yb, 0);
243 m.add_at_edge (X_AXIS, LEFT, yb, 0);
245 m.align_to (X_AXIS, LEFT);
254 Multi_measure_rest::church_rest (Grob *me, Font_metric *musfont, int measure_count,
258 int symbol_count = 0;
259 Real symbols_width = 0.0;
260 double total_duration = measure_count * pow (2.0, -calc_measure_duration_log (me));
262 while (total_duration > 0)
264 int dl = calc_closest_duration_log (me, total_duration, false);
265 double duration = pow (2.0, -dl);
267 total_duration -= duration;
269 Stencil r = musfont->find_by_name (Rest::glyph_name (me, dl, "", true));
272 Real staff_space = Staff_symbol_referencer::staff_space (me);
273 r.translate_axis (staff_space, Y_AXIS);
275 symbols_width += r.extent (X_AXIS).length ();
276 mols = scm_cons (r.smobbed_copy (), mols);
280 /* Make outer padding this much bigger. */
281 Real outer_padding_factor = 1.5;
282 Real inner_padding = (space - symbols_width)
283 / (2 * outer_padding_factor + (symbol_count - 1));
284 if (inner_padding < 0)
288 for (SCM s = mols; scm_is_pair (s); s = scm_cdr (s))
289 mol.add_at_edge (X_AXIS, LEFT, *unsmob_stencil (scm_car (s)),
291 mol.align_to (X_AXIS, LEFT);
292 mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
298 Multi_measure_rest::add_column (Grob *me, Item *c)
300 add_bound_item (dynamic_cast<Spanner *> (me), c);
304 Multi_measure_rest::calculate_spacing_rods (Grob *me, Real length)
306 Spanner *sp = dynamic_cast<Spanner *> (me);
307 if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
309 programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
313 Item *li = sp->get_bound (LEFT)->get_column ();
314 Item *ri = sp->get_bound (RIGHT)->get_column ();
315 Item *lb = li->find_prebroken_piece (RIGHT);
316 Item *rb = ri->find_prebroken_piece (LEFT);
318 Item *combinations[4][2] = {{li, ri},
324 for (int i = 0; i < 4; i++)
326 Item *li = combinations[i][0];
327 Item *ri = combinations[i][1];
333 rod.item_drul_[LEFT] = li;
334 rod.item_drul_[RIGHT] = ri;
336 rod.distance_ = Paper_column::minimum_distance (li, ri)
338 + 2 * robust_scm2double (me->get_property ("bound-padding"), 1.0);
340 Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0);
341 rod.distance_ = max (rod.distance_, minlen);
346 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods, 1);
348 Multi_measure_rest::set_spacing_rods (SCM smob)
350 Grob *me = unsmob_grob (smob);
351 Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
352 calculate_spacing_rods (me, sym_width);
354 return SCM_UNSPECIFIED;
357 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_text_rods, 1);
359 Multi_measure_rest::set_text_rods (SCM smob)
361 Grob *me = unsmob_grob (smob);
362 Stencil *stil = me->get_stencil ();
365 Real len = (stil && !stil->extent (X_AXIS).is_empty ())
366 ? stil->extent (X_AXIS).length ()
368 calculate_spacing_rods (me, len);
370 return SCM_UNSPECIFIED;
373 ADD_INTERFACE (Multi_measure_rest,
374 "A rest that spans a whole number of measures.",
382 "round-up-exceptions "
383 "round-up-to-longer-rest "
386 "usable-duration-logs "