]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
use breve rests for measure lengths
[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 "warn.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
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   Molecule 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
68 /*
69    [TODO]                                      17
70    variable-sized multi-measure rest symbol: |====| ??
71 */
72 MAKE_SCHEME_CALLBACK (Multi_measure_rest,brew_molecule,1);
73 SCM
74 Multi_measure_rest::brew_molecule (SCM smob) 
75 {
76   Grob *me = unsmob_grob (smob);
77   Spanner * sp = dynamic_cast<Spanner*> (me);
78
79   SCM alist_chain = Font_interface::font_alist_chain (me);
80
81   Interval sp_iv;
82   Direction d = LEFT;
83
84   Grob *common = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
85   do
86     {
87       Item * col = sp->get_bound (d)->get_column ();
88
89       Interval coldim = col->extent (common, X_AXIS);
90
91       sp_iv[d] = coldim[-d]  ;
92     }
93   while ((flip (&d)) != LEFT);
94
95   Real space = sp_iv.length();
96
97   Real rx  = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
98   /*
99     we gotta stay clear of sp_iv, so move a bit to the right if
100     needed.
101    */
102   Real x_off = (sp_iv[LEFT] -  rx) >? 0;
103
104
105   Molecule mol;
106   mol.add_molecule (symbol_molecule (me, space));
107
108   int measures = 0;
109   SCM m (me->get_grob_property ("measure-count"));
110   if (gh_number_p (m))
111     {
112       measures = gh_scm2int (m);
113     }
114
115   SCM s = me->get_grob_property ("number-threshold");
116   if (measures > gh_scm2int (s))
117     {
118       Molecule s = Text_item::text2molecule (me,
119                                              scm_makfrom0str (to_string (measures).to_str0 ()),
120                                              alist_chain);
121
122       s.align_to (X_AXIS, CENTER);
123       s.translate_axis (gh_scm2double (me->get_grob_property ("padding")) + 2,
124                         Y_AXIS);
125
126       s.translate_axis (mol.extent (X_AXIS).center (),  X_AXIS);
127       mol.add_molecule (s);
128     }
129   mol.translate_axis (x_off, X_AXIS);
130   return mol.smobbed_copy ();
131 }
132
133
134 Molecule
135 Multi_measure_rest::symbol_molecule (Grob *me, Real space)
136 {
137   int measures = 0;
138   SCM m (me->get_grob_property ("measure-count"));
139   if (gh_number_p (m))
140     {
141       measures = gh_scm2int (m);
142     }
143   if (measures <= 0)
144     return Molecule();
145   
146
147   SCM limit = me->get_grob_property ("expand-limit");
148   if (measures > gh_scm2int (limit))
149     {
150       Real padding = 0.15;  
151       Molecule s =  big_rest (me, (1.0 - 2*padding) * space);
152       s.translate_axis (padding * space,  X_AXIS); 
153       return s;
154     }
155
156   SCM alist_chain = Font_interface::font_alist_chain (me);
157
158   SCM style_chain =
159     Font_interface::add_style (me, ly_symbol2scm ("mmrest-symbol"),
160                                alist_chain);
161
162   Real staff_space = Staff_symbol_referencer::staff_space (me);
163   Font_metric *musfont
164     = Font_interface::get_font (me,style_chain);
165
166   SCM sml = me->get_grob_property ("use-breve-rest");
167
168   if (measures == 1)
169     {
170       if (sml == SCM_BOOL_T)
171         {
172           Molecule s = musfont->find_by_name (Rest::glyph_name (me, -1, ""));
173
174           s.translate_axis ((space - s.extent (X_AXIS).length ())/2, X_AXIS);
175       
176           return s ;
177         }
178       else
179         {
180           Molecule s = musfont->find_by_name (Rest::glyph_name (me, 0, ""));
181
182           /*
183             ugh.
184            */
185           if (Staff_symbol_referencer::get_position (me) == 0.0)
186             s.translate_axis (staff_space, Y_AXIS);
187
188           s.translate_axis ((space - s.extent (X_AXIS).length ())/2, X_AXIS);
189       
190           return s ;
191         }
192     }
193   else
194     {
195       return  church_rest (me, musfont, measures, space);
196     }
197 }
198
199
200
201 Molecule
202 Multi_measure_rest::big_rest (Grob *me, Real width)
203 {
204   Real thick = gh_scm2double (me->get_grob_property ("thickness"));
205   Real ss = Staff_symbol_referencer::staff_space (me);
206   
207   Real slt = me->get_paper ()->get_var ("linethickness");
208   Real y = slt * thick/2 * ss;
209   Box b(Interval (0, width), Interval (-y, y));
210   Real ythick = slt * ss;
211   
212   Molecule m =  Lookup::filledbox (b);
213   Molecule yb = Lookup::filledbox (Box (Interval (-ythick, ythick), Interval (-ss, ss)));
214
215   m.add_at_edge (X_AXIS, RIGHT, yb, -ythick);
216   m.add_at_edge (X_AXIS, LEFT, yb, -ythick);
217
218   m.align_to (X_AXIS, LEFT);
219   
220   return m;
221 }
222
223 /*
224   Kirchenpause (?)
225  */
226 Molecule
227 Multi_measure_rest::church_rest (Grob*me, Font_metric *musfont, int measures,
228                                  Real space)
229 {
230   SCM mols = SCM_EOL; 
231
232   /*
233    see Wanske pp. 125
234   */
235   int l = measures;
236   int count = 0;
237   Real symbols_width = 0.0;
238
239   SCM sml = me->get_grob_property ("use-breve-rest");
240
241   while (l)
242     {
243       if (sml == SCM_BOOL_T)
244         {
245           int k;
246           if (l >= 2)
247             {
248               l-=2;
249               k = -2;
250             }
251           else
252             {
253               l -= 1;
254               k = -1;
255             }
256
257           Molecule r (musfont->find_by_name ("rests-" + to_string (k)));
258           symbols_width += r.extent (X_AXIS).length ();
259           mols = gh_cons (r.smobbed_copy (), mols);
260         }
261        else
262         {
263           int k;
264           if (l >= 4)
265             {
266               l-=4;
267               k = -2;
268             }
269           else if (l>= 2)
270             {
271               l -= 2;
272               k = -1;
273             }
274           else
275             {
276               k = 0;
277               l --;
278             }
279
280           Molecule r (musfont->find_by_name ("rests-" + to_string (k)));
281           if (k == 0)
282             {
283               Real staff_space = Staff_symbol_referencer::staff_space (me);
284               r.translate_axis (staff_space, Y_AXIS);
285             }
286           symbols_width += r.extent (X_AXIS).length ();
287           mols = gh_cons (r.smobbed_copy (), mols);
288         }
289       count ++;
290     }
291
292   
293
294   Real outer_padding_factor = 1.5; //     make outer padding this much bigger.
295   Real inner_padding = (space - symbols_width) / (2 * outer_padding_factor + (count-1)); 
296   if (inner_padding < 0)
297     {
298       inner_padding = 1.0;
299     }
300   
301   Molecule mol; 
302   for (SCM  s = mols; gh_pair_p (s); s = gh_cdr(s))
303     {
304       mol.add_at_edge (X_AXIS, LEFT, *unsmob_molecule (gh_car (s)), inner_padding);
305     }
306   mol.align_to (X_AXIS, LEFT);
307   mol.translate_axis (outer_padding_factor *  inner_padding, X_AXIS);
308
309   return mol;
310 }
311
312 void
313 Multi_measure_rest::add_column (Grob*me,Item* c)
314 {
315   add_bound_item (dynamic_cast<Spanner*> (me),c);
316 }
317
318
319 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
320
321 SCM
322 Multi_measure_rest::set_spacing_rods (SCM smob)
323 {
324   return SCM_UNSPECIFIED;
325
326   Grob*me = unsmob_grob (smob);
327
328   Spanner*sp = dynamic_cast<Spanner*> (me);
329   if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
330     {
331       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
332       return SCM_UNSPECIFIED;
333     }
334
335   Item * l = sp->get_bound (LEFT)->get_column ();
336   Item * r = sp->get_bound (RIGHT)->get_column ();
337   Item * lb = l->find_prebroken_piece (RIGHT);
338   Item * rb = r->find_prebroken_piece (LEFT);      
339   
340   Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
341
342   Real sym_width = symbol_molecule (me, 0.0).extent (X_AXIS).length ();
343   
344   for (int i=0; i < 4; i++)
345     {
346       Item * l =  combinations[i][0];
347       Item *r = combinations[i][1];
348
349       if (!l || !r)
350         continue;
351
352       Rod rod;
353       rod.item_l_drul_[LEFT] = l;
354       rod.item_l_drul_[RIGHT] = r;
355
356       
357       rod.distance_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
358         + sym_width  + 2.0;                     // 2.0 = magic!
359   
360       rod.add_to_cols ();
361     }
362   return SCM_UNSPECIFIED;
363 }
364
365
366
367 ADD_INTERFACE (Multi_measure_rest,"multi-measure-rest-interface",
368   "A rest that spans a whole number of measures.  For typesetting the
369 numbers, fields from font-interface may be used.
370
371
372 ",
373   "expand-limit measure-count number-threshold padding thickness use-breve-rest");