]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
''
[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--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10 #include "multi-measure-rest.hh"
11 #include "debug.hh"
12 #include "paper-def.hh"
13 #include "paper-column.hh" // urg
14 #include "font-interface.hh"
15 #include "rest.hh"
16 #include "molecule.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
24 bool
25 Multi_measure_rest::has_interface (Grob*me)
26 {
27   return me->has_interface (ly_symbol2scm ("multi-measure-rest-interface"));
28 }
29
30 MAKE_SCHEME_CALLBACK (Multi_measure_rest,percent,1);
31 SCM
32 Multi_measure_rest::percent (SCM smob)
33 {
34   Grob *me = unsmob_grob (smob);
35   Spanner *sp = dynamic_cast<Spanner*> (me);
36   
37   Molecule r = Percent_repeat_item_interface::x_percent (me, 1,  0.75, 1.6);
38
39   // ugh copy & paste.
40   
41   Interval sp_iv;
42   Direction d = LEFT;
43   do
44     {
45       Item * col = sp->get_bound (d)->column_l ();
46
47       Interval coldim = col->extent (0, X_AXIS);
48
49       sp_iv[d] = coldim[-d]  ;
50     }
51   while ((flip (&d)) != LEFT);
52   Real x_off = 0.0;
53
54   Real rx  = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
55   /*
56     we gotta stay clear of sp_iv, so move a bit to the right if
57     needed.
58    */
59   x_off += (sp_iv[LEFT] -  rx) >? 0;
60
61   /*
62     center between stuff.
63    */
64   x_off += sp_iv.length ()/ 2;
65
66   r.translate_axis (x_off,X_AXIS);
67
68   
69   return r.smobbed_copy ();
70 }
71
72
73 /*
74    [TODO]                                      17
75    variable-sized multi-measure rest symbol: |====| ??
76 */
77 MAKE_SCHEME_CALLBACK (Multi_measure_rest,brew_molecule,1);
78 SCM
79 Multi_measure_rest::brew_molecule (SCM smob) 
80 {
81   Grob *me = unsmob_grob (smob);
82   Spanner * sp = dynamic_cast<Spanner*> (me);
83
84   SCM alist_chain = Font_interface::font_alist_chain (me);
85
86   Interval sp_iv;
87   Direction d = LEFT;
88
89   Grob *common = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
90   do
91     {
92       Item * col = sp->get_bound (d)->column_l ();
93
94       Interval coldim = col->extent (common, X_AXIS);
95
96       sp_iv[d] = coldim[-d]  ;
97     }
98   while ((flip (&d)) != LEFT);
99
100   Real space = sp_iv.length();
101
102   Real rx  = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
103   /*
104     we gotta stay clear of sp_iv, so move a bit to the right if
105     needed.
106    */
107   Real x_off = (sp_iv[LEFT] -  rx) >? 0;
108
109
110   Molecule mol;
111   mol.add_molecule (symbol_molecule (me, space));
112
113   int measures = 0;
114   SCM m (me->get_grob_property ("measure-count"));
115   if (gh_number_p (m))
116     {
117       measures = gh_scm2int (m);
118     }
119
120   SCM s = me->get_grob_property ("number-threshold");
121   if (measures > gh_scm2int (s))
122     {
123       Molecule s = Text_item::text2molecule (me,
124                                              ly_str02scm (to_str (measures).ch_C ()),
125                                              alist_chain);
126
127       s.align_to (X_AXIS, CENTER);
128       s.translate_axis (gh_scm2double (me->get_grob_property ("padding")) + 2,
129                         Y_AXIS);
130
131       s.translate_axis (mol.extent (X_AXIS).center (),  X_AXIS);
132       mol.add_molecule (s);
133     }
134   mol.translate_axis (x_off, X_AXIS);
135   return mol.smobbed_copy ();
136 }
137
138
139 Molecule
140 Multi_measure_rest::symbol_molecule (Grob *me, Real space)
141 {
142   int measures = 0;
143   SCM m (me->get_grob_property ("measure-count"));
144   if (gh_number_p (m))
145     {
146       measures = gh_scm2int (m);
147     }
148   
149
150   SCM limit = me->get_grob_property ("expand-limit");
151   if (measures <= 0)
152     return Molecule();
153
154   if (measures > gh_scm2int (limit))
155     {
156       Real padding = 0.15;  
157       Molecule s =  big_rest (me, (1.0 - 2*padding) * space);
158       s.translate_axis (padding * space,  X_AXIS); 
159       return s;
160     }
161
162   SCM alist_chain = Font_interface::font_alist_chain (me);
163
164   SCM style_chain =
165     Font_interface::add_style (me, ly_symbol2scm ("mmrest-symbol"),
166                                alist_chain);
167
168   Real staff_space = Staff_symbol_referencer::staff_space (me);
169   Font_metric *musfont
170     = Font_interface::get_font (me,style_chain);
171
172   if (measures == 1)
173     {
174       Molecule s = musfont->find_by_name (Rest::glyph_name (me, 0, ""));
175
176       /*
177         ugh.
178        */
179       if (Staff_symbol_referencer::position_f (me) == 0.0)
180         s.translate_axis (staff_space, Y_AXIS);
181
182       s.translate_axis ((space - s.extent (X_AXIS).length ())/2, X_AXIS);
183       
184       return s ;
185     }
186   else
187     {
188       return  church_rest (me, musfont, measures, space);
189     }
190 }
191
192
193
194 Molecule
195 Multi_measure_rest::big_rest (Grob *me, Real width)
196 {
197   Real thick = gh_scm2double (me->get_grob_property ("thickness"));
198   Real ss = Staff_symbol_referencer::staff_space (me);
199   
200   Real slt = me->paper_l ()->get_var ("linethickness");
201   Real y = slt * thick/2 * ss;
202   Box b(Interval (0, width), Interval (-y, y));
203   Real ythick = slt * ss;
204   
205   Molecule m =  Lookup::filledbox (b);
206   Molecule yb = Lookup::filledbox (Box (Interval (-ythick, ythick), Interval (-ss, ss)));
207
208   m.add_at_edge (X_AXIS, RIGHT, yb, -ythick);
209   m.add_at_edge (X_AXIS, LEFT, yb, -ythick);
210
211   m.align_to (X_AXIS, LEFT);
212   
213   return m;
214 }
215
216 /*
217   Kirchenpause (?)
218  */
219 Molecule
220 Multi_measure_rest::church_rest (Grob*me, Font_metric *musfont, int measures,
221                                  Real space)
222 {
223   SCM mols = SCM_EOL; 
224
225   /*
226    see Wanske pp. 125
227   */
228   int l = measures;
229   int count = 0;
230   Real symbols_width = 0.0;
231   while (l)
232     {
233       int k;
234       if (l >= 4)
235         {
236           l-=4;
237           k = -2;
238         }
239       else if (l>= 2)
240         {
241           l -= 2;
242           k = -1;
243         }
244       else
245         {
246           k = 0;
247           l --;
248         }
249
250       Molecule r (musfont->find_by_name ("rests-" + to_str (k)));
251       if (k == 0)
252         {
253           Real staff_space = Staff_symbol_referencer::staff_space (me);
254           r.translate_axis (staff_space, Y_AXIS);
255         }
256       symbols_width += r.extent (X_AXIS).length ();
257       mols = gh_cons (r.smobbed_copy (), mols);
258       count ++;
259     }
260
261   
262
263   Real outer_padding_factor = 1.5; //     make outer padding this much bigger.
264   Real inner_padding = (space - symbols_width) / (2 * outer_padding_factor + (count-1)); 
265   if (inner_padding < 0)
266     {
267       inner_padding = 1.0;
268     }
269   
270   Molecule mol; 
271   for (SCM  s = mols; gh_pair_p (s); s = gh_cdr(s))
272     {
273       mol.add_at_edge (X_AXIS, LEFT, *unsmob_molecule (gh_car (s)), inner_padding);
274     }
275   mol.align_to (X_AXIS, LEFT);
276   mol.translate_axis (outer_padding_factor *  inner_padding, X_AXIS);
277
278   return mol;
279 }
280
281 void
282 Multi_measure_rest::add_column (Grob*me,Item* c)
283 {
284   add_bound_item (dynamic_cast<Spanner*> (me),c);
285 }
286
287
288 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
289
290 SCM
291 Multi_measure_rest::set_spacing_rods (SCM smob)
292 {
293   return SCM_UNSPECIFIED;
294
295   Grob*me = unsmob_grob (smob);
296
297   Spanner*sp = dynamic_cast<Spanner*> (me);
298   if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
299     {
300       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
301       return SCM_UNSPECIFIED;
302     }
303
304   Item * l = sp->get_bound (LEFT)->column_l ();
305   Item * r = sp->get_bound (RIGHT)->column_l ();
306   Item * lb = l->find_prebroken_piece (RIGHT);
307   Item * rb = r->find_prebroken_piece (LEFT);      
308   
309   Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
310
311   Real sym_width = symbol_molecule (me, 0.0).extent (X_AXIS).length ();
312   
313   for (int i=0; i < 4; i++)
314     {
315       Item * l =  combinations[i][0];
316       Item *r = combinations[i][1];
317
318       if (!l || !r)
319         continue;
320
321       Rod rod;
322       rod.item_l_drul_[LEFT] = l;
323       rod.item_l_drul_[RIGHT] = r;
324
325       
326       rod.distance_f_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
327         + sym_width  + 2.0;                     // 2.0 = magic!
328   
329       rod.add_to_cols ();
330     }
331   return SCM_UNSPECIFIED;
332 }
333
334
335
336 ADD_INTERFACE (Multi_measure_rest,"multi-measure-rest-interface",
337   "A rest that spans a whole number of measures.  For typesetting the
338 numbers, fields from font-interface may be used.
339
340
341 ",
342   "expand-limit measure-count number-threshold padding thickness");