]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest.cc
80849691beb8dd993781cecf4a1b9fde3afcba9e
[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   /*
36    [TODO]                                      17
37  * variable-sized multi-measure rest symbol: |====| ??
38 */
39 MAKE_SCHEME_CALLBACK(Multi_measure_rest,brew_molecule,1);
40 SCM
41 Multi_measure_rest::brew_molecule (SCM smob) 
42 {
43   Grob *me = unsmob_grob (smob);
44   Spanner * sp = dynamic_cast<Spanner*> (me);
45
46   SCM alist_chain = Font_interface::font_alist_chain (me);
47
48   
49   SCM style_chain =
50     Font_interface::add_style (me, ly_symbol2scm ("mmrest-symbol"),
51                                alist_chain);
52
53   Font_metric *musfont
54     = Font_interface::get_font (me,style_chain);
55                         
56   Real staff_space = Staff_symbol_referencer::staff_space (me);
57
58   Interval sp_iv;
59   Direction d = LEFT;
60   do
61     {
62       Item * col = sp->get_bound (d)->column_l ();
63
64       Interval coldim = col->extent (0, X_AXIS);
65
66       sp_iv[d] = coldim[-d]  ;
67     }
68   while ((flip (&d)) != LEFT);
69   Molecule mol;
70   Real x_off = 0.0;
71
72   Real rx  = sp->get_bound (LEFT)->relative_coordinate (0, X_AXIS);
73   /*
74     we gotta stay clear of sp_iv, so move a bit to the right if
75     needed.
76    */
77   x_off += (sp_iv[LEFT] -  rx) >? 0;
78
79   /*
80     center between stuff.
81    */
82   x_off += sp_iv.length ()/ 2;
83
84   
85   Molecule s;
86
87   int measures = 1;
88   SCM m (me->get_grob_property ("measure-count"));
89   if (gh_number_p (m))
90     {
91       measures = gh_scm2int (m);
92     }
93   
94
95   SCM limit = me->get_grob_property ("expand-limit");
96   if (measures <= gh_scm2int (limit))
97     {
98       /*
99         Build a rest from smaller parts. Distances inbetween are
100         really variable, see Wanske pp. 125 */
101
102       int l = measures;
103       while (l)
104         {
105           int k;
106           if (l >= 4)
107             {
108               l-=4;
109               k = -2;
110             }
111           else if (l>= 2)
112             {
113               l -= 2;
114               k = -1;
115             }
116           else
117             {
118               k = 0;
119               l --;
120             }
121
122           Real pad = s.empty_b ()
123             ? 0.0 : gh_scm2double (me->get_grob_property ("padding")) * staff_space;
124
125           Molecule r (musfont->find_by_name ("rests-" + to_str (k)));
126           if (k == 0)
127             r.translate_axis (staff_space, Y_AXIS);
128           
129           s.add_at_edge (X_AXIS, RIGHT, r, pad);
130         }
131
132
133       s.align_to (X_AXIS, CENTER);
134     }
135   else 
136     {
137       String idx =  ("rests-") + to_str (-4);
138       s = musfont->find_by_name (idx);
139     }
140   
141   mol.add_molecule (s);
142
143   if (measures > 1)
144     {
145       Molecule s = Text_item::text2molecule (me,
146                                              ly_str02scm (to_str (measures).ch_C ()),
147                                              alist_chain);
148       s.align_to (X_AXIS, CENTER);
149       s.translate_axis (3.0 * staff_space, Y_AXIS);
150       mol.add_molecule (s);
151     }
152   mol.translate_axis (x_off, X_AXIS);
153   return mol.smobbed_copy();
154 }
155
156 /*
157   UGH. JUNKME elt prop "columns" isn't really needed. 
158  */
159 void
160 Multi_measure_rest::add_column (Grob*me,Item* c)
161 {
162   Pointer_group_interface::add_element (me, "columns",c);
163
164   add_bound_item (dynamic_cast<Spanner*> (me),c);
165 }
166
167
168 MAKE_SCHEME_CALLBACK (Multi_measure_rest, set_spacing_rods,1);
169
170 SCM
171 Multi_measure_rest::set_spacing_rods (SCM smob)
172 {
173   Grob*me = unsmob_grob (smob);
174
175   Spanner*sp = dynamic_cast<Spanner*> (me);
176   if (!(sp->get_bound (LEFT) && sp->get_bound (RIGHT)))
177     {
178       programming_error ("Multi_measure_rest::get_rods (): I am not spanned!");
179       return SCM_UNSPECIFIED;
180     }
181
182   Item * l = sp->get_bound (LEFT)->column_l ();
183   Item * r = sp->get_bound (RIGHT)->column_l ();
184   Item * lb = l->find_prebroken_piece (RIGHT);
185   Item * rb = r->find_prebroken_piece (LEFT);      
186   
187   Item* combinations[4][2]={{l,r}, {lb,r}, {l,rb},{lb,rb}};
188   Real staff_space = Staff_symbol_referencer::staff_space (me);
189   for (int i=0; i < 4; i++)
190     {
191       Item * l =  combinations[i][0];
192       Item *r = combinations[i][1];
193
194       if (!l || !r)
195         continue;
196
197       Rod rod;
198       rod.item_l_drul_[LEFT] = l;
199       rod.item_l_drul_[RIGHT] = r;
200
201         /*
202           should do something more advanced.
203          */
204       rod.distance_f_ = l->extent (l, X_AXIS)[BIGGER] - r->extent (r, X_AXIS)[SMALLER]
205         + gh_scm2double (me->get_grob_property ("minimum-width")) * staff_space;
206   
207       rod.add_to_cols ();
208     }
209   return SCM_UNSPECIFIED;
210 }
211