]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
* lily/tie-column-format.cc (set_chord_outline): add dots into
[lilypond.git] / lily / tie.cc
1 /*
2   tie.cc -- implement Tie
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "tie.hh"
10 #include "spanner.hh"
11 #include "lookup.hh"
12 #include "output-def.hh"
13 #include "rhythmic-head.hh"
14 #include "bezier.hh"
15 #include "paper-column.hh"
16 #include "warn.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "directional-element-interface.hh"
19 #include "bezier.hh"
20 #include "stem.hh"
21 #include "note-head.hh"
22 #include "tie-column.hh"
23 #include "grob-array.hh"
24
25 int
26 Tie::compare (Grob *const &s1,
27               Grob *const &s2)
28 {
29   return sign (Tie::get_position (s1) - Tie::get_position (s2));
30 }
31
32 void
33 Tie::set_head (Grob *me, Direction d, Grob *h)
34 {
35   dynamic_cast<Spanner *> (me)->set_bound (d, h);
36 }
37
38 Grob *
39 Tie::head (Grob *me, Direction d)
40 {
41   Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
42   if (Note_head::has_interface (it))
43     return it;
44   else
45     return 0;
46 }
47
48 int
49 Tie::get_column_rank (Grob *me, Direction d)
50 {
51   Spanner *span = dynamic_cast<Spanner *> (me);
52   Grob *h = head (me, d);
53   if (!h)
54     h = span->get_bound (d);
55
56   Grob *col = dynamic_cast<Item *> (h)->get_column ();
57   return Paper_column::get_rank (col);
58 }
59
60 int
61 Tie::get_position (Grob *me)
62 {
63   Direction d = LEFT;
64   do
65     {
66       Grob *h = head (me, d);
67       if (h)
68         return (int) Staff_symbol_referencer::get_position (h);
69     }
70   while (flip (&d) != LEFT);
71
72   /*
73
74   TODO: this is theoretically possible for ties across more than 2
75   systems.. We should look at the first broken copy.
76   
77   */
78   programming_error ("Tie without heads. Suicide");
79   me->suicide ();
80   return 0;
81 }
82
83 /*
84   Default:  Put the tie oppositie of the stem [Wanske p231]
85
86   In case of chords: Tie_column takes over
87
88   The direction of the Tie is more complicated (See [Ross] p136 and
89   further).
90
91   (what about linebreaks? )
92 */
93 Direction
94 Tie::get_default_dir (Grob *me)
95 {
96   Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
97   Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
98   if (sl && sr)
99     {
100       if (get_grob_direction (sl) == UP
101           && get_grob_direction (sr) == UP)
102         return DOWN;
103     }
104   else if (sl || sr)
105     {
106       Item *s = sl ? sl : sr;
107       return -get_grob_direction (s);
108     }
109
110   return UP;
111 }
112
113
114 MAKE_SCHEME_CALLBACK(Tie, calc_direction, 1);
115 SCM
116 Tie::calc_direction (SCM smob)
117 {
118   Grob *me = unsmob_grob (smob);
119   Grob *yparent = me->get_parent (Y_AXIS);
120   if (Tie_column::has_interface (yparent)
121       && unsmob_grob_array (yparent->get_object ("ties"))
122       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
123     {
124       /* trigger positioning. */
125       (void) yparent->get_property ("positioning-done");
126     }
127   else
128     set_grob_direction (me, Tie::get_default_dir (me));
129
130   return SCM_UNSPECIFIED;
131 }
132
133 Interval
134 Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
135                               int *staff_position,
136                               bool *in_between,
137                               Tie_details const &details
138                               )
139 {
140   Real staff_space = Staff_symbol_referencer::staff_space (me);
141   Direction dir = get_grob_direction (me);
142   Interval attachments;
143   Direction d = LEFT;
144   do
145     {
146       attachments[d]
147         = robust_relative_extent (me->get_bound (d),
148                                   common,
149                                   X_AXIS)[-d]
150         - gap * d;
151     }
152   while (flip (&d) != LEFT);
153
154   if (attachments.length () < details.between_length_limit_ * staff_space)
155     {
156       /*
157         Let short ties start over note heads, instead of between.
158       */
159       Drul_array<bool> allow (true, true);
160
161       Direction d = LEFT;
162       do {
163         if (Note_head::has_interface (me->get_bound (d)))
164           {
165             Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
166             if (get_grob_direction (stem) == dir
167                 && -d == dir)
168               allow[d] = false;
169           }
170       } while (flip (&d) != LEFT);
171
172       if (allow[LEFT] && allow[RIGHT])
173         {
174           *staff_position += dir;
175           do
176             {
177               if (Note_head::has_interface (me->get_bound (d)))
178                 {
179                   Interval extent
180                     = robust_relative_extent (me->get_bound (d),
181                                               common, X_AXIS);
182
183                   attachments[d] = extent.linear_combination (- 0.5 * d);
184                   *in_between = false;
185                 }
186             }
187           while (flip (&d) != LEFT);
188         }
189     }
190
191   return attachments;
192 }  
193
194 Interval
195 get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
196                         Real y)
197 {
198   Interval attachments;
199   Direction d = LEFT;
200   do
201     {
202       attachments[d] = skyline_height (skylines[d], y, -d);
203     }
204   while (flip (&d) != LEFT);
205   
206   return attachments;
207 }
208                         
209 void
210 Tie::get_configuration (Grob *me_grob, Grob *common,
211                         Tie_configuration *conf,
212                         Drul_array< Array < Skyline_entry > > const *skylines,
213                         Tie_details const &details
214                         )
215 {
216   Spanner *me = dynamic_cast<Spanner*> (me_grob);
217   if (!head (me, LEFT) && !head (me, RIGHT))
218     {
219       programming_error ("tie without heads");
220       me->suicide ();
221       return ;
222     }
223
224   /*
225     UGH. Don't mirror Tie_configuration.
226    */
227   conf->head_position_ = (int) Tie::get_position (me);
228   if (!conf->dir_)
229     conf->dir_ = get_grob_direction (me);
230   if (!conf->dir_)
231     conf->dir_ = get_default_dir (me);
232
233   Real staff_space = details.staff_space_;
234   bool in_between = true;
235   Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
236
237   if (conf->attachment_x_.is_empty())
238     {
239       if (!skylines)
240         conf->attachment_x_ = get_default_attachments (me, common, gap,
241                                                        &conf->position_,
242                                                        &in_between, details);
243       else
244         {
245           Real y = staff_space * 0.5 * conf->position_;
246           conf->attachment_x_ = get_skyline_attachment (*skylines, y);
247           conf->attachment_x_.widen (-gap);
248         }
249     }
250
251   Bezier b = slur_shape (conf->attachment_x_.length(),
252                          details.height_limit_,
253                          details.ratio_);
254   b.scale (1, conf->dir_);
255   
256   Offset middle = b.curve_point (0.5);
257   Offset edge = b.curve_point (0.0);
258
259   conf->position_ = int (rint (conf->position_));
260   
261   Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
262   bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
263   bool fits_in_space =
264     (dy < 0.6 * staff_space);
265   
266   /*
267     Avoid dot
268    */
269   Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
270   int dot_pos = left_dot
271     ? int (Staff_symbol_referencer::get_position (left_dot))
272     : 0;
273   if (left_dot
274       && (conf->position_ == dot_pos
275           || conf->position_ + conf->dir_ == dot_pos))
276     {
277       conf->position_ += conf->dir_;
278       in_space = !in_space;
279
280       if (conf->position_ == dot_pos)
281         {
282           conf->position_ += conf->dir_;
283           in_space = !in_space;
284         }
285       
286       if (skylines)
287         {
288           Real y = staff_space * 0.5 * conf->position_;
289           conf->attachment_x_ = get_skyline_attachment (*skylines, y);
290           conf->attachment_x_.widen (-gap);
291           Bezier b = slur_shape (conf->attachment_x_.length(),
292                                  details.height_limit_,
293                                  details.ratio_);
294           Offset middle = b.curve_point (0.5);
295           Offset edge = b.curve_point (0.0);
296           dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
297           fits_in_space =
298             (dy < 0.6 * staff_space);
299         }
300     }
301   
302   /*
303     Avoid flag.
304   */
305   Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
306   if (left_stem)
307     {
308       Stencil flag = Stem::get_translated_flag (left_stem);
309       Real y = conf->position_ * staff_space * 0.5;
310       if (flag.extent (Y_AXIS).contains (y))
311         {
312           conf->position_ += conf->dir_;
313           in_space = !in_space;
314         }
315     }
316
317   if (in_space != fits_in_space)
318     {
319       if (in_space)
320         {
321           conf->position_ += conf->dir_;
322         }
323       else
324         {
325           in_space = true;
326           conf->position_ += conf->dir_;
327         }
328
329       /*
330         ugh: code dup.
331        */
332       if (skylines)
333         {
334           Real y = staff_space * 0.5 * conf->position_;
335           conf->attachment_x_ = get_skyline_attachment (*skylines, y);
336           conf->attachment_x_.widen (-gap);
337               
338           Bezier b = slur_shape (conf->attachment_x_.length(),
339                                  details.height_limit_,
340                                  details.ratio_);
341           Offset middle = b.curve_point (0.5);
342           Offset edge = b.curve_point (0.0);
343           dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
344           fits_in_space =
345             (dy < 0.6 * staff_space);
346         }
347     }
348
349
350   /*
351     Putting larger in-space ties next to the notes forces
352     the edges to be opposite (Y-wise) to the tie direction.
353    */
354   if (conf->position_ == conf->head_position_
355       && in_space
356       && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
357       && dy > 0.3 * staff_space
358       )
359     {
360       conf->position_ += 2 * conf->dir_; 
361     }
362
363   if (!in_between
364       && in_space
365       && abs (conf->position_ - conf->head_position_) <= 1)
366     {
367       int amount = conf->dir_;
368       if (sign (conf->position_) != conf->dir_
369           || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
370         amount *= 2;
371
372       conf->position_ += amount;
373     }
374   
375   if (in_space)
376     {
377       if ((sign (conf->position_) != conf->dir_
378            || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
379           &&
380           ((abs (conf->position_ - conf->head_position_) <= 1
381             && fabs (dy) < 0.45 * staff_space)
382            || fabs (dy) < 0.6 * staff_space))
383         {
384           /*
385             vertically center in space.
386           */
387           conf->center_tie_vertically (details);
388         }
389       else
390         {
391           conf->delta_y_ = 
392             conf->dir_ * staff_space * (- 0.3);
393         }
394     }
395   else
396     {
397       Real where = 0.5 * conf->dir_;
398       
399       Real rounding_dy = (where - middle[Y_AXIS]);
400       conf->delta_y_ = rounding_dy;
401
402       if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
403                  + conf->position_ * staff_space * 0.5
404                  + conf->delta_y_) <
405           conf->dir_ * conf->head_position_ * 0.5 * staff_space)
406         {
407           if (Staff_symbol_referencer::staff_radius (me) >  abs (conf->head_position_) / 2)
408             conf->position_ +=  2 * conf->dir_;
409           else
410             conf->position_ += conf->dir_;
411         }
412     }
413
414
415   if (skylines)
416     {
417       Real half_space = 0.5 * staff_space;
418       Real y = conf->position_ * half_space;
419       
420       conf->attachment_x_ = get_skyline_attachment (*skylines, y);
421       conf->attachment_x_.widen (-gap);
422     }
423 }
424
425
426 void
427 Tie::set_default_control_points (Grob *me_grob)
428 {
429   Spanner *me = dynamic_cast<Spanner*> (me_grob);
430   Grob *common  = me;
431   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
432   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
433   
434   Tie_configuration conf;
435   int tie_position = (int) Tie::get_position (me);
436   conf.position_ = tie_position;
437   
438   Tie_details details;
439   details.init (me);
440   get_configuration (me, common, &conf, 0, details);
441   set_control_points (me, common, conf, details);
442 }
443
444 void
445 Tie::set_control_points (Grob *me,
446                          Grob *common,
447                          Tie_configuration const &conf,
448                          Tie_details const &details
449                          )
450 {
451   Bezier b = conf.get_bezier (details);
452   b.scale (1, conf.dir_);
453   b.translate (Offset (conf.attachment_x_[LEFT]
454                        - me->relative_coordinate (common, X_AXIS),
455                        0.5 * conf.position_ * details.staff_space_
456                        + conf.delta_y_
457                        ));
458   
459   SCM controls = SCM_EOL;
460   for (int i = 4; i--;)
461     {
462       if (!b.control_[i].is_sane ())
463         programming_error ("Insane offset");
464       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
465     }
466   me->set_property ("control-points", controls);
467 }
468
469 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
470 SCM
471 Tie::calc_control_points (SCM smob)
472 {
473   Grob *me = unsmob_grob (smob);
474
475   // trigger Tie-column
476   (void)  get_grob_direction (me);
477
478   Grob *yparent = me->get_parent (Y_AXIS);
479   if (Tie_column::has_interface (yparent)
480       && unsmob_grob_array (yparent->get_object ("ties"))
481       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
482     {
483       /* trigger positioning. */
484       (void) yparent->get_property ("positioning-done");
485     }
486
487   if (!scm_is_pair (me->get_property ("control-points")))
488     {
489       set_default_control_points (me);
490     }
491
492   return SCM_UNSPECIFIED;
493 }
494
495
496 MAKE_SCHEME_CALLBACK (Tie, print, 1);
497 SCM
498 Tie::print (SCM smob)
499 {
500   Grob *me = unsmob_grob (smob);
501   
502   SCM cp = me->get_property ("control-points");
503
504   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
505   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
506   Real thick = base_thick * staff_thick;
507
508   Bezier b;
509   int i = 0;
510   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
511     {
512       b.control_[i] = ly_scm2offset (scm_car (s));
513       i++;
514     }
515
516   Stencil a;
517
518   SCM p = me->get_property ("dash-period");
519   SCM f = me->get_property ("dash-fraction");
520   if (scm_is_number (p) && scm_is_number (f))
521     a = Lookup::dashed_slur (b,
522                              thick,
523                              robust_scm2double (p, 1.0),
524                              robust_scm2double (f, 0));
525   else
526     a = Lookup::slur (b,
527                       get_grob_direction (me) * staff_thick,
528                       thick);
529
530   return a.smobbed_copy ();
531 }
532
533 ADD_INTERFACE (Tie,
534                "tie-interface",
535                
536                "A tie connecting two noteheads. \n\n"
537                ,
538                
539
540                /* properties */
541                "control-points "
542                "dash-fraction "
543                "dash-period "
544                "details "
545                "direction "
546                "thickness "
547                "x-gap ");