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