]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
*** empty log message ***
[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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10 #include "multi-measure-rest.hh"
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 "stencil.hh"
17 #include "misc.hh"
18 #include "spanner.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "text-item.hh"
21 #include "percent-repeat-item.hh"
22 #include "lookup.hh"
23 #include "separation-item.hh"
24
25 MAKE_SCHEME_CALLBACK (Multi_measure_rest,percent,1);
26 SCM
27 Multi_measure_rest::percent (SCM smob)
28 {
29   Grob *me = unsmob_grob (smob);
30   Spanner *sp = dynamic_cast<Spanner*> (me);
31   
32   Stencil r = Percent_repeat_item_interface::x_percent (me, 1,  0.75, 1.6);
33
34   // ugh copy & paste.
35   
36   Interval sp_iv;
37   Direction d = LEFT;
38   do
39     {
40       Item * col = sp->get_bound (d)->get_column ();
41
42       Interval coldim = col->extent (0, X_AXIS);
43
44       sp_iv[d] = coldim[-d]  ;
45     }
46   while ((flip (&d)) != LEFT);
47   Real x_off = 0.0;
48
49   Real rx  = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
50   /*
51     we gotta stay clear of sp_iv, so move a bit to the right if
52     needed.
53    */
54   x_off += (sp_iv[LEFT] -  rx) >? 0;
55
56   /*
57     center between stuff.
58    */
59   x_off += sp_iv.length ()/ 2;
60
61   r.translate_axis (x_off,X_AXIS);
62
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 = (sp_iv[LEFT] -  rx) >? 0;
98
99
100   Stencil mol;
101   mol.add_stencil (symbol_stencil (me, space));
102
103   int measures = 0;
104   SCM m (me->get_property ("measure-count"));
105   if (scm_is_number (m))
106     {
107       measures = scm_to_int (m);
108     }
109
110   mol.translate_axis (x_off, X_AXIS);
111   return mol.smobbed_copy ();
112 }
113
114
115
116 Stencil
117 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
118 {
119   int measures = 0;
120   SCM m (me->get_property ("measure-count"));
121   if (scm_is_number (m))
122     {
123       measures = scm_to_int (m);
124     }
125   if (measures <= 0)
126     return Stencil ();
127   
128
129   SCM limit = me->get_property ("expand-limit");
130   if (measures > scm_to_int (limit))
131     {
132       Real padding = 0.15;  
133       Stencil s =  big_rest (me, (1.0 - 2*padding) * space);
134       s.translate_axis (padding * space,  X_AXIS); 
135       return s;
136     }
137
138   SCM alist_chain = Font_interface::music_font_alist_chain (me);
139
140   Real staff_space = Staff_symbol_referencer::staff_space (me);
141   Font_metric *musfont
142     = select_font (me->get_paper (), alist_chain);
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     {
172       return  church_rest (me, musfont, measures, space);
173     }
174 }
175
176
177 /*
178   WIDTH can also be 0 to determine the minimum size of the object.
179  */
180 Stencil
181 Multi_measure_rest::big_rest (Grob *me, Real width)
182 {
183   Real thick_thick =robust_scm2double (me->get_property ("thick-thickness"), 1.0);
184   Real hair_thick = robust_scm2double (me->get_property ("hair-thickness"), .1);
185
186
187   Real ss = Staff_symbol_referencer::staff_space (me);
188   Real slt = me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness"));
189   Real y = slt * thick_thick/2 * ss;
190   Real ythick = hair_thick * slt * ss;
191   Box b (Interval (0,  0 >? (width - 2 * ythick)), Interval (-y, y));
192   
193   Real blot = width ? (.8 * (y <? ythick)) : 0.0;
194   
195   Stencil m =  Lookup::round_filled_box (b, blot);
196   Stencil yb = Lookup::round_filled_box (Box (Interval (-0.5, 0.5)* ythick, Interval (-ss, ss)), blot);
197
198   m.add_at_edge (X_AXIS, RIGHT, yb, 0, 0);
199   m.add_at_edge (X_AXIS, LEFT, yb, 0, 0);
200
201   m.align_to (X_AXIS, LEFT);
202   
203   return m;
204 }
205
206 /*
207   Kirchenpause (?)
208  */
209 Stencil
210 Multi_measure_rest::church_rest (Grob *me, Font_metric *musfont, int measures,
211                                  Real space)
212 {
213   SCM mols = SCM_EOL; 
214
215   /* See Wanske pp. 125  */
216   int l = measures;
217   int count = 0;
218   Real symbols_width = 0.0;
219
220   SCM sml = me->get_property ("use-breve-rest");
221
222   while (l)
223     {
224       if (sml == SCM_BOOL_T)
225         {
226           int k;
227           if (l >= 2)
228             {
229               l -= 2;
230               k = -2;
231             }
232           else
233             {
234               l -= 1;
235               k = -1;
236             }
237
238           Stencil r (musfont->find_by_name ("rests-" + to_string (k)));
239           symbols_width += r.extent (X_AXIS).length ();
240           mols = scm_cons (r.smobbed_copy (), mols);
241         }
242        else
243         {
244           int k;
245           if (l >= 4)
246             {
247               l -= 4;
248               k = -2;
249             }
250           else if (l>= 2)
251             {
252               l -= 2;
253               k = -1;
254             }
255           else
256             {
257               k = 0;
258               l--;
259             }
260
261           Stencil r (musfont->find_by_name ("rests-" + to_string (k)));
262           if (k == 0)
263             {
264               Real staff_space = Staff_symbol_referencer::staff_space (me);
265               r.translate_axis (staff_space, Y_AXIS);
266             }
267           symbols_width += r.extent (X_AXIS).length ();
268           mols = scm_cons (r.smobbed_copy (), mols);
269         }
270       count ++;
271     }
272
273   
274  /* Make outer padding this much bigger.  */
275   Real outer_padding_factor = 1.5;
276   Real inner_padding = (space - symbols_width)
277     / (2 * outer_padding_factor + (count-1)); 
278   if (inner_padding < 0)
279     inner_padding = 1.0;
280   
281   Stencil mol; 
282   for (SCM  s = mols; scm_is_pair (s); s = scm_cdr (s))
283       mol.add_at_edge (X_AXIS, LEFT, *unsmob_stencil (scm_car (s)),
284                        inner_padding, 0);
285   mol.align_to (X_AXIS, LEFT);
286   mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
287
288   return mol;
289 }
290
291 void
292 Multi_measure_rest::add_column (Grob *me, Item *c)
293 {
294   add_bound_item (dynamic_cast<Spanner*> (me), c);
295 }
296
297 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods, 1);
298 SCM
299 Multi_measure_rest::set_spacing_rods (SCM smob)
300 {
301   Grob *me = unsmob_grob (smob);
302
303   Spanner*sp = dynamic_cast<Spanner*> (me);
304   if (!(sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
305     {
306       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
307       return SCM_UNSPECIFIED;
308     }
309
310   Item *li = sp->get_bound (LEFT)->get_column ();
311   Item *ri = sp->get_bound (RIGHT)->get_column ();
312   Item *lb = li->find_prebroken_piece (RIGHT);
313   Item *rb = ri->find_prebroken_piece (LEFT);      
314   
315   Item *combinations[4][2] = {{li,ri},
316                               {lb,ri},
317                               {li,rb},
318                               {lb,rb}};
319
320   Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
321   
322   for (int i=0; i < 4; i++)
323     {
324       Item *li = combinations[i][0];
325       Item *ri = combinations[i][1];
326
327       if (!li || !ri)
328         continue;
329
330       Rod rod;
331       rod.item_l_drul_[LEFT] = li;
332       rod.item_l_drul_[RIGHT] = ri;
333
334       rod.distance_ = li->extent (li, X_AXIS)[BIGGER]
335         - ri->extent (ri, X_AXIS)[SMALLER]
336         /* 2.0 = magic! */
337         + sym_width  + 2.0;
338   
339       Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0);
340       rod.distance_ = max (rod.distance_, minlen);
341       rod.add_to_cols ();
342     }
343   return SCM_UNSPECIFIED;
344 }
345
346 ADD_INTERFACE (Multi_measure_rest,"multi-measure-rest-interface",
347                "A rest that spans a whole number of measures.",
348                "expand-limit measure-count hair-thickness thick-thickness use-breve-rest minimum-length");
349