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