]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
patch::: 1.3.136.jcn3
[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--2001 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 "group-interface.hh"
19 #include "spanner.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "text-item.hh"
22
23 void
24 Multi_measure_rest::set_interface (Grob*me)
25 {
26   me->set_interface (ly_symbol2scm ("multi-measure-rest-interface"));
27 }
28
29 bool
30 Multi_measure_rest::has_interface (Grob*me)
31 {
32   return me->has_interface (ly_symbol2scm ("multi-measure-rest-interface"));
33 }
34
35 MAKE_SCHEME_CALLBACK (Multi_measure_rest,percent,1);
36 SCM
37 Multi_measure_rest::percent (SCM smob)
38 {
39   Grob *me = unsmob_grob (smob);
40   Spanner *sp = dynamic_cast<Spanner*> (me);
41   
42   Font_metric *musfont = Font_interface::get_default_font (me);
43                         
44   Molecule r (musfont->find_by_name ("scripts-repeatsign"));
45
46   // ugh copy & paste.
47   
48   Interval sp_iv;
49   Direction d = LEFT;
50   do
51     {
52       Item * col = sp->get_bound (d)->column_l ();
53
54       Interval coldim = col->extent (0, X_AXIS);
55
56       sp_iv[d] = coldim[-d]  ;
57     }
58   while ((flip (&d)) != LEFT);
59   Real x_off = 0.0;
60
61   Real rx  = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
62   /*
63     we gotta stay clear of sp_iv, so move a bit to the right if
64     needed.
65    */
66   x_off += (sp_iv[LEFT] -  rx) >? 0;
67
68   /*
69     center between stuff.
70    */
71   x_off += sp_iv.length ()/ 2;
72
73   r.translate_axis (x_off,X_AXIS);
74
75   
76   return r.smobbed_copy ();
77 }
78
79
80 /*
81    [TODO]                                      17
82    variable-sized multi-measure rest symbol: |====| ??
83 */
84 MAKE_SCHEME_CALLBACK (Multi_measure_rest,brew_molecule,1);
85 SCM
86 Multi_measure_rest::brew_molecule (SCM smob) 
87 {
88   Grob *me = unsmob_grob (smob);
89   if (to_boolean (me->get_grob_property ("skip-timestep")))
90     {
91       me->set_grob_property ("skip-timestep", SCM_EOL);
92       return SCM_EOL;
93     }
94   
95   Spanner * sp = dynamic_cast<Spanner*> (me);
96
97   SCM alist_chain = Font_interface::font_alist_chain (me);
98
99   
100   SCM style_chain =
101     Font_interface::add_style (me, ly_symbol2scm ("mmrest-symbol"),
102                                alist_chain);
103
104   Font_metric *musfont
105     = Font_interface::get_font (me,style_chain);
106                         
107   Real staff_space = Staff_symbol_referencer::staff_space (me);
108
109   Interval sp_iv;
110   Direction d = LEFT;
111   do
112     {
113       Item * col = sp->get_bound (d)->column_l ();
114
115       Interval coldim = col->extent (0, X_AXIS);
116
117       sp_iv[d] = coldim[-d]  ;
118     }
119   while ((flip (&d)) != LEFT);
120   Molecule mol;
121   Real x_off = 0.0;
122
123   Real rx  = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
124   /*
125     we gotta stay clear of sp_iv, so move a bit to the right if
126     needed.
127    */
128   x_off += (sp_iv[LEFT] -  rx) >? 0;
129
130   /*
131     center between stuff.
132    */
133   x_off += sp_iv.length ()/ 2;
134
135   
136   Molecule s;
137
138   int measures = 1;
139   SCM m (me->get_grob_property ("measure-count"));
140   if (gh_number_p (m))
141     {
142       measures = gh_scm2int (m);
143     }
144   
145
146   SCM limit = me->get_grob_property ("expand-limit");
147   if (measures <= gh_scm2int (limit))
148     {
149       /*
150         Build a rest from smaller parts. Distances inbetween are
151         really variable, see Wanske pp. 125 */
152
153       int l = measures;
154       while (l)
155         {
156           int k;
157           if (l >= 4)
158             {
159               l-=4;
160               k = -2;
161             }
162           else if (l>= 2)
163             {
164               l -= 2;
165               k = -1;
166             }
167           else
168             {
169               k = 0;
170               l --;
171             }
172
173           Real pad = s.empty_b ()
174             ? 0.0 : gh_scm2double (me->get_grob_property ("padding")) * staff_space;
175
176           Molecule r (musfont->find_by_name ("rests-" + to_str (k)));
177           if (k == 0)
178             r.translate_axis (staff_space, Y_AXIS);
179           
180           s.add_at_edge (X_AXIS, RIGHT, r, pad);
181         }
182
183
184       s.align_to (X_AXIS, CENTER);
185     }
186   else 
187     {
188       String idx = ("rests-") + to_str (-4);
189       s = musfont->find_by_name (idx);
190     }
191   
192   mol.add_molecule (s);
193
194   if (measures > 1)
195     {
196       Molecule s = Text_item::text2molecule (me,
197                                              ly_str02scm (to_str (measures).ch_C ()),
198                                              alist_chain);
199       s.align_to (X_AXIS, CENTER);
200       s.translate_axis (3.0 * staff_space, Y_AXIS);
201       mol.add_molecule (s);
202     }
203   mol.translate_axis (x_off, X_AXIS);
204   return mol.smobbed_copy ();
205 }
206
207 /*
208   UGH. JUNKME elt prop "columns" isn't really needed. 
209  */
210 void
211 Multi_measure_rest::add_column (Grob*me,Item* c)
212 {
213   Pointer_group_interface::add_element (me, "columns",c);
214
215   add_bound_item (dynamic_cast<Spanner*> (me),c);
216 }
217
218
219 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
220
221 SCM
222 Multi_measure_rest::set_spacing_rods (SCM smob)
223 {
224   Grob*me = unsmob_grob (smob);
225
226   Spanner*sp = dynamic_cast<Spanner*> (me);
227   if (! (sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
228     {
229       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
230       return SCM_UNSPECIFIED;
231     }
232
233   Item * l = sp->get_bound (LEFT)->column_l ();
234   Item * r = sp->get_bound (RIGHT)->column_l ();
235   Item * lb = l->find_prebroken_piece (RIGHT);
236   Item * rb = r->find_prebroken_piece (LEFT);      
237   
238   Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
239   Real staff_space = Staff_symbol_referencer::staff_space (me);
240   for (int i=0; i < 4; i++)
241     {
242       Item * l =  combinations[i][0];
243       Item *r = combinations[i][1];
244
245       if (!l || !r)
246         continue;
247
248       Rod rod;
249       rod.item_l_drul_[LEFT] = l;
250       rod.item_l_drul_[RIGHT] = r;
251
252         /*
253           should do something more advanced.
254          */
255       rod.distance_f_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
256         + gh_scm2double (me->get_grob_property ("minimum-width")) * staff_space;
257   
258       rod.add_to_cols ();
259     }
260   return SCM_UNSPECIFIED;
261 }
262