]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-spanner.cc
release: 1.3.54
[lilypond.git] / lily / tuplet-spanner.cc
1 /*
2   plet-spanner.cc -- implement Tuplet_spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9
10 #include "beam.hh"
11 #include "box.hh"
12 #include "debug.hh"
13 #include "lookup.hh"
14 #include "molecule.hh"
15 #include "paper-column.hh"
16 #include "paper-def.hh"
17 #include "tuplet-spanner.hh"
18 #include "stem.hh"
19 #include "note-column.hh"
20 #include "dimensions.hh"
21 #include "group-interface.hh"
22 #include "directional-element-interface.hh"
23
24
25 Tuplet_spanner::Tuplet_spanner ()
26 {
27   set_elt_property ("beams", SCM_EOL);
28   set_elt_property ("columns", SCM_EOL);
29
30   // ugh.
31   set_elt_property ("delta-y", gh_int2scm (0));
32 }
33
34 /*
35   TODO. 
36  */
37 Molecule 
38 Tuplet_spanner::do_brew_molecule () const
39 {
40   Molecule  mol;
41
42   // Default behaviour: number always, bracket when no beam!
43   bool par_beam = to_boolean (get_elt_property ("parallel-beam"));
44   bool bracket_visibility = !par_beam;
45   bool number_visibility = true;
46
47   SCM bracket = get_elt_property ("tuplet-bracket-visibility");
48   if (gh_boolean_p (bracket))
49     {
50       bracket_visibility = gh_scm2bool (bracket);
51     }
52   else if (bracket == ly_symbol2scm ("if-no-beam"))
53     bracket_visibility = !par_beam;
54
55   SCM numb = get_elt_property ("tuplet-number-visibility");  
56   if (gh_boolean_p (numb))
57     {
58       number_visibility = gh_scm2bool (numb);
59     }
60   else if (bracket == ly_symbol2scm ("if-no-beam"))
61     number_visibility = !par_beam;
62   
63   if (gh_pair_p (get_elt_property ("columns")))
64     {
65       Link_array<Note_column> column_arr=
66         Group_interface__extract_elements (this, (Note_column*)0, "columns");
67         
68       Real ncw = column_arr.top ()->extent(X_AXIS).length ();
69       Real w = spanner_length () + ncw;
70
71
72       Real staff_space = paper_l ()->get_var ("interline");
73       Direction dir = directional_element (this).get ();
74       Real dy = gh_scm2double (get_elt_property ("delta-y"));
75       SCM number = get_elt_property ("text");
76       if (gh_string_p (number) && number_visibility)
77         {
78           Molecule
79             num (lookup_l ()->text ("italic",
80                                     ly_scm2string (number), paper_l ()));
81           num.align_to (X_AXIS, CENTER);
82           num.translate_axis (w/2, X_AXIS);
83           num.align_to (Y_AXIS, CENTER);
84           num.translate_axis (dir * staff_space, Y_AXIS);
85         
86           num.translate_axis (dy/2, Y_AXIS);
87
88           mol.add_molecule (num);
89         }
90       
91       Real thick = paper_l ()->get_var ("tuplet_thick");
92       if (bracket_visibility)      
93         {
94           Real gap = paper_l () -> get_var ("tuplet_spanner_gap");
95           Real height = staff_space;
96           SCM at =gh_list(ly_symbol2scm ("tuplet"),
97                                        gh_double2scm (height),
98                                        gh_double2scm (gap),
99                                        gh_double2scm (w),
100                                        gh_double2scm (dy),
101                                        gh_double2scm (thick),
102                                        gh_int2scm (dir),
103                                        SCM_UNDEFINED);
104
105           Box b;
106           mol.add_molecule (Molecule (b, at));
107         }
108
109       //mol.translate_axis (dir * staff_space, Y_AXIS);
110     }
111   return mol;
112 }
113   
114 void
115 Tuplet_spanner::do_add_processing ()
116 {
117   if (gh_pair_p (get_elt_property ("columns")))
118     {
119       Link_array<Note_column> column_arr=
120         Group_interface__extract_elements (this, (Note_column*)0, "columns");
121       
122       set_bound (LEFT, column_arr[0]);
123       set_bound (RIGHT, column_arr.top ());  
124     }
125 }
126
127
128
129 /*
130   use first -> last note for slope, and then correct for disturbing
131   notes in between.  */
132 void
133 Tuplet_spanner::calc_position_and_height (Real *offset, Real * dy) const
134 {
135   Link_array<Note_column> column_arr=
136     Group_interface__extract_elements (this, (Note_column*)0, "columns");
137
138
139   Score_element * common = common_refpoint (get_elt_property ("columns"), Y_AXIS);
140   
141   Direction d = directional_element (this).get ();
142
143   /*
144     Use outer non-rest columns to determine slope
145    */
146   int l = 0;
147   while (l <column_arr.size() && column_arr[l]->rest_b())
148     l ++;
149
150   int r = column_arr.size ()- 1;
151   while (r >= l && column_arr[r]->rest_b())
152     r--;
153   
154   if (l < r)
155     {
156       *dy = column_arr[r]->extent (Y_AXIS) [d] + column_arr[r]->relative_coordinate (common, Y_AXIS)
157         - column_arr[l]->extent (Y_AXIS) [d] - column_arr[l]->relative_coordinate (common, Y_AXIS);
158     }
159   else
160     * dy = 0;
161
162
163   *offset = - d * infinity_f;
164
165   if (!column_arr.size ())
166     return;
167   
168   Real x0 = column_arr[0]->relative_coordinate (0, X_AXIS);
169   Real x1 = column_arr.top ()->relative_coordinate (0, X_AXIS);
170   
171   Real factor = column_arr.size () > 1 ? 1/(x1 - x0) : 1.0;
172   
173   for (int i = 0; i < column_arr.size ();  i++)
174     {
175       Real notey = column_arr[i]->extent (Y_AXIS)[d] +
176         column_arr[i]->relative_coordinate (common, Y_AXIS)
177         ;
178       Real x = column_arr[i]->relative_coordinate (0, X_AXIS) - x0;
179       Real tuplety =  *dy * x * factor;
180
181       if (notey * d > (*offset + tuplety) * d)
182         *offset = notey - tuplety; 
183     }
184 }
185
186 /*
187   use first -> last note for slope,
188 */
189 void
190 Tuplet_spanner::calc_dy (Real * dy) const
191 {
192   Link_array<Note_column> column_arr=
193     Group_interface__extract_elements (this, (Note_column*)0, "columns");
194
195  
196   Direction d = directional_element (this).get ();
197   *dy = column_arr.top ()->extent (Y_AXIS) [d]
198     - column_arr[0]->extent (Y_AXIS) [d];
199 }
200
201 void
202 Tuplet_spanner::after_line_breaking ()
203 {
204   Link_array<Note_column> column_arr=
205     Group_interface__extract_elements (this, (Note_column*)0, "columns");
206
207   if (!column_arr.size ())
208     {
209       set_elt_property ("transparent", SCM_BOOL_T);
210       set_extent_callback (0, X_AXIS);
211       set_extent_callback (0, Y_AXIS);
212     }
213
214   Direction d = directional_element (this).get ();
215   if (!d)
216     {
217       d = get_default_dir ();
218       directional_element (this).set (d);
219
220     }
221   Real dy, offset;
222
223   calc_position_and_height  (&offset,&dy);
224   
225   set_elt_property ("delta-y", gh_double2scm (dy));
226
227   translate_axis (offset, Y_AXIS);
228   
229   if (scm_ilength (get_elt_property ("beams")) == 1)
230     {
231       SCM bs = get_elt_property ("beams");
232       Score_element *b = unsmob_element (gh_car (bs));
233       Beam * beam_l = dynamic_cast<Beam*> (b);
234       if (!broken_b () 
235           && get_bound (LEFT)->column_l () == beam_l->get_bound (LEFT)->column_l ()
236           && get_bound (RIGHT)->column_l () == beam_l->get_bound (RIGHT)->column_l ())
237         set_elt_property ("parallel-beam", SCM_BOOL_T);
238     }
239 }
240
241
242 Direction
243 Tuplet_spanner::get_default_dir () const
244 {
245   Direction d = UP;
246   SCM dir_sym =get_elt_property ("dir-forced");
247   if (isdir_b (dir_sym))
248     {
249       d= to_dir (dir_sym);
250       if (d != CENTER)
251         return d;
252     }
253
254   d = UP ;
255   for (SCM s = get_elt_property ("columns"); gh_pair_p (s); s = gh_cdr (s))
256     {
257       Score_element * sc = unsmob_element (gh_car (s));
258       Note_column * nc = dynamic_cast<Note_column*> (sc);
259       if (nc->dir () < 0) 
260         {
261           d = DOWN;
262           break;
263         }
264     }
265   
266   return d;
267 }
268
269 void
270 Tuplet_spanner::add_beam (Beam *b)
271 {
272   add_dependency (b);
273   Group_interface gi (this, "beams");
274   gi.add_element (b);
275 }
276
277 void
278 Tuplet_spanner::add_column (Note_column*n)
279 {
280   Group_interface gi (this, "columns");
281   gi.add_element (n);
282
283   add_dependency (n);
284 }
285
286