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