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