]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-bracket.cc
f3ec69bc998bde30e4af057026a3b5505eda1df9
[lilypond.git] / lily / tuplet-bracket.cc
1 /*
2   plet-spanner.cc -- implement Tuplet_bracket
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9
10 #include "beam.hh"
11 #include "box.hh"
12 #include "debug.hh"
13 #include "font-interface.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 /*
27   TODO:
28   staff-line collisions for horizontal tuplet brackets.
29  */
30
31
32
33 void
34 Tuplet_bracket::set_interface (Grob*me)
35 {
36   me->set_interface (ly_symbol2scm ("tuplet-bracket"));
37 }
38
39 /*
40   TODO: use stem->beam fields to find Beams. Autobeams aren't found
41   through the engraver mechanism.  */
42 MAKE_SCHEME_CALLBACK (Tuplet_bracket,brew_molecule,1);
43 SCM
44 Tuplet_bracket::brew_molecule (SCM smob) 
45 {
46   Grob *me= unsmob_grob (smob);
47   Molecule  mol;
48
49   // Default behaviour: number always, bracket when no beam!
50   bool par_beam = to_boolean (me->get_grob_property ("parallel-beam"));
51   bool bracket_visibility = !par_beam;
52   bool number_visibility = true;
53
54   SCM bracket = me->get_grob_property ("tuplet-bracket-visibility");
55   if (gh_boolean_p (bracket))
56     {
57       bracket_visibility = gh_scm2bool (bracket);
58     }
59   else if (bracket == ly_symbol2scm ("if-no-beam"))
60     bracket_visibility = !par_beam;
61
62   SCM numb = me->get_grob_property ("tuplet-number-visibility");  
63   if (gh_boolean_p (numb))
64     {
65       number_visibility = gh_scm2bool (numb);
66     }
67   else if (bracket == ly_symbol2scm ("if-no-beam"))
68     number_visibility = !par_beam;
69   
70   if (gh_pair_p (me->get_grob_property ("columns")))
71     {
72       Link_array<Grob> column_arr=
73         Pointer_group_interface__extract_elements (me, (Grob*)0, "columns");
74         
75       Real ncw = column_arr.top ()->extent (column_arr.top (), X_AXIS).length ();
76       Real w = dynamic_cast<Spanner*> (me)->spanner_length () + ncw;
77
78       Real staff_space = 1.0;
79       Direction dir = Directional_element_interface::get (me);
80       Real dy = gh_scm2double (me->get_grob_property ("delta-y"));
81       SCM number = me->get_grob_property ("text");
82       if (gh_string_p (number) && number_visibility)
83         {
84           SCM properties = Font_interface::font_alist_chain (me);
85           Molecule num = Text_item::text2molecule (me, number, properties);
86           num.align_to (X_AXIS, CENTER);
87           num.translate_axis (w/2, X_AXIS);
88           num.align_to (Y_AXIS, CENTER);
89           num.translate_axis (dir * staff_space, Y_AXIS);
90         
91           num.translate_axis (dy/2, Y_AXIS);
92
93           mol.add_molecule (num);
94         }
95       
96       if (bracket_visibility)      
97         {
98           Real  lt =  me->paper_l ()->get_var ("stafflinethickness");
99           
100           SCM thick = me->get_grob_property ("thick");
101           SCM gap = me->get_grob_property ("number-gap");
102           
103           SCM at =gh_list (ly_symbol2scm ("tuplet"),
104                           gh_double2scm (1.0),
105                           gap,
106                           gh_double2scm (w),
107                           gh_double2scm (dy),
108                           gh_double2scm (gh_scm2double (thick)* lt),
109                           gh_int2scm (dir),
110                           SCM_UNDEFINED);
111
112           Box b;
113           mol.add_molecule (Molecule (b, at));
114         }
115     }
116   return mol.smobbed_copy ();
117 }
118
119
120
121
122 /*
123   use first -> last note for slope, and then correct for disturbing
124   notes in between.  */
125 void
126 Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy) 
127 {
128   Link_array<Grob> column_arr=
129     Pointer_group_interface__extract_elements (me, (Grob*)0, "columns");
130
131
132   Grob * commony = me->common_refpoint (me->get_grob_property ("columns"), Y_AXIS);
133   Grob * commonx = me->common_refpoint (me->get_grob_property ("columns"), X_AXIS);  
134   
135   Direction d = Directional_element_interface::get (me);
136
137   /*
138     Use outer non-rest columns to determine slope
139    */
140   int l = 0;
141   while (l <column_arr.size () && Note_column::rest_b (column_arr[l]))
142     l ++;
143
144   int r = column_arr.size ()- 1;
145   while (r >= l && Note_column::rest_b (column_arr[r]))
146     r--;
147   
148   if (l < r)
149     {
150       *dy = column_arr[r]->extent (commony, Y_AXIS) [d]
151         - column_arr[l]->extent (commony, Y_AXIS) [d] ;
152     }
153   else
154     * dy = 0;
155
156
157   *offset = - d * infinity_f;
158
159   if (!column_arr.size ())
160     return;
161   
162   Real x0 = column_arr[0]->relative_coordinate (commonx, X_AXIS);
163   Real x1 = column_arr.top ()->relative_coordinate (commonx, X_AXIS);
164   
165   Real factor = column_arr.size () > 1 ? 1/ (x1 - x0) : 1.0;
166   
167   for (int i = 0; i < column_arr.size ();  i++)
168     {
169       Real notey = column_arr[i]->extent (commony, Y_AXIS)[d] 
170         - me->relative_coordinate (commony, Y_AXIS);
171
172       Real x = column_arr[i]->relative_coordinate (commonx, X_AXIS) - x0;
173       Real tuplety =  *dy * x * factor;
174
175       if (notey * d > (*offset + tuplety) * d)
176         *offset = notey - tuplety; 
177     }
178 }
179
180 /*
181   use first -> last note for slope,
182 */
183 void
184 Tuplet_bracket::calc_dy (Grob*me,Real * dy)
185 {
186   Link_array<Grob> column_arr=
187     Pointer_group_interface__extract_elements (me, (Grob*)0, "columns");
188
189   /*
190     ugh. refps.
191    */
192   Direction d = Directional_element_interface::get (me);
193   *dy = column_arr.top ()->extent (column_arr.top (), Y_AXIS) [d]
194     - column_arr[0]->extent (column_arr[0], Y_AXIS) [d];
195 }
196 MAKE_SCHEME_CALLBACK (Tuplet_bracket,after_line_breaking,1);
197
198 SCM
199 Tuplet_bracket::after_line_breaking (SCM smob)
200 {
201   Grob * me = unsmob_grob (smob);
202   Link_array<Note_column> column_arr=
203     Pointer_group_interface__extract_elements (me, (Note_column*)0, "columns");
204   Spanner *sp = dynamic_cast<Spanner*> (me);
205
206
207   if (!column_arr.size ())
208     {
209       me->suicide ();
210       return SCM_UNSPECIFIED;
211     }
212
213   Direction d = Directional_element_interface::get (me);
214   if (!d)
215     {
216       d = Tuplet_bracket::get_default_dir (me);
217       Directional_element_interface::set (me, d);
218
219     }
220   Real dy, offset;
221
222   calc_position_and_height (me,&offset,&dy);
223   
224   me->set_grob_property ("delta-y", gh_double2scm (dy));
225
226   me->translate_axis (offset, Y_AXIS);
227   
228   if (scm_ilength (me->get_grob_property ("beams")) == 1)
229     {
230       SCM bs = me->get_grob_property ("beams");
231       Grob *b = unsmob_grob (gh_car (bs));
232       Spanner * beam_l = dynamic_cast<Spanner *> (b);
233       if (!sp->broken_b () 
234           && sp->get_bound (LEFT)->column_l () == beam_l->get_bound (LEFT)->column_l ()
235           && sp->get_bound (RIGHT)->column_l () == beam_l->get_bound (RIGHT)->column_l ())
236         me->set_grob_property ("parallel-beam", SCM_BOOL_T);
237     }
238   return SCM_UNSPECIFIED;
239 }
240
241
242 Direction
243 Tuplet_bracket::get_default_dir (Grob*me)
244 {
245   Direction d = UP;
246   SCM dir_sym =me->get_grob_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 = me->get_grob_property ("columns"); gh_pair_p (s); s = gh_cdr (s))
256     {
257       Grob * nc = unsmob_grob (gh_car (s));
258       if (Note_column::dir (nc) < 0) 
259         {
260           d = DOWN;
261           break;
262         }
263     }
264   
265   return d;
266 }
267
268 void
269 Tuplet_bracket::add_beam (Grob*me, Grob *b)
270 {
271   me->add_dependency (b);
272   Pointer_group_interface::add_element (me, "beams",b);
273 }
274
275 void
276 Tuplet_bracket::add_column (Grob*me, Item*n)
277 {
278   Pointer_group_interface::add_element (me, "columns",n);
279   me->add_dependency (n);
280
281   add_bound_item (dynamic_cast<Spanner*> (me), n);
282 }
283
284