]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
* flower/include/axis.hh: rename from axes.hh
[lilypond.git] / lily / multi-measure-rest.cc
1 /*
2   multi-measure-rest.cc -- implement Multi_measure_rest
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "multi-measure-rest.hh"
10
11 #include "warn.hh"
12 #include "output-def.hh"
13 #include "paper-column.hh" // urg
14 #include "font-interface.hh"
15 #include "rest.hh"
16 #include "misc.hh"
17 #include "spanner.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "text-interface.hh"
20 #include "percent-repeat-item.hh"
21 #include "lookup.hh"
22 #include "separation-item.hh"
23
24 MAKE_SCHEME_CALLBACK (Multi_measure_rest, percent, 1);
25 SCM
26 Multi_measure_rest::percent (SCM smob)
27 {
28   Grob *me = unsmob_grob (smob);
29   Spanner *sp = dynamic_cast<Spanner *> (me);
30
31   Stencil r = Percent_repeat_item_interface::x_percent (me, 1, 0.75, 1.6);
32
33   // ugh copy & paste.
34
35   Interval sp_iv;
36   Direction d = LEFT;
37   do
38     {
39       Item *col = sp->get_bound (d)->get_column ();
40
41       Interval coldim = col->extent (0, X_AXIS);
42
43       sp_iv[d] = coldim[-d];
44     }
45   while ((flip (&d)) != LEFT);
46   Real x_off = 0.0;
47
48   Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
49   /*
50     we gotta stay clear of sp_iv, so move a bit to the right if
51     needed.
52   */
53   x_off += max (sp_iv[LEFT] - rx, 0.0);
54
55   /*
56     center between stuff.
57   */
58   x_off += sp_iv.length () / 2;
59
60   r.translate_axis (x_off, X_AXIS);
61
62   return r.smobbed_copy ();
63 }
64
65 MAKE_SCHEME_CALLBACK (Multi_measure_rest, print, 1);
66 SCM
67 Multi_measure_rest::print (SCM smob)
68 {
69   Grob *me = unsmob_grob (smob);
70   Spanner *sp = dynamic_cast<Spanner *> (me);
71
72   Interval sp_iv;
73   Direction d = LEFT;
74
75   Grob *common = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
76   do
77     {
78       Item *b = sp->get_bound (d);
79
80       Interval coldim = (Separation_item::has_interface (b))
81         ? Separation_item::relative_width (b, common)
82         : b->extent (common, X_AXIS);
83
84       sp_iv[d] = coldim.is_empty () ? b->relative_coordinate (common, X_AXIS) : coldim[-d];
85     }
86   while ((flip (&d)) != LEFT);
87
88   Real space = sp_iv.length ();
89
90   Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
91   /*
92     we gotta stay clear of sp_iv, so move a bit to the right if
93     needed.
94   */
95   Real x_off = max (sp_iv[LEFT] - rx, 0.0);
96
97   Stencil mol;
98   mol.add_stencil (symbol_stencil (me, space));
99
100   int measures = 0;
101   SCM m (me->get_property ("measure-count"));
102   if (scm_is_number (m))
103     {
104       measures = scm_to_int (m);
105     }
106
107   mol.translate_axis (x_off, X_AXIS);
108   return mol.smobbed_copy ();
109 }
110
111 Stencil
112 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
113 {
114   int measures = 0;
115   SCM m (me->get_property ("measure-count"));
116   if (scm_is_number (m))
117     {
118       measures = scm_to_int (m);
119     }
120   if (measures <= 0)
121     return Stencil ();
122
123   SCM limit = me->get_property ("expand-limit");
124   if (measures > scm_to_int (limit))
125     {
126       Real padding = 0.15;
127       Stencil s = big_rest (me, (1.0 - 2 * padding) * space);
128       s.translate_axis (padding * space, X_AXIS);
129       return s;
130     }
131
132   SCM alist_chain = Font_interface::music_font_alist_chain (me);
133
134   Real staff_space = Staff_symbol_referencer::staff_space (me);
135   Font_metric *musfont
136     = select_font (me->get_layout (), alist_chain);
137
138   SCM sml = me->get_property ("use-breve-rest");
139   if (measures == 1)
140     {
141       if (to_boolean (sml))
142         {
143           Stencil s = musfont->find_by_name (Rest::glyph_name (me, -1, "", false));
144
145           s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
146
147           return s;
148         }
149       else
150         {
151           Stencil s = musfont->find_by_name (Rest::glyph_name (me, 0, "", true));
152
153           /*
154             ugh.
155           */
156           if (Staff_symbol_referencer::get_position (me) == 0.0)
157             s.translate_axis (staff_space, Y_AXIS);
158
159           s.translate_axis ((space - s.extent (X_AXIS).length ()) / 2, X_AXIS);
160
161           return s;
162         }
163     }
164   else
165     {
166       return church_rest (me, musfont, measures, space);
167     }
168 }
169
170 /*
171   WIDTH can also be 0 to determine the minimum size of the object.
172 */
173 Stencil
174 Multi_measure_rest::big_rest (Grob *me, Real width)
175 {
176   Real thick_thick = robust_scm2double (me->get_property ("thick-thickness"), 1.0);
177   Real hair_thick = robust_scm2double (me->get_property ("hair-thickness"), .1);
178
179   Real ss = Staff_symbol_referencer::staff_space (me);
180   Real slt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
181   Real y = slt * thick_thick / 2 * ss;
182   Real ythick = hair_thick * slt * ss;
183   Box b (Interval (0.0, max (0.0, (width - 2 * ythick))), Interval (-y, y));
184
185   Real blot = width ? (.8 * min (y, ythick)) : 0.0;
186
187   Stencil m = Lookup::round_filled_box (b, blot);
188   Stencil yb = Lookup::round_filled_box (Box (Interval (-0.5, 0.5) * ythick, Interval (-ss, ss)), blot);
189
190   m.add_at_edge (X_AXIS, RIGHT, yb, 0, 0);
191   m.add_at_edge (X_AXIS, LEFT, yb, 0, 0);
192
193   m.align_to (X_AXIS, LEFT);
194
195   return m;
196 }
197
198 /*
199   Kirchenpause (?)
200 */
201 Stencil
202 Multi_measure_rest::church_rest (Grob *me, Font_metric *musfont, int measures,
203                                  Real space)
204 {
205   SCM mols = SCM_EOL;
206
207   /* See Wanske pp. 125  */
208   int l = measures;
209   int count = 0;
210   Real symbols_width = 0.0;
211
212   SCM sml = me->get_property ("use-breve-rest");
213
214   while (l)
215     {
216       if (sml == SCM_BOOL_T)
217         {
218           int k;
219           if (l >= 2)
220             {
221               l -= 2;
222               k = -2;
223             }
224           else
225             {
226               l -= 1;
227               k = -1;
228             }
229
230           Stencil r (musfont->find_by_name ("rests." + to_string (k)));
231           symbols_width += r.extent (X_AXIS).length ();
232           mols = scm_cons (r.smobbed_copy (), mols);
233         }
234       else
235         {
236           int k;
237           if (l >= 4)
238             {
239               l -= 4;
240               k = -2;
241             }
242           else if (l >= 2)
243             {
244               l -= 2;
245               k = -1;
246             }
247           else
248             {
249               k = 0;
250               l--;
251             }
252
253           Stencil r (musfont->find_by_name ("rests." + to_string (k)));
254           if (k == 0)
255             {
256               Real staff_space = Staff_symbol_referencer::staff_space (me);
257               r.translate_axis (staff_space, Y_AXIS);
258             }
259           symbols_width += r.extent (X_AXIS).length ();
260           mols = scm_cons (r.smobbed_copy (), mols);
261         }
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, 0);
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 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods, 1);
289 SCM
290 Multi_measure_rest::set_spacing_rods (SCM smob)
291 {
292   Grob *me = unsmob_grob (smob);
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 SCM_UNSPECIFIED;
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   Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
312
313   for (int i = 0; i < 4; i++)
314     {
315       Item *li = combinations[i][0];
316       Item *ri = combinations[i][1];
317
318       if (!li || !ri)
319         continue;
320
321       Rod rod;
322       rod.item_drul_[LEFT] = li;
323       rod.item_drul_[RIGHT] = ri;
324
325       rod.distance_ = li->extent (li, X_AXIS)[BIGGER]
326         - ri->extent (ri, X_AXIS)[SMALLER]
327         /* 2.0 = magic! */
328         + sym_width + 2.0;
329
330       Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0);
331       rod.distance_ = max (rod.distance_, minlen);
332       rod.add_to_cols ();
333     }
334   return SCM_UNSPECIFIED;
335 }
336
337 ADD_INTERFACE (Multi_measure_rest, "multi-measure-rest-interface",
338                "A rest that spans a whole number of measures.",
339                "expand-limit measure-count hair-thickness thick-thickness use-breve-rest minimum-length");
340