]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
* scm/define-context-properties.scm
[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 #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     conf->position_ += 2*conf->dir_;
361   
362   
363   if (in_space)
364     {
365       if ((abs (conf->position_ - conf->head_position_) <= 1
366            && fabs (dy) < 0.45 * staff_space)
367           || fabs (dy) < 0.6 * staff_space)
368         {
369           /*
370             vertically center in space.
371           */
372           conf->center_tie_vertically (details);
373         }
374       else
375         {
376           conf->delta_y_ = 
377             conf->dir_ * staff_space * (- 0.3);
378         }
379     }
380   else
381     {
382       Real where = 0.5 * conf->dir_;
383       
384       Real rounding_dy = (where - middle[Y_AXIS]);
385       conf->delta_y_ = rounding_dy;
386
387       if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
388                  + conf->position_ * staff_space * 0.5
389                  + conf->delta_y_) <
390           conf->dir_ * conf->head_position_ * 0.5 * staff_space)
391         {
392           if (Staff_symbol_referencer::staff_radius (me) >  abs (conf->head_position_) / 2)
393             conf->position_ +=  2 * conf->dir_;
394           else
395             conf->position_ += conf->dir_;
396         }
397     }
398
399
400   if (skylines)
401     {
402       Real half_space = 0.5 * staff_space;
403       Real y = conf->position_ * half_space;
404       
405       conf->attachment_x_ = get_skyline_attachment (*skylines, y);
406       conf->attachment_x_.widen (-gap);
407     }
408 }
409
410
411 void
412 Tie::set_default_control_points (Grob *me_grob)
413 {
414   Spanner *me = dynamic_cast<Spanner*> (me_grob);
415   Grob *common  = me;
416   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
417   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
418   
419   Tie_configuration conf;
420   int tie_position = (int) Tie::get_position (me);
421   conf.position_ = tie_position;
422   
423   Tie_details details;
424   details.init (me);
425   get_configuration (me, common, &conf, 0, details);
426   set_control_points (me, common, conf, details);
427 }
428
429 void
430 Tie::set_control_points (Grob *me,
431                          Grob *common,
432                          Tie_configuration const &conf,
433                          Tie_details const &details
434                          )
435 {
436   Bezier b = conf.get_bezier (details);
437   b.scale (1, conf.dir_);
438   b.translate (Offset (conf.attachment_x_[LEFT]
439                        - me->relative_coordinate (common, X_AXIS),
440                        0.5 * conf.position_ * details.staff_space_
441                        + conf.delta_y_
442                        ));
443   
444   SCM controls = SCM_EOL;
445   for (int i = 4; i--;)
446     {
447       if (!b.control_[i].is_sane ())
448         programming_error ("Insane offset");
449       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
450     }
451   me->set_property ("control-points", controls);
452 }
453
454 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
455 SCM
456 Tie::calc_control_points (SCM smob)
457 {
458   Grob *me = unsmob_grob (smob);
459
460   // trigger Tie-column
461   (void)  get_grob_direction (me);
462
463   if (!scm_is_pair (me->get_property ("control-points")))
464     {
465       set_default_control_points (me);
466     }
467
468   return SCM_UNSPECIFIED;
469 }
470
471
472 MAKE_SCHEME_CALLBACK (Tie, print, 1);
473 SCM
474 Tie::print (SCM smob)
475 {
476   Grob *me = unsmob_grob (smob);
477   
478   SCM cp = me->get_property ("control-points");
479
480   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
481   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
482   Real thick = base_thick * staff_thick;
483
484   Bezier b;
485   int i = 0;
486   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
487     {
488       b.control_[i] = ly_scm2offset (scm_car (s));
489       i++;
490     }
491
492   Stencil a;
493
494   SCM p = me->get_property ("dash-period");
495   SCM f = me->get_property ("dash-fraction");
496   if (scm_is_number (p) && scm_is_number (f))
497     a = Lookup::dashed_slur (b,
498                              thick,
499                              robust_scm2double (p, 1.0),
500                              robust_scm2double (f, 0));
501   else
502     a = Lookup::slur (b,
503                       get_grob_direction (me) * staff_thick,
504                       thick);
505
506   return a.smobbed_copy ();
507 }
508
509 ADD_INTERFACE (Tie,
510                "tie-interface",
511                
512                "A tie connecting two noteheads. \n\n"
513                ,
514                
515
516                /* properties */
517                "control-points "
518                "dash-fraction "
519                "dash-period "
520                "details "
521                "direction "
522                "thickness "
523                "x-gap ");