]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-spanner.cc
release: 1.3.68
[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
26 void
27 Tuplet_spanner::set_interface (Score_element*me)
28 {
29   me-> set_elt_pointer ("beams", SCM_EOL);
30   me->set_elt_pointer ("columns", SCM_EOL);
31 }
32
33 /*
34   TODO. 
35  */
36
37 MAKE_SCHEME_SCORE_ELEMENT_CALLBACK(Tuplet_spanner,brew_molecule);
38
39 SCM
40 Tuplet_spanner::brew_molecule (SCM smob) 
41 {
42   Score_element *me= unsmob_element (smob);
43   Molecule  mol;
44
45   // Default behaviour: number always, bracket when no beam!
46   bool par_beam = to_boolean (me->get_elt_property ("parallel-beam"));
47   bool bracket_visibility = !par_beam;
48   bool number_visibility = true;
49
50   SCM bracket = me->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 = me->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 (me->get_elt_pointer ("columns")))
67     {
68       Link_array<Note_column> column_arr=
69         Pointer_group_interface__extract_elements (me, (Note_column*)0, "columns");
70         
71       Real ncw = column_arr.top ()->extent(X_AXIS).length ();
72       Real w = dynamic_cast<Spanner*>(me)->spanner_length () + ncw;
73
74       Real staff_space = me->paper_l ()->get_var ("interline");
75       Direction dir = Directional_element_interface (me).get ();
76       Real dy = gh_scm2double (me->get_elt_property ("delta-y"));
77       SCM number = me->get_elt_property ("text");
78       if (gh_string_p (number) && number_visibility)
79         {
80           Molecule
81             num (me->lookup_l ()->text ("italic",
82                                     ly_scm2string (number), me->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       if (bracket_visibility)      
94         {
95           SCM ss = me->paper_l ()->get_scmvar ("staffspace");
96           SCM lt =  me->paper_l ()->get_scmvar ("stafflinethickness");
97           
98           SCM thick = me->get_elt_property ("thick");
99           SCM gap = me->get_elt_property ("number-gap");
100           
101           SCM at =gh_list(ly_symbol2scm ("tuplet"),
102                           ss,
103                           scm_product (gap, ss),
104                           gh_double2scm (w),
105                           gh_double2scm (dy),
106                           scm_product (thick, lt),
107                           gh_int2scm (dir),
108                           SCM_UNDEFINED);
109
110           Box b;
111           mol.add_molecule (Molecule (b, at));
112         }
113     }
114   return mol.create_scheme();
115 }
116
117
118
119
120 /*
121   use first -> last note for slope, and then correct for disturbing
122   notes in between.  */
123 void
124 Tuplet_spanner::calc_position_and_height (Score_element*me,Real *offset, Real * dy) 
125 {
126   Link_array<Note_column> column_arr=
127     Pointer_group_interface__extract_elements (me, (Note_column*)0, "columns");
128
129
130   Score_element * common = me->common_refpoint (me->get_elt_pointer ("columns"), Y_AXIS);
131   
132   Direction d = Directional_element_interface (me).get ();
133
134   /*
135     Use outer non-rest columns to determine slope
136    */
137   int l = 0;
138   while (l <column_arr.size() && column_arr[l]->rest_b())
139     l ++;
140
141   int r = column_arr.size ()- 1;
142   while (r >= l && column_arr[r]->rest_b())
143     r--;
144   
145   if (l < r)
146     {
147       *dy = column_arr[r]->extent (Y_AXIS) [d] + column_arr[r]->relative_coordinate (common, Y_AXIS)
148         - column_arr[l]->extent (Y_AXIS) [d] - column_arr[l]->relative_coordinate (common, Y_AXIS);
149     }
150   else
151     * dy = 0;
152
153
154   *offset = - d * infinity_f;
155
156   if (!column_arr.size ())
157     return;
158   
159   Real x0 = column_arr[0]->relative_coordinate (0, X_AXIS);
160   Real x1 = column_arr.top ()->relative_coordinate (0, X_AXIS);
161   
162   Real factor = column_arr.size () > 1 ? 1/(x1 - x0) : 1.0;
163   
164   for (int i = 0; i < column_arr.size ();  i++)
165     {
166       Real notey = column_arr[i]->extent (Y_AXIS)[d] +
167         column_arr[i]->relative_coordinate (common, Y_AXIS)
168         ;
169       Real x = column_arr[i]->relative_coordinate (0, X_AXIS) - x0;
170       Real tuplety =  *dy * x * factor;
171
172       if (notey * d > (*offset + tuplety) * d)
173         *offset = notey - tuplety; 
174     }
175 }
176
177 /*
178   use first -> last note for slope,
179 */
180 void
181 Tuplet_spanner::calc_dy (Score_element*me,Real * dy)
182 {
183   Link_array<Note_column> column_arr=
184     Pointer_group_interface__extract_elements (me, (Note_column*)0, "columns");
185
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_SCORE_ELEMENT_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_pointer ("beams")) == 1)
224     {
225       SCM bs = me->get_elt_pointer ("beams");
226       Score_element *b = unsmob_element (gh_car (bs));
227       Beam * beam_l = dynamic_cast<Beam*> (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_pointer ("columns"); gh_pair_p (s); s = gh_cdr (s))
251     {
252       Score_element * sc = unsmob_element (gh_car (s));
253       Note_column * nc = dynamic_cast<Note_column*> (sc);
254       if (nc->dir () < 0) 
255         {
256           d = DOWN;
257           break;
258         }
259     }
260   
261   return d;
262 }
263
264 void
265 Tuplet_spanner::add_beam (Score_element*me, Score_element *b)
266 {
267 me->add_dependency (b);
268   Pointer_group_interface gi (me, "beams");
269   gi.add_element (b);
270 }
271
272 void
273 Tuplet_spanner::add_column (Score_element*me, Item*n)
274 {
275   Pointer_group_interface gi (me, "columns");
276   gi.add_element (n);
277   me->add_dependency (n);
278
279   add_bound_item (dynamic_cast<Spanner*> (me), n);
280 }
281
282