]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-spanner.cc
release: 1.3.56
[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 (SCM s)
26   : Spanner (s)
27 {
28   set_elt_pointer ("beams", SCM_EOL);
29   set_elt_pointer ("columns", SCM_EOL);
30
31   // ugh.
32   set_elt_property ("delta-y", gh_int2scm (0));
33 }
34
35 /*
36   TODO. 
37  */
38 MAKE_SCHEME_SCORE_ELEMENT_CALLBACKS(Tuplet_spanner)
39 Molecule 
40 Tuplet_spanner::do_brew_molecule () const
41 {
42   Molecule  mol;
43
44   // Default behaviour: number always, bracket when no beam!
45   bool par_beam = to_boolean (get_elt_property ("parallel-beam"));
46   bool bracket_visibility = !par_beam;
47   bool number_visibility = true;
48
49   SCM bracket = get_elt_property ("tuplet-bracket-visibility");
50   if (gh_boolean_p (bracket))
51     {
52       bracket_visibility = gh_scm2bool (bracket);
53     }
54   else if (bracket == ly_symbol2scm ("if-no-beam"))
55     bracket_visibility = !par_beam;
56
57   SCM numb = get_elt_property ("tuplet-number-visibility");  
58   if (gh_boolean_p (numb))
59     {
60       number_visibility = gh_scm2bool (numb);
61     }
62   else if (bracket == ly_symbol2scm ("if-no-beam"))
63     number_visibility = !par_beam;
64   
65   if (gh_pair_p (get_elt_pointer ("columns")))
66     {
67       Link_array<Note_column> column_arr=
68         Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
69         
70       Real ncw = column_arr.top ()->extent(X_AXIS).length ();
71       Real w = spanner_length () + ncw;
72
73
74       Real staff_space = paper_l ()->get_var ("interline");
75       Direction dir = directional_element (this).get ();
76       Real dy = gh_scm2double (get_elt_property ("delta-y"));
77       SCM number = get_elt_property ("text");
78       if (gh_string_p (number) && number_visibility)
79         {
80           Molecule
81             num (lookup_l ()->text ("italic",
82                                     ly_scm2string (number), paper_l ()));
83           num.align_to (X_AXIS, CENTER);
84           num.translate_axis (w/2, X_AXIS);
85           num.align_to (Y_AXIS, CENTER);
86           num.translate_axis (dir * staff_space, Y_AXIS);
87         
88           num.translate_axis (dy/2, Y_AXIS);
89
90           mol.add_molecule (num);
91         }
92       
93       Real thick = paper_l ()->get_var ("tuplet_thick");
94       if (bracket_visibility)      
95         {
96           Real gap = paper_l () -> get_var ("tuplet_spanner_gap");
97           Real height = staff_space;
98           SCM at =gh_list(ly_symbol2scm ("tuplet"),
99                                        gh_double2scm (height),
100                                        gh_double2scm (gap),
101                                        gh_double2scm (w),
102                                        gh_double2scm (dy),
103                                        gh_double2scm (thick),
104                                        gh_int2scm (dir),
105                                        SCM_UNDEFINED);
106
107           Box b;
108           mol.add_molecule (Molecule (b, at));
109         }
110
111       //mol.translate_axis (dir * staff_space, Y_AXIS);
112     }
113   return mol;
114 }
115   
116 void
117 Tuplet_spanner::do_add_processing ()
118 {
119   if (gh_pair_p (get_elt_pointer ("columns")))
120     {
121       Link_array<Note_column> column_arr=
122         Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
123       
124       set_bound (LEFT, column_arr[0]);
125       set_bound (RIGHT, column_arr.top ());  
126     }
127 }
128
129
130
131 /*
132   use first -> last note for slope, and then correct for disturbing
133   notes in between.  */
134 void
135 Tuplet_spanner::calc_position_and_height (Real *offset, Real * dy) const
136 {
137   Link_array<Note_column> column_arr=
138     Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
139
140
141   Score_element * common = common_refpoint (get_elt_pointer ("columns"), Y_AXIS);
142   
143   Direction d = directional_element (this).get ();
144
145   /*
146     Use outer non-rest columns to determine slope
147    */
148   int l = 0;
149   while (l <column_arr.size() && column_arr[l]->rest_b())
150     l ++;
151
152   int r = column_arr.size ()- 1;
153   while (r >= l && column_arr[r]->rest_b())
154     r--;
155   
156   if (l < r)
157     {
158       *dy = column_arr[r]->extent (Y_AXIS) [d] + column_arr[r]->relative_coordinate (common, Y_AXIS)
159         - column_arr[l]->extent (Y_AXIS) [d] - column_arr[l]->relative_coordinate (common, Y_AXIS);
160     }
161   else
162     * dy = 0;
163
164
165   *offset = - d * infinity_f;
166
167   if (!column_arr.size ())
168     return;
169   
170   Real x0 = column_arr[0]->relative_coordinate (0, X_AXIS);
171   Real x1 = column_arr.top ()->relative_coordinate (0, X_AXIS);
172   
173   Real factor = column_arr.size () > 1 ? 1/(x1 - x0) : 1.0;
174   
175   for (int i = 0; i < column_arr.size ();  i++)
176     {
177       Real notey = column_arr[i]->extent (Y_AXIS)[d] +
178         column_arr[i]->relative_coordinate (common, Y_AXIS)
179         ;
180       Real x = column_arr[i]->relative_coordinate (0, X_AXIS) - x0;
181       Real tuplety =  *dy * x * factor;
182
183       if (notey * d > (*offset + tuplety) * d)
184         *offset = notey - tuplety; 
185     }
186 }
187
188 /*
189   use first -> last note for slope,
190 */
191 void
192 Tuplet_spanner::calc_dy (Real * dy) const
193 {
194   Link_array<Note_column> column_arr=
195     Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
196
197  
198   Direction d = directional_element (this).get ();
199   *dy = column_arr.top ()->extent (Y_AXIS) [d]
200     - column_arr[0]->extent (Y_AXIS) [d];
201 }
202
203 void
204 Tuplet_spanner::after_line_breaking ()
205 {
206   Link_array<Note_column> column_arr=
207     Pointer_group_interface__extract_elements (this, (Note_column*)0, "columns");
208
209   if (!column_arr.size ())
210     {
211       suicide ();
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_pointer ("beams")) == 1)
230     {
231       SCM bs = get_elt_pointer ("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_pointer ("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   Pointer_group_interface gi (this, "beams");
274   gi.add_element (b);
275 }
276
277 void
278 Tuplet_spanner::add_column (Note_column*n)
279 {
280   Pointer_group_interface gi (this, "columns");
281   gi.add_element (n);
282
283   add_dependency (n);
284 }
285
286