Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / multi-measure-rest.cc
blob9628402a5ecec1ce899d4ef1d4ea57002b7529af
1 /*
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 "lookup.hh"
24 #include "misc.hh"
25 #include "output-def.hh"
26 #include "paper-column.hh" // urg
27 #include "percent-repeat-item.hh"
28 #include "rest.hh"
29 #include "separation-item.hh"
30 #include "spanner.hh"
31 #include "staff-symbol-referencer.hh"
32 #include "system.hh"
33 #include "text-interface.hh"
34 #include "warn.hh"
36 Interval
37 Multi_measure_rest::bar_width (Spanner *me)
39 SCM spacing_pair = me->get_property ("spacing-pair");
40 Interval iv;
41 Direction d = LEFT;
44 Item *col = me->get_bound (d)->get_column ();
45 SCM align_sym
46 = (scm_is_pair (spacing_pair)
47 ? index_get_cell (spacing_pair, d)
48 : ly_symbol2scm ("staff-bar"));
49 Interval coldim = Paper_column::break_align_width (col, align_sym);
51 iv[d] = coldim[-d];
53 while (flip (&d) != LEFT);
55 return iv;
58 MAKE_SCHEME_CALLBACK (Multi_measure_rest, percent, 1);
59 SCM
60 Multi_measure_rest::percent (SCM smob)
62 Grob *me = unsmob_grob (smob);
63 Spanner *sp = dynamic_cast<Spanner *> (me);
65 Stencil r = Percent_repeat_item_interface::x_percent (me, 1);
66 r.translate_axis (-r.extent (X_AXIS).center (), X_AXIS);
68 // ugh copy & paste.
70 Grob *common_x = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT),
71 X_AXIS);
72 Interval sp_iv = bar_width (sp);
73 Real x_off = 0.0;
75 Real rx = sp->get_bound (LEFT)->relative_coordinate (common_x, X_AXIS);
77 we gotta stay clear of sp_iv, so move a bit to the right if
78 needed.
80 x_off += max (sp_iv[LEFT] - rx, 0.0);
83 center between stuff.
85 x_off += sp_iv.length () / 2;
87 r.translate_axis (x_off, X_AXIS);
89 return r.smobbed_copy ();
92 MAKE_SCHEME_CALLBACK (Multi_measure_rest, print, 1);
93 SCM
94 Multi_measure_rest::print (SCM smob)
96 Grob *me = unsmob_grob (smob);
97 Spanner *sp = dynamic_cast<Spanner *> (me);
99 Interval sp_iv = bar_width (sp);
100 Real space = sp_iv.length ();
102 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
104 we gotta stay clear of sp_iv, so move a bit to the right if
105 needed.
107 Real x_off = max (sp_iv[LEFT] - rx, 0.0);
109 Stencil mol;
110 mol.add_stencil (symbol_stencil (me, space));
112 int measures = 0;
113 SCM m (me->get_property ("measure-count"));
114 if (scm_is_number (m))
115 measures = scm_to_int (m);
117 mol.translate_axis (x_off, X_AXIS);
118 return mol.smobbed_copy ();
121 Stencil
122 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
124 int measures = 0;
125 SCM m (me->get_property ("measure-count"));
126 if (scm_is_number (m))
127 measures = scm_to_int (m);
128 if (measures <= 0)
129 return Stencil ();
131 SCM limit = me->get_property ("expand-limit");
132 if (measures > scm_to_int (limit))
134 Real padding = 0.15;
135 Stencil s = big_rest (me, (1.0 - 2 * padding) * space);
136 s.translate_axis (padding * space, X_AXIS);
137 return s;
140 Real staff_space = Staff_symbol_referencer::staff_space (me);
142 Font_metric *musfont = Font_interface::get_default_font (me);
144 SCM sml = me->get_property ("use-breve-rest");
145 if (measures == 1)
147 if (to_boolean (sml))
149 Stencil s = musfont->find_by_name (Rest::glyph_name (me, -1, "", false));
151 s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
153 return s;
155 else
157 Stencil s = musfont->find_by_name (Rest::glyph_name (me, 0, "", true));
160 ugh.
162 if (Staff_symbol_referencer::get_position (me) == 0.0)
163 s.translate_axis (staff_space, Y_AXIS);
165 s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
167 return s;
170 else
171 return church_rest (me, musfont, measures, space);
175 WIDTH can also be 0 to determine the minimum size of the object.
177 Stencil
178 Multi_measure_rest::big_rest (Grob *me, Real width)
180 Real thick_thick = robust_scm2double (me->get_property ("thick-thickness"), 1.0);
181 Real hair_thick = robust_scm2double (me->get_property ("hair-thickness"), .1);
183 Real ss = Staff_symbol_referencer::staff_space (me);
184 Real slt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
185 Real y = slt * thick_thick / 2 * ss;
186 Real ythick = hair_thick * slt * ss;
187 Box b (Interval (0.0, max (0.0, (width - 2 * ythick))), Interval (-y, y));
189 Real blot = width ? (.8 * min (y, ythick)) : 0.0;
191 Stencil m = Lookup::round_filled_box (b, blot);
192 Stencil yb = Lookup::round_filled_box (Box (Interval (-0.5, 0.5) * ythick, Interval (-ss, ss)), blot);
194 m.add_at_edge (X_AXIS, RIGHT, yb, 0);
195 m.add_at_edge (X_AXIS, LEFT, yb, 0);
197 m.align_to (X_AXIS, LEFT);
199 return m;
203 Kirchenpause (?)
205 Stencil
206 Multi_measure_rest::church_rest (Grob *me, Font_metric *musfont, int measures,
207 Real space)
209 SCM mols = SCM_EOL;
211 /* See Wanske pp. 125 */
212 int l = measures;
213 int count = 0;
214 Real symbols_width = 0.0;
216 bool use_breve = to_boolean (me->get_property ("use-breve-rest"));
218 while (l)
220 if (use_breve)
222 int k;
223 if (l >= 2)
225 l -= 2;
226 k = -2;
228 else
230 l -= 1;
231 k = -1;
234 Stencil r (musfont->find_by_name ("rests." + to_string (k)));
235 symbols_width += r.extent (X_AXIS).length ();
236 mols = scm_cons (r.smobbed_copy (), mols);
238 else
240 int k;
241 if (l >= 4)
243 l -= 4;
244 k = -2;
246 else if (l >= 2)
248 l -= 2;
249 k = -1;
251 else
253 k = 0;
254 l--;
257 Stencil r (musfont->find_by_name ("rests." + to_string (k)));
258 if (k == 0)
260 Real staff_space = Staff_symbol_referencer::staff_space (me);
261 r.translate_axis (staff_space, Y_AXIS);
263 symbols_width += r.extent (X_AXIS).length ();
264 mols = scm_cons (r.smobbed_copy (), mols);
266 count++;
269 /* Make outer padding this much bigger. */
270 Real outer_padding_factor = 1.5;
271 Real inner_padding = (space - symbols_width)
272 / (2 * outer_padding_factor + (count - 1));
273 if (inner_padding < 0)
274 inner_padding = 1.0;
276 Stencil mol;
277 for (SCM s = mols; scm_is_pair (s); s = scm_cdr (s))
278 mol.add_at_edge (X_AXIS, LEFT, *unsmob_stencil (scm_car (s)),
279 inner_padding);
280 mol.align_to (X_AXIS, LEFT);
281 mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
283 return mol;
286 void
287 Multi_measure_rest::add_column (Grob *me, Item *c)
289 add_bound_item (dynamic_cast<Spanner *> (me), c);
292 void
293 Multi_measure_rest::calculate_spacing_rods (Grob *me, Real length)
295 Spanner *sp = dynamic_cast<Spanner *> (me);
296 if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
298 programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
299 return;
302 Item *li = sp->get_bound (LEFT)->get_column ();
303 Item *ri = sp->get_bound (RIGHT)->get_column ();
304 Item *lb = li->find_prebroken_piece (RIGHT);
305 Item *rb = ri->find_prebroken_piece (LEFT);
307 Item *combinations[4][2] = {{li, ri},
308 {lb, ri},
309 {li, rb},
310 {lb, rb}};
312 for (int i = 0; i < 4; i++)
314 Item *li = combinations[i][0];
315 Item *ri = combinations[i][1];
317 if (!li || !ri)
318 continue;
320 Rod rod;
321 rod.item_drul_[LEFT] = li;
322 rod.item_drul_[RIGHT] = ri;
324 rod.distance_ = Paper_column::minimum_distance (li, ri)
325 + length
326 + 2 * robust_scm2double (me->get_property ("bound-padding"), 1.0);
328 Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0);
329 rod.distance_ = max (rod.distance_, minlen);
330 rod.add_to_cols ();
334 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods, 1);
336 Multi_measure_rest::set_spacing_rods (SCM smob)
338 Grob *me = unsmob_grob (smob);
339 Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
340 calculate_spacing_rods (me, sym_width);
342 return SCM_UNSPECIFIED;
345 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_text_rods, 1);
347 Multi_measure_rest::set_text_rods (SCM smob)
349 Grob *me = unsmob_grob (smob);
350 Stencil *stil = me->get_stencil ();
352 /* FIXME uncached */
353 Real len = (stil && !stil->extent (X_AXIS).is_empty ())
354 ? stil->extent (X_AXIS).length ()
355 : 0.0;
356 calculate_spacing_rods (me, len);
358 return SCM_UNSPECIFIED;
361 ADD_INTERFACE (Multi_measure_rest,
362 "A rest that spans a whole number of measures.",
364 /* properties */
365 "bound-padding "
366 "expand-limit "
367 "hair-thickness "
368 "measure-count "
369 "minimum-length "
370 "spacing-pair "
371 "thick-thickness "
372 "use-breve-rest "