]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
* lily/accidental-placement.cc (position_accidentals): bugfix in
[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 "paper-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 (gh_number_p (m))
106     {
107       measures = gh_scm2int (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 (gh_number_p (m))
122     {
123       measures = gh_scm2int (m);
124     }
125   if (measures <= 0)
126     return Stencil ();
127   
128
129   SCM limit = me->get_property ("expand-limit");
130   if (measures > gh_scm2int (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::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 (sml == SCM_BOOL_T)
148         {
149           Stencil s = musfont->find_by_name (Rest::glyph_name (me, -1, ""));
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, ""));
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_realvar (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   /*
216    see Wanske pp. 125
217   */
218   int l = measures;
219   int count = 0;
220   Real symbols_width = 0.0;
221
222   SCM sml = me->get_property ("use-breve-rest");
223
224   while (l)
225     {
226       if (sml == SCM_BOOL_T)
227         {
228           int k;
229           if (l >= 2)
230             {
231               l-=2;
232               k = -2;
233             }
234           else
235             {
236               l -= 1;
237               k = -1;
238             }
239
240           Stencil r (musfont->find_by_name ("rests-" + to_string (k)));
241           symbols_width += r.extent (X_AXIS).length ();
242           mols = gh_cons (r.smobbed_copy (), mols);
243         }
244        else
245         {
246           int k;
247           if (l >= 4)
248             {
249               l-=4;
250               k = -2;
251             }
252           else if (l>= 2)
253             {
254               l -= 2;
255               k = -1;
256             }
257           else
258             {
259               k = 0;
260               l --;
261             }
262
263           Stencil r (musfont->find_by_name ("rests-" + to_string (k)));
264           if (k == 0)
265             {
266               Real staff_space = Staff_symbol_referencer::staff_space (me);
267               r.translate_axis (staff_space, Y_AXIS);
268             }
269           symbols_width += r.extent (X_AXIS).length ();
270           mols = gh_cons (r.smobbed_copy (), mols);
271         }
272       count ++;
273     }
274
275   
276
277   Real outer_padding_factor = 1.5; //     make outer padding this much bigger.
278   Real inner_padding = (space - symbols_width) / (2 * outer_padding_factor + (count-1)); 
279   if (inner_padding < 0)
280     {
281       inner_padding = 1.0;
282     }
283   
284   Stencil mol; 
285   for (SCM  s = mols; gh_pair_p (s); s = gh_cdr (s))
286     {
287       mol.add_at_edge (X_AXIS, LEFT, *unsmob_stencil (gh_car (s)), inner_padding, 0);
288     }
289   mol.align_to (X_AXIS, LEFT);
290   mol.translate_axis (outer_padding_factor *  inner_padding, X_AXIS);
291
292   return mol;
293 }
294
295 void
296 Multi_measure_rest::add_column (Grob*me,Item* c)
297 {
298   add_bound_item (dynamic_cast<Spanner*> (me),c);
299 }
300
301
302 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
303 SCM
304 Multi_measure_rest::set_spacing_rods (SCM smob)
305 {
306   Grob*me = unsmob_grob (smob);
307
308   Spanner*sp = dynamic_cast<Spanner*> (me);
309   if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
310     {
311       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
312       return SCM_UNSPECIFIED;
313     }
314
315   Item * l = sp->get_bound (LEFT)->get_column ();
316   Item * r = sp->get_bound (RIGHT)->get_column ();
317   Item * lb = l->find_prebroken_piece (RIGHT);
318   Item * rb = r->find_prebroken_piece (LEFT);      
319   
320   Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
321
322   Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
323   
324   for (int i=0; i < 4; i++)
325     {
326       Item * l =  combinations[i][0];
327       Item *r = combinations[i][1];
328
329       if (!l || !r)
330         continue;
331
332       Rod rod;
333       rod.item_l_drul_[LEFT] = l;
334       rod.item_l_drul_[RIGHT] = r;
335
336       
337       rod.distance_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
338         + sym_width  + 2.0;                     // 2.0 = magic!
339   
340       Real minlen  = robust_scm2double (me->get_property ("minimum-length"), 0.0);
341       rod.distance_ = max (rod.distance_,
342                           minlen);
343       rod.add_to_cols ();
344     }
345   return SCM_UNSPECIFIED;
346 }
347
348
349
350
351 ADD_INTERFACE (Multi_measure_rest,"multi-measure-rest-interface",
352                "A rest that spans a whole number of measures.",
353                "expand-limit measure-count hair-thickness thick-thickness use-breve-rest minimum-length");
354