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