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