lilypond-0.1.57
[lilypond.git] / lily / slur.cc
blobfcc57eab1d7050b16affe003f82451fd585ff07a
1 /*
2 slur.cc -- implement Slur
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 /*
10 TODO:
12 think about crossing stems.
13 Begin and end should be treated as a Script.
15 #include "slur.hh"
16 #include "scalar.hh"
17 #include "lookup.hh"
18 #include "paper-def.hh"
19 #include "note-column.hh"
20 #include "stem.hh"
21 #include "p-col.hh"
22 #include "molecule.hh"
23 #include "debug.hh"
24 #include "boxes.hh"
25 #include "bezier.hh"
26 #include "main.hh"
28 IMPLEMENT_IS_TYPE_B1(Slur,Spanner);
30 void
31 Slur::add (Note_column*n)
33 encompass_arr_.push (n);
34 add_dependency (n);
37 void
38 Slur::set_default_dir ()
40 dir_ = DOWN;
41 for (int i=0; i < encompass_arr_.size (); i ++)
43 if (encompass_arr_[i]->dir_ < 0)
45 dir_ = UP;
46 break;
51 void
52 Slur::do_add_processing ()
54 set_bounds (LEFT, encompass_arr_[0]);
55 if (encompass_arr_.size () > 1)
56 set_bounds (RIGHT, encompass_arr_.top ());
59 void
60 Slur::do_pre_processing ()
62 // don't set directions
65 void
66 Slur::do_substitute_dependency (Score_elem*o, Score_elem*n)
68 int i;
69 while ((i = encompass_arr_.find_i ((Note_column*)o->item ())) >=0)
71 if (n)
72 encompass_arr_[i] = (Note_column*)n->item ();
73 else
74 encompass_arr_.del (i);
78 static int
79 Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
81 return Item::left_right_compare (n1, n2);
84 void
85 Slur::do_post_processing ()
87 encompass_arr_.sort (Note_column_compare);
88 if (!dir_)
89 set_default_dir ();
90 Real interline_f = paper ()->interline_f ();
91 Real inter_f = interline_f / 2;
93 /*
94 [OSU]: slur and tie placement
96 slurs:
97 * x = centre of head (upside-down: inner raakpunt stem) - d * gap
99 * y = length < 5ss : horizontal raakpunt + d * 0.25 ss
100 y = length >= 5ss : y next interline - d * 0.25 ss
101 --> height <= 5 length ?? we use <= 3 length, now...
103 * suggested gap = ss / 5;
105 // jcn: 1/5 seems so small?
106 Real gap_f = interline_f / 2; // 5;
108 Drul_array<Note_column*> extrema;
109 extrema[LEFT] = encompass_arr_[0];
110 extrema[RIGHT] = encompass_arr_.top ();
112 Direction d=LEFT;
113 Real nw_f = paper ()->note_width ();
117 if (extrema[d] != spanned_drul_[d])
119 dx_f_drul_[d] = -d
120 *(spanned_drul_[d]->width ().length () -0.5*nw_f);
122 else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_)
124 dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_->height ()[dir_]);
125 /* normal slur from notehead centre to notehead centre, minus gap */
126 // ugh: diff between old and new slurs
127 if (!experimental_features_global_b)
128 dx_f_drul_[d] += -d * gap_f;
129 else
130 dx_f_drul_[d] += 0.5 * nw_f - d * gap_f;
132 else
134 dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()[dir_])* inter_f;
136 dy_f_drul_[d] += dir_ * interline_f;
138 while (flip(&d) != LEFT);
141 Real
142 Slur::height_f () const
144 Bezier_bow bow (paper ());
145 Array<Offset> notes = get_notes ();
146 bow.set (notes, dir_);
148 Real height = 0;
149 Real dy1 = bow.calc_f (height);
150 if (!dy1)
151 return height;
153 height = dy1;
154 bow.set (notes, dir_);
155 Real dy2 = bow.calc_f (height);
156 if (!dy2)
157 return height;
159 if (abs (dy2 - dy1) < paper ()->rule_thickness ())
160 return height;
163 Assume
164 dy = B (h)
165 with
166 B (h) = a * h + b;
168 Then we get for height = h{dy=0}
170 Real a = (dy2 - dy1) / dy1;
171 Real b = dy1;
172 height = -b / a;
174 if (check_debug && !monitor->silent_b ("Slur"))
176 cout << "************" << endl;
177 cout << "dy1: " << dy1 << endl;
178 cout << "dy2: " << dy2 << endl;
179 cout << "a: " << a << endl;
180 cout << "b: " << b << endl;
181 cout << "h: " << height << endl;
184 return height;
187 Molecule*
188 Slur::brew_molecule_p () const
190 if (!experimental_features_global_b)
191 return Bow::brew_molecule_p ();
193 Molecule* mol_p = new Molecule;
195 Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
197 Real dx_f = width ().length ();
198 dx_f += (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
200 Atom a = paper ()->lookup_l ()->control_slur (get_controls (), dx_f, dy_f);
202 Real interline_f = paper ()->interline_f ();
203 Real gap_f = interline_f / 2; // 5;
204 Real nw_f = paper ()->note_width ();
205 a.translate (Offset (dx_f + 0.5 * nw_f + gap_f, dy_f + dy_f_drul_[LEFT]));
206 mol_p->add (a);
207 return mol_p;
210 Array<Offset>
211 Slur::get_notes () const
213 Real interline = paper ()->interline_f ();
214 Real notewidth = paper ()->note_width ();
215 Real internote = interline / 2;
217 Stem* left_stem = encompass_arr_[0]->stem_l_;
218 Real left_x = left_stem->hpos_f ();
219 // ugh, do bow corrections (see brew_mol)
220 left_x += dx_f_drul_[LEFT] + 0.5 * notewidth;
222 // ugh, do bow corrections (see brew_mol)
223 Real left_y = dy_f_drul_[LEFT];
224 // ugh, where does this asymmetry come from?
225 if (dir_ == DOWN)
226 left_y -= dir_ * internote;
229 urg, corrections for broken slurs: extra begin or end position
231 int first = 0;
232 int n = encompass_arr_.size ();
233 if (encompass_arr_[0] != spanned_drul_[LEFT])
235 n += 1;
236 first = 1;
237 left_x = spanned_drul_[LEFT]->width ().length ();
238 left_y = 0;
240 if (encompass_arr_.top () != spanned_drul_[RIGHT])
241 n += 1;
243 Array<Offset> notes;
244 notes.set_size (n);
246 Real dx = width ().length ();
247 dx += (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
248 dx = dx <? 1000;
249 dx = dx >? 2 * interline;
251 Real dy = (dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
252 if (abs (dy) > 1000)
253 dy = sign (dy) * 1000;
255 notes[0].x () = 0;
256 notes[0].y () = 0;
257 notes[n - 1].x () = dx;
258 notes[n - 1].y () = dy;
259 for (int i = 1; i < n - 1; i++)
261 Stem* stem = encompass_arr_[i - first]->stem_l_;
263 set x to middle of notehead or on exact x position of stem,
264 according to slur direction
266 Real x = stem->hpos_f () - left_x + notewidth / 2;
267 if (stem->dir_ != dir_)
268 x += notewidth / 2;
269 else if (stem->dir_ == UP)
270 x += notewidth;
271 Real y = stem->dir_ == dir_ ? stem->stem_end_f ()
272 : stem->stem_begin_f () + 2.5 * dir_;
275 leave a gap: slur mustn't touch head/stem
277 y += 2.5 * dir_;
278 y *= internote;
279 y -= left_y;
281 notes[i].x () = x;
282 notes[i].y () = y;
284 return notes;
287 Array<Offset>
288 Slur::get_controls () const
290 Bezier_bow b (paper ());
291 b.set (get_notes (), dir_);
292 b.calc ();
293 Array<Offset> controls;
294 controls.set_size (8);
295 for (int i = 0; i < 4; i++)
296 controls[i] = b.control_[i];
297 for (int i = 0; i < 4; i++)
298 controls[i + 4] = b.return_[i];
299 return controls;