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