]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
Revert "Makes multi-measure rests span the correct horizontal length."
[lilypond.git] / lily / multi-measure-rest.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2014 Jan Nieuwenhuizen <janneke@gnu.org>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "multi-measure-rest.hh"
21
22 #include "directional-element-interface.hh"
23 #include "font-interface.hh"
24 #include "international.hh"
25 #include "lookup.hh"
26 #include "misc.hh"
27 #include "moment.hh"
28 #include "output-def.hh"
29 #include "paper-column.hh" // urg
30 #include "percent-repeat-item.hh"
31 #include "rest.hh"
32 #include "separation-item.hh"
33 #include "spanner.hh"
34 #include "staff-symbol.hh"
35 #include "staff-symbol-referencer.hh"
36 #include "system.hh"
37 #include "text-interface.hh"
38 #include "warn.hh"
39
40 Interval
41 Multi_measure_rest::bar_width (Spanner *me)
42 {
43   SCM spacing_pair = me->get_property ("spacing-pair");
44   Interval iv;
45   for (LEFT_and_RIGHT (d))
46     {
47       Item *col = me->get_bound (d)->get_column ();
48       SCM align_sym
49         = (scm_is_pair (spacing_pair)
50            ? index_get_cell (spacing_pair, d)
51            : ly_symbol2scm ("staff-bar"));
52       Interval coldim = Paper_column::break_align_width (col, align_sym);
53
54       iv[d] = coldim[-d];
55     }
56
57   return iv;
58 }
59
60 MAKE_SCHEME_CALLBACK (Multi_measure_rest, percent, 1);
61 SCM
62 Multi_measure_rest::percent (SCM smob)
63 {
64   Grob *me = Grob::unsmob (smob);
65   Spanner *sp = dynamic_cast<Spanner *> (me);
66
67   Stencil r = Percent_repeat_item_interface::x_percent (me, 1);
68   r.translate_axis (-r.extent (X_AXIS).center (), X_AXIS);
69
70   // ugh copy & paste.
71
72   Grob *common_x = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT),
73                                                           X_AXIS);
74   Interval sp_iv = bar_width (sp);
75   Real x_off = 0.0;
76
77   Real rx = sp->get_bound (LEFT)->relative_coordinate (common_x, X_AXIS);
78   /*
79     we gotta stay clear of sp_iv, so move a bit to the right if
80     needed.
81   */
82   x_off += max (sp_iv[LEFT] - rx, 0.0);
83
84   /*
85     center between stuff.
86   */
87   x_off += sp_iv.length () / 2;
88
89   r.translate_axis (x_off, X_AXIS);
90
91   return r.smobbed_copy ();
92 }
93
94 MAKE_SCHEME_CALLBACK (Multi_measure_rest, print, 1);
95 SCM
96 Multi_measure_rest::print (SCM smob)
97 {
98   Grob *me = Grob::unsmob (smob);
99   Spanner *sp = dynamic_cast<Spanner *> (me);
100
101   Interval sp_iv = bar_width (sp);
102   Real space = sp_iv.length ();
103
104   Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
105   /*
106     we gotta stay clear of sp_iv, so move a bit to the right if
107     needed.
108   */
109   Real x_off = max (sp_iv[LEFT] - rx, 0.0);
110
111   Stencil mol;
112   mol.add_stencil (symbol_stencil (me, space));
113
114   mol.translate_axis (x_off, X_AXIS);
115   return mol.smobbed_copy ();
116 }
117
118 MAKE_SCHEME_CALLBACK (Multi_measure_rest, height, 1);
119 SCM
120 Multi_measure_rest::height (SCM smob)
121 {
122   Grob *me = Grob::unsmob (smob);
123
124   Real space = 1000000; // something very large...
125
126   Stencil mol;
127   mol.add_stencil (symbol_stencil (me, space));
128
129   return ly_interval2scm (mol.extent (Y_AXIS));
130 }
131
132 int
133 calc_closest_duration_log (Grob *me, double duration, bool force_round_up)
134 {
135   bool round_up = force_round_up
136                   || to_boolean (me->get_property ("round-up-to-longer-rest"));
137   int closest_usable_duration_log;
138
139   // Out of range initial values.
140   if (round_up)
141     closest_usable_duration_log = -15; // high value
142   else
143     closest_usable_duration_log = 15; // low value
144   int minimum_usable_duration_log = -15;
145   int maximum_usable_duration_log = 15;
146
147   SCM duration_logs_list = me->get_property ("usable-duration-logs");
148   if (to_boolean (scm_null_p (duration_logs_list))
149       || !to_boolean (scm_list_p (duration_logs_list)))
150     {
151       warning (_ ("usable-duration-logs must be a non-empty list."
152                   "  Falling back to whole rests."));
153       closest_usable_duration_log = 0;
154     }
155   else
156     {
157       for (SCM s = duration_logs_list; scm_is_pair (s); s = scm_cdr (s))
158         {
159           int dur_log = scm_to_int (scm_car (s));
160           if (dur_log > minimum_usable_duration_log)
161             minimum_usable_duration_log = dur_log;
162           if (dur_log < maximum_usable_duration_log)
163             maximum_usable_duration_log = dur_log;
164           double dur = pow (2.0, -dur_log);
165           if (round_up)
166             {
167               if (duration <= dur && dur_log > closest_usable_duration_log)
168                 closest_usable_duration_log = dur_log;
169             }
170           else
171             {
172               if (duration >= dur && dur_log < closest_usable_duration_log)
173                 closest_usable_duration_log = dur_log;
174             }
175         }
176     }
177
178   if (closest_usable_duration_log == 15)
179     closest_usable_duration_log = minimum_usable_duration_log;
180   if (closest_usable_duration_log == -15)
181     closest_usable_duration_log = maximum_usable_duration_log;
182
183   return closest_usable_duration_log;
184 }
185
186 int
187 calc_measure_duration_log (Grob *me)
188 {
189   SCM sml = dynamic_cast<Spanner *> (me)->get_bound (LEFT)
190             ->get_property ("measure-length");
191   Rational ml = (Moment::is_smob (sml)) ? Moment::unsmob (sml)->main_part_
192                 : Rational (1);
193   double measure_duration = ml.Rational::to_double ();
194   bool force_round_up = to_boolean (scm_list_p (scm_member (scm_cons (scm_from_int64 (ml.numerator ()),
195                                                             scm_from_int64 (ml.denominator ())),
196                                                             me->get_property ("round-up-exceptions"))));
197   return calc_closest_duration_log (me, measure_duration, force_round_up);
198 }
199
200 Stencil
201 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
202 {
203   int measure_count = 0;
204   SCM m (me->get_property ("measure-count"));
205   if (scm_is_number (m))
206     measure_count = scm_to_int (m);
207   if (measure_count <= 0)
208     return Stencil ();
209
210   SCM limit = me->get_property ("expand-limit");
211   if (measure_count > scm_to_int (limit))
212     {
213       Real padding = 0.15;
214       Stencil s = big_rest (me, (1.0 - 2 * padding) * space);
215       s.translate_axis (padding * space, X_AXIS);
216       return s;
217     }
218
219   Font_metric *musfont = Font_interface::get_default_font (me);
220   int mdl = calc_measure_duration_log (me);
221
222   if (measure_count == 1)
223     {
224       if (me->get_property ("staff-position") == SCM_EOL)
225         {
226           int dir = get_grob_direction (me);
227           Real pos = Rest::staff_position_internal (me, mdl, dir);
228           me->set_property ("staff-position", scm_from_double (pos));
229         }
230  
231       Stencil s = musfont->find_by_name (Rest::glyph_name (me, mdl, "", true, 0.0));
232
233       s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
234       return s;
235     }
236   else
237     return church_rest (me, musfont, measure_count, space);
238 }
239
240 /*
241   WIDTH can also be 0 to determine the minimum size of the object.
242 */
243 Stencil
244 Multi_measure_rest::big_rest (Grob *me, Real width)
245 {
246   Real thick_thick = robust_scm2double (me->get_property ("thick-thickness"), 1.0);
247   Real hair_thick = robust_scm2double (me->get_property ("hair-thickness"), .1);
248
249   Real ss = Staff_symbol_referencer::staff_space (me);
250   Real slt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
251   Real y = slt * thick_thick / 2 * ss;
252   Real ythick = hair_thick * slt * ss;
253   Box b (Interval (0.0, max (0.0, (width - 2 * ythick))), Interval (-y, y));
254
255   Real blot = width ? (.8 * min (y, ythick)) : 0.0;
256
257   Stencil m = Lookup::round_filled_box (b, blot);
258   Stencil yb = Lookup::round_filled_box (Box (Interval (-0.5, 0.5) * ythick, Interval (-ss, ss)), blot);
259
260   m.add_at_edge (X_AXIS, RIGHT, yb, 0);
261   m.add_at_edge (X_AXIS, LEFT, yb, 0);
262
263   m.align_to (X_AXIS, LEFT);
264
265   return m;
266 }
267
268 /*
269   Kirchenpause (?)
270 */
271 Stencil
272 Multi_measure_rest::church_rest (Grob *me, Font_metric *musfont, int measure_count,
273                                  Real space)
274 {
275   SCM mols = SCM_EOL;
276   int symbol_count = 0;
277   Real symbols_width = 0.0;
278   double total_duration = measure_count * pow (2.0, -calc_measure_duration_log (me));
279
280   SCM staff_position = me->get_property ("staff-position");
281
282   if (!scm_is_number (staff_position))
283     {
284       // Staff position is somewhat icky regarding its definition for
285       // compatibility reasons.  It is intended to be the baseline of
286       // a breve rest.  However, when the staff space is more than
287       // single space (like with tablature), it looks better if all
288       // rests are actually hanging.  So staff position, in reality,
289       // is the semi-breve position - 2.  Everything else is
290       // calculated from there.
291       int dir = get_grob_direction (me);
292       Real pos = Rest::staff_position_internal (me, 0, dir);
293       me->set_property ("staff-position", scm_from_double (pos - 2));
294     }
295
296   while (total_duration > 0)
297     {
298       int dl = calc_closest_duration_log (me, total_duration, false);
299       double duration = pow (2.0, -dl);
300
301       total_duration -= duration;
302
303       Stencil r = musfont->find_by_name (Rest::glyph_name (me, dl, "", true, 2));
304
305       Real staff_space = Staff_symbol_referencer::staff_space (me);
306       if (dl == 0)
307         {
308           r.translate_axis (staff_space, Y_AXIS);
309         }
310       else
311         {
312           r.translate_axis (staff_space-r.extent (Y_AXIS).at (UP), Y_AXIS);
313         }
314       symbols_width += r.extent (X_AXIS).length ();
315       mols = scm_cons (r.smobbed_copy (), mols);
316       symbol_count++;
317     }
318
319   /* Make outer padding this much bigger.  */
320   Real outer_padding_factor = 1.5;
321   Real inner_padding = (space - symbols_width)
322                        / (2 * outer_padding_factor + (symbol_count - 1));
323   if (inner_padding < 0)
324     inner_padding = 1.0;
325
326   Stencil mol;
327   for (SCM s = mols; scm_is_pair (s); s = scm_cdr (s))
328     mol.add_at_edge (X_AXIS, LEFT, *Stencil::unsmob (scm_car (s)),
329                      inner_padding);
330   mol.align_to (X_AXIS, LEFT);
331   mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
332
333   return mol;
334 }
335
336 void
337 Multi_measure_rest::add_column (Grob *me, Item *c)
338 {
339   add_bound_item (dynamic_cast<Spanner *> (me), c);
340 }
341
342 void
343 Multi_measure_rest::calculate_spacing_rods (Grob *me, Real length)
344 {
345   Spanner *sp = dynamic_cast<Spanner *> (me);
346   if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
347     {
348       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
349       return;
350     }
351
352   Item *li = sp->get_bound (LEFT)->get_column ();
353   Item *ri = sp->get_bound (RIGHT)->get_column ();
354   Item *lb = li->find_prebroken_piece (RIGHT);
355   Item *rb = ri->find_prebroken_piece (LEFT);
356
357   Item *combinations[4][2] = {{li, ri},
358     {lb, ri},
359     {li, rb},
360     {lb, rb}
361   };
362
363   for (int i = 0; i < 4; i++)
364     {
365       Item *li = combinations[i][0];
366       Item *ri = combinations[i][1];
367
368       if (!li || !ri)
369         continue;
370
371       Rod rod;
372       rod.item_drul_[LEFT] = li;
373       rod.item_drul_[RIGHT] = ri;
374
375       rod.distance_ = Paper_column::minimum_distance (li, ri)
376                       + length
377                       + 2 * robust_scm2double (me->get_property ("bound-padding"), 1.0);
378
379       Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0);
380       rod.distance_ = max (rod.distance_, minlen);
381       rod.add_to_cols ();
382     }
383 }
384
385 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods, 1);
386 SCM
387 Multi_measure_rest::set_spacing_rods (SCM smob)
388 {
389   Grob *me = Grob::unsmob (smob);
390   Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
391   calculate_spacing_rods (me, sym_width);
392
393   return SCM_UNSPECIFIED;
394 }
395
396 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_text_rods, 1);
397 SCM
398 Multi_measure_rest::set_text_rods (SCM smob)
399 {
400   Grob *me = Grob::unsmob (smob);
401   Stencil *stil = me->get_stencil ();
402
403   /* FIXME uncached */
404   Real len = (stil && !stil->extent (X_AXIS).is_empty ())
405              ? stil->extent (X_AXIS).length ()
406              : 0.0;
407   calculate_spacing_rods (me, len);
408
409   return SCM_UNSPECIFIED;
410 }
411
412 ADD_INTERFACE (Multi_measure_rest,
413                "A rest that spans a whole number of measures.",
414
415                /* properties */
416                "bound-padding "
417                "expand-limit "
418                "hair-thickness "
419                "measure-count "
420                "minimum-length "
421                "round-up-exceptions "
422                "round-up-to-longer-rest "
423                "spacing-pair "
424                "thick-thickness "
425                "usable-duration-logs "
426               );