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