]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-spanner.cc
patch::: 1.3.99.jcn2
[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 "font-metric.hh"
14 #include "molecule.hh"
15 #include "paper-column.hh"
16 #include "paper-def.hh"
17 #include "text-item.hh"
18 #include "tuplet-spanner.hh"
19 #include "stem.hh"
20 #include "note-column.hh"
21 #include "dimensions.hh"
22 #include "group-interface.hh"
23 #include "directional-element-interface.hh"
24 #include "spanner.hh"
25
26 void
27 Tuplet_spanner::set_interface (Score_element*me)
28 {
29   me->set_interface (ly_symbol2scm ("tuplet-bracket"));
30 }
31
32 /*
33   TODO. 
34  */
35
36 MAKE_SCHEME_CALLBACK(Tuplet_spanner,brew_molecule,1);
37
38 SCM
39 Tuplet_spanner::brew_molecule (SCM smob) 
40 {
41   Score_element *me= unsmob_element (smob);
42   Molecule  mol;
43
44   // Default behaviour: number always, bracket when no beam!
45   bool par_beam = to_boolean (me->get_elt_property ("parallel-beam"));
46   bool bracket_visibility = !par_beam;
47   bool number_visibility = true;
48
49   SCM bracket = me->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 = me->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 (me->get_elt_property ("columns")))
66     {
67       Link_array<Score_element> column_arr=
68         Pointer_group_interface__extract_elements (me, (Score_element*)0, "columns");
69         
70       Real ncw = column_arr.top ()->extent(column_arr.top (), X_AXIS).length ();
71       Real w = dynamic_cast<Spanner*>(me)->spanner_length () + ncw;
72
73       Real staff_space = me->paper_l ()->get_var ("staffspace");
74       Direction dir = Directional_element_interface::get (me);
75       Real dy = gh_scm2double (me->get_elt_property ("delta-y"));
76       SCM number = me->get_elt_property ("text");
77       if (gh_string_p (number) && number_visibility)
78         {
79           SCM properties = gh_list ( me->mutable_property_alist_,
80                                      me->immutable_property_alist_,
81                                     
82                                     SCM_UNDEFINED);
83           Molecule num = Text_item::text2molecule (me, number, properties);
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 * commony = me->common_refpoint (me->get_elt_property ("columns"), Y_AXIS);
132   Score_element * commonx = me->common_refpoint (me->get_elt_property ("columns"), X_AXIS);  
133   
134   Direction d = Directional_element_interface::get (me);
135
136   /*
137     Use outer non-rest columns to determine slope
138    */
139   int l = 0;
140   while (l <column_arr.size() && Note_column::rest_b(column_arr[l]))
141     l ++;
142
143   int r = column_arr.size ()- 1;
144   while (r >= l && Note_column::rest_b(column_arr[r]))
145     r--;
146   
147   if (l < r)
148     {
149       *dy = column_arr[r]->extent (commony, Y_AXIS) [d]
150         - column_arr[l]->extent (commony, Y_AXIS) [d] ;
151     }
152   else
153     * dy = 0;
154
155
156   *offset = - d * infinity_f;
157
158   if (!column_arr.size ())
159     return;
160   
161   Real x0 = column_arr[0]->relative_coordinate (commonx, X_AXIS);
162   Real x1 = column_arr.top ()->relative_coordinate (commonx, X_AXIS);
163   
164   Real factor = column_arr.size () > 1 ? 1/(x1 - x0) : 1.0;
165   
166   for (int i = 0; i < column_arr.size ();  i++)
167     {
168       Real notey = column_arr[i]->extent (commony, Y_AXIS)[d] 
169         - me->relative_coordinate (commony, Y_AXIS);
170
171       Real x = column_arr[i]->relative_coordinate (commonx, X_AXIS) - x0;
172       Real tuplety =  *dy * x * factor;
173
174       if (notey * d > (*offset + tuplety) * d)
175         *offset = notey - tuplety; 
176     }
177 }
178
179 /*
180   use first -> last note for slope,
181 */
182 void
183 Tuplet_spanner::calc_dy (Score_element*me,Real * dy)
184 {
185   Link_array<Score_element> column_arr=
186     Pointer_group_interface__extract_elements (me, (Score_element*)0, "columns");
187
188   /*
189     ugh. refps.
190    */
191   Direction d = Directional_element_interface::get (me);
192   *dy = column_arr.top ()->extent (column_arr.top (), Y_AXIS) [d]
193     - column_arr[0]->extent (column_arr[0], Y_AXIS) [d];
194 }
195 MAKE_SCHEME_CALLBACK(Tuplet_spanner,after_line_breaking,1);
196
197 SCM
198 Tuplet_spanner::after_line_breaking (SCM smob)
199 {
200   Score_element * me = unsmob_element (smob);
201   Link_array<Note_column> column_arr=
202     Pointer_group_interface__extract_elements (me, (Note_column*)0, "columns");
203   Spanner *sp = dynamic_cast<Spanner*> (me);
204
205
206   if (!column_arr.size ())
207     {
208       me->suicide ();
209       return SCM_UNSPECIFIED;
210     }
211
212   Direction d = Directional_element_interface::get (me);
213   if (!d)
214     {
215       d = Tuplet_spanner::get_default_dir (me);
216       Directional_element_interface::set (me, d);
217
218     }
219   Real dy, offset;
220
221   calc_position_and_height (me,&offset,&dy);
222   
223   me->set_elt_property ("delta-y", gh_double2scm (dy));
224
225   me->translate_axis (offset, Y_AXIS);
226   
227   if (scm_ilength (me->get_elt_property ("beams")) == 1)
228     {
229       SCM bs = me->get_elt_property ("beams");
230       Score_element *b = unsmob_element (gh_car (bs));
231       Spanner * beam_l = dynamic_cast<Spanner *> (b);
232       if (!sp->broken_b () 
233           && sp->get_bound (LEFT)->column_l () == beam_l->get_bound (LEFT)->column_l ()
234           && sp->get_bound (RIGHT)->column_l () == beam_l->get_bound (RIGHT)->column_l ())
235         me->set_elt_property ("parallel-beam", SCM_BOOL_T);
236     }
237   return SCM_UNSPECIFIED;
238 }
239
240
241 Direction
242 Tuplet_spanner::get_default_dir (Score_element*me)
243 {
244   Direction d = UP;
245   SCM dir_sym =me->get_elt_property ("dir-forced");
246   if (isdir_b (dir_sym))
247     {
248       d= to_dir (dir_sym);
249       if (d != CENTER)
250         return d;
251     }
252
253   d = UP ;
254   for (SCM s = me->get_elt_property ("columns"); gh_pair_p (s); s = gh_cdr (s))
255     {
256       Score_element * nc = unsmob_element (gh_car (s));
257       if (Note_column::dir (nc) < 0) 
258         {
259           d = DOWN;
260           break;
261         }
262     }
263   
264   return d;
265 }
266
267 void
268 Tuplet_spanner::add_beam (Score_element*me, Score_element *b)
269 {
270   me->add_dependency (b);
271   Pointer_group_interface::add_element (me, "beams",b);
272 }
273
274 void
275 Tuplet_spanner::add_column (Score_element*me, Item*n)
276 {
277   Pointer_group_interface::add_element (me, "columns",n);
278   me->add_dependency (n);
279
280   add_bound_item (dynamic_cast<Spanner*> (me), n);
281 }
282
283