]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
Merge branch 'Paco' into staging
[lilypond.git] / lily / multi-measure-rest.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2012 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 "duration.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 "spacing-options.hh"
34 #include "spanner.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 = unsmob_grob (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 = unsmob_grob (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 int
119 calc_closest_duration_log (Grob *me, double duration, bool force_round_up, bool paranoid)
120 {
121   bool round_up = force_round_up
122                   || to_boolean (me->get_property ("round-up-to-longer-rest"));
123   int closest_usable_duration_log;
124
125   // Out of range initial values.
126   if (round_up)
127     closest_usable_duration_log = -15; // high value
128   else
129     closest_usable_duration_log = 15; // low value
130   int minimum_usable_duration_log = -15;
131   int maximum_usable_duration_log = 15;
132
133   SCM duration_logs_list = me->get_property ("usable-duration-logs");
134   if (paranoid
135       && (to_boolean (scm_null_p (duration_logs_list))
136           || !to_boolean (scm_list_p (duration_logs_list))))
137     {
138       warning (_ ("usable-duration-logs must be a non-empty list.  Falling back to whole rests."));
139       closest_usable_duration_log = 0;
140     }
141   else
142     {
143       for (SCM s = duration_logs_list; scm_is_pair (s); s = scm_cdr (s))
144         {
145           int dur_log = scm_to_int (scm_car (s));
146           if (dur_log > minimum_usable_duration_log)
147             minimum_usable_duration_log = dur_log;
148           if (dur_log < maximum_usable_duration_log)
149             maximum_usable_duration_log = dur_log;
150           double dur = pow (2.0, -dur_log);
151           if (round_up)
152             {
153               if (duration <= dur && dur_log > closest_usable_duration_log)
154                 closest_usable_duration_log = dur_log;
155             }
156           else
157             {
158               if (duration >= dur && dur_log < closest_usable_duration_log)
159                 closest_usable_duration_log = dur_log;
160             }
161         }
162     }
163
164   if (closest_usable_duration_log == 15)
165     closest_usable_duration_log = minimum_usable_duration_log;
166   if (closest_usable_duration_log == -15)
167     closest_usable_duration_log = maximum_usable_duration_log;
168
169   return closest_usable_duration_log;
170 }
171
172 int
173 calc_measure_duration_log (Grob *me, bool paranoid)
174 {
175   SCM sml = dynamic_cast<Spanner *> (me)->get_bound (LEFT)
176             ->get_property ("measure-length");
177   Rational ml = (unsmob_moment (sml)) ? unsmob_moment (sml)->main_part_
178                 : Rational (1);
179   double measure_duration = ml.Rational::to_double ();
180   bool force_round_up = to_boolean (scm_list_p (scm_member (scm_cons (scm_from_int64 (ml.numerator ()),
181                                                             scm_from_int64 (ml.denominator ())),
182                                                             me->get_property ("round-up-exceptions"))));
183
184   return calc_closest_duration_log (me, measure_duration, force_round_up, paranoid);
185 }
186
187 Stencil
188 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
189 {
190   int measure_count = 0;
191   SCM m (me->get_property ("measure-count"));
192   if (scm_is_number (m))
193     measure_count = scm_to_int (m);
194   if (measure_count <= 0)
195     return Stencil ();
196
197   SCM limit = me->get_property ("expand-limit");
198   if (measure_count > scm_to_int (limit))
199     {
200       Real padding = 0.15;
201       Stencil s = big_rest (me, (1.0 - 2 * padding) * space);
202       s.translate_axis (padding * space, X_AXIS);
203       return s;
204     }
205
206   Font_metric *musfont = Font_interface::get_default_font (me);
207   int mdl = calc_measure_duration_log (me, true);
208
209   if (measure_count == 1)
210     {
211       if (mdl == 0 && me->get_property ("staff-position") == SCM_EOL)
212         {
213           if (Staff_symbol_referencer::on_staff_line (me, 2))
214             me->set_property ("staff-position", scm_from_int (2));
215           else if (Staff_symbol_referencer::on_staff_line (me, 3))
216             me->set_property ("staff-position", scm_from_int (3));
217         }
218
219       Stencil s = musfont->find_by_name (Rest::glyph_name (me, mdl, "", true));
220
221       s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
222       return s;
223     }
224   else
225     return church_rest (me, musfont, measure_count, space);
226 }
227
228 /*
229   WIDTH can also be 0 to determine the minimum size of the object.
230 */
231 Stencil
232 Multi_measure_rest::big_rest (Grob *me, Real width)
233 {
234   Real thick_thick = robust_scm2double (me->get_property ("thick-thickness"), 1.0);
235   Real hair_thick = robust_scm2double (me->get_property ("hair-thickness"), .1);
236
237   Real ss = Staff_symbol_referencer::staff_space (me);
238   Real slt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
239   Real y = slt * thick_thick / 2 * ss;
240   Real ythick = hair_thick * slt * ss;
241   Box b (Interval (0.0, max (0.0, (width - 2 * ythick))), Interval (-y, y));
242
243   Real blot = width ? (.8 * min (y, ythick)) : 0.0;
244
245   Stencil m = Lookup::round_filled_box (b, blot);
246   Stencil yb = Lookup::round_filled_box (Box (Interval (-0.5, 0.5) * ythick, Interval (-ss, ss)), blot);
247
248   m.add_at_edge (X_AXIS, RIGHT, yb, 0);
249   m.add_at_edge (X_AXIS, LEFT, yb, 0);
250
251   m.align_to (X_AXIS, LEFT);
252
253   return m;
254 }
255
256 /*
257   Kirchenpause (?)
258 */
259 Stencil
260 Multi_measure_rest::church_rest (Grob *me, Font_metric *musfont, int measure_count,
261                                  Real space)
262 {
263   SCM mols = SCM_EOL;
264   int symbol_count = 0;
265   Real symbols_width = 0.0;
266   double total_duration = measure_count * pow (2.0, -calc_measure_duration_log (me, true));
267
268   while (total_duration > 0)
269     {
270       int dl = calc_closest_duration_log (me, total_duration, false, true);
271       double duration = pow (2.0, -dl);
272
273       total_duration -= duration;
274
275       Stencil r = musfont->find_by_name (Rest::glyph_name (me, dl, "", true));
276       if (dl == 0)
277         {
278           Real staff_space = Staff_symbol_referencer::staff_space (me);
279           r.translate_axis (staff_space, Y_AXIS);
280         }
281       symbols_width += r.extent (X_AXIS).length ();
282       mols = scm_cons (r.smobbed_copy (), mols);
283       symbol_count++;
284     }
285
286   /* Make outer padding this much bigger.  */
287   Real outer_padding_factor = 1.5;
288   Real inner_padding = (space - symbols_width)
289                        / (2 * outer_padding_factor + (symbol_count - 1));
290   if (inner_padding < 0)
291     inner_padding = 1.0;
292
293   Stencil mol;
294   for (SCM s = mols; scm_is_pair (s); s = scm_cdr (s))
295     mol.add_at_edge (X_AXIS, LEFT, *unsmob_stencil (scm_car (s)),
296                      inner_padding);
297   mol.align_to (X_AXIS, LEFT);
298   mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
299
300   return mol;
301 }
302
303 void
304 Multi_measure_rest::add_column (Grob *me, Item *c)
305 {
306   add_bound_item (dynamic_cast<Spanner *> (me), c);
307 }
308
309 void
310 Multi_measure_rest::calculate_spacing_rods (Grob *me, Real length)
311 {
312   Spanner *sp = dynamic_cast<Spanner *> (me);
313   if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
314     {
315       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
316       return;
317     }
318
319   Item *li = sp->get_bound (LEFT)->get_column ();
320   Item *ri = sp->get_bound (RIGHT)->get_column ();
321   Item *lb = li->find_prebroken_piece (RIGHT);
322   Item *rb = ri->find_prebroken_piece (LEFT);
323   Grob *spacing = unsmob_grob (li->get_object ("spacing"));
324   if (!spacing)
325     spacing = unsmob_grob (ri->get_object ("spacing"));
326   if (!spacing)
327     me->warning ("Using naive multi measure rest spacing.");
328   else
329     {
330       Spacing_options options;
331       options.init_from_grob (me);
332       int dl = calc_measure_duration_log (me, false);
333       Duration dur = Duration (dl, 0);
334       Rational rat = dur.get_length ();
335       length = max (length, options.get_duration_space (rat));
336     }
337
338   Item *combinations[4][2] = {{li, ri},
339     {lb, ri},
340     {li, rb},
341     {lb, rb}
342   };
343
344   for (int i = 0; i < 4; i++)
345     {
346       Item *li = combinations[i][0];
347       Item *ri = combinations[i][1];
348
349       if (!li || !ri)
350         continue;
351
352       Rod rod;
353       rod.item_drul_[LEFT] = li;
354       rod.item_drul_[RIGHT] = ri;
355
356       rod.distance_ = Paper_column::minimum_distance (li, ri)
357                       + length
358                       + 2 * robust_scm2double (me->get_property ("bound-padding"), 1.0);
359
360       Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0);
361       rod.distance_ = max (rod.distance_, minlen);
362       rod.add_to_cols ();
363     }
364 }
365
366 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods, 1);
367 SCM
368 Multi_measure_rest::set_spacing_rods (SCM smob)
369 {
370   Grob *me = unsmob_grob (smob);
371   Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
372   calculate_spacing_rods (me, sym_width);
373
374   return SCM_UNSPECIFIED;
375 }
376
377 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_text_rods, 1);
378 SCM
379 Multi_measure_rest::set_text_rods (SCM smob)
380 {
381   Grob *me = unsmob_grob (smob);
382   Stencil *stil = me->get_stencil ();
383
384   /* FIXME uncached */
385   Real len = (stil && !stil->extent (X_AXIS).is_empty ())
386              ? stil->extent (X_AXIS).length ()
387              : 0.0;
388   calculate_spacing_rods (me, len);
389
390   return SCM_UNSPECIFIED;
391 }
392
393 ADD_INTERFACE (Multi_measure_rest,
394                "A rest that spans a whole number of measures.",
395
396                /* properties */
397                "bound-padding "
398                "expand-limit "
399                "hair-thickness "
400                "measure-count "
401                "minimum-length "
402                "round-up-exceptions "
403                "round-up-to-longer-rest "
404                "spacing-pair "
405                "thick-thickness "
406                "usable-duration-logs "
407               );