]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-bracket.cc
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[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 "warn.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::get_stem (g)
48       && Note_column::dir (g) == my_dir)
49     {
50       g = Note_column::get_stem (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::get_stem (cols[0]); 
64   Grob *s2 = Note_column::get_stem (cols.top());    
65
66   Grob*b1 = s1 ? Stem::get_beam (s1) : 0;
67   Grob*b2 = s2 ? Stem::get_beam (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   if (beam_stems.size() == 0)
79     {
80       programming_error ("Beam under tuplet bracket has no stems!");
81       *equally_long = 0;
82       return 0;
83     }
84   
85   *equally_long = (beam_stems[0] == s1 && beam_stems.top() == s2);
86   return b1;
87 }
88
89
90 /*
91   TODO:
92
93   in the case that there is no bracket, but there is a (single) beam,
94   follow beam precisely for determining tuplet number location.
95   
96  */
97 MAKE_SCHEME_CALLBACK (Tuplet_bracket,brew_molecule,1);
98 SCM
99 Tuplet_bracket::brew_molecule (SCM smob) 
100 {
101   Grob *me= unsmob_grob (smob);
102   Molecule  mol;
103   Link_array<Grob> columns=
104     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
105
106   if (!columns.size ())
107     return mol.smobbed_copy ();
108
109   bool equally_long = false;
110   Grob * par_beam = parallel_beam (me, columns, &equally_long);
111
112   Spanner*sp = dynamic_cast<Spanner*> (me);  
113
114   bool bracket_visibility = !(par_beam && equally_long);
115   bool number_visibility = true;
116
117   /*
118     Fixme: the type of this prop is sucky.
119    */
120   SCM bracket = me->get_grob_property ("bracket-visibility");
121   if (gh_boolean_p (bracket))
122     {
123       bracket_visibility = gh_scm2bool (bracket);
124     }
125   else if (bracket == ly_symbol2scm ("if-no-beam"))
126     bracket_visibility = !par_beam;
127
128   SCM numb = me->get_grob_property ("number-visibility");  
129   if (gh_boolean_p (numb))
130     {
131       number_visibility = gh_scm2bool (numb);
132     }
133   else if (numb == ly_symbol2scm ("if-no-beam"))
134     number_visibility = !par_beam;
135         
136   Grob * commonx = columns[0]->common_refpoint (columns.top (),X_AXIS);
137
138   /*
139     Tuplet brackets are normally not broken, but we shouldn't crash if
140     they are.
141    */
142   commonx = commonx->common_refpoint (sp->get_bound(LEFT), X_AXIS);
143   commonx = commonx->common_refpoint (sp->get_bound(RIGHT), X_AXIS);  
144   
145   Direction dir = Directional_element_interface::get (me);
146
147   Grob * lgr = get_x_bound_grob (columns[0], dir);
148   Grob * rgr = get_x_bound_grob (columns.top(), dir);  
149   Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
150   Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
151
152   Real w = x1 -x0;
153
154   Real ly = gh_scm2double (me->get_grob_property ("left-position"));
155   Real ry = gh_scm2double (me->get_grob_property ("right-position"));  
156   SCM number = me->get_grob_property ("text");
157   
158   if (gh_string_p (number) && number_visibility)
159     {
160       SCM properties = Font_interface::font_alist_chain (me);
161       Molecule num = Text_item::text2molecule (me, number, properties);
162       num.align_to (X_AXIS, CENTER);
163       num.translate_axis (w/2, X_AXIS);
164       num.align_to (Y_AXIS, CENTER);
165         
166       num.translate_axis ((ry-ly)/2, Y_AXIS);
167
168       mol.add_molecule (num);
169     }
170       
171   if (bracket_visibility)      
172     {
173       Real  lt =  me->get_paper ()->get_var ("linethickness");
174   
175       SCM thick = me->get_grob_property ("thickness");
176       if (gh_number_p (thick))
177         lt *= gh_scm2double (thick);
178       
179       SCM gap = me->get_grob_property ("gap");
180       SCM ew = me->get_grob_property ("edge-widen");
181       SCM eh = me->get_grob_property ("edge-height");
182       SCM sp = me->get_grob_property ("shorten-pair");
183       
184       Direction d = LEFT;
185       Drul_array<Real> height, width, shorten;
186       do {
187         width[d] =  height[d] = shorten[d] = 0.0;
188         if ( ly_number_pair_p (ew) )
189           width[d] +=  gh_scm2double (index_get_cell (ew, d));
190         if ( ly_number_pair_p (eh) )
191           height[d] += gh_scm2double (index_get_cell (eh, d)) * - dir;
192         if ( ly_number_pair_p (sp) )
193           shorten[d] +=  gh_scm2double (index_get_cell (sp, d));
194       }
195       while (flip (&d) != LEFT);
196       
197       Molecule brack = make_bracket (Y_AXIS,
198                                      w, ry - ly, lt,
199                                      height,
200                                      gh_scm2double (gap),
201                                      width,
202                                      shorten);
203       mol.add_molecule (brack);
204     }
205   
206   mol.translate_axis (ly, Y_AXIS);
207   mol.translate_axis (x0  - sp->get_bound (LEFT)->relative_coordinate (commonx,X_AXIS),X_AXIS);
208   return mol.smobbed_copy ();
209 }
210
211 /*
212   should move to lookup?
213  */
214 Molecule
215 Tuplet_bracket::make_bracket (Axis protusion_axis,
216                               Real dx, Real dy, Real thick, Drul_array<Real> height,
217                               Real gap,
218                               Drul_array<Real> widen,
219                               Drul_array<Real> shorten)
220 {
221   Real len = Offset (dx,dy).length ();
222   Real gapx = dx * (gap /  len);
223   Real gapy = dy * (gap /  len);
224   Drul_array<Real> shortx, shorty;
225   Direction d = LEFT;
226   do {
227     shortx[d] = dx * (shorten[d] /  len);
228     shorty[d] = dy * (shorten[d] /  len);
229   }
230   while (flip (&d) != LEFT);
231   Axis other = other_axis (protusion_axis);
232   
233   Molecule l1 = Lookup::line (thick, Offset(shortx[LEFT], shorty[LEFT]),
234                               Offset ( (dx - gapx)/2, (dy - gapy)/2 ));
235
236   Molecule l2 = Lookup::line (thick, Offset((dx + gapx) / 2,(dy + gapy) / 2),
237                               Offset (dx - shortx[RIGHT], dy - shorty[RIGHT]));
238
239   Offset protusion;
240   protusion[other] = -widen[LEFT];
241   protusion[protusion_axis] = height[LEFT];
242   Molecule p1 = Lookup::line (thick, 
243                               Offset(shortx[LEFT], shorty[LEFT]), 
244                               Offset(shortx[LEFT], shorty[LEFT]) + protusion);
245   protusion[other] = widen[RIGHT];
246   protusion[protusion_axis] = height[RIGHT];
247   Molecule p2 = Lookup::line (thick, 
248                               Offset(dx - shortx[RIGHT], dy - shorty[RIGHT]), 
249                               Offset(dx - shortx[RIGHT], dy - shorty[RIGHT]) + protusion);  
250
251   Molecule m;
252   m.add_molecule (p1);
253   m.add_molecule (p2);
254   m.add_molecule (l1);
255   m.add_molecule (l2);
256
257   return m;  
258 }
259
260
261 /*
262   use first -> last note for slope, and then correct for disturbing
263   notes in between.  */
264 void
265 Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy) 
266 {
267   Link_array<Grob> columns=
268     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
269
270
271   SCM cols = me->get_grob_property ("note-columns");
272   Grob * commony = common_refpoint_of_list (cols, me, Y_AXIS);
273   Grob * commonx = common_refpoint_of_list (cols, me, X_AXIS);  
274   
275   Direction dir = Directional_element_interface::get (me);
276
277   /*
278     Use outer non-rest columns to determine slope
279    */
280   int l = 0;
281   while (l <columns.size () && Note_column::rest_b (columns[l]))
282     l ++;
283
284   int r = columns.size ()- 1;
285   while (r >= l && Note_column::rest_b (columns[r]))
286     r--;
287   
288   if (l < r)
289     {
290       *dy = columns[r]->extent (commony, Y_AXIS) [dir]
291         - columns[l]->extent (commony, Y_AXIS) [dir] ;
292     }
293   else
294     * dy = 0;
295
296
297   *offset = - dir * infinity_f;
298
299   if (!columns.size ())
300     return;
301
302
303   
304   Grob * lgr = get_x_bound_grob (columns[0], dir);
305   Grob * rgr = get_x_bound_grob (columns.top(), dir);  
306   Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
307   Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
308
309
310     /*
311       Slope.
312     */
313   Real factor = columns.size () > 1 ? 1/ (x1 - x0) : 1.0;
314   
315   for (int i = 0; i < columns.size ();  i++)
316     {
317       Real notey = columns[i]->extent (commony, Y_AXIS)[dir] 
318         - me->relative_coordinate (commony, Y_AXIS);
319       
320       Real x = columns[i]->relative_coordinate (commonx, X_AXIS) - x0;
321       Real tuplety =  *dy * x * factor;
322
323       if (notey * dir > (*offset + tuplety) * dir)
324         *offset = notey - tuplety; 
325     }
326
327   // padding
328   *offset +=  gh_scm2double (me->get_grob_property ("padding")) *dir;
329
330   
331   /*
332     horizontal brackets should not collide with staff lines.
333     
334    */
335   Real ss= Staff_symbol_referencer::staff_space (me);
336   if (*dy == 0 && fabs (*offset) <  ss * Staff_symbol_referencer::staff_radius (me))
337     {
338       // quantize, then do collision check.
339       *offset *= 2 / ss;
340       
341       *offset = rint (*offset);
342       if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
343         *offset += dir;
344
345       *offset *= 0.5 * ss;
346     }
347   
348 }
349
350 /*
351   use first -> last note for slope,
352 */
353 void
354 Tuplet_bracket::calc_dy (Grob*me,Real * dy)
355 {
356   Link_array<Grob> columns=
357     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
358
359   /*
360     ugh. refps.
361    */
362   Direction d = Directional_element_interface::get (me);
363   *dy = columns.top ()->extent (columns.top (), Y_AXIS) [d]
364     - columns[0]->extent (columns[0], Y_AXIS) [d];
365 }
366
367
368 /*
369   We depend on the beams if there are any.
370  */
371 MAKE_SCHEME_CALLBACK (Tuplet_bracket,before_line_breaking,1);
372 SCM
373 Tuplet_bracket::before_line_breaking (SCM smob)
374 {
375   Grob *me = unsmob_grob (smob);
376   Link_array<Grob> columns=
377     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
378
379
380   for (int i = columns.size(); i--;)
381     {
382       Grob * s =Note_column::get_stem (columns[i]);
383       Grob * b = s ? Stem::get_beam (s): 0;
384       if (b)
385         me->add_dependency (b);
386     }
387   return SCM_UNDEFINED;
388 }
389
390 MAKE_SCHEME_CALLBACK (Tuplet_bracket,after_line_breaking,1);
391
392 SCM
393 Tuplet_bracket::after_line_breaking (SCM smob)
394 {
395   Grob * me = unsmob_grob (smob);
396   Link_array<Grob> columns=
397     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
398
399   if (!columns.size ())
400     {
401       me->suicide ();
402       return SCM_UNSPECIFIED;
403     }
404   if (dynamic_cast<Spanner*> (me)->broken_b ())
405     {
406       me->warning ( "Tuplet_bracket was across linebreak. Farewell cruel world.");
407       me->suicide();
408       return SCM_UNSPECIFIED;
409     }
410   
411   Direction dir = Directional_element_interface::get (me);
412   if (!dir)
413     {
414       dir = Tuplet_bracket::get_default_dir (me);
415       Directional_element_interface::set (me, dir);
416     }
417   
418   bool equally_long = false;
419   Grob * par_beam = parallel_beam (me, columns, &equally_long);
420
421   /*
422     We follow the beam only if there is one, and we are next to it.
423    */
424   Real dy, offset;
425   if (!par_beam
426       || Directional_element_interface::get (par_beam) != dir)
427     {
428       calc_position_and_height (me,&offset,&dy);
429     }
430   else
431     {
432       SCM ps =  par_beam->get_grob_property ("positions"); 
433
434       Real lp = gh_scm2double (gh_car (ps));
435       Real rp = gh_scm2double (gh_cdr (ps));
436
437       /*
438         duh. magic.
439        */
440       offset = lp + dir * (0.5 + gh_scm2double (me->get_grob_property ("padding")));
441       dy = rp- lp;
442     }
443   
444   
445   SCM lp =  me->get_grob_property ("left-position");
446   SCM rp = me->get_grob_property ("right-position");  
447   
448   if (gh_number_p (lp) && !gh_number_p (rp))
449     {
450       rp = gh_double2scm (gh_scm2double (lp) + dy);
451     }
452   else if (gh_number_p (rp) && !gh_number_p (lp))
453     {
454       lp = gh_double2scm (gh_scm2double (rp) - dy);
455     }
456   else if (!gh_number_p (rp) && !gh_number_p (lp))
457     {
458       lp = gh_double2scm (offset);
459       rp = gh_double2scm (offset +dy);
460     }
461
462   me->set_grob_property ("left-position", lp);
463   me->set_grob_property ("right-position", rp);
464
465   return SCM_UNSPECIFIED;
466 }
467
468
469 /*
470   similar to slur.
471  */
472 Direction
473 Tuplet_bracket::get_default_dir (Grob*me)
474 {
475   Direction d = UP;
476   for (SCM s = me->get_grob_property ("note-columns"); gh_pair_p (s); s = ly_cdr (s))
477     {
478       Grob * nc = unsmob_grob (ly_car (s));
479       if (Note_column::dir (nc) < 0) 
480         {
481           d = DOWN;
482           break;
483         }
484     }
485   return d;
486 }
487
488 void
489 Tuplet_bracket::add_column (Grob*me, Item*n)
490 {
491   Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
492   me->add_dependency (n);
493
494   add_bound_item (dynamic_cast<Spanner*> (me), n);
495 }
496
497
498
499
500
501
502 ADD_INTERFACE (Tuplet_bracket,"tuplet-bracket-interface",
503   "A bracket with a number in the middle, used for tuplets.",
504   "note-columns edge-widen edge-height shorten-pair padding gap left-position right-position bracket-visibility number-visibility thickness direction");
505