lilypond-1.3.147
[lilypond.git] / lily / multi-measure-rest.cc
blob890c0d1562901f24928140aea8ccd0361ce30ce0
1 /*
2 multi-measure-rest.cc -- implement Multi_measure_rest
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
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 "group-interface.hh"
19 #include "spanner.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "text-item.hh"
22 #include "percent-repeat-item.hh"
25 void
26 Multi_measure_rest::set_interface (Grob*me)
28 me->set_interface (ly_symbol2scm ("multi-measure-rest-interface"));
31 bool
32 Multi_measure_rest::has_interface (Grob*me)
34 return me->has_interface (ly_symbol2scm ("multi-measure-rest-interface"));
37 MAKE_SCHEME_CALLBACK (Multi_measure_rest,percent,1);
38 SCM
39 Multi_measure_rest::percent (SCM smob)
42 Grob *me = unsmob_grob (smob);
43 Spanner *sp = dynamic_cast<Spanner*> (me);
45 Font_metric *musfont = Font_interface::get_default_font (me);
47 Molecule r = Percent_repeat_item_interface::x_percent (me, 1, 0.75, 1.6);
49 // ugh copy & paste.
51 Interval sp_iv;
52 Direction d = LEFT;
55 Item * col = sp->get_bound (d)->column_l ();
57 Interval coldim = col->extent (0, X_AXIS);
59 sp_iv[d] = coldim[-d] ;
61 while ((flip (&d)) != LEFT);
62 Real x_off = 0.0;
64 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
66 we gotta stay clear of sp_iv, so move a bit to the right if
67 needed.
69 x_off += (sp_iv[LEFT] - rx) >? 0;
72 center between stuff.
74 x_off += sp_iv.length ()/ 2;
76 r.translate_axis (x_off,X_AXIS);
79 return r.smobbed_copy ();
84 [TODO] 17
85 variable-sized multi-measure rest symbol: |====| ??
87 MAKE_SCHEME_CALLBACK (Multi_measure_rest,brew_molecule,1);
88 SCM
89 Multi_measure_rest::brew_molecule (SCM smob)
91 Grob *me = unsmob_grob (smob);
92 if (to_boolean (me->get_grob_property ("skip-timestep")))
94 me->set_grob_property ("skip-timestep", SCM_EOL);
95 return SCM_EOL;
98 Spanner * sp = dynamic_cast<Spanner*> (me);
100 SCM alist_chain = Font_interface::font_alist_chain (me);
103 SCM style_chain =
104 Font_interface::add_style (me, ly_symbol2scm ("mmrest-symbol"),
105 alist_chain);
107 Font_metric *musfont
108 = Font_interface::get_font (me,style_chain);
110 Real staff_space = Staff_symbol_referencer::staff_space (me);
112 Interval sp_iv;
113 Direction d = LEFT;
116 Item * col = sp->get_bound (d)->column_l ();
118 Interval coldim = col->extent (0, X_AXIS);
120 sp_iv[d] = coldim[-d] ;
122 while ((flip (&d)) != LEFT);
123 Molecule mol;
124 Real x_off = 0.0;
126 Real rx = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
128 we gotta stay clear of sp_iv, so move a bit to the right if
129 needed.
131 x_off += (sp_iv[LEFT] - rx) >? 0;
134 center between stuff.
136 x_off += sp_iv.length ()/ 2;
139 Molecule s;
141 int measures = 0;
142 SCM m (me->get_grob_property ("measure-count"));
143 if (gh_number_p (m))
145 measures = gh_scm2int (m);
149 SCM limit = me->get_grob_property ("expand-limit");
150 if (measures <= 0)
151 return SCM_EOL;
152 if (measures == 1)
154 s = musfont->find_by_name (Rest::glyph_name (me, 0, ""));
157 ugh.
159 if (Staff_symbol_referencer::position_f (me) == 0.0)
160 s.translate_axis (Staff_symbol_referencer::staff_space (me), Y_AXIS);
162 else if (measures <= gh_scm2int (limit))
165 Build a rest from smaller parts. Distances inbetween are
166 really variable, see Wanske pp. 125 */
168 int l = measures;
169 while (l)
171 int k;
172 if (l >= 4)
174 l-=4;
175 k = -2;
177 else if (l>= 2)
179 l -= 2;
180 k = -1;
182 else
184 k = 0;
185 l --;
188 Real pad = s.empty_b ()
189 ? 0.0 : gh_scm2double (me->get_grob_property ("padding")) * staff_space;
191 Molecule r (musfont->find_by_name ("rests-" + to_str (k)));
192 if (k == 0)
193 r.translate_axis (staff_space, Y_AXIS);
195 s.add_at_edge (X_AXIS, RIGHT, r, pad);
199 s.align_to (X_AXIS, CENTER);
201 else
203 String idx = ("rests-") + to_str (-4);
204 s = musfont->find_by_name (idx);
207 mol.add_molecule (s);
209 if (measures > 1)
211 Molecule s = Text_item::text2molecule (me,
212 ly_str02scm (to_str (measures).ch_C ()),
213 alist_chain);
214 s.align_to (X_AXIS, CENTER);
215 s.translate_axis (3.0 * staff_space, Y_AXIS);
216 mol.add_molecule (s);
218 mol.translate_axis (x_off, X_AXIS);
219 return mol.smobbed_copy ();
223 UGH. JUNKME elt prop "columns" isn't really needed.
225 void
226 Multi_measure_rest::add_column (Grob*me,Item* c)
228 Pointer_group_interface::add_element (me, "columns",c);
230 add_bound_item (dynamic_cast<Spanner*> (me),c);
234 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
237 Multi_measure_rest::set_spacing_rods (SCM smob)
239 Grob*me = unsmob_grob (smob);
241 Spanner*sp = dynamic_cast<Spanner*> (me);
242 if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
244 programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
245 return SCM_UNSPECIFIED;
248 Item * l = sp->get_bound (LEFT)->column_l ();
249 Item * r = sp->get_bound (RIGHT)->column_l ();
250 Item * lb = l->find_prebroken_piece (RIGHT);
251 Item * rb = r->find_prebroken_piece (LEFT);
253 Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
254 Real staff_space = Staff_symbol_referencer::staff_space (me);
255 for (int i=0; i < 4; i++)
257 Item * l = combinations[i][0];
258 Item *r = combinations[i][1];
260 if (!l || !r)
261 continue;
263 Rod rod;
264 rod.item_l_drul_[LEFT] = l;
265 rod.item_l_drul_[RIGHT] = r;
268 should do something more advanced.
270 rod.distance_f_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
271 + gh_scm2double (me->get_grob_property ("minimum-width")) * staff_space;
273 rod.add_to_cols ();
275 return SCM_UNSPECIFIED;