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