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