]> 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 /*
10   TODO:
11
12   - tuplet bracket should probably be subject to the same rules as
13   beam sloping/quanting.
14
15   - There is no support for kneed brackets, or nested brackets.
16
17   - number placement for parallel beams should be much more advanced:
18     for sloped beams some extra horizontal offset must be introduced.
19
20   - number placement is usually done over the center note, not the
21     graphical center.
22   
23  */
24
25 #include <math.h>
26
27 #include "beam.hh"
28 #include "box.hh"
29 #include "debug.hh"
30 #include "font-interface.hh"
31 #include "molecule.hh"
32 #include "paper-def.hh"
33 #include "text-item.hh"
34 #include "tuplet-bracket.hh"
35 #include "stem.hh"
36 #include "note-column.hh"
37 #include "group-interface.hh"
38 #include "directional-element-interface.hh"
39 #include "spanner.hh"
40 #include "staff-symbol-referencer.hh"
41 #include "lookup.hh"
42
43
44 static Grob*
45 get_x_bound_grob (Grob *g, Direction my_dir)
46 {
47   if (Note_column::stem_l (g)
48       && Note_column::dir (g) == my_dir)
49     {
50       g = Note_column::stem_l (g);
51     }
52   return g;
53 }
54
55
56
57 Grob*
58 Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> const &cols, bool *equally_long)
59 {
60   /*
61     ugh: code dup. 
62   */
63   Grob *s1 = Note_column::stem_l (cols[0]); 
64   Grob *s2 = Note_column::stem_l (cols.top());    
65
66   Grob*b1 = s1 ? Stem::beam_l (s1) : 0;
67   Grob*b2 = s2 ? Stem::beam_l (s2) : 0;
68   
69   Spanner*sp = dynamic_cast<Spanner*> (me);  
70
71   *equally_long= false;
72   if (! ( b1 && (b1 == b2) && !sp->broken_b() ))
73       return 0;
74
75   Link_array<Grob> beam_stems = Pointer_group_interface__extract_grobs
76     (b1, (Grob*)0, "stems");
77
78   
79   *equally_long = (beam_stems[0] == s1 && beam_stems.top() == s2);
80   return b1;
81 }
82
83
84 /*
85   TODO:
86
87   in the case that there is no bracket, but there is a (single) beam,
88   follow beam precisely for determining tuplet number location.
89   
90  */
91 MAKE_SCHEME_CALLBACK (Tuplet_bracket,brew_molecule,1);
92 SCM
93 Tuplet_bracket::brew_molecule (SCM smob) 
94 {
95   Grob *me= unsmob_grob (smob);
96   Molecule  mol;
97   Link_array<Grob> column_arr=
98     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
99
100   if (!column_arr.size ())
101     return mol.smobbed_copy ();
102
103   bool equally_long = false;
104   Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
105
106   Spanner*sp = dynamic_cast<Spanner*> (me);  
107
108   bool bracket_visibility = !(par_beam && equally_long);
109   bool number_visibility = true;
110
111   /*
112     Fixme: the type of this prop is sucky.
113    */
114   SCM bracket = me->get_grob_property ("bracket-visibility");
115   if (gh_boolean_p (bracket))
116     {
117       bracket_visibility = gh_scm2bool (bracket);
118     }
119   else if (bracket == ly_symbol2scm ("if-no-beam"))
120     bracket_visibility = !par_beam;
121
122   SCM numb = me->get_grob_property ("number-visibility");  
123   if (gh_boolean_p (numb))
124     {
125       number_visibility = gh_scm2bool (numb);
126     }
127   else if (numb == ly_symbol2scm ("if-no-beam"))
128     number_visibility = !par_beam;
129         
130   Grob * commonx = column_arr[0]->common_refpoint (column_arr.top (),X_AXIS);
131   Direction dir = Directional_element_interface::get (me);
132
133   Grob * lgr = get_x_bound_grob (column_arr[0], dir);
134   Grob * rgr = get_x_bound_grob (column_arr.top(), dir);  
135   Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
136   Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
137
138   Real w = x1 -x0;
139
140   Real ly = gh_scm2double (me->get_grob_property ("left-position"));
141   Real ry = gh_scm2double (me->get_grob_property ("right-position"));  
142   SCM number = me->get_grob_property ("text");
143   
144   if (gh_string_p (number) && number_visibility)
145     {
146       SCM properties = Font_interface::font_alist_chain (me);
147       Molecule num = Text_item::text2molecule (me, number, properties);
148       num.align_to (X_AXIS, CENTER);
149       num.translate_axis (w/2, X_AXIS);
150       num.align_to (Y_AXIS, CENTER);
151         
152       num.translate_axis ((ry-ly)/2, Y_AXIS);
153
154       mol.add_molecule (num);
155     }
156       
157   if (bracket_visibility)      
158     {
159       Real  lt =  me->paper_l ()->get_var ("linethickness");
160   
161       SCM thick = me->get_grob_property ("thickness");
162       if (gh_number_p (thick))
163         lt *= gh_scm2double (thick);
164       
165       SCM gap = me->get_grob_property ("gap");
166       SCM ew = me->get_grob_property ("edge-width");
167       SCM eh = me->get_grob_property ("edge-height");
168       SCM sp = me->get_grob_property ("shorten-pair");
169       
170       Direction d = LEFT;
171       Drul_array<Real> height, width, shorten;
172       do {
173         width[d] =  height[d] = shorten[d] = 0.0;
174         if ( ly_number_pair_p (ew) )
175           width[d] +=  gh_scm2double (index_cell (ew, d)) * d;
176         if ( ly_number_pair_p (eh) )
177           height[d] += gh_scm2double (index_cell (eh, d));
178         if ( ly_number_pair_p (sp) )
179           shorten[d] +=  gh_scm2double (index_cell (sp, d));
180       }
181       while (flip (&d) != LEFT);
182       
183       Molecule brack = make_bracket (Y_AXIS,
184                                      w, ry - ly, lt,
185                                      -height[LEFT]*dir, -height[RIGHT]*dir,
186                                      gh_scm2double (gap),
187                                      width[LEFT], width[RIGHT],
188                                      shorten[LEFT], shorten[RIGHT]);
189       mol.add_molecule (brack);
190     }
191   
192   mol.translate_axis (ly, Y_AXIS);
193   mol.translate_axis (x0  - sp->get_bound (LEFT)->relative_coordinate (commonx,X_AXIS),X_AXIS);
194   return mol.smobbed_copy ();
195 }
196
197 /*
198   should move to lookup?
199
200   argh. this interface is confusing : we have a shorten as well as a
201   widen.
202
203   arg. should use drul_arrays here.
204  */
205 Molecule
206 Tuplet_bracket::make_bracket (Axis protusion_axis,
207                               Real dx, Real dy, Real thick, Real left_height,
208                               Real right_height,
209                               Real gap,
210                               Real left_widen, Real right_widen,
211                               Real left_shorten, Real right_shorten)
212 {
213   Real len = Offset (dx,dy).length ();
214   Real gapx = dx * (gap /  len);
215   Real gapy = dy * (gap /  len);
216   Real lshortx = dx * (left_shorten /  len);
217   Real lshorty = dy * (left_shorten /  len);
218   Real rshortx = dx * (right_shorten /  len);
219   Real rshorty = dy * (right_shorten /  len);
220   Axis other = other_axis (protusion_axis);
221   
222   Molecule l1 = Lookup::line (thick, Offset(lshortx, lshorty),
223                               Offset ( (dx - gapx)/2, (dy - gapy)/2 ));
224
225   Molecule l2 = Lookup::line (thick, Offset((dx + gapx) / 2,(dy + gapy) / 2),
226                               Offset (dx - rshortx, dy - rshorty));
227
228   Offset protusion;
229   protusion[other] = -left_widen;
230   protusion[protusion_axis] = left_height;
231   Molecule p1 = Lookup::line (thick, 
232                               Offset(lshortx, lshorty), 
233                               Offset(lshortx, lshorty) + protusion);
234   protusion[other] = right_widen;
235   protusion[protusion_axis] = right_height;
236   Molecule p2 = Lookup::line (thick, 
237                               Offset(dx - rshortx, dy - rshorty), 
238                               Offset(dx - rshortx, dy - rshorty) + protusion);  
239
240   Molecule m;
241   m.add_molecule (p1);
242   m.add_molecule (p2);
243   m.add_molecule (l1);
244   m.add_molecule (l2);
245
246   return m;  
247 }
248
249
250 /*
251   use first -> last note for slope, and then correct for disturbing
252   notes in between.  */
253 void
254 Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy) 
255 {
256   Link_array<Grob> column_arr=
257     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
258
259
260   SCM cols = me->get_grob_property ("note-columns");
261   Grob * commony = common_refpoint_of_list (cols, me, Y_AXIS);
262   Grob * commonx = common_refpoint_of_list (cols, me, X_AXIS);  
263   
264   Direction dir = Directional_element_interface::get (me);
265
266   /*
267     Use outer non-rest columns to determine slope
268    */
269   int l = 0;
270   while (l <column_arr.size () && Note_column::rest_b (column_arr[l]))
271     l ++;
272
273   int r = column_arr.size ()- 1;
274   while (r >= l && Note_column::rest_b (column_arr[r]))
275     r--;
276   
277   if (l < r)
278     {
279       *dy = column_arr[r]->extent (commony, Y_AXIS) [dir]
280         - column_arr[l]->extent (commony, Y_AXIS) [dir] ;
281     }
282   else
283     * dy = 0;
284
285
286   *offset = - dir * infinity_f;
287
288   if (!column_arr.size ())
289     return;
290
291
292   
293   Grob * lgr = get_x_bound_grob (column_arr[0], dir);
294   Grob * rgr = get_x_bound_grob (column_arr.top(), dir);  
295   Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
296   Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
297
298
299     /*
300       Slope.
301     */
302   Real factor = column_arr.size () > 1 ? 1/ (x1 - x0) : 1.0;
303   
304   for (int i = 0; i < column_arr.size ();  i++)
305     {
306       Real notey = column_arr[i]->extent (commony, Y_AXIS)[dir] 
307         - me->relative_coordinate (commony, Y_AXIS);
308
309       Real x = column_arr[i]->relative_coordinate (commonx, X_AXIS) - x0;
310       Real tuplety =  *dy * x * factor;
311
312       if (notey * dir > (*offset + tuplety) * dir)
313         *offset = notey - tuplety; 
314     }
315
316   // padding
317   *offset +=  gh_scm2double (me->get_grob_property ("padding")) *dir;
318
319   
320   /*
321     horizontal brackets should not collide with staff lines.
322    */
323   if (*dy == 0)
324     {
325       // quantize, then do collision check.
326       Real ss= Staff_symbol_referencer::staff_space (me);
327       *offset *= 2 / ss;
328       
329       *offset = rint (*offset);
330       if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
331         *offset += dir;
332
333       *offset *= 0.5 * ss;
334     }
335   
336 }
337
338 /*
339   use first -> last note for slope,
340 */
341 void
342 Tuplet_bracket::calc_dy (Grob*me,Real * dy)
343 {
344   Link_array<Grob> column_arr=
345     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
346
347   /*
348     ugh. refps.
349    */
350   Direction d = Directional_element_interface::get (me);
351   *dy = column_arr.top ()->extent (column_arr.top (), Y_AXIS) [d]
352     - column_arr[0]->extent (column_arr[0], Y_AXIS) [d];
353 }
354
355
356 /*
357   We depend on the beams if there are any.
358  */
359 MAKE_SCHEME_CALLBACK (Tuplet_bracket,before_line_breaking,1);
360 SCM
361 Tuplet_bracket::before_line_breaking (SCM smob)
362 {
363   Grob *me = unsmob_grob (smob);
364   Link_array<Grob> column_arr=
365     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
366
367
368   for (int i = column_arr.size(); i--;)
369     {
370       Grob * s =Note_column::stem_l (column_arr[i]);
371       Grob * b = s ? Stem::beam_l (s): 0;
372       if (b)
373         me->add_dependency (b);
374     }
375   return SCM_UNDEFINED;
376 }
377
378 MAKE_SCHEME_CALLBACK (Tuplet_bracket,after_line_breaking,1);
379
380 SCM
381 Tuplet_bracket::after_line_breaking (SCM smob)
382 {
383   Grob * me = unsmob_grob (smob);
384   Link_array<Grob> column_arr=
385     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
386
387   if (!column_arr.size ())
388     {
389       me->suicide ();
390       return SCM_UNSPECIFIED;
391     }
392   if (dynamic_cast<Spanner*> (me)->broken_b ())
393     {
394       me->warning ( "Tuplet_bracket was across linebreak. Farewell cruel world.");
395       me->suicide();
396       return SCM_UNSPECIFIED;
397     }
398   
399   Direction dir = Directional_element_interface::get (me);
400   if (!dir)
401     {
402       dir = Tuplet_bracket::get_default_dir (me);
403       Directional_element_interface::set (me, dir);
404     }
405   
406   bool equally_long = false;
407   Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
408
409   /*
410     We follow the beam only if there is one, and we are next to it.
411    */
412   Real dy, offset;
413   if (!par_beam
414       || Directional_element_interface::get (par_beam) != dir)
415     {
416       calc_position_and_height (me,&offset,&dy);
417     }
418   else
419     {
420       SCM ps =  par_beam->get_grob_property ("positions"); 
421
422       Real lp = gh_scm2double (gh_car (ps));
423       Real rp = gh_scm2double (gh_cdr (ps));
424
425       /*
426         duh. magic.
427        */
428       offset = lp + dir * (0.5 + gh_scm2double (me->get_grob_property ("padding")));
429       dy = rp- lp;
430     }
431   
432   
433   SCM lp =  me->get_grob_property ("left-position");
434   SCM rp = me->get_grob_property ("right-position");  
435   
436   if (gh_number_p (lp) && !gh_number_p (rp))
437     {
438       rp = gh_double2scm (gh_scm2double (lp) + dy);
439     }
440   else if (gh_number_p (rp) && !gh_number_p (lp))
441     {
442       lp = gh_double2scm (gh_scm2double (rp) - dy);
443     }
444   else if (!gh_number_p (rp) && !gh_number_p (lp))
445     {
446       lp = gh_double2scm (offset);
447       rp = gh_double2scm (offset +dy);
448     }
449
450   me->set_grob_property ("left-position", lp);
451   me->set_grob_property ("right-position", rp);
452
453   return SCM_UNSPECIFIED;
454 }
455
456
457 /*
458   similar to slur.
459  */
460 Direction
461 Tuplet_bracket::get_default_dir (Grob*me)
462 {
463   Direction d = UP;
464   for (SCM s = me->get_grob_property ("note-columns"); gh_pair_p (s); s = ly_cdr (s))
465     {
466       Grob * nc = unsmob_grob (ly_car (s));
467       if (Note_column::dir (nc) < 0) 
468         {
469           d = DOWN;
470           break;
471         }
472     }
473   return d;
474 }
475
476 void
477 Tuplet_bracket::add_column (Grob*me, Item*n)
478 {
479   Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
480   me->add_dependency (n);
481
482   add_bound_item (dynamic_cast<Spanner*> (me), n);
483 }
484
485
486
487
488
489
490 ADD_INTERFACE (Tuplet_bracket,"tuplet-bracket-interface",
491   "A bracket with a number in the middle, used for tuplets.",
492   "note-columns edge-width edge-height shorten-pair padding gap left-position right-position bracket-visibility number-visibility thickness direction");
493