*** empty log message ***
[lilypond.git] / lily / multi-measure-rest.cc
blob51101be0e2e6f1995dd9810df505789c5b35d060
1 /*
2 multi-measure-rest.cc -- implement Multi_measure_rest
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
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"
25 MAKE_SCHEME_CALLBACK (Multi_measure_rest,percent,1);
26 SCM
27 Multi_measure_rest::percent (SCM smob)
29 Grob *me = unsmob_grob (smob);
30 Spanner *sp = dynamic_cast<Spanner*> (me);
32 Stencil r = Percent_repeat_item_interface::x_percent (me, 1, 0.75, 1.6);
34 // ugh copy & paste.
36 Interval sp_iv;
37 Direction d = LEFT;
40 Item * col = sp->get_bound (d)->get_column ();
42 Interval coldim = col->extent (0, X_AXIS);
44 sp_iv[d] = coldim[-d] ;
46 while ((flip (&d)) != LEFT);
47 Real x_off = 0.0;
49 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
51 we gotta stay clear of sp_iv, so move a bit to the right if
52 needed.
54 x_off += (sp_iv[LEFT] - rx) >? 0;
57 center between stuff.
59 x_off += sp_iv.length ()/ 2;
61 r.translate_axis (x_off,X_AXIS);
64 return r.smobbed_copy ();
67 MAKE_SCHEME_CALLBACK (Multi_measure_rest,print,1);
68 SCM
69 Multi_measure_rest::print (SCM smob)
71 Grob *me = unsmob_grob (smob);
72 Spanner * sp = dynamic_cast<Spanner*> (me);
74 Interval sp_iv;
75 Direction d = LEFT;
77 Grob *common = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
80 Item * b = sp->get_bound (d);
82 Interval coldim = (Separation_item::has_interface (b))
83 ? Separation_item::relative_width (b, common)
84 : b->extent (common, X_AXIS);
86 sp_iv[d] = coldim.is_empty () ? b->relative_coordinate (common, X_AXIS) : coldim[-d];
88 while ((flip (&d)) != LEFT);
90 Real space = sp_iv.length ();
92 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
94 we gotta stay clear of sp_iv, so move a bit to the right if
95 needed.
97 Real x_off = (sp_iv[LEFT] - rx) >? 0;
100 Stencil mol;
101 mol.add_stencil (symbol_stencil (me, space));
103 int measures = 0;
104 SCM m (me->get_property ("measure-count"));
105 if (gh_number_p (m))
107 measures = gh_scm2int (m);
110 mol.translate_axis (x_off, X_AXIS);
111 return mol.smobbed_copy ();
116 Stencil
117 Multi_measure_rest::symbol_stencil (Grob *me, Real space)
119 int measures = 0;
120 SCM m (me->get_property ("measure-count"));
121 if (gh_number_p (m))
123 measures = gh_scm2int (m);
125 if (measures <= 0)
126 return Stencil ();
129 SCM limit = me->get_property ("expand-limit");
130 if (measures > gh_scm2int (limit))
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;
138 SCM alist_chain = Font_interface::text_font_alist_chain (me);
140 Real staff_space = Staff_symbol_referencer::staff_space (me);
141 Font_metric *musfont
142 = select_font (me->get_paper (), alist_chain);
144 SCM sml = me->get_property ("use-breve-rest");
145 if (measures == 1)
147 if (to_boolean (sml))
149 Stencil s = musfont->find_by_name (Rest::glyph_name (me, -1, ""));
151 s.translate_axis ((space - s.extent (X_AXIS).length ())/2, X_AXIS);
153 return s ;
155 else
157 Stencil s = musfont->find_by_name (Rest::glyph_name (me, 0, ""));
160 ugh.
162 if (Staff_symbol_referencer::get_position (me) == 0.0)
163 s.translate_axis (staff_space, Y_AXIS);
165 s.translate_axis ((space - s.extent (X_AXIS).length ())/2, X_AXIS);
167 return s ;
170 else
172 return church_rest (me, musfont, measures, space);
178 WIDTH can also be 0 to determine the minimum size of the object.
180 Stencil
181 Multi_measure_rest::big_rest (Grob *me, Real width)
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);
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));
193 Real blot = width ? (.8 * (y <? ythick)) : 0.0;
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);
198 m.add_at_edge (X_AXIS, RIGHT, yb, 0, 0);
199 m.add_at_edge (X_AXIS, LEFT, yb, 0, 0);
201 m.align_to (X_AXIS, LEFT);
203 return m;
207 Kirchenpause (?)
209 Stencil
210 Multi_measure_rest::church_rest (Grob*me, Font_metric *musfont, int measures,
211 Real space)
213 SCM mols = SCM_EOL;
216 see Wanske pp. 125
218 int l = measures;
219 int count = 0;
220 Real symbols_width = 0.0;
222 SCM sml = me->get_property ("use-breve-rest");
224 while (l)
226 if (sml == SCM_BOOL_T)
228 int k;
229 if (l >= 2)
231 l-=2;
232 k = -2;
234 else
236 l -= 1;
237 k = -1;
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);
244 else
246 int k;
247 if (l >= 4)
249 l-=4;
250 k = -2;
252 else if (l>= 2)
254 l -= 2;
255 k = -1;
257 else
259 k = 0;
260 l --;
263 Stencil r (musfont->find_by_name ("rests-" + to_string (k)));
264 if (k == 0)
266 Real staff_space = Staff_symbol_referencer::staff_space (me);
267 r.translate_axis (staff_space, Y_AXIS);
269 symbols_width += r.extent (X_AXIS).length ();
270 mols = gh_cons (r.smobbed_copy (), mols);
272 count ++;
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)
281 inner_padding = 1.0;
284 Stencil mol;
285 for (SCM s = mols; gh_pair_p (s); s = gh_cdr (s))
287 mol.add_at_edge (X_AXIS, LEFT, *unsmob_stencil (gh_car (s)), inner_padding, 0);
289 mol.align_to (X_AXIS, LEFT);
290 mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
292 return mol;
295 void
296 Multi_measure_rest::add_column (Grob*me,Item* c)
298 add_bound_item (dynamic_cast<Spanner*> (me),c);
302 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
304 Multi_measure_rest::set_spacing_rods (SCM smob)
306 Grob*me = unsmob_grob (smob);
308 Spanner*sp = dynamic_cast<Spanner*> (me);
309 if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
311 programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
312 return SCM_UNSPECIFIED;
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);
320 Item* combinations[4][2] = {{l,r},
321 {lb,r},
322 {l,rb},
323 {lb,rb}};
325 Real sym_width = symbol_stencil (me, 0.0).extent (X_AXIS).length ();
327 for (int i=0; i < 4; i++)
329 Item *l = combinations[i][0];
330 Item *r = combinations[i][1];
332 if (!l || !r)
333 continue;
335 Rod rod;
336 rod.item_l_drul_[LEFT] = l;
337 rod.item_l_drul_[RIGHT] = r;
340 rod.distance_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
341 + sym_width + 2.0; // 2.0 = magic!
343 Real minlen = robust_scm2double (me->get_property ("minimum-length"), 0.0);
344 rod.distance_ = max (rod.distance_,
345 minlen);
346 rod.add_to_cols ();
348 return SCM_UNSPECIFIED;
354 ADD_INTERFACE (Multi_measure_rest,"multi-measure-rest-interface",
355 "A rest that spans a whole number of measures.",
356 "expand-limit measure-count hair-thickness thick-thickness use-breve-rest minimum-length");