* lily/music-iterator.cc (quit, do_quit): new function: break link
[lilypond.git] / lily / multi-measure-rest.cc
blob69d9ec64feee7bded5785d550a45d4450717d427
1 /*
2 multi-measure-rest.cc -- implement Multi_measure_rest
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2002 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"
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 Molecule 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 ();
69 [TODO] 17
70 variable-sized multi-measure rest symbol: |====| ??
72 MAKE_SCHEME_CALLBACK (Multi_measure_rest,brew_molecule,1);
73 SCM
74 Multi_measure_rest::brew_molecule (SCM smob)
76 Grob *me = unsmob_grob (smob);
77 Spanner * sp = dynamic_cast<Spanner*> (me);
79 SCM alist_chain = Font_interface::font_alist_chain (me);
81 Interval sp_iv;
82 Direction d = LEFT;
84 Grob *common = sp->get_bound (LEFT)->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
87 Item * col = sp->get_bound (d)->get_column ();
89 Interval coldim = col->extent (common, X_AXIS);
91 sp_iv[d] = coldim[-d] ;
93 while ((flip (&d)) != LEFT);
95 Real space = sp_iv.length();
97 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
99 we gotta stay clear of sp_iv, so move a bit to the right if
100 needed.
102 Real x_off = (sp_iv[LEFT] - rx) >? 0;
105 Molecule mol;
106 mol.add_molecule (symbol_molecule (me, space));
108 int measures = 0;
109 SCM m (me->get_grob_property ("measure-count"));
110 if (gh_number_p (m))
112 measures = gh_scm2int (m);
115 SCM s = me->get_grob_property ("number-threshold");
116 if (measures > gh_scm2int (s))
118 Molecule s = Text_item::text2molecule (me,
119 scm_makfrom0str (to_string (measures).to_str0 ()),
120 alist_chain);
122 s.align_to (X_AXIS, CENTER);
123 s.translate_axis (gh_scm2double (me->get_grob_property ("padding")) + 2,
124 Y_AXIS);
126 s.translate_axis (mol.extent (X_AXIS).center (), X_AXIS);
127 mol.add_molecule (s);
129 mol.translate_axis (x_off, X_AXIS);
130 return mol.smobbed_copy ();
134 Molecule
135 Multi_measure_rest::symbol_molecule (Grob *me, Real space)
137 int measures = 0;
138 SCM m (me->get_grob_property ("measure-count"));
139 if (gh_number_p (m))
141 measures = gh_scm2int (m);
143 if (measures <= 0)
144 return Molecule();
147 SCM limit = me->get_grob_property ("expand-limit");
148 if (measures > gh_scm2int (limit))
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;
156 SCM alist_chain = Font_interface::font_alist_chain (me);
158 SCM style_chain =
159 Font_interface::add_style (me, ly_symbol2scm ("mmrest-symbol"),
160 alist_chain);
162 Real staff_space = Staff_symbol_referencer::staff_space (me);
163 Font_metric *musfont
164 = Font_interface::get_font (me,style_chain);
166 if (measures == 1)
168 Molecule s = musfont->find_by_name (Rest::glyph_name (me, 0, ""));
171 ugh.
173 if (Staff_symbol_referencer::get_position (me) == 0.0)
174 s.translate_axis (staff_space, Y_AXIS);
176 s.translate_axis ((space - s.extent (X_AXIS).length ())/2, X_AXIS);
178 return s ;
180 else
182 return church_rest (me, musfont, measures, space);
188 Molecule
189 Multi_measure_rest::big_rest (Grob *me, Real width)
191 Real thick = gh_scm2double (me->get_grob_property ("thickness"));
192 Real ss = Staff_symbol_referencer::staff_space (me);
194 Real slt = me->get_paper ()->get_var ("linethickness");
195 Real y = slt * thick/2 * ss;
196 Box b(Interval (0, width), Interval (-y, y));
197 Real ythick = slt * ss;
199 Molecule m = Lookup::filledbox (b);
200 Molecule yb = Lookup::filledbox (Box (Interval (-ythick, ythick), Interval (-ss, ss)));
202 m.add_at_edge (X_AXIS, RIGHT, yb, -ythick);
203 m.add_at_edge (X_AXIS, LEFT, yb, -ythick);
205 m.align_to (X_AXIS, LEFT);
207 return m;
211 Kirchenpause (?)
213 Molecule
214 Multi_measure_rest::church_rest (Grob*me, Font_metric *musfont, int measures,
215 Real space)
217 SCM mols = SCM_EOL;
220 see Wanske pp. 125
222 int l = measures;
223 int count = 0;
224 Real symbols_width = 0.0;
225 while (l)
227 int k;
228 if (l >= 4)
230 l-=4;
231 k = -2;
233 else if (l>= 2)
235 l -= 2;
236 k = -1;
238 else
240 k = 0;
241 l --;
244 Molecule r (musfont->find_by_name ("rests-" + to_string (k)));
245 if (k == 0)
247 Real staff_space = Staff_symbol_referencer::staff_space (me);
248 r.translate_axis (staff_space, Y_AXIS);
250 symbols_width += r.extent (X_AXIS).length ();
251 mols = gh_cons (r.smobbed_copy (), mols);
252 count ++;
257 Real outer_padding_factor = 1.5; // make outer padding this much bigger.
258 Real inner_padding = (space - symbols_width) / (2 * outer_padding_factor + (count-1));
259 if (inner_padding < 0)
261 inner_padding = 1.0;
264 Molecule mol;
265 for (SCM s = mols; gh_pair_p (s); s = gh_cdr(s))
267 mol.add_at_edge (X_AXIS, LEFT, *unsmob_molecule (gh_car (s)), inner_padding);
269 mol.align_to (X_AXIS, LEFT);
270 mol.translate_axis (outer_padding_factor * inner_padding, X_AXIS);
272 return mol;
275 void
276 Multi_measure_rest::add_column (Grob*me,Item* c)
278 add_bound_item (dynamic_cast<Spanner*> (me),c);
282 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
285 Multi_measure_rest::set_spacing_rods (SCM smob)
287 return SCM_UNSPECIFIED;
289 Grob*me = unsmob_grob (smob);
291 Spanner*sp = dynamic_cast<Spanner*> (me);
292 if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
294 programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
295 return SCM_UNSPECIFIED;
298 Item * l = sp->get_bound (LEFT)->get_column ();
299 Item * r = sp->get_bound (RIGHT)->get_column ();
300 Item * lb = l->find_prebroken_piece (RIGHT);
301 Item * rb = r->find_prebroken_piece (LEFT);
303 Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
305 Real sym_width = symbol_molecule (me, 0.0).extent (X_AXIS).length ();
307 for (int i=0; i < 4; i++)
309 Item * l = combinations[i][0];
310 Item *r = combinations[i][1];
312 if (!l || !r)
313 continue;
315 Rod rod;
316 rod.item_l_drul_[LEFT] = l;
317 rod.item_l_drul_[RIGHT] = r;
320 rod.distance_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
321 + sym_width + 2.0; // 2.0 = magic!
323 rod.add_to_cols ();
325 return SCM_UNSPECIFIED;
330 ADD_INTERFACE (Multi_measure_rest,"multi-measure-rest-interface",
331 "A rest that spans a whole number of measures. For typesetting the
332 numbers, fields from font-interface may be used.
336 "expand-limit measure-count number-threshold padding thickness");