]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
8353e19812d10d45215075ca2bb6f2b3bb65407d
[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   do
89     {
90       Item * col = sp->get_bound (d)->column_l ();
91
92       Interval coldim = col->extent (0, X_AXIS);
93
94       sp_iv[d] = coldim[-d]  ;
95     }
96   while ((flip (&d)) != LEFT);
97   Molecule mol;
98   Real x_off = 0.0;
99
100   Real rx  = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
101   /*
102     we gotta stay clear of sp_iv, so move a bit to the right if
103     needed.
104    */
105   x_off += (sp_iv[LEFT] -  rx) >? 0;
106
107   /*
108     center between stuff.
109    */
110   x_off += sp_iv.length ()/ 2;
111
112   mol.add_molecule (symbol_molecule (me));
113
114   int measures = 0;
115   SCM m (me->get_grob_property ("measure-count"));
116   if (gh_number_p (m))
117     {
118       measures = gh_scm2int (m);
119     }
120
121   if (measures > 1)
122     {
123       Molecule s = Text_item::text2molecule (me,
124                                              ly_str02scm (to_str (measures).ch_C ()),
125                                              alist_chain);
126       s.align_to (X_AXIS, CENTER);
127       s.translate_axis (3.0, Y_AXIS);
128       mol.add_molecule (s);
129     }
130   mol.translate_axis (x_off, X_AXIS);
131   return mol.smobbed_copy ();
132 }
133
134
135 Molecule
136 Multi_measure_rest::symbol_molecule (Grob *me)
137 {
138   SCM alist_chain = Font_interface::font_alist_chain (me);
139
140   SCM style_chain =
141     Font_interface::add_style (me, ly_symbol2scm ("mmrest-symbol"),
142                                alist_chain);
143
144   Real staff_space = Staff_symbol_referencer::staff_space (me);
145   Font_metric *musfont
146     = Font_interface::get_font (me,style_chain);
147   Molecule s;
148
149   int measures = 0;
150   SCM m (me->get_grob_property ("measure-count"));
151   if (gh_number_p (m))
152     {
153       measures = gh_scm2int (m);
154     }
155   
156
157   SCM limit = me->get_grob_property ("expand-limit");
158   if (measures <= 0)
159     return s;
160   if (measures == 1)
161     {
162       s = musfont->find_by_name (Rest::glyph_name (me, 0, ""));
163
164       /*
165         ugh.
166        */
167       if (Staff_symbol_referencer::position_f (me) == 0.0)
168         s.translate_axis (staff_space, Y_AXIS);
169     }
170   else if (measures <= gh_scm2int (limit))
171     {
172       Real pad = gh_scm2double (me->get_grob_property ("padding")) * staff_space;
173       s = church_rest (musfont, measures, staff_space, pad);
174     }
175   else 
176     {
177       Real w = gh_scm2double (me->get_grob_property ("width"));
178       s = big_rest (me, w);
179     }
180   return s;
181 }
182
183 Molecule
184 Multi_measure_rest::big_rest (Grob *me, Real width)
185 {
186   Real thick = gh_scm2double (me->get_grob_property ("thickness"));
187   Real ss = Staff_symbol_referencer::staff_space (me);
188   
189   Real slt = me->paper_l ()->get_var ("stafflinethickness");
190   Real y = slt * thick/2 * ss;
191   Box b(Interval (0, width), Interval (-y, y));
192   Real ythick = slt * ss;
193   
194   Molecule m =  Lookup::filledbox (b);
195   Molecule yb = Lookup::filledbox (Box (Interval (-ythick, ythick), Interval (-ss, ss)));
196
197   m.add_at_edge (X_AXIS, RIGHT, yb, -ythick);
198   m.add_at_edge (X_AXIS, LEFT, yb, -ythick);
199
200   m.align_to (X_AXIS, CENTER);
201   
202   return m;
203 }
204
205 /*
206   Kirchenpause (?)
207   
208  */
209 Molecule
210 Multi_measure_rest::church_rest (Font_metric *musfont, int measures,
211                                  Real staff_space, Real padding)
212 {
213   Molecule s;
214  /*
215         Build a rest from smaller parts. Distances inbetween are
216         really variable, see Wanske pp. 125 */
217    
218   int l = measures;
219   while (l)
220     {
221       int k;
222       if (l >= 4)
223         {
224           l-=4;
225           k = -2;
226         }
227       else if (l>= 2)
228         {
229           l -= 2;
230           k = -1;
231         }
232       else
233         {
234           k = 0;
235           l --;
236         }
237
238       Real pad = s.empty_b () ? 0.0 : padding;
239
240       Molecule r (musfont->find_by_name ("rests-" + to_str (k)));
241       if (k == 0)
242         r.translate_axis (staff_space, Y_AXIS);
243           
244       s.add_at_edge (X_AXIS, RIGHT, r, pad);
245     }
246
247   s.align_to (X_AXIS, CENTER);
248   return s;
249 }
250
251 /*
252   UGH. JUNKME elt prop "columns" isn't really needed. 
253  */
254 void
255 Multi_measure_rest::add_column (Grob*me,Item* c)
256 {
257   add_bound_item (dynamic_cast<Spanner*> (me),c);
258 }
259
260
261 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
262
263 SCM
264 Multi_measure_rest::set_spacing_rods (SCM smob)
265 {
266   Grob*me = unsmob_grob (smob);
267
268   Spanner*sp = dynamic_cast<Spanner*> (me);
269   if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
270     {
271       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
272       return SCM_UNSPECIFIED;
273     }
274
275   Item * l = sp->get_bound (LEFT)->column_l ();
276   Item * r = sp->get_bound (RIGHT)->column_l ();
277   Item * lb = l->find_prebroken_piece (RIGHT);
278   Item * rb = r->find_prebroken_piece (LEFT);      
279   
280   Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
281   Real staff_space = Staff_symbol_referencer::staff_space (me);
282   for (int i=0; i < 4; i++)
283     {
284       Item * l =  combinations[i][0];
285       Item *r = combinations[i][1];
286
287       if (!l || !r)
288         continue;
289
290       Rod rod;
291       rod.item_l_drul_[LEFT] = l;
292       rod.item_l_drul_[RIGHT] = r;
293
294       /*
295         should do something more advanced.
296       */
297       Molecule m = symbol_molecule(me);
298       
299       rod.distance_f_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
300         + m.extent (X_AXIS).length () + 2.5*staff_space;
301   
302       rod.add_to_cols ();
303     }
304   return SCM_UNSPECIFIED;
305 }
306
307
308
309 ADD_INTERFACE (Multi_measure_rest,"multi-measure-rest-interface",
310   "A rest that spans a whole number of measures.  For typesetting the
311 numbers, fields from font-interface may be used.
312
313 padding is the space between number and rest. Measured in staffspace.
314
315 ",
316   "thickness measure-count expand-limit padding");