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