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