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