]> 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 ( gh_pair_p (ew) )
175           width[d] +=  gh_scm2double (index_cell (ew, d)) * d;
176         if ( gh_pair_p (eh) )
177           height[d] += gh_scm2double (index_cell (eh, d));
178         if ( gh_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       mol.translate_axis (dir * max(height[LEFT], height[RIGHT]), Y_AXIS);
191     }
192
193   mol.translate_axis (ly, Y_AXIS);
194   mol.translate_axis (x0  - sp->get_bound (LEFT)->relative_coordinate (commonx,X_AXIS),X_AXIS);
195   return mol.smobbed_copy ();
196 }
197
198 /*
199   should move to lookup?
200  */
201 Molecule
202 Tuplet_bracket::make_bracket (Axis protusion_axis,
203                               Real dx, Real dy, Real thick, Real left_height,
204                               Real right_height, Real gap, Real left_widen,
205                               Real right_widen, Real left_shorten, Real right_shorten)
206 {
207   Real len = Offset (dx,dy).length ();
208   Real gapx = dx * (gap /  len);
209   Real gapy = dy * (gap /  len);
210   Real lshortx = dx * (left_shorten /  len);
211   Real lshorty = dy * (left_shorten /  len);
212   Real rshortx = dx * (right_shorten /  len);
213   Real rshorty = dy * (right_shorten /  len);
214   Axis other = other_axis (protusion_axis);
215   
216   Molecule l1 = Lookup::line (thick, Offset(lshortx, lshorty),
217                               Offset ( (dx - gapx)/2, (dy - gapy)/2 ));
218
219   Molecule l2 = Lookup::line (thick, Offset((dx + gapx) / 2,(dy + gapy) / 2),
220                               Offset (dx - rshortx, dy - rshorty));
221
222   Offset protusion;
223   protusion[other] = -left_widen;
224   protusion[protusion_axis] = left_height;
225   Molecule p1 = Lookup::line (thick, 
226                               Offset(lshortx, lshorty), 
227                               Offset(lshortx, lshorty) + protusion);
228   protusion[other] = right_widen;
229   protusion[protusion_axis] = right_height;
230   Molecule p2 = Lookup::line (thick, 
231                               Offset(dx - rshortx, dy - rshorty), 
232                               Offset(dx - rshortx, dy - rshorty) + protusion);  
233
234   Molecule m;
235   m.add_molecule (p1);
236   m.add_molecule (p2);
237   m.add_molecule (l1);
238   m.add_molecule (l2);
239
240   return m;  
241 }
242
243
244 /*
245   use first -> last note for slope, and then correct for disturbing
246   notes in between.  */
247 void
248 Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy) 
249 {
250   Link_array<Grob> column_arr=
251     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
252
253
254   SCM cols = me->get_grob_property ("note-columns");
255   Grob * commony = common_refpoint_of_list (cols, me, Y_AXIS);
256   Grob * commonx = common_refpoint_of_list (cols, me, X_AXIS);  
257   
258   Direction dir = Directional_element_interface::get (me);
259
260   /*
261     Use outer non-rest columns to determine slope
262    */
263   int l = 0;
264   while (l <column_arr.size () && Note_column::rest_b (column_arr[l]))
265     l ++;
266
267   int r = column_arr.size ()- 1;
268   while (r >= l && Note_column::rest_b (column_arr[r]))
269     r--;
270   
271   if (l < r)
272     {
273       *dy = column_arr[r]->extent (commony, Y_AXIS) [dir]
274         - column_arr[l]->extent (commony, Y_AXIS) [dir] ;
275     }
276   else
277     * dy = 0;
278
279
280   *offset = - dir * infinity_f;
281
282   if (!column_arr.size ())
283     return;
284
285
286   
287   Grob * lgr = get_x_bound_grob (column_arr[0], dir);
288   Grob * rgr = get_x_bound_grob (column_arr.top(), dir);  
289   Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
290   Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
291
292
293     /*
294       Slope.
295     */
296   Real factor = column_arr.size () > 1 ? 1/ (x1 - x0) : 1.0;
297   
298   for (int i = 0; i < column_arr.size ();  i++)
299     {
300       Real notey = column_arr[i]->extent (commony, Y_AXIS)[dir] 
301         - me->relative_coordinate (commony, Y_AXIS);
302
303       Real x = column_arr[i]->relative_coordinate (commonx, X_AXIS) - x0;
304       Real tuplety =  *dy * x * factor;
305
306       if (notey * dir > (*offset + tuplety) * dir)
307         *offset = notey - tuplety; 
308     }
309
310   // padding
311   *offset +=  gh_scm2double (me->get_grob_property ("padding")) *dir;
312
313   
314   /*
315     horizontal brackets should not collide with staff lines.
316    */
317   if (*dy == 0)
318     {
319       // quantize, then do collision check.
320       Real ss= Staff_symbol_referencer::staff_space (me);
321       *offset *= 2 / ss;
322       
323       *offset = rint (*offset);
324       if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
325         *offset += dir;
326
327       *offset *= 0.5 * ss;
328     }
329   
330 }
331
332 /*
333   use first -> last note for slope,
334 */
335 void
336 Tuplet_bracket::calc_dy (Grob*me,Real * dy)
337 {
338   Link_array<Grob> column_arr=
339     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
340
341   /*
342     ugh. refps.
343    */
344   Direction d = Directional_element_interface::get (me);
345   *dy = column_arr.top ()->extent (column_arr.top (), Y_AXIS) [d]
346     - column_arr[0]->extent (column_arr[0], Y_AXIS) [d];
347 }
348
349
350 /*
351   We depend on the beams if there are any.
352  */
353 MAKE_SCHEME_CALLBACK (Tuplet_bracket,before_line_breaking,1);
354 SCM
355 Tuplet_bracket::before_line_breaking (SCM smob)
356 {
357   Grob *me = unsmob_grob (smob);
358   Link_array<Grob> column_arr=
359     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
360
361
362   for (int i = column_arr.size(); i--;)
363     {
364       Grob * s =Note_column::stem_l (column_arr[i]);
365       Grob * b = s ? Stem::beam_l (s): 0;
366       if (b)
367         me->add_dependency (b);
368     }
369   return SCM_UNDEFINED;
370 }
371
372 MAKE_SCHEME_CALLBACK (Tuplet_bracket,after_line_breaking,1);
373
374 SCM
375 Tuplet_bracket::after_line_breaking (SCM smob)
376 {
377   Grob * me = unsmob_grob (smob);
378   Link_array<Grob> column_arr=
379     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
380
381   if (!column_arr.size ())
382     {
383       me->suicide ();
384       return SCM_UNSPECIFIED;
385     }
386   if (dynamic_cast<Spanner*> (me)->broken_b ())
387     {
388       me->warning ( "Tuplet_bracket was across linebreak. Farewell cruel world.");
389       me->suicide();
390       return SCM_UNSPECIFIED;
391     }
392   
393   Direction dir = Directional_element_interface::get (me);
394   if (!dir)
395     {
396       dir = Tuplet_bracket::get_default_dir (me);
397       Directional_element_interface::set (me, dir);
398     }
399   
400   bool equally_long = false;
401   Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
402
403   /*
404     We follow the beam only if there is one, and we are next to it.
405    */
406   Real dy, offset;
407   if (!par_beam
408       || Directional_element_interface::get (par_beam) != dir)
409     {
410       calc_position_and_height (me,&offset,&dy);
411     }
412   else
413     {
414       SCM ps =  par_beam->get_grob_property ("positions"); 
415
416       Real lp = gh_scm2double (gh_car (ps));
417       Real rp = gh_scm2double (gh_cdr (ps));
418
419       /*
420         duh. magic.
421        */
422       offset = lp + dir * (0.5 + gh_scm2double (me->get_grob_property ("padding")));
423       dy = rp- lp;
424     }
425   
426   
427   SCM lp =  me->get_grob_property ("left-position");
428   SCM rp = me->get_grob_property ("right-position");  
429   
430   if (gh_number_p (lp) && !gh_number_p (rp))
431     {
432       rp = gh_double2scm (gh_scm2double (lp) + dy);
433     }
434   else if (gh_number_p (rp) && !gh_number_p (lp))
435     {
436       lp = gh_double2scm (gh_scm2double (rp) - dy);
437     }
438   else if (!gh_number_p (rp) && !gh_number_p (lp))
439     {
440       lp = gh_double2scm (offset);
441       rp = gh_double2scm (offset +dy);
442     }
443
444   me->set_grob_property ("left-position", lp);
445   me->set_grob_property ("right-position", rp);
446
447   return SCM_UNSPECIFIED;
448 }
449
450
451 /*
452   similar to slur.
453  */
454 Direction
455 Tuplet_bracket::get_default_dir (Grob*me)
456 {
457   Direction d = UP;
458   for (SCM s = me->get_grob_property ("note-columns"); gh_pair_p (s); s = ly_cdr (s))
459     {
460       Grob * nc = unsmob_grob (ly_car (s));
461       if (Note_column::dir (nc) < 0) 
462         {
463           d = DOWN;
464           break;
465         }
466     }
467   return d;
468 }
469
470 void
471 Tuplet_bracket::add_column (Grob*me, Item*n)
472 {
473   Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
474   me->add_dependency (n);
475
476   add_bound_item (dynamic_cast<Spanner*> (me), n);
477 }
478
479
480
481
482
483
484 ADD_INTERFACE (Tuplet_bracket,"tuplet-bracket-interface",
485   "A bracket with a number in the middle, used for tuplets.",
486   "note-columns edge-width edge-height shorten-pair padding gap left-position right-position bracket-visibility number-visibility thickness direction");
487