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