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