]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
oops. Include $CONFIGFILE.make, not
[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 {
201   Spanner *me = dynamic_cast<Spanner*> (me_grob);
202   if (!head (me, LEFT) && !head (me, RIGHT))
203     {
204       programming_error ("tie without heads");
205       me->suicide ();
206       return ;
207     }
208
209   /*
210     UGH. Don't mirror Tie_configuration.
211    */
212   conf->head_position_ = (int) Tie::get_position (me);
213   if (!conf->dir_)
214     conf->dir_ = get_grob_direction (me);
215   if (!conf->dir_)
216     conf->dir_ = get_default_dir (me);
217
218   Real staff_space = problem.details_.staff_space_;
219   bool in_between = true;
220   Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
221
222   if (conf->attachment_x_.is_empty())
223     {
224 #if 0
225       if (!skylines)
226         conf->attachment_x_ = get_default_attachments (me, common, gap,
227                                                        &conf->position_,
228                                                        &in_between, problem.details_);
229 #endif
230       Real y = staff_space * 0.5 * conf->position_;
231       conf->attachment_x_ = problem.get_attachment (y);
232       conf->attachment_x_.widen (-gap);
233     }
234
235   Bezier b = slur_shape (conf->attachment_x_.length(),
236                          problem.details_.height_limit_,
237                          problem.details_.ratio_);
238   b.scale (1, conf->dir_);
239   
240   Offset middle = b.curve_point (0.5);
241   Offset edge = b.curve_point (0.0);
242
243   conf->position_ = int (rint (conf->position_));
244   
245   Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
246   bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
247   bool fits_in_space =
248     (dy < 0.6 * staff_space);
249   
250   /*
251     Avoid dot
252    */
253   Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
254   int dot_pos = left_dot
255     ? int (Staff_symbol_referencer::get_position (left_dot))
256     : 0;
257   if (left_dot
258       && (conf->position_ == dot_pos
259           || conf->position_ + conf->dir_ == dot_pos))
260     {
261       conf->position_ += conf->dir_;
262       in_space = !in_space;
263
264       if (conf->position_ == dot_pos)
265         {
266           conf->position_ += conf->dir_;
267           in_space = !in_space;
268         }
269       
270       Real y = staff_space * 0.5 * conf->position_;
271       conf->attachment_x_ = problem.get_attachment (y);
272       conf->attachment_x_.widen (-gap);
273       Bezier b = slur_shape (conf->attachment_x_.length(),
274                              problem.details_.height_limit_,
275                              problem.details_.ratio_);
276       Offset middle = b.curve_point (0.5);
277       Offset edge = b.curve_point (0.0);
278       dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
279       fits_in_space =
280         (dy < 0.6 * staff_space);
281     }
282   
283   /*
284     Avoid flag.
285   */
286   Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
287   if (left_stem)
288     {
289       Stencil flag = Stem::get_translated_flag (left_stem);
290       Real y = conf->position_ * staff_space * 0.5;
291       if (flag.extent (Y_AXIS).contains (y))
292         {
293           conf->position_ += conf->dir_;
294           in_space = !in_space;
295         }
296     }
297
298   if (in_space != fits_in_space)
299     {
300       if (in_space)
301         {
302           conf->position_ += conf->dir_;
303         }
304       else
305         {
306           in_space = true;
307           conf->position_ += conf->dir_;
308         }
309
310       /*
311         ugh: code dup.
312       */
313       Real y = staff_space * 0.5 * conf->position_;
314       conf->attachment_x_ = problem.get_attachment (y);
315       conf->attachment_x_.widen (-gap);
316               
317       Bezier b = slur_shape (conf->attachment_x_.length(),
318                              problem.details_.height_limit_,
319                              problem.details_.ratio_);
320       Offset middle = b.curve_point (0.5);
321       Offset edge = b.curve_point (0.0);
322       dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
323       fits_in_space =
324         (dy < 0.6 * staff_space);
325     }
326
327
328   /*
329     Putting larger in-space ties next to the notes forces
330     the edges to be opposite (Y-wise) to the tie direction.
331    */
332   if (conf->position_ == conf->head_position_
333       && in_space
334       && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
335       && dy > 0.3 * staff_space
336       )
337     {
338       conf->position_ += 2 * conf->dir_; 
339     }
340
341   if (!in_between
342       && in_space
343       && abs (conf->position_ - conf->head_position_) <= 1)
344     {
345       int amount = conf->dir_;
346       if (sign (conf->position_) != conf->dir_
347           || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
348         amount *= 2;
349
350       conf->position_ += amount;
351     }
352   
353   if (in_space)
354     {
355       if ((sign (conf->position_) != conf->dir_
356            || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
357           &&
358           ((abs (conf->position_ - conf->head_position_) <= 1
359             && fabs (dy) < 0.45 * staff_space)
360            || fabs (dy) < 0.6 * staff_space))
361         {
362           /*
363             vertically center in space.
364           */
365           conf->center_tie_vertically (problem.details_);
366         }
367       else
368         {
369           conf->delta_y_ = 
370             conf->dir_ * staff_space * (- 0.3);
371         }
372     }
373   else
374     {
375       Real where = 0.5 * conf->dir_;
376       
377       Real rounding_dy = (where - middle[Y_AXIS]);
378       conf->delta_y_ = rounding_dy;
379
380       if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
381                  + conf->position_ * staff_space * 0.5
382                  + conf->delta_y_) <
383           conf->dir_ * conf->head_position_ * 0.5 * staff_space)
384         {
385           if (Staff_symbol_referencer::staff_radius (me) >  abs (conf->head_position_) / 2)
386             conf->position_ +=  2 * conf->dir_;
387           else
388             conf->position_ += conf->dir_;
389         }
390     }
391
392
393   Real half_space = 0.5 * staff_space;
394   Real y = conf->position_ * half_space;
395       
396   conf->attachment_x_ = problem.get_attachment (y);
397   conf->attachment_x_.widen (-gap);
398 }
399
400
401 void
402 Tie::set_default_control_points (Grob *me_grob)
403 {
404   Spanner *me = dynamic_cast<Spanner*> (me_grob);
405   Grob *common  = me;
406   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
407   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
408   
409   Tie_configuration conf;
410   int tie_position = (int) Tie::get_position (me);
411   conf.position_ = tie_position;
412   
413   Tie_formatting_problem problem;
414   problem.from_tie (me);
415   
416   get_configuration (me,  &conf, problem);
417   set_control_points (me, problem.common_x_refpoint (),
418                       conf, problem.details_);
419 }
420
421 void
422 Tie::set_control_points (Grob *me,
423                          Grob *common,
424                          Tie_configuration const &conf,
425                          Tie_details const &details
426                          )
427 {
428   Bezier b = conf.get_bezier (details);
429   b.scale (1, conf.dir_);
430   b.translate (Offset (conf.attachment_x_[LEFT]
431                        - me->relative_coordinate (common, X_AXIS),
432                        0.5 * conf.position_ * details.staff_space_
433                        + conf.delta_y_
434                        ));
435   
436   SCM controls = SCM_EOL;
437   for (int i = 4; i--;)
438     {
439       if (!b.control_[i].is_sane ())
440         programming_error ("Insane offset");
441       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
442     }
443   me->set_property ("control-points", controls);
444 }
445
446 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
447 SCM
448 Tie::calc_control_points (SCM smob)
449 {
450   Grob *me = unsmob_grob (smob);
451
452   // trigger Tie-column
453   (void)  get_grob_direction (me);
454
455   Grob *yparent = me->get_parent (Y_AXIS);
456   if (Tie_column::has_interface (yparent)
457       && unsmob_grob_array (yparent->get_object ("ties"))
458       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
459     {
460       /* trigger positioning. */
461       (void) yparent->get_property ("positioning-done");
462     }
463
464   if (!scm_is_pair (me->get_property ("control-points")))
465     {
466       set_default_control_points (me);
467     }
468
469   return SCM_UNSPECIFIED;
470 }
471
472
473 MAKE_SCHEME_CALLBACK (Tie, print, 1);
474 SCM
475 Tie::print (SCM smob)
476 {
477   Grob *me = unsmob_grob (smob);
478   
479   SCM cp = me->get_property ("control-points");
480
481   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
482   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
483   Real thick = base_thick * staff_thick;
484
485   Bezier b;
486   int i = 0;
487   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
488     {
489       b.control_[i] = ly_scm2offset (scm_car (s));
490       i++;
491     }
492
493   Stencil a;
494
495   SCM p = me->get_property ("dash-period");
496   SCM f = me->get_property ("dash-fraction");
497   if (scm_is_number (p) && scm_is_number (f))
498     a = Lookup::dashed_slur (b,
499                              thick,
500                              robust_scm2double (p, 1.0),
501                              robust_scm2double (f, 0));
502   else
503     a = Lookup::slur (b,
504                       get_grob_direction (me) * staff_thick,
505                       thick);
506
507   return a.smobbed_copy ();
508 }
509
510 ADD_INTERFACE (Tie,
511                "tie-interface",
512                
513                "A tie connecting two noteheads. \n\n"
514                ,
515                
516
517                /* properties */
518                "control-points "
519                "dash-fraction "
520                "dash-period "
521                "details "
522                "direction "
523                "thickness "
524                "x-gap ");