*** empty log message ***
[lilypond.git] / lily / multi-measure-rest.cc
blob4de06c3f0ae7f9f131ceb577d1320441501ddb7d
1 /*
2 multi-measure-rest.cc -- implement Multi_measure_rest
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2003 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 "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"
24 MAKE_SCHEME_CALLBACK (Multi_measure_rest,percent,1);
25 SCM
26 Multi_measure_rest::percent (SCM smob)
28 Grob *me = unsmob_grob (smob);
29 Spanner *sp = dynamic_cast<Spanner*> (me);
31 Molecule r = Percent_repeat_item_interface::x_percent (me, 1, 0.75, 1.6);
33 // ugh copy & paste.
35 Interval sp_iv;
36 Direction d = LEFT;
39 Item * col = sp->get_bound (d)->get_column ();
41 Interval coldim = col->extent (0, X_AXIS);
43 sp_iv[d] = coldim[-d] ;
45 while ((flip (&d)) != LEFT);
46 Real x_off = 0.0;
48 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
50 we gotta stay clear of sp_iv, so move a bit to the right if
51 needed.
53 x_off += (sp_iv[LEFT] - rx) >? 0;
56 center between stuff.
58 x_off += sp_iv.length ()/ 2;
60 r.translate_axis (x_off,X_AXIS);
63 return r.smobbed_copy ();
66 MAKE_SCHEME_CALLBACK (Multi_measure_rest,brew_molecule,1);
67 SCM
68 Multi_measure_rest::brew_molecule (SCM smob)
70 Grob *me = unsmob_grob (smob);
71 Spanner * sp = dynamic_cast<Spanner*> (me);
73 Interval sp_iv;
74 Direction d = LEFT;
76 Grob *common = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
79 Item * col = sp->get_bound (d)->get_column ();
81 Interval coldim = col->extent (common, X_AXIS);
83 sp_iv[d] = coldim[-d] ;
85 while ((flip (&d)) != LEFT);
87 Real space = sp_iv.length();
89 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
91 we gotta stay clear of sp_iv, so move a bit to the right if
92 needed.
94 Real x_off = (sp_iv[LEFT] - rx) >? 0;
97 Molecule mol;
98 mol.add_molecule (symbol_molecule (me, space));
100 int measures = 0;
101 SCM m (me->get_grob_property ("measure-count"));
102 if (gh_number_p (m))
104 measures = gh_scm2int (m);
107 mol.translate_axis (x_off, X_AXIS);
108 return mol.smobbed_copy ();
113 Molecule
114 Multi_measure_rest::symbol_molecule (Grob *me, Real space)
116 int measures = 0;
117 SCM m (me->get_grob_property ("measure-count"));
118 if (gh_number_p (m))
120 measures = gh_scm2int (m);
122 if (measures <= 0)
123 return Molecule();
126 SCM limit = me->get_grob_property ("expand-limit");
127 if (measures > gh_scm2int (limit))
129 Real padding = 0.15;
130 Molecule s = big_rest (me, (1.0 - 2*padding) * space);
131 s.translate_axis (padding * space, X_AXIS);
132 return s;
135 SCM alist_chain = Font_interface::font_alist_chain (me);
137 Real staff_space = Staff_symbol_referencer::staff_space (me);
138 Font_metric *musfont
139 = Font_interface::get_font (me,alist_chain);
141 SCM sml = me->get_grob_property ("use-breve-rest");
142 if (measures == 1)
144 if (sml == SCM_BOOL_T)
146 Molecule s = musfont->find_by_name (Rest::glyph_name (me, -1, ""));
148 s.translate_axis ((space - s.extent (X_AXIS).length ())/2, X_AXIS);
150 return s ;
152 else
154 Molecule s = musfont->find_by_name (Rest::glyph_name (me, 0, ""));
157 ugh.
159 if (Staff_symbol_referencer::get_position (me) == 0.0)
160 s.translate_axis (staff_space, Y_AXIS);
162 s.translate_axis ((space - s.extent (X_AXIS).length ())/2, X_AXIS);
164 return s ;
167 else
169 return church_rest (me, musfont, measures, space);
175 Molecule
176 Multi_measure_rest::big_rest (Grob *me, Real width)
178 Real tthick = gh_scm2double (me->get_grob_property ("thick-thickness"));
179 Real hair_thick = gh_scm2double (me->get_grob_property ("hair-thickness"));
182 Real ss = Staff_symbol_referencer::staff_space (me);
184 Real slt = me->get_paper ()->get_var ("linethickness");
185 Real y = slt * tthick/2 * ss;
186 Real ythick = hair_thick * slt * ss;
187 Box b(Interval (0, width - 2 * ythick), Interval (-y, y));
189 Molecule m = Lookup::filledbox (b);
190 Molecule yb = Lookup::filledbox (Box (Interval (-0.5, 0.5)* ythick, Interval (-ss, ss)));
192 m.add_at_edge (X_AXIS, RIGHT, yb, 0, 0);
193 m.add_at_edge (X_AXIS, LEFT, yb, 0, 0);
195 m.align_to (X_AXIS, LEFT);
197 return m;
201 Kirchenpause (?)
203 Molecule
204 Multi_measure_rest::church_rest (Grob*me, Font_metric *musfont, int measures,
205 Real space)
207 SCM mols = SCM_EOL;
210 see Wanske pp. 125
212 int l = measures;
213 int count = 0;
214 Real symbols_width = 0.0;
216 SCM sml = me->get_grob_property ("use-breve-rest");
218 while (l)
220 if (sml == SCM_BOOL_T)
222 int k;
223 if (l >= 2)
225 l-=2;
226 k = -2;
228 else
230 l -= 1;
231 k = -1;
234 Molecule r (musfont->find_by_name ("rests-" + to_string (k)));
235 symbols_width += r.extent (X_AXIS).length ();
236 mols = gh_cons (r.smobbed_copy (), mols);
238 else
240 int k;
241 if (l >= 4)
243 l-=4;
244 k = -2;
246 else if (l>= 2)
248 l -= 2;
249 k = -1;
251 else
253 k = 0;
254 l --;
257 Molecule r (musfont->find_by_name ("rests-" + to_string (k)));
258 if (k == 0)
260 Real staff_space = Staff_symbol_referencer::staff_space (me);
261 r.translate_axis (staff_space, Y_AXIS);
263 symbols_width += r.extent (X_AXIS).length ();
264 mols = gh_cons (r.smobbed_copy (), mols);
266 count ++;
271 Real outer_padding_factor = 1.5; // make outer padding this much bigger.
272 Real inner_padding = (space - symbols_width) / (2 * outer_padding_factor + (count-1));
273 if (inner_padding < 0)
275 inner_padding = 1.0;
278 Molecule mol;
279 for (SCM s = mols; gh_pair_p (s); s = gh_cdr(s))
281 mol.add_at_edge (X_AXIS, LEFT, *unsmob_molecule (gh_car (s)), inner_padding, 0);
283 mol.align_to (X_AXIS, LEFT);
284 mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
286 return mol;
289 void
290 Multi_measure_rest::add_column (Grob*me,Item* c)
292 add_bound_item (dynamic_cast<Spanner*> (me),c);
296 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
299 Multi_measure_rest::set_spacing_rods (SCM smob)
301 Grob*me = unsmob_grob (smob);
303 Spanner*sp = dynamic_cast<Spanner*> (me);
304 if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
306 programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
307 return SCM_UNSPECIFIED;
310 Item * l = sp->get_bound (LEFT)->get_column ();
311 Item * r = sp->get_bound (RIGHT)->get_column ();
312 Item * lb = l->find_prebroken_piece (RIGHT);
313 Item * rb = r->find_prebroken_piece (LEFT);
315 Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
317 Real sym_width = symbol_molecule (me, 0.0).extent (X_AXIS).length ();
319 for (int i=0; i < 4; i++)
321 Item * l = combinations[i][0];
322 Item *r = combinations[i][1];
324 if (!l || !r)
325 continue;
327 Rod rod;
328 rod.item_l_drul_[LEFT] = l;
329 rod.item_l_drul_[RIGHT] = r;
332 rod.distance_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
333 + sym_width + 2.0; // 2.0 = magic!
335 rod.add_to_cols ();
337 return SCM_UNSPECIFIED;
343 ADD_INTERFACE (Multi_measure_rest,"multi-measure-rest-interface",
344 "A rest that spans a whole number of measures.",
345 "expand-limit measure-count hair-thickness thick-thickness use-breve-rest");