]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
* scm/lily.scm (define-scheme-options): add paper-size option.
[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
12 #include "spanner.hh"
13 #include "lookup.hh"
14 #include "output-def.hh"
15 #include "rhythmic-head.hh"
16 #include "bezier.hh"
17 #include "paper-column.hh"
18 #include "warn.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "directional-element-interface.hh"
21 #include "bezier.hh"
22 #include "stem.hh"
23 #include "note-head.hh"
24 #include "tie-column.hh"
25
26 int
27 Tie::compare (Grob *const &s1,
28               Grob *const &s2)
29 {
30   return sign (Tie::get_position (s1) - Tie::get_position (s2));
31 }
32
33 void
34 Tie::set_head (Grob *me, Direction d, Grob *h)
35 {
36   dynamic_cast<Spanner *> (me)->set_bound (d, h);
37   me->add_dependency (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 = head (me, LEFT) ? LEFT : RIGHT;
66   return (int) Staff_symbol_referencer::get_position (head (me, d));
67 }
68
69 /*
70   Default:  Put the tie oppositie of the stem [Wanske p231]
71
72   In case of chords: Tie_column takes over
73
74   The direction of the Tie is more complicated (See [Ross] p136 and
75   further).
76
77   (what about linebreaks? )
78 */
79 Direction
80 Tie::get_default_dir (Grob *me)
81 {
82   Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
83   Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
84   if (sl && sr)
85     {
86       if (get_grob_direction (sl) == UP
87           && get_grob_direction (sr) == UP)
88         return DOWN;
89     }
90   else if (sl || sr)
91     {
92       Item *s = sl ? sl : sr;
93       return -get_grob_direction (s);
94     }
95
96   return UP;
97 }
98
99 void
100 Tie::set_direction (Grob *me)
101 {
102   if (!get_grob_direction (me))
103     {
104       if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
105         Tie_column::set_directions (me->get_parent (Y_AXIS));
106       else
107         set_grob_direction (me, Tie::get_default_dir (me));
108     }
109 }
110
111 Interval
112 Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
113                               int *staff_position,
114                               bool *in_between,
115                               Tie_details const &details
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 () < details.between_length_limit_ * 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   Real staff_space = details.staff_space_;
212   bool in_between = true;
213   Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
214
215   if (conf->attachment_x_.is_empty())
216     {
217       if (!skylines)
218         conf->attachment_x_ = get_default_attachments (me, common, gap,
219                                                        &conf->position_,
220                                                        &in_between, details);
221       else
222         {
223           Real y = staff_space * 0.5 * conf->position_;
224           conf->attachment_x_ = get_skyline_attachment (*skylines, y);
225           conf->attachment_x_.widen (-gap);
226         }
227     }
228
229   Bezier b = slur_shape (conf->attachment_x_.length(),
230                          details.height_limit_,
231                          details.ratio_);
232   b.scale (1, conf->dir_);
233   
234   Offset middle = b.curve_point (0.5);
235   Offset edge = b.curve_point (0.0);
236
237   conf->position_ = int (rint (conf->position_));
238   
239   Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
240   bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
241   bool fits_in_space =
242     (dy < 0.6 * staff_space);
243   
244   /*
245     Avoid dot
246    */
247   Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
248   int dot_pos = left_dot
249     ? int (Staff_symbol_referencer::get_position (left_dot))
250     : 0;
251   if (left_dot
252       && (conf->position_ == dot_pos
253           || conf->position_ + conf->dir_ == dot_pos))
254     {
255       conf->position_ += conf->dir_;
256       in_space = !in_space;
257
258       if (skylines)
259         {
260           Real y = staff_space * 0.5 * conf->position_;
261           conf->attachment_x_ = get_skyline_attachment (*skylines, y);
262           conf->attachment_x_.widen (-gap);
263           Bezier b = slur_shape (conf->attachment_x_.length(),
264                                  details.height_limit_,
265                                  details.ratio_);
266           Offset middle = b.curve_point (0.5);
267           Offset edge = b.curve_point (0.0);
268           dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
269           fits_in_space =
270             (dy < 0.6 * staff_space);
271         }
272     }
273   
274   /*
275     Avoid flag.
276   */
277   Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
278   if (left_stem)
279     {
280       Stencil flag = Stem::get_translated_flag (left_stem);
281       Real y = conf->position_ * staff_space * 0.5;
282       if (flag.extent (Y_AXIS).contains (y))
283         {
284           conf->position_ += conf->dir_;
285           in_space = !in_space;
286         }
287     }
288
289   if (in_space != fits_in_space)
290     {
291       if (in_space)
292         {
293           conf->position_ += conf->dir_;
294         }
295       else
296         {
297           in_space = true;
298           conf->position_ += conf->dir_;
299         }
300
301       /*
302         ugh: code dup.
303        */
304       if (skylines)
305         {
306           Real y = staff_space * 0.5 * conf->position_;
307           conf->attachment_x_ = get_skyline_attachment (*skylines, y);
308           conf->attachment_x_.widen (-gap);
309               
310           Bezier b = slur_shape (conf->attachment_x_.length(),
311                                  details.height_limit_,
312                                  details.ratio_);
313           Offset middle = b.curve_point (0.5);
314           Offset edge = b.curve_point (0.0);
315           dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
316           fits_in_space =
317             (dy < 0.6 * staff_space);
318         }
319     }
320
321
322   /*
323     Putting larger in-space ties next to the notes forces
324     the edges to be opposite (Y-wise) to the tie direction.
325    */
326   if (conf->position_ == conf->head_position_
327       && in_space
328       && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
329       && dy > 0.3 * staff_space
330       )
331     {
332       conf->position_ += 2 * conf->dir_; 
333     }
334
335   if (!in_between
336       && in_space
337       && abs (conf->position_ - conf->head_position_) <= 1)
338     conf->position_ += 2*conf->dir_;
339   
340   
341   if (in_space)
342     {
343       if ((abs (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) >  abs (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\n"
505                ,
506                
507
508                /* properties */
509                "control-points "
510                "dash-fraction "
511                "dash-period "
512                "details "
513                "direction "
514                "thickness "
515                "x-gap ");