]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
(get_configuration): only shift tie by a whole staff
[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 (skylines)
281         {
282           Real y = staff_space * 0.5 * conf->position_;
283           conf->attachment_x_ = get_skyline_attachment (*skylines, y);
284           conf->attachment_x_.widen (-gap);
285           Bezier b = slur_shape (conf->attachment_x_.length(),
286                                  details.height_limit_,
287                                  details.ratio_);
288           Offset middle = b.curve_point (0.5);
289           Offset edge = b.curve_point (0.0);
290           dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
291           fits_in_space =
292             (dy < 0.6 * staff_space);
293         }
294     }
295   
296   /*
297     Avoid flag.
298   */
299   Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
300   if (left_stem)
301     {
302       Stencil flag = Stem::get_translated_flag (left_stem);
303       Real y = conf->position_ * staff_space * 0.5;
304       if (flag.extent (Y_AXIS).contains (y))
305         {
306           conf->position_ += conf->dir_;
307           in_space = !in_space;
308         }
309     }
310
311   if (in_space != fits_in_space)
312     {
313       if (in_space)
314         {
315           conf->position_ += conf->dir_;
316         }
317       else
318         {
319           in_space = true;
320           conf->position_ += conf->dir_;
321         }
322
323       /*
324         ugh: code dup.
325        */
326       if (skylines)
327         {
328           Real y = staff_space * 0.5 * conf->position_;
329           conf->attachment_x_ = get_skyline_attachment (*skylines, y);
330           conf->attachment_x_.widen (-gap);
331               
332           Bezier b = slur_shape (conf->attachment_x_.length(),
333                                  details.height_limit_,
334                                  details.ratio_);
335           Offset middle = b.curve_point (0.5);
336           Offset edge = b.curve_point (0.0);
337           dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
338           fits_in_space =
339             (dy < 0.6 * staff_space);
340         }
341     }
342
343
344   /*
345     Putting larger in-space ties next to the notes forces
346     the edges to be opposite (Y-wise) to the tie direction.
347    */
348   if (conf->position_ == conf->head_position_
349       && in_space
350       && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
351       && dy > 0.3 * staff_space
352       )
353     {
354       conf->position_ += 2 * conf->dir_; 
355     }
356
357   if (!in_between
358       && in_space
359       && abs (conf->position_ - conf->head_position_) <= 1)
360     {
361       int amount = conf->dir_;
362       if (sign (conf->position_) != conf->dir_
363           || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
364         amount *= 2;
365
366       conf->position_ += amount;
367     }
368   
369   if (in_space)
370     {
371       if ((sign (conf->position_) != conf->dir_
372            || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
373           &&
374           ((abs (conf->position_ - conf->head_position_) <= 1
375             && fabs (dy) < 0.45 * staff_space)
376            || fabs (dy) < 0.6 * staff_space))
377         {
378           /*
379             vertically center in space.
380           */
381           conf->center_tie_vertically (details);
382         }
383       else
384         {
385           conf->delta_y_ = 
386             conf->dir_ * staff_space * (- 0.3);
387         }
388     }
389   else
390     {
391       Real where = 0.5 * conf->dir_;
392       
393       Real rounding_dy = (where - middle[Y_AXIS]);
394       conf->delta_y_ = rounding_dy;
395
396       if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
397                  + conf->position_ * staff_space * 0.5
398                  + conf->delta_y_) <
399           conf->dir_ * conf->head_position_ * 0.5 * staff_space)
400         {
401           if (Staff_symbol_referencer::staff_radius (me) >  abs (conf->head_position_) / 2)
402             conf->position_ +=  2 * conf->dir_;
403           else
404             conf->position_ += conf->dir_;
405         }
406     }
407
408
409   if (skylines)
410     {
411       Real half_space = 0.5 * staff_space;
412       Real y = conf->position_ * half_space;
413       
414       conf->attachment_x_ = get_skyline_attachment (*skylines, y);
415       conf->attachment_x_.widen (-gap);
416     }
417 }
418
419
420 void
421 Tie::set_default_control_points (Grob *me_grob)
422 {
423   Spanner *me = dynamic_cast<Spanner*> (me_grob);
424   Grob *common  = me;
425   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
426   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
427   
428   Tie_configuration conf;
429   int tie_position = (int) Tie::get_position (me);
430   conf.position_ = tie_position;
431   
432   Tie_details details;
433   details.init (me);
434   get_configuration (me, common, &conf, 0, details);
435   set_control_points (me, common, conf, details);
436 }
437
438 void
439 Tie::set_control_points (Grob *me,
440                          Grob *common,
441                          Tie_configuration const &conf,
442                          Tie_details const &details
443                          )
444 {
445   Bezier b = conf.get_bezier (details);
446   b.scale (1, conf.dir_);
447   b.translate (Offset (conf.attachment_x_[LEFT]
448                        - me->relative_coordinate (common, X_AXIS),
449                        0.5 * conf.position_ * details.staff_space_
450                        + conf.delta_y_
451                        ));
452   
453   SCM controls = SCM_EOL;
454   for (int i = 4; i--;)
455     {
456       if (!b.control_[i].is_sane ())
457         programming_error ("Insane offset");
458       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
459     }
460   me->set_property ("control-points", controls);
461 }
462
463 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
464 SCM
465 Tie::calc_control_points (SCM smob)
466 {
467   Grob *me = unsmob_grob (smob);
468
469   // trigger Tie-column
470   (void)  get_grob_direction (me);
471
472   if (!scm_is_pair (me->get_property ("control-points")))
473     {
474       set_default_control_points (me);
475     }
476
477   return SCM_UNSPECIFIED;
478 }
479
480
481 MAKE_SCHEME_CALLBACK (Tie, print, 1);
482 SCM
483 Tie::print (SCM smob)
484 {
485   Grob *me = unsmob_grob (smob);
486   
487   SCM cp = me->get_property ("control-points");
488
489   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
490   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
491   Real thick = base_thick * staff_thick;
492
493   Bezier b;
494   int i = 0;
495   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
496     {
497       b.control_[i] = ly_scm2offset (scm_car (s));
498       i++;
499     }
500
501   Stencil a;
502
503   SCM p = me->get_property ("dash-period");
504   SCM f = me->get_property ("dash-fraction");
505   if (scm_is_number (p) && scm_is_number (f))
506     a = Lookup::dashed_slur (b,
507                              thick,
508                              robust_scm2double (p, 1.0),
509                              robust_scm2double (f, 0));
510   else
511     a = Lookup::slur (b,
512                       get_grob_direction (me) * staff_thick,
513                       thick);
514
515   return a.smobbed_copy ();
516 }
517
518 ADD_INTERFACE (Tie,
519                "tie-interface",
520                
521                "A tie connecting two noteheads. \n\n"
522                ,
523                
524
525                /* properties */
526                "control-points "
527                "dash-fraction "
528                "dash-period "
529                "details "
530                "direction "
531                "thickness "
532                "x-gap ");