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