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