]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
release: 1.3.98
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <math.h>
9
10 #include "spanner.hh"
11 #include "lookup.hh"
12 #include "paper-def.hh"
13 #include "tie.hh"
14 #include "rhythmic-head.hh"
15 #include "bezier.hh"
16 #include "paper-column.hh"
17 #include "debug.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "directional-element-interface.hh"
20 #include "molecule.hh"
21 #include "bezier-bow.hh"
22 #include "stem.hh"
23 #include "note-head.hh"
24
25 /*
26   tie: Connect two noteheads.
27
28   What if we have
29
30   c4 ~ \clef bass ; c4 or
31
32   c4 \staffchange c4
33
34   do we have non-horizontal ties then?
35   */
36
37
38 void
39 Tie::set_head (Score_element*me,Direction d, Item * head_l)
40 {
41   assert (!head (me,d));
42   index_set_cell (me->get_elt_property ("heads"), d, head_l->self_scm ());
43   
44   dynamic_cast<Spanner*> (me)->set_bound (d, head_l);
45   me->add_dependency (head_l);
46 }
47
48 void
49 Tie::set_interface (Score_element*me)
50 {
51   me->set_elt_property ("heads", gh_cons (SCM_EOL, SCM_EOL));
52   me->set_interface (ly_symbol2scm ("tie-interface"));
53 }
54
55 bool
56 Tie::has_interface (Score_element*me)
57 {
58   return me->has_interface (ly_symbol2scm ("tie-interface"));
59 }
60
61 Score_element*
62 Tie::head (Score_element*me, Direction d) 
63 {
64   SCM c = me->get_elt_property ("heads");
65   c = index_cell (c, d);
66
67   return unsmob_element (c);
68 }
69
70 Real
71 Tie::position_f (Score_element*me) 
72 {
73   Direction d = head (me,LEFT) ? LEFT:RIGHT;
74   return Staff_symbol_referencer::position_f (head (me,d));
75 }
76
77
78 /*
79   Default:  Put the tie oppositie of the stem [Wanske p231]
80
81   In case of chords: Tie_column takes over
82   
83   The direction of the Tie is more complicated (See [Ross] p136 and
84   further).
85 */
86 Direction
87 Tie::get_default_dir (Score_element*me) 
88 {
89   Item * sl =  head(me,LEFT) ? Rhythmic_head::stem_l (head (me,LEFT)) :0;
90   Item * sr =  head(me,RIGHT) ? Rhythmic_head::stem_l (head (me,RIGHT)) :0;  
91
92   if (sl && sr)
93     {
94       if (Directional_element_interface::get (sl) == UP
95           && Directional_element_interface::get (sr) == UP)
96         return DOWN;
97     }
98   else if (sl || sr)
99     {
100       Item *s = sl ? sl : sr;
101       return - Directional_element_interface::get (s);
102     }
103
104   
105   return UP;
106 }
107
108
109 SCM
110 Tie::get_control_points (SCM smob)
111 {  
112   Spanner*me = dynamic_cast<Spanner*> (unsmob_element (smob));
113   Direction headdir = CENTER; 
114   if (head (me,LEFT))
115     headdir = LEFT;
116   else if (head(me,RIGHT))
117     headdir = RIGHT;
118   else
119     {
120       programming_error ("Tie without heads.");
121       me->suicide ();
122       return SCM_UNSPECIFIED;
123     }
124   
125   if (!Directional_element_interface::get (me))
126     Directional_element_interface::set (me, Tie::get_default_dir (me));
127   
128   Real staff_space = Staff_symbol_referencer::staff_space (me);
129
130   Real x_gap_f = gh_scm2double (me->get_elt_property ("x-gap"));
131
132   Score_element* l = me->get_bound (LEFT);
133   Score_element* r = me->get_bound (RIGHT);  
134
135   Score_element* commonx = me->common_refpoint (l, X_AXIS);
136   commonx = me->common_refpoint (r, X_AXIS);
137   
138   Real left_x;
139
140   /*
141     this is a kludge: the tie has to be long enough to be
142     visible, but should not go through key sigs.
143
144     (please fixme)
145    */
146   Real lambda = 0.5;            
147   
148   if (Note_head::has_interface (me->get_bound (LEFT)))
149     left_x = l->extent (l, X_AXIS)[RIGHT] + x_gap_f;
150   else
151     left_x = l->extent (l, X_AXIS).linear_combination (lambda);
152   
153
154   Real width;
155   if (Note_head::has_interface (me->get_bound (LEFT))
156       && Note_head::has_interface (me->get_bound (RIGHT)))
157     {
158       width = 
159         + r->extent (commonx,X_AXIS)[LEFT]
160         - l->extent (commonx, X_AXIS)[RIGHT]
161         -2 * x_gap_f;
162     }
163   else
164     {
165       if (Note_head::has_interface (me->get_bound (LEFT)))
166         width = r->relative_coordinate (commonx, X_AXIS)
167           - l->extent (commonx, X_AXIS)[RIGHT]
168           - 2 * x_gap_f;
169       else
170         width =
171           - l->extent (commonx, X_AXIS).linear_combination (lambda)  
172           + r->extent (commonx, X_AXIS)[LEFT]
173           - 2 * x_gap_f;
174     }
175   
176   Direction dir = Directional_element_interface::get(me);
177
178   SCM details = me->get_elt_property ("details");
179
180   SCM lim // groetjes aan de chirurgendochter.
181     = scm_assq (ly_symbol2scm ("height-limit"),details);
182   
183   Real h_inf = gh_scm2double (gh_cdr (lim)) *  staff_space;
184   Real r_0 = gh_scm2double (gh_cdr (scm_assq (ly_symbol2scm ("ratio"),details)));
185
186   Bezier b  = slur_shape (width, h_inf, r_0);
187   
188   Offset leave_dir = b.control_[1] - b.control_[0];
189
190   Score_element *hed =head (me, headdir);
191   Real dx = (hed->extent (hed, X_AXIS).length () + x_gap_f)/2.0;
192   Real max_gap = leave_dir[Y_AXIS] * dx / leave_dir[X_AXIS];
193
194   /*
195     for small ties (t small) we want to start in the Y-center (so dy = 0), for
196     large ties, the tie should appear to come from the center of the
197     head, so dy = max_gap
198
199     maybe use a different formula?
200
201     TODO: what if 2 heads have different size.
202
203     TODO: for small ties, it is better to start over the heads
204     iso. next to the heads. 
205   */
206   Real t = (width / staff_space - 5.0); // ugh.
207   Real dy = t > 0 ? max_gap * sqr (t / (1 + t)) : 0.0;
208
209   Real ypos = Tie::position_f (me) * staff_space/2 + dir * dy;
210
211   /*
212     todo: prevent ending / staffline collision.
213
214     todo: tie / stem collision
215    */
216
217   b = slur_shape(width,h_inf, r_0);
218   b.scale (1, dir);
219   b.translate (Offset (left_x, ypos));
220   
221
222   /*
223     Avoid colliding of the horizontal part with stafflines.
224
225     
226     TODO: redo this, heuristic is half-baken, and ties often look ugly
227     as a result.
228
229     TODO: doesn't work when on staff with even number of lines.
230    */
231   Array<Real> horizontal (b.solve_derivative (Offset (1,0)));
232   if (horizontal.size ())
233     {
234       /*
235         ugh. Doesnt work for non-horizontal curves.
236        */
237       Real y = b.curve_point (horizontal[0])[Y_AXIS];
238
239       Real ry = rint (y/staff_space) * staff_space;
240       Real diff = ry - y;
241       Real newy = y;
242
243       Real clear = staff_space * gh_scm2double (me->get_elt_property ("staffline-clearance"));
244
245         if (fabs (y) <= Staff_symbol_referencer::staff_radius (me)
246           && fabs (diff) < clear)
247         {
248           Real y1 = ry + clear;
249           Real y2 = ry - clear;
250           
251           newy =  (fabs (y1 - y) < fabs (y2 - y)) ? y1 : y2;
252           
253           //      newy = ry - 0.5 * staff_space * sign (diff) ;
254
255           /*
256             we don't want horizontal ties
257            */
258           if (fabs (newy - b.control_[0][Y_AXIS]) < 1e-2)
259             {
260               newy = newy + dir * staff_space; 
261             }
262         }
263
264       Real y0 = b.control_ [0][Y_AXIS];
265       b.control_[2][Y_AXIS] = 
266       b.control_[1][Y_AXIS] =
267         (b.control_[1][Y_AXIS] - y0)  * ((newy - y0) / (y - y0)) + y0; 
268     }
269   else
270     programming_error ("Tie is nowhere horizontal");
271
272
273
274   SCM controls = SCM_EOL;
275   for (int i= 4; i--;)
276     controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
277   return controls;
278 }
279
280 MAKE_SCHEME_CALLBACK(Tie,set_spacing_rods,1);
281
282 /*
283   TODO: set minimum distances for begin/end of line
284  */
285 SCM
286 Tie::set_spacing_rods (SCM smob)  
287 {
288   Score_element*me = unsmob_element (smob);
289   Spanner*sp = dynamic_cast<Spanner*> (me);
290   Rod r;
291
292   r.item_l_drul_ [LEFT]=sp->get_bound (LEFT);
293   r.item_l_drul_ [RIGHT]=sp->get_bound (RIGHT);  
294   
295   r.distance_f_
296     = gh_scm2double (me->get_elt_property ("minimum-length"))
297     * me->paper_l ()->get_var ("staffspace");
298   r.add_to_cols ();
299   return SCM_UNSPECIFIED;
300 }
301
302 MAKE_SCHEME_CALLBACK(Tie,brew_molecule,1);
303 SCM
304 Tie::brew_molecule (SCM smob) 
305 {
306   Score_element*me = unsmob_element (smob);
307
308   SCM cp = me->get_elt_property ("control-points");
309   if (cp == SCM_EOL)
310     {
311       cp = get_control_points (smob);
312       me->set_elt_property ("control-points", cp);
313     }
314   
315   Real thick =
316     gh_scm2double (me->get_elt_property ("thickness"))
317     * me->paper_l ()->get_var ("stafflinethickness");
318
319   Bezier b;
320   int i = 0;
321   for (SCM s= cp; s != SCM_EOL; s = gh_cdr (s))
322     {
323       b.control_[i] = ly_scm2offset (gh_car (s));
324       i++;
325     }
326   
327    Molecule a = me->lookup_l ()->slur (b, Directional_element_interface::get (me) * thick, thick);
328    
329    return a.create_scheme ();
330 }
331
332