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