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