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