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