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