]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
release: 1.3.83
[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   The direction of the Tie is more complicated (See [Ross] p136 and
80   further), the case of multiple ties is handled by Tie_column.
81 */
82 Direction
83 Tie::get_default_dir (Score_element*me) 
84 {
85   Item * sl =  head(me,LEFT) ? Rhythmic_head::stem_l (head (me,LEFT)) :0;
86   Item * sr =  head(me,RIGHT) ? Rhythmic_head::stem_l (head (me,RIGHT)) :0;  
87
88
89   if (sl && sr)
90     {
91       if (Directional_element_interface::get (sl) == UP
92           && Directional_element_interface::get (sr) == UP)
93         return DOWN;
94     }
95   else if (sl || sr)
96     {
97       Item *s = sl ? sl : sr;
98       return - Directional_element_interface::get (s);
99     }
100
101   
102   return UP;
103 }
104
105
106 SCM
107 Tie::get_control_points (SCM smob)
108 {  
109   Spanner*me = dynamic_cast<Spanner*> (unsmob_element (smob));
110   Direction headdir = CENTER; 
111   if (head (me,LEFT))
112     headdir = LEFT;
113   else if (head(me,RIGHT))
114     headdir = RIGHT;
115   else
116     {
117       programming_error ("Tie without heads.");
118       me->suicide ();
119       return SCM_UNSPECIFIED;
120     }
121   
122   if (!Directional_element_interface::get (me))
123     Directional_element_interface::set (me, Tie::get_default_dir (me));
124   
125   Real staff_space = Staff_symbol_referencer::staff_space (me);
126
127   Real x_gap_f = me->paper_l ()->get_var ("tie_x_gap");
128
129   Score_element* commonx = me->common_refpoint (me->get_bound (LEFT), X_AXIS);
130   commonx = me->common_refpoint (me->get_bound (RIGHT), X_AXIS);
131   
132   Score_element* l = me->get_bound (LEFT);
133   Score_element* r = me->get_bound (RIGHT);  
134
135   Real left_x;
136   if (Note_head::has_interface (me->get_bound (LEFT)))
137     left_x = l->extent (X_AXIS)[RIGHT] + x_gap_f;
138   else
139     left_x = l->extent (X_AXIS).length () / 2;
140
141   Real width;
142   if (Note_head::has_interface (me->get_bound (LEFT))
143       && Note_head::has_interface (me->get_bound (RIGHT)))
144     {
145       width = r->relative_coordinate (commonx, X_AXIS)
146         + r->extent (X_AXIS)[LEFT]
147         - l->relative_coordinate (commonx, X_AXIS)
148         - l->extent (X_AXIS)[RIGHT]
149         -2 * x_gap_f;
150     }
151   else
152     {
153       if (Note_head::has_interface (me->get_bound (LEFT)))
154         width = r->relative_coordinate (commonx, X_AXIS)
155           - l->relative_coordinate (commonx, X_AXIS)
156           - l->extent (X_AXIS)[RIGHT]
157           - 2 * x_gap_f;
158       else
159         width = r->relative_coordinate (commonx, X_AXIS)
160           - l->extent (X_AXIS).length () / 2
161           + r->extent (X_AXIS)[LEFT]
162           - l->relative_coordinate (commonx, X_AXIS)
163           - 2 * x_gap_f;
164     }
165   
166   Direction dir = Directional_element_interface::get(me);
167   
168   Real h_inf = me->paper_l ()->get_var ("tie_height_limit_factor") * staff_space;
169   Real r_0 = me->paper_l ()->get_var ("tie_ratio");
170
171
172   Bezier b  = slur_shape (width, h_inf, r_0);
173   
174   Offset leave_dir = b.control_[1] - b.control_[0];
175
176   Real dx = (head (me, headdir)->extent (X_AXIS).length () + x_gap_f)/2.0;
177   Real max_gap = leave_dir[Y_AXIS] * dx / leave_dir[X_AXIS];
178
179   /*
180     for small ties (t small) we want to start in the Y-center (so dy = 0), for
181     large ties, the tie should appear to come from the center of the
182     head, so dy = max_gap
183
184     maybe use a different formula?
185
186     TODO: what if 2 heads have different size.
187
188     TODO: for small ties, it is better to start over the heads
189     iso. next to the heads. 
190   */
191   Real t = (width / staff_space - 5.0); // ugh.
192   Real dy = t > 0 ? max_gap * sqr (t / (1 + t)) : 0.0;
193
194   Real ypos = Tie::position_f (me) * staff_space/2 + dir * dy;
195
196   /*
197     todo: prevent ending / staffline collision.
198
199     todo: tie / stem collision
200    */
201
202   b = slur_shape(width,h_inf, r_0);
203   b.scale (1, dir);
204   b.translate (Offset (left_x, ypos));
205   
206
207   /*
208     Avoid colliding of the horizontal part with stafflines.
209     
210     should do me for slurs as well.
211
212    */
213   Array<Real> horizontal (b.solve_derivative (Offset (1,0)));
214   if (horizontal.size ())
215     {
216       /*
217         ugh. Doesnt work for non-horizontal curves.
218        */
219       Real y = b.curve_point (horizontal[0])[Y_AXIS];
220
221       Real ry = rint (y/staff_space) * staff_space;
222       Real diff = ry - y;
223       Real newy = y;
224       if (fabs (y) <= Staff_symbol_referencer::staff_radius (me)
225           && fabs (diff) < me->paper_l ()->get_var ("tie_staffline_clearance"))
226         {
227           newy = ry - 0.5 * staff_space * sign (diff) ;
228         }
229
230       Real y0 = b.control_ [0][Y_AXIS];
231       b.control_[2][Y_AXIS] = 
232       b.control_[1][Y_AXIS] =
233         (b.control_[1][Y_AXIS] - y0)  * ((newy - y0) / (y - y0)) + y0; 
234     }
235   else
236     programming_error ("Tie is nowhere horizontal");
237
238
239
240   SCM controls = SCM_EOL;
241   for (int i= 4; i--;)
242     controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
243   return controls;
244 }
245
246 MAKE_SCHEME_CALLBACK(Tie,set_spacing_rods);
247
248 /*
249   TODO: set minimum distances for begin/end of line
250  */
251 SCM
252 Tie::set_spacing_rods (SCM smob)  
253 {
254   Score_element*me = unsmob_element (smob);
255   Spanner*sp = dynamic_cast<Spanner*> (me);
256   Rod r;
257
258   r.item_l_drul_ [LEFT]=sp->get_bound (LEFT);
259   r.item_l_drul_ [RIGHT]=sp->get_bound (RIGHT);  
260   
261   r.distance_f_
262     = gh_scm2double (me->get_elt_property ("minimum-length"))
263     * me->paper_l ()->get_var ("staffspace");
264   r.add_to_cols ();
265   return SCM_UNSPECIFIED;
266 }
267
268 MAKE_SCHEME_CALLBACK(Tie,brew_molecule);
269 SCM
270 Tie::brew_molecule (SCM smob) 
271 {
272   Score_element*me = unsmob_element (smob);
273
274   SCM cp = me->get_elt_property ("control-points");
275   if (cp == SCM_EOL)
276     {
277       cp = get_control_points (smob);
278       me->set_elt_property ("control-points", cp);
279     }
280   
281   Real thick =
282     gh_scm2double (me->get_elt_property ("thickness"))
283     * me->paper_l ()->get_var ("stafflinethickness");
284
285   Bezier b;
286   int i = 0;
287   for (SCM s= cp; s != SCM_EOL; s = gh_cdr (s))
288     {
289       b.control_[i] = ly_scm2offset (gh_car (s));
290       i++;
291     }
292   
293    Molecule a = me->lookup_l ()->slur (b, Directional_element_interface::get (me) * thick, thick);
294    
295    return a.create_scheme ();
296 }
297
298