]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
* lily/tie.cc: remove minimum-length
[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 /*
28   tie: Connect two noteheads.
29
30   What if we have
31
32   c4 ~ \clef bass ; c4 or
33
34   c4 \staffchange c4
35
36   do we have non-horizontal ties then?
37 */
38
39 void
40 Tie::set_head (Grob *me, Direction d, Grob *h)
41 {
42   assert (!head (me, d));
43   index_set_cell (me->get_property ("head-pair"), d, h->self_scm ());
44
45   dynamic_cast<Spanner *> (me)->set_bound (d, h);
46   me->add_dependency (h);
47 }
48
49 void
50 Tie::set_interface (Grob *me)
51 {
52   me->set_property ("head-pair", scm_cons (SCM_EOL, SCM_EOL));
53 }
54
55 Grob *
56 Tie::head (Grob *me, Direction d)
57 {
58   SCM c = me->get_property ("head-pair");
59
60   if (scm_is_pair (c))
61     return unsmob_grob (index_get_cell (c, d));
62   else
63     return 0;
64 }
65
66 int
67 Tie::get_column_rank (Grob *me, Direction d)
68 {
69   Spanner *span = dynamic_cast<Spanner *> (me);
70   Grob *h = head (me, d);
71   if (!h)
72     h = span->get_bound (d);
73
74   Grob *col = dynamic_cast<Item *> (h)->get_column ();
75   return Paper_column::get_rank (col);
76 }
77
78 Real
79 Tie::get_position (Grob *me)
80 {
81   Direction d = head (me, LEFT) ? LEFT : RIGHT;
82   return Staff_symbol_referencer::get_position (head (me, d));
83 }
84
85 /*
86   Default:  Put the tie oppositie of the stem [Wanske p231]
87
88   In case of chords: Tie_column takes over
89
90   The direction of the Tie is more complicated (See [Ross] p136 and
91   further).
92
93   (what about linebreaks? )
94 */
95 Direction
96 Tie::get_default_dir (Grob *me)
97 {
98   Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
99   Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
100   if (sl && sr)
101     {
102       if (get_grob_direction (sl) == UP
103           && get_grob_direction (sr) == UP)
104         return DOWN;
105     }
106   else if (sl || sr)
107     {
108       Item *s = sl ? sl : sr;
109       return -get_grob_direction (s);
110     }
111
112   return UP;
113 }
114
115 void
116 Tie::set_direction (Grob *me)
117 {
118   if (!get_grob_direction (me))
119     {
120       if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
121         Tie_column::set_directions (me->get_parent (Y_AXIS));
122       else
123         set_grob_direction (me, Tie::get_default_dir (me));
124     }
125 }
126
127
128 SCM
129 Tie::get_configuration (Grob *me_grob,
130                         Grob **common,
131                         Tie_configuration *conf)
132 {
133   Spanner *me = dynamic_cast<Spanner*> (me_grob);
134   
135   if (!head (me, LEFT) && !head (me, RIGHT))
136     {
137       programming_error ("tie without heads");
138       me->suicide ();
139       return SCM_EOL;
140     }
141
142   set_direction (me);
143   int tie_position = (int) Tie::get_position (me);
144   
145   Direction dir = get_grob_direction (me);
146
147   Real staff_space = Staff_symbol_referencer::staff_space (me);
148   Real staff_position = tie_position;
149
150   Direction d = LEFT;
151   Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
152   do
153     {
154       attachments[d]
155         = robust_relative_extent (me->get_bound (d),
156                                   common[X_AXIS],
157                                   X_AXIS)[-d]
158         - gap * d;
159     }
160   while (flip (&d) != LEFT);
161
162   bool in_between = true;
163   if (attachments.length () < 0.6 * staff_space)
164     {
165       /*
166         Let short ties start over note heads, instead of between.
167        */
168       Drul_array<bool> allow (true, true);
169
170       Direction d = LEFT;
171       do {
172         if (Note_head::has_interface (me->get_bound (d)))
173           {
174             Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
175             if (get_grob_direction (stem) == dir
176                 && -d == dir)
177               allow[d] = false;
178           }
179       } while (flip (&d) != LEFT);
180
181       if (allow[LEFT] && allow[RIGHT])
182         {
183           staff_position += dir;
184           do
185             {
186               if (Note_head::has_interface (me->get_bound (d)))
187                 {
188                   Interval extent
189                     = robust_relative_extent (me->get_bound (d),
190                                               common[X_AXIS], X_AXIS);
191
192                   attachments[d] = extent.linear_combination (- 0.5 * d);
193                   in_between = false;
194                 }
195             }
196           while (flip (&d) != LEFT);
197         }
198     }
199
200   SCM details = me->get_property ("details");
201
202   SCM limit
203     = scm_assq (ly_symbol2scm ("height-limit"), details);
204
205   Real h_inf = robust_scm2double (scm_cdr (limit), 0.75) * staff_space;
206   Real r_0 = robust_scm2double (scm_cdr (scm_assq (ly_symbol2scm ("ratio"), details)),
207                                 .333);
208
209   Bezier b = slur_shape (attachments.length(),
210                          h_inf, r_0);
211   b.scale (1, dir);
212   
213   Offset middle = b.curve_point (0.5);
214   Offset edge = b.curve_point (0.0);
215
216   staff_position = rint (staff_position);
217   
218   Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
219   bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) staff_position));
220   bool fits_in_space =
221     (dy < 0.6 * staff_space);
222   
223   /*
224     Avoid dot
225    */
226   Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
227   if (left_dot && in_space)
228     {
229       if (staff_position == Staff_symbol_referencer::get_position (left_dot))
230         {
231           staff_position += dir;
232           in_space = false;
233         }
234     }
235
236   /*
237     Avoid flag.
238    */
239   Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
240   if (left_stem)
241     {
242       Stencil flag = Stem::get_translated_flag (left_stem);
243       Real y = staff_position * staff_space * 0.5;
244       if (flag.extent (Y_AXIS).contains (y))
245         {
246           staff_position += dir;
247           in_space = !in_space;
248         }
249     }
250
251   /*
252     Putting larger in-space ties next to the notes forces
253     the edges to be opposite (Y-wise) to the tie direction.
254    */
255   if (staff_position == tie_position
256       && in_space
257       && dy > 0.3 * staff_space)
258     {
259       staff_position += 2 * dir; 
260     }
261
262   if (in_space != fits_in_space)
263     {
264       if (in_space)
265         {
266           staff_position += dir;
267         }
268       else
269         {
270           in_space = true;
271           staff_position += dir;
272         }
273     }
274
275   if (!in_between
276       && in_space
277       && fabs (staff_position - tie_position) <= 1)
278     staff_position += 2*dir;
279   
280   
281   if (in_space)
282     {
283       if (fabs (dy) < 0.4 * staff_space)
284         {
285           /*
286             vertically center in space.
287           */
288           Offset middle = b.curve_point (0.5);
289           Offset edge = b.curve_point (0.0);
290
291           Real center = (edge[Y_AXIS] + middle[Y_AXIS])/2.0;
292
293           conf->edge_y_ = staff_position * staff_space * 0.5
294             - center;
295         }
296       else
297         {
298           conf->edge_y_ = 
299             (staff_position - dir) * staff_space * 0.5
300             + dir * 0.2 * staff_space;
301         }
302     }
303   else
304     {
305       Real where = 0.5 * dir;
306       
307       Real rounding_dy = (where - middle[Y_AXIS]);
308       conf->edge_y_ = 0.5 * staff_position * staff_space + rounding_dy;
309
310       if (dir * b.curve_point (0.0)[Y_AXIS] <
311           dir * tie_position * 0.5 * staff_space)
312         conf->edge_y_ +=  staff_space * dir; 
313     }
314
315   conf->position_ = staff_position;
316   conf->dir_ = dir;
317   conf->attachment_x_ = attachments;
318 }
319
320
321 SCM
322 Tie::get_control_points (SCM smob)
323 {
324   Spanner *me = unsmob_spanner (smob);
325
326   Grob *common[NO_AXES] = {
327     0, 0
328   };
329   for (int a = X_AXIS; a < NO_AXES; a++)
330     {
331       Axis ax ((Axis) a); 
332       common[ax] = me->get_bound (LEFT)->common_refpoint (me, ax); 
333       common[ax] = me->get_bound (RIGHT)->common_refpoint (common[a], ax); 
334     }
335   
336   Tie_configuration conf;
337   get_configuration (me, common, &conf);
338   
339   SCM details = me->get_property ("details");
340   SCM limit
341     = scm_assq (ly_symbol2scm ("height-limit"), details);
342
343   Real h_inf = robust_scm2double (scm_cdr (limit), 0.75) * staff_space;
344   Real r_0 = robust_scm2double (scm_cdr (scm_assq (ly_symbol2scm ("ratio"),
345                                                    details)),
346                                 .333);
347
348   Bezier b = slur_shape (conf->attachment_x_.length(),
349                          h_inf, r_0);
350   b.scale (1, conf->dir_);
351   
352   Bezier b;
353   
354   b.translate (Offset (conf->attachment_x_[LEFT]
355                        - me->relative_coordinate (common[X_AXIS], X_AXIS), 0));
356   
357   SCM controls = SCM_EOL;
358   for (int i = 4; i--;)
359     controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
360
361   return controls;
362 }
363
364
365
366 MAKE_SCHEME_CALLBACK (Tie, print, 1);
367 SCM
368 Tie::print (SCM smob)
369 {
370   Grob *me = unsmob_grob (smob);
371
372   SCM cp = me->get_property ("control-points");
373   if (!scm_is_pair (cp))                // list is more accurate
374     {
375       cp = get_control_points (smob);
376       me->set_property ("control-points", cp);
377     }
378
379   if (!scm_is_pair (cp))
380     return Stencil ().smobbed_copy ();
381
382   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
383   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
384   Real thick = base_thick * staff_thick;
385
386   Bezier b;
387   int i = 0;
388   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
389     {
390       b.control_[i] = ly_scm2offset (scm_car (s));
391       i++;
392     }
393
394   Stencil a;
395
396   SCM p = me->get_property ("dash-period");
397   SCM f = me->get_property ("dash-fraction");
398   if (scm_is_number (p) && scm_is_number (f))
399     a = Lookup::dashed_slur (b,
400                              thick,
401                              robust_scm2double (p, 1.0),
402                              robust_scm2double (f, 0));
403   else
404     a = Lookup::slur (b,
405                       get_grob_direction (me) * staff_thick,
406                       thick);
407
408   return a.smobbed_copy ();
409 }
410
411 ADD_INTERFACE (Tie,
412                "tie-interface",
413                "A tie connecting two noteheads.\n",
414                
415                "control-points "
416                "dash-fraction"
417                "dash-period "
418                "details "
419                "direction "
420                "head-pair "
421                "thickness "
422                "x-gap ");
423
424
425