]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
release: 1.3.68
[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 "lookup.hh"
11 #include "paper-def.hh"
12 #include "tie.hh"
13 #include "rhythmic-head.hh"
14 #include "bezier.hh"
15 #include "paper-column.hh"
16 #include "debug.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "directional-element-interface.hh"
19 #include "molecule.hh"
20 #include "bezier-bow.hh"
21 #include "stem.hh"
22
23 void
24 Tie::set_head (Score_element*me,Direction d, Item * head_l)
25 {
26   assert (!head (me,d));
27   index_set_cell (me->get_elt_pointer ("heads"), d, head_l->self_scm_);
28   
29   dynamic_cast<Spanner*> (me)->set_bound (d, head_l);
30   me->add_dependency (head_l);
31 }
32
33 Tie::Tie(SCM s)
34   : Spanner (s)
35 {
36   dy_f_drul_[LEFT] = dy_f_drul_[RIGHT] = 0.0;
37   dx_f_drul_[LEFT] = dx_f_drul_[RIGHT] = 0.0;
38 }
39 void
40 Tie::set_interface (Score_element*me)
41 {
42   me->set_elt_pointer ("heads", gh_cons (SCM_EOL, SCM_EOL));
43 }
44
45 Rhythmic_head* 
46 Tie::head (Score_element*me, Direction d) 
47 {
48   SCM c = me->get_elt_pointer ("heads");
49   c = index_cell (c, d);
50
51   return dynamic_cast<Rhythmic_head*> (unsmob_element (c));  
52 }
53
54 Real
55 Tie::position_f (Score_element*me) 
56 {
57   return head (me,LEFT)
58     ? Staff_symbol_referencer_interface (head (me,LEFT)).position_f ()
59     : Staff_symbol_referencer_interface (head (me,RIGHT)).position_f () ;  
60 }
61
62
63 /*
64   ugh: direction of the Tie is more complicated.  See [Ross] p136 and further
65  */
66 Direction
67 Tie::get_default_dir (Score_element*me) 
68 {
69   Stem * sl =  head(me,LEFT) ? head (me,LEFT)->stem_l () :0;
70   Stem * sr =  head(me,RIGHT) ? head (me,RIGHT)->stem_l () :0;  
71
72   if (sl && Directional_element_interface (sl).get () == UP
73       && sr && Directional_element_interface (sr).get () == UP)
74     return DOWN;
75   else
76     return UP;
77 }
78
79
80
81 MAKE_SCHEME_SCORE_ELEMENT_CALLBACK(Tie,after_line_breaking);
82 SCM
83 Tie::after_line_breaking (SCM smob)
84 {
85   Tie*me = dynamic_cast<Tie*> (unsmob_element (smob));
86   
87   if (!head (me,LEFT) && !head (me,RIGHT))
88     {
89       programming_error ("Tie without heads.");
90       me->suicide ();
91       return SCM_UNDEFINED;
92     }
93
94   if (!Directional_element_interface (me).get ())
95     Directional_element_interface (me).set (Tie::get_default_dir (me));
96   
97   Real staff_space = Staff_symbol_referencer_interface (me).staff_space ();
98   Real half_space = staff_space / 2;
99   Real x_gap_f = me->paper_l ()->get_var ("tie_x_gap");
100   Real y_gap_f = me->paper_l ()->get_var ("tie_y_gap");
101
102   /* 
103    Slur and tie placement [OSU]
104
105    Ties:
106
107        * x = inner vertical tangent - d * gap
108
109    */
110
111
112   /*
113     OSU: not different for outer notes, so why all me code?
114     ie,  can we drop me, or should it be made switchable.
115    */
116   if (head (me,LEFT))
117     me->dx_f_drul_[LEFT] = Tie::head (me,LEFT)->extent (X_AXIS).length ();
118   else
119     me->dx_f_drul_[LEFT] = dynamic_cast<Spanner*>(me)->get_broken_left_end_align ();
120   me->dx_f_drul_[LEFT] += x_gap_f;
121   me->dx_f_drul_[RIGHT] -= x_gap_f;
122
123   /* 
124    Slur and tie placement [OSU]  -- check me
125
126    Ties:
127
128        * y = dx <  5ss: horizontal tangent
129          y = dx >= 5ss: y next interline - d * 0.25 ss
130
131          which probably means that OSU assumes that
132
133             dy <= 5 dx
134
135          for smal slurs
136    */
137
138
139   Real ypos = Tie::position_f (me);
140
141   Real y_f = half_space * ypos; 
142   int ypos_i = int (ypos);
143  
144   Real dx_f = me->extent (X_AXIS).length () + me->dx_f_drul_[RIGHT] - me->dx_f_drul_[LEFT];
145   Direction dir = Directional_element_interface (me).get();
146   if (dx_f < me->paper_l ()->get_var ("tie_staffspace_length"))
147     {
148       if (abs (ypos_i) % 2)
149         y_f += dir * half_space;
150       y_f += dir * y_gap_f;
151     }
152   else
153     {
154       if (! (abs (ypos_i) % 2))
155         y_f += dir * half_space;
156       y_f += dir * half_space;
157       y_f -= dir * y_gap_f;
158     }
159   
160   me->dy_f_drul_[LEFT] = me->dy_f_drul_[RIGHT] = y_f;
161
162   return SCM_UNDEFINED;
163 }
164
165
166
167 Array<Rod>
168 Tie::get_rods () const
169 {
170   Array<Rod> a;
171   Rod r;
172
173   r.item_l_drul_ [LEFT]=get_bound (LEFT);
174   r.item_l_drul_ [RIGHT]=get_bound (RIGHT);  
175   
176   r.distance_f_ = paper_l ()->get_var ("tie_x_minimum");
177   a.push (r);
178   return a;
179 }
180
181 MAKE_SCHEME_SCORE_ELEMENT_CALLBACK(Tie,brew_molecule);
182
183 SCM
184 Tie::brew_molecule (SCM smob) 
185 {
186   Score_element*me = unsmob_element (smob);
187   Real thick = me->paper_l ()->get_var ("tie_thickness");
188   Bezier one = dynamic_cast<Tie*> (me)->get_curve ();
189
190   Molecule a;
191   SCM d =  me->get_elt_property ("dashed");
192   if (gh_number_p (d))
193     a = me->lookup_l ()->dashed_slur (one, thick, gh_scm2int (d));
194   else
195     a = me->lookup_l ()->slur (one, Directional_element_interface (me).get () * thick, thick);
196   
197   return a.create_scheme(); 
198 }
199
200
201
202 Bezier
203 Tie::get_curve () const
204 {
205   Score_element*me = (Score_element*)this;
206   Direction d (Directional_element_interface (me).get ());
207   Bezier_bow b (get_encompass_offset_arr (), d);
208
209   Real staff_space = Staff_symbol_referencer_interface (me).staff_space ();
210   Real h_inf = paper_l ()->get_var ("tie_height_limit_factor") * staff_space;
211   Real r_0 = paper_l ()->get_var ("tie_ratio");
212
213   b.set_default_bezier (h_inf, r_0);
214   Bezier c = b.get_bezier ();
215
216   /* should do me for slurs as well. */
217   Array<Real> horizontal (c.solve_derivative (Offset (1,0)));
218
219   if (horizontal.size ())
220     {
221       /*
222         ugh. Doesnt work for non-horizontal curves.
223        */
224       Real y = c.curve_point (horizontal[0])[Y_AXIS];
225
226       Real ry = rint (y/staff_space) * staff_space;
227       Real diff = ry - y;
228       Real newy = y;
229       if (fabs (y) <= 2.0
230           && fabs (diff) < paper_l ()->get_var ("tie_staffline_clearance"))
231         {
232           newy = ry - 0.5 * staff_space * sign (diff) ;
233         }
234
235       Real y0 = c.control_ [0][Y_AXIS];
236       c.control_[2][Y_AXIS] = 
237       c.control_[1][Y_AXIS] =
238         (c.control_[1][Y_AXIS] - y0)  * ((newy - y0) / (y - y0)) + y0; 
239     }
240   else
241     programming_error ("Tie is nowhere horizontal");
242   return c;
243 }
244
245 Array<Offset>
246 Tie::get_encompass_offset_arr () const
247 {
248   Array<Offset> offset_arr;
249   offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
250   offset_arr.push (Offset (spanner_length () + dx_f_drul_[RIGHT],
251                            dy_f_drul_[RIGHT]));
252                       
253   return offset_arr;
254 }
255
256