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