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