]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
Merge branch 'master' of git://git.savannah.gnu.org/lilypond.git
[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 "lookup.hh"
24 #include "misc.hh"
25 #include "moment.hh"
26 #include "output-def.hh"
27 #include "paper-column.hh" // urg
28 #include "percent-repeat-item.hh"
29 #include "rest.hh"
30 #include "separation-item.hh"
31 #include "spanner.hh"
32 #include "staff-symbol-referencer.hh"
33 #include "system.hh"
34 #include "text-interface.hh"
35 #include "warn.hh"
36
37 Interval
38 Multi_measure_rest::bar_width (Spanner *me)
39 {
40   SCM spacing_pair = me->get_property ("spacing-pair");
41   Interval iv;
42   Direction d = LEFT;
43   do
44     {
45       Item *col = me->get_bound (d)->get_column ();
46       SCM align_sym
47         = (scm_is_pair (spacing_pair)
48            ? index_get_cell (spacing_pair, d)
49            : ly_symbol2scm ("staff-bar"));
50       Interval coldim = Paper_column::break_align_width (col, align_sym);
51
52       iv[d] = coldim[-d];
53     }
54   while (flip (&d) != LEFT);
55
56   return iv;
57 }
58
59 MAKE_SCHEME_CALLBACK (Multi_measure_rest, percent, 1);
60 SCM
61 Multi_measure_rest::percent (SCM smob)
62 {
63   Grob *me = unsmob_grob (smob);
64   Spanner *sp = dynamic_cast<Spanner *> (me);
65
66   Stencil r = Percent_repeat_item_interface::x_percent (me, 1);
67   r.translate_axis (-r.extent (X_AXIS).center (), X_AXIS);
68
69   // ugh copy & paste.
70
71   Grob *common_x = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT),
72                                                           X_AXIS);
73   Interval sp_iv = bar_width (sp);
74   Real x_off = 0.0;
75
76   Real rx = sp->get_bound (LEFT)->relative_coordinate (common_x, X_AXIS);
77   /*
78     we gotta stay clear of sp_iv, so move a bit to the right if
79     needed.
80   */
81   x_off += max (sp_iv[LEFT] - rx, 0.0);
82
83   /*
84     center between stuff.
85   */
86   x_off += sp_iv.length () / 2;
87
88   r.translate_axis (x_off, X_AXIS);
89
90   return r.smobbed_copy ();
91 }
92
93 MAKE_SCHEME_CALLBACK (Multi_measure_rest, print, 1);
94 SCM
95 Multi_measure_rest::print (SCM smob)
96 {
97   Grob *me = unsmob_grob (smob);
98   Spanner *sp = dynamic_cast<Spanner *> (me);
99
100   Interval sp_iv = bar_width (sp);
101   Real space = sp_iv.length ();
102
103   Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
104   /*
105     we gotta stay clear of sp_iv, so move a bit to the right if
106     needed.
107   */
108   Real x_off = max (sp_iv[LEFT] - rx, 0.0);
109
110   Stencil mol;
111   mol.add_stencil (symbol_stencil (me, space));
112
113   int measures = 0;
114   SCM m (me->get_property ("measure-count"));
115   if (scm_is_number (m))
116     measures = scm_to_int (m);
117
118   mol.translate_axis (x_off, X_AXIS);
119   return mol.smobbed_copy ();
120 }
121
122 int
123 measure_duration_log (Grob *me)
124 {
125   SCM sml = dynamic_cast<Spanner *> (me)->get_bound (LEFT)
126                                         ->get_property ("measure-length");
127   bool round = to_boolean (me->get_property ("round-to-longer-rest"));
128   Rational ml = (unsmob_moment (sml)) ? unsmob_moment (sml)->main_part_ : Rational (1);
129
130   double duration_log = -log_2 (ml.Rational::to_double ());
131   int measure_duration_log = int (ceil (duration_log));
132   if (round && duration_log - measure_duration_log < 0)
133     measure_duration_log--;
134
135   SCM duration_logs_list = me->get_property ("usable-duration-logs");
136   int closest_list_elt = -15; // -15 is out of range.
137
138   for (int i = 0; i < scm_to_int (scm_length (duration_logs_list)); i++)
139   {
140     int list_elt = scm_to_int (scm_list_ref (duration_logs_list, scm_from_int (i)));
141     int shortest_distance = abs (measure_duration_log - closest_list_elt);
142     int distance = abs (measure_duration_log - list_elt);
143     if (distance < shortest_distance)
144       closest_list_elt = list_elt;
145   }
146
147   return closest_list_elt;
148 }
149
150 Stencil
151 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
152 {
153   int measures = 0;
154   SCM m (me->get_property ("measure-count"));
155   if (scm_is_number (m))
156     measures = scm_to_int (m);
157   if (measures <= 0)
158     return Stencil ();
159
160   SCM limit = me->get_property ("expand-limit");
161   if (measures > scm_to_int (limit))
162     {
163       Real padding = 0.15;
164       Stencil s = big_rest (me, (1.0 - 2 * padding) * space);
165       s.translate_axis (padding * space, X_AXIS);
166       return s;
167     }
168
169   Real staff_space = Staff_symbol_referencer::staff_space (me);
170
171   Font_metric *musfont = Font_interface::get_default_font (me);
172   int mdl = measure_duration_log (me);
173
174   if (measures == 1)
175     {
176       Stencil s = musfont->find_by_name (Rest::glyph_name (me, mdl, "", true));
177       if (mdl == 0 && Staff_symbol_referencer::get_position (me) == 0.0)
178         s.translate_axis (staff_space, Y_AXIS);
179
180       s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
181       return s;
182     }
183   else
184     return church_rest (me, musfont, measures, space);
185 }
186
187 /*
188   WIDTH can also be 0 to determine the minimum size of the object.
189 */
190 Stencil
191 Multi_measure_rest::big_rest (Grob *me, Real width)
192 {
193   Real thick_thick = robust_scm2double (me->get_property ("thick-thickness"), 1.0);
194   Real hair_thick = robust_scm2double (me->get_property ("hair-thickness"), .1);
195
196   Real ss = Staff_symbol_referencer::staff_space (me);
197   Real slt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
198   Real y = slt * thick_thick / 2 * ss;
199   Real ythick = hair_thick * slt * ss;
200   Box b (Interval (0.0, max (0.0, (width - 2 * ythick))), Interval (-y, y));
201
202   Real blot = width ? (.8 * min (y, ythick)) : 0.0;
203
204   Stencil m = Lookup::round_filled_box (b, blot);
205   Stencil yb = Lookup::round_filled_box (Box (Interval (-0.5, 0.5) * ythick, Interval (-ss, ss)), blot);
206
207   m.add_at_edge (X_AXIS, RIGHT, yb, 0);
208   m.add_at_edge (X_AXIS, LEFT, yb, 0);
209
210   m.align_to (X_AXIS, LEFT);
211
212   return m;
213 }
214
215 /*
216   Kirchenpause (?)
217 */
218 Stencil
219 Multi_measure_rest::church_rest (Grob *me, Font_metric *musfont, int measures,
220                                  Real space)
221 {
222   SCM mols = SCM_EOL;
223
224   int l = measures;
225   int count = 0;
226   Real symbols_width = 0.0;
227   SCM duration_logs_list = me->get_property ("usable-duration-logs");
228   int longest_church_rest = 10; // 10 is out of range.
229   for (int i = 0; i < scm_to_int (scm_length (duration_logs_list)); i++)
230   {
231     longest_church_rest = min (longest_church_rest,
232                                scm_to_int (scm_list_ref (duration_logs_list,
233                                                          scm_from_int (i))));
234   }
235
236   while (l)
237   {
238     int k;
239     int i = longest_church_rest - 1;
240     int length;
241     int mdl = measure_duration_log (me);
242
243     do
244     {
245       i++;
246       length = int (pow (2.0, -i));
247     }
248     while (i <= 0 &&
249            !(l >= length && mdl >= longest_church_rest - i));
250
251     l -= length;
252     k = mdl + i;
253
254     Stencil r (musfont->find_by_name ("rests." + to_string (k)));
255     if (k == 0)
256     {
257       Real staff_space = Staff_symbol_referencer::staff_space (me);
258       r.translate_axis (staff_space, Y_AXIS);
259     }
260     symbols_width += r.extent (X_AXIS).length ();
261     mols = scm_cons (r.smobbed_copy (), mols);
262     count++;
263   }
264
265   /* Make outer padding this much bigger.  */
266   Real outer_padding_factor = 1.5;
267   Real inner_padding = (space - symbols_width)
268     / (2 * outer_padding_factor + (count - 1));
269   if (inner_padding < 0)
270     inner_padding = 1.0;
271
272   Stencil mol;
273   for (SCM s = mols; scm_is_pair (s); s = scm_cdr (s))
274     mol.add_at_edge (X_AXIS, LEFT, *unsmob_stencil (scm_car (s)),
275                      inner_padding);
276   mol.align_to (X_AXIS, LEFT);
277   mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
278
279   return mol;
280 }
281
282 void
283 Multi_measure_rest::add_column (Grob *me, Item *c)
284 {
285   add_bound_item (dynamic_cast<Spanner *> (me), c);
286 }
287
288 void
289 Multi_measure_rest::calculate_spacing_rods (Grob *me, Real length)
290 {
291   Spanner *sp = dynamic_cast<Spanner *> (me);
292   if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
293     {
294       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
295       return;
296     }
297
298   Item *li = sp->get_bound (LEFT)->get_column ();
299   Item *ri = sp->get_bound (RIGHT)->get_column ();
300   Item *lb = li->find_prebroken_piece (RIGHT);
301   Item *rb = ri->find_prebroken_piece (LEFT);
302
303   Item *combinations[4][2] = {{li, ri},
304                               {lb, ri},
305                               {li, rb},
306                               {lb, rb}};
307
308   for (int i = 0; i < 4; i++)
309     {
310       Item *li = combinations[i][0];
311       Item *ri = combinations[i][1];
312
313       if (!li || !ri)
314         continue;
315
316       Rod rod;
317       rod.item_drul_[LEFT] = li;
318       rod.item_drul_[RIGHT] = ri;
319
320       rod.distance_ = Paper_column::minimum_distance (li, ri)
321         + length
322         + 2 * robust_scm2double (me->get_property ("bound-padding"), 1.0);
323
324       Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0);
325       rod.distance_ = max (rod.distance_, minlen);
326       rod.add_to_cols ();
327     }
328 }
329
330 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods, 1);
331 SCM
332 Multi_measure_rest::set_spacing_rods (SCM smob)
333 {
334   Grob *me = unsmob_grob (smob);
335   Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
336   calculate_spacing_rods (me, sym_width);
337
338   return SCM_UNSPECIFIED;
339 }
340
341 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_text_rods, 1);
342 SCM
343 Multi_measure_rest::set_text_rods (SCM smob)
344 {
345   Grob *me = unsmob_grob (smob);
346   Stencil *stil = me->get_stencil ();
347
348   /* FIXME uncached */
349   Real len = (stil && !stil->extent (X_AXIS).is_empty ())
350     ? stil->extent (X_AXIS).length ()
351     : 0.0;
352   calculate_spacing_rods (me, len);
353
354   return SCM_UNSPECIFIED;
355 }
356
357 ADD_INTERFACE (Multi_measure_rest,
358                "A rest that spans a whole number of measures.",
359
360                /* properties */
361                "bound-padding "
362                "expand-limit "
363                "hair-thickness "
364                "measure-count "
365                "minimum-length "
366                "round-to-longer-rest "
367                "spacing-pair "
368                "thick-thickness "
369                "usable-duration-logs "
370                );