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