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