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