summaryrefslogtreecommitdiff
path: root/lily/slur.cc
blob: ef773b2cc6e5baf54f18d50bccd710572fa5a83f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
  slur.cc -- implement  Slur

  source file of the GNU LilyPond music typesetter

  (c) 1996,  1997--1998, 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
    Jan Nieuwenhuizen <janneke@gnu.org>
*/

/*
  [TODO]
    * URG: share code with tie
    * begin and end should be treated as a Script.
    * damping
    * slur from notehead to stemend: c''()b''
 */

#include "slur.hh"
#include "scalar.hh"
#include "lookup.hh"
#include "paper-def.hh"
#include "note-column.hh"
#include "stem.hh"
#include "p-col.hh"
#include "molecule.hh"
#include "debug.hh"
#include "box.hh"
#include "bezier.hh"
#include "encompass-info.hh"
#include "main.hh"


Slur::Slur ()
{
}

void
Slur::add_column (Note_column*n)
{
  if (!n->head_l_arr_.size ())
    warning (_ ("Putting slur over rest."));
  encompass_arr_.push (n);
  n->stem_l_->slur_l_ = this;
  add_dependency (n);
}

void
Slur::set_default_dir ()
{
  dir_ = DOWN;
  for (int i=0; i < encompass_arr_.size (); i ++) 
    {
      if (encompass_arr_[i]->dir_ < 0) 
	{
	  dir_ = UP;
	  break;
	}
    }
}

void
Slur::do_add_processing ()
{
  set_bounds (LEFT, encompass_arr_[0]);    
  if (encompass_arr_.size () > 1)
    set_bounds (RIGHT, encompass_arr_.top ());
}

void
Slur::do_pre_processing ()
{
  // don't set directions
}

void
Slur::do_substitute_dependency (Score_element*o, Score_element*n)
{
  int i;
  while ((i = encompass_arr_.find_i (dynamic_cast<Note_column *> (o))) >=0) 
    {
      if (n)
	encompass_arr_[i] = dynamic_cast<Note_column *> (n);
      else
	encompass_arr_.del (i);
    }
}

static int 
Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
{
  return Item::left_right_compare (n1, n2);
}

void
Slur::do_post_processing ()
{
  encompass_arr_.sort (Note_column_compare);
  if (!dir_)
    set_default_dir ();

  Real interline_f = paper ()->interline_f ();
  Real internote_f = interline_f / 2;
  // URG
  Real notewidth_f = paper ()->note_width () * 0.8;

  /* 
   [OSU]: slur and tie placement

   slurs:
   * x = centre of head (upside-down: inner raakpunt stem) - d * gap

   * y = length < 5ss : horizontal raakpunt + d * 0.25 ss
     y = length >= 5ss : y next interline - d * 0.25 ss
     --> height <= 5 length ?? we use <= 3 length, now...
   */
  
  Real gap_f = paper ()->get_var ("slur_x_gap");

  Drul_array<Note_column*> extrema;
  extrema[LEFT] = encompass_arr_[0];
  extrema[RIGHT] = encompass_arr_.top ();

  Direction d=LEFT;
 
  do 
    {
      /*
        broken slur
       */
      if (extrema[d] != spanned_drul_[d]) 
	{
	  // ugh -- check if needed
	  dx_f_drul_[d] = -d 
	    *(spanned_drul_[d]->extent (X_AXIS).length () - 0.5 * notewidth_f);

	  // prebreak
	  if (d == RIGHT)
	    {
	      dx_f_drul_[LEFT] = spanned_drul_[LEFT]->extent (X_AXIS).length ();

	      // urg -- check if needed
	      if (encompass_arr_.size () > 1)
		dx_f_drul_[RIGHT] += notewidth_f;
	    }
	}
      /*
        normal slur
       */
      else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_ 
	       && extrema[d]->head_l_arr_.size ()) 
        {
	  Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
	  dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_-> extent (Y_AXIS)[dir_]);
	  dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
	  if (dir_ == extrema[d]->stem_l_->dir_)
	    {
	      if (dir_ == d)
		dx_f_drul_[d] += 0.5 * (dir_ * d) * d * notewidth_f;
	      else
		dx_f_drul_[d] += 0.25 * (dir_ * d) * d * notewidth_f;
	    }
	}
	else 
	  {
	    Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
	    dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
				       [dir_]) * internote_f;
	    dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
	}
	dy_f_drul_[d] += dir_ * interline_f;
	if (extrema[d]->stem_l_ && (dir_ == extrema[d]->stem_l_->dir_))
	  dy_f_drul_[d] -= dir_ * internote_f;
      }
  while (flip(&d) != LEFT);

  // now that both are set, do dependent
  do 
    {
      /*
        broken slur
       */
      if (extrema[d] != spanned_drul_[d]) 
        {
	  Direction u = d;
	  flip(&u);

	  // postbreak
	  if (d == LEFT)
	    dy_f_drul_[u] += dir_ * internote_f;

	  dy_f_drul_[d] = dy_f_drul_[(Direction)-d];
	}
     }
  while (flip(&d) != LEFT);

  /*
    Avoid too steep slurs.
      * slur from notehead to stemend: c''()b''
   */
  Real damp_f = paper ()->get_var ("slur_slope_damping");
  Offset d_off = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
    dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
  d_off.x () += extent (X_AXIS).length ();

  Real ratio_f = abs (d_off.y () / d_off.x ());
  if (ratio_f > damp_f)
    dy_f_drul_[(Direction)(- dir_ * sign (d_off.y ()))] +=
      dir_ * (ratio_f - damp_f) * d_off.x ();
}

Array<Offset>
Slur::get_encompass_offset_arr () const
{
  Real notewidth = paper ()->note_width () * 0.8;
  Real gap = paper ()->get_var ("slur_x_gap");
  Real internote = paper ()->internote_f ();

  Offset left = Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
  left.x () += encompass_arr_[0]->stem_l_->hpos_f ();

  /*
    <URG>
    i don't understand these two, but *must* for symmetry 
    look at encompass array: 
       lilypond -D input/test/slur-symmetry*.ly
       lilypond -D input/test/sleur.ly

    do_post_processing should have calculated these into
    dx_f_drul_[], no??

   */

  if (dir_ != encompass_arr_[0]->stem_l_->dir_)
    left.x () += - 0.5 * notewidth * encompass_arr_[0]->stem_l_->dir_
      + gap;
  else if (encompass_arr_[0]->stem_l_->dir_ == UP)
    left.x () -= notewidth;

  if ((dir_ == encompass_arr_[0]->stem_l_->dir_) 
    && (encompass_arr_[0]->stem_l_->dir_ == DOWN))
    left.y () -= internote * encompass_arr_[0]->stem_l_->dir_;
  /* </URG> */

  Offset d = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
    dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
  d.x () += extent (X_AXIS).length ();

  int first = 1;
  int last = encompass_arr_.size () - 1;

  // prebreak
  if (encompass_arr_.top () != spanned_drul_[RIGHT])
    last++;

  // postbreak
  if (encompass_arr_[0] != spanned_drul_[LEFT])
    first--;

  Array<Offset> notes;
  notes.push (Offset (0,0));

  for (int i = first; i < last; i++)
    {
      Encompass_info info (encompass_arr_[i], dir_);
      notes.push (info.o_ - left);
    }
  Encompass_info info (encompass_arr_[encompass_arr_.size () - 1], dir_);
  // urg:
  Slur* urg = (Slur*)this;
  urg->interstaff_f_ = info.interstaff_f_;
  d.y () += interstaff_f_;

  // prebreak
  if (interstaff_f_ && (encompass_arr_.top () != spanned_drul_[RIGHT]))
    {
      Encompass_info info (encompass_arr_[encompass_arr_.size () - 1], dir_);
      d.y () -= info.o_.y () - interstaff_f_;
    }

  notes.push (d);

  return notes;
}

Interval
Slur::do_width () const
{
  Real min_f = paper ()->get_var ("slur_x_minimum");
  Interval width_int = Bow::do_width ();
  return width_int.length () < min_f ? Interval (0, min_f) : width_int;
}

Array<Rod>
Slur::get_rods () const
{
  Array<Rod> a;
  Rod r;
  r.item_l_drul_ = spanned_drul_;
  r.distance_f_ = do_width ().length ();
  a.push (r);
  return a;
}