]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
* lily/tie.cc (get_configuration): Replace fabs -> abs for integer
[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                               )
116 {
117   Real staff_space = Staff_symbol_referencer::staff_space (me);
118   Direction dir = get_grob_direction (me);
119   Interval attachments;
120   Direction d = LEFT;
121   do
122     {
123       attachments[d]
124         = robust_relative_extent (me->get_bound (d),
125                                   common,
126                                   X_AXIS)[-d]
127         - gap * d;
128     }
129   while (flip (&d) != LEFT);
130
131   if (attachments.length () < 0.6 * staff_space)
132     {
133       /*
134         Let short ties start over note heads, instead of between.
135       */
136       Drul_array<bool> allow (true, true);
137
138       Direction d = LEFT;
139       do {
140         if (Note_head::has_interface (me->get_bound (d)))
141           {
142             Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
143             if (get_grob_direction (stem) == dir
144                 && -d == dir)
145               allow[d] = false;
146           }
147       } while (flip (&d) != LEFT);
148
149       if (allow[LEFT] && allow[RIGHT])
150         {
151           *staff_position += dir;
152           do
153             {
154               if (Note_head::has_interface (me->get_bound (d)))
155                 {
156                   Interval extent
157                     = robust_relative_extent (me->get_bound (d),
158                                               common, X_AXIS);
159
160                   attachments[d] = extent.linear_combination (- 0.5 * d);
161                   *in_between = false;
162                 }
163             }
164           while (flip (&d) != LEFT);
165         }
166     }
167
168   return attachments;
169 }  
170
171 Interval
172 get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
173                         Real y)
174 {
175   Interval attachments;
176   Direction d = LEFT;
177   do
178     {
179       attachments[d] = skyline_height (skylines[d], y, -d);
180     }
181   while (flip (&d) != LEFT);
182   
183   return attachments;
184 }
185                         
186 void
187 Tie::get_configuration (Grob *me_grob, Grob *common,
188                         Tie_configuration *conf,
189                         Drul_array< Array < Skyline_entry > > const *skylines,
190                         Tie_details const &details
191                         )
192 {
193   Spanner *me = dynamic_cast<Spanner*> (me_grob);
194   if (!head (me, LEFT) && !head (me, RIGHT))
195     {
196       programming_error ("tie without heads");
197       me->suicide ();
198       return ;
199     }
200
201   /*
202     UGH. Don't mirror Tie_configuration.
203    */
204   conf->head_position_ = (int) Tie::get_position (me);
205   if (!conf->dir_)
206     conf->dir_ = get_grob_direction (me);
207   if (!conf->dir_)
208     conf->dir_ = get_default_dir (me);
209     
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);
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       conf->position_ += 2 * conf->dir_; 
332     }
333
334   if (!in_between
335       && in_space
336       && abs (conf->position_ - conf->head_position_) <= 1)
337     conf->position_ += 2*conf->dir_;
338   
339   
340   if (in_space)
341     {
342       if ((abs (conf->position_ - conf->head_position_) <= 1
343            && fabs (dy) < 0.45 * staff_space)
344           || fabs (dy) < 0.6 * staff_space)
345         {
346           /*
347             vertically center in space.
348           */
349           conf->center_tie_vertically (details);
350         }
351       else
352         {
353           conf->delta_y_ = 
354             conf->dir_ * staff_space * (- 0.3);
355         }
356     }
357   else
358     {
359       Real where = 0.5 * conf->dir_;
360       
361       Real rounding_dy = (where - middle[Y_AXIS]);
362       conf->delta_y_ = rounding_dy;
363
364       if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
365                  + conf->position_ * staff_space * 0.5
366                  + conf->delta_y_) <
367           conf->dir_ * conf->head_position_ * 0.5 * staff_space)
368         {
369           if (Staff_symbol_referencer::staff_radius (me) >  abs (conf->head_position_) / 2)
370             conf->position_ +=  2 * conf->dir_;
371           else
372             conf->position_ += conf->dir_;
373         }
374     }
375
376
377   if (skylines)
378     {
379       Real half_space = 0.5 * staff_space;
380       Real y = conf->position_ * half_space;
381       
382       conf->attachment_x_ = get_skyline_attachment (*skylines, y);
383       conf->attachment_x_.widen (-gap);
384     }
385 }
386
387
388 void
389 Tie::set_default_control_points (Grob *me_grob)
390 {
391   Spanner *me = dynamic_cast<Spanner*> (me_grob);
392   Grob *common  = me;
393   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
394   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
395   
396   Tie_configuration conf;
397   if (!get_grob_direction (me))
398     set_grob_direction (me, get_default_dir (me));
399
400   int tie_position = (int) Tie::get_position (me);
401   conf.position_ = tie_position;
402   
403   Tie_details details;
404   details.init (me);
405   get_configuration (me, common, &conf, 0, details);
406   set_control_points (me, common, conf, details);
407 }
408
409 void
410 Tie::set_control_points (Grob *me,
411                          Grob *common,
412                          Tie_configuration const &conf,
413                          Tie_details const &details
414                          )
415 {
416   Bezier b = conf.get_bezier (details);
417   b.scale (1, conf.dir_);
418   b.translate (Offset (conf.attachment_x_[LEFT]
419                        - me->relative_coordinate (common, X_AXIS),
420                        0.5 * conf.position_ * details.staff_space_
421                        + conf.delta_y_
422                        ));
423   
424   SCM controls = SCM_EOL;
425   for (int i = 4; i--;)
426     {
427       if (!b.control_[i].is_sane ())
428         programming_error ("Insane offset");
429       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
430     }
431   me->set_property ("control-points", controls);
432 }
433
434
435
436 MAKE_SCHEME_CALLBACK (Tie, print, 1);
437 SCM
438 Tie::print (SCM smob)
439 {
440   Grob *me = unsmob_grob (smob);
441
442   if (CENTER == get_grob_direction (me))
443     set_direction (me);
444
445   if (!get_grob_direction (me))
446     me->programming_error ("Tie direction not set."); 
447     
448   SCM cp = me->get_property ("control-points");
449   if (!scm_is_pair (cp))
450     {
451       /*
452         UGH.  dependency tracking!
453        */
454       if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
455         {
456           Tie_column::set_directions (me->get_parent (Y_AXIS));
457         }
458
459       cp = me->get_property ("control-points");
460     }
461   
462   if (!scm_is_pair (cp))
463     {
464       set_default_control_points (me);
465       cp = me->get_property ("control-points");
466     }
467
468   if (!scm_is_pair (cp))
469     return Stencil ().smobbed_copy ();
470
471   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
472   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
473   Real thick = base_thick * staff_thick;
474
475   Bezier b;
476   int i = 0;
477   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
478     {
479       b.control_[i] = ly_scm2offset (scm_car (s));
480       i++;
481     }
482
483   Stencil a;
484
485   SCM p = me->get_property ("dash-period");
486   SCM f = me->get_property ("dash-fraction");
487   if (scm_is_number (p) && scm_is_number (f))
488     a = Lookup::dashed_slur (b,
489                              thick,
490                              robust_scm2double (p, 1.0),
491                              robust_scm2double (f, 0));
492   else
493     a = Lookup::slur (b,
494                       get_grob_direction (me) * staff_thick,
495                       thick);
496
497   return a.smobbed_copy ();
498 }
499
500 ADD_INTERFACE (Tie,
501                "tie-interface",
502                
503                "A tie connecting two noteheads.\n",
504
505                /* properties */
506                "control-points "
507                "dash-fraction "
508                "dash-period "
509                "details "
510                "direction "
511                "thickness "
512                "x-gap ");