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