]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
* lily/tie-column.cc (set_directions): set directions only once.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "tie.hh"
10
11 #include <math.h>
12
13 #include "spanner.hh"
14 #include "lookup.hh"
15 #include "output-def.hh"
16 #include "rhythmic-head.hh"
17 #include "bezier.hh"
18 #include "paper-column.hh"
19 #include "warn.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "directional-element-interface.hh"
22 #include "bezier.hh"
23 #include "stem.hh"
24 #include "note-head.hh"
25 #include "tie-column.hh"
26
27 /*
28   tie: Connect two noteheads.
29
30   What if we have
31
32   c4 ~ \clef bass ; c4 or
33
34   c4 \staffchange c4
35
36   do we have non-horizontal ties then?
37 */
38
39 void
40 Tie::set_head (Grob *me, Direction d, Grob *h)
41 {
42   dynamic_cast<Spanner *> (me)->set_bound (d, h);
43   me->add_dependency (h);
44 }
45
46 Grob *
47 Tie::head (Grob *me, Direction d)
48 {
49   Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
50   if (Note_head::has_interface (it))
51     return it;
52   else
53     return 0;
54 }
55
56 int
57 Tie::get_column_rank (Grob *me, Direction d)
58 {
59   Spanner *span = dynamic_cast<Spanner *> (me);
60   Grob *h = head (me, d);
61   if (!h)
62     h = span->get_bound (d);
63
64   Grob *col = dynamic_cast<Item *> (h)->get_column ();
65   return Paper_column::get_rank (col);
66 }
67
68 Real
69 Tie::get_position (Grob *me)
70 {
71   Direction d = head (me, LEFT) ? LEFT : RIGHT;
72   return Staff_symbol_referencer::get_position (head (me, d));
73 }
74
75 /*
76   Default:  Put the tie oppositie of the stem [Wanske p231]
77
78   In case of chords: Tie_column takes over
79
80   The direction of the Tie is more complicated (See [Ross] p136 and
81   further).
82
83   (what about linebreaks? )
84 */
85 Direction
86 Tie::get_default_dir (Grob *me)
87 {
88   Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
89   Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
90   if (sl && sr)
91     {
92       if (get_grob_direction (sl) == UP
93           && get_grob_direction (sr) == UP)
94         return DOWN;
95     }
96   else if (sl || sr)
97     {
98       Item *s = sl ? sl : sr;
99       return -get_grob_direction (s);
100     }
101
102   return UP;
103 }
104
105 void
106 Tie::set_direction (Grob *me)
107 {
108   if (!get_grob_direction (me))
109     {
110       if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
111         Tie_column::set_directions (me->get_parent (Y_AXIS));
112       else
113         set_grob_direction (me, Tie::get_default_dir (me));
114     }
115 }
116
117
118 void
119 Tie::get_configuration (Grob *me_grob, Grob **common,
120                         Tie_configuration *conf)
121 {
122   Spanner *me = dynamic_cast<Spanner*> (me_grob);
123   if (!head (me, LEFT) && !head (me, RIGHT))
124     {
125       programming_error ("tie without heads");
126       me->suicide ();
127       return ;
128     }
129
130   Direction dir = CENTER;
131   
132   int tie_position = (int) Tie::get_position (me);
133   int staff_position = conf->position_;
134
135   if (conf->dir_)
136     {
137       dir = conf->dir_;
138     }
139   else
140     {
141       dir = get_grob_direction (me);
142       if (!dir)
143         dir = get_default_dir (me);
144     }
145
146   Real staff_space = Staff_symbol_referencer::staff_space (me);
147
148   bool in_between = true;
149   Interval attachments = conf->attachment_x_;
150   if (attachments.is_empty())
151     {
152       Direction d = LEFT;
153       Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
154       do
155         {
156           attachments[d]
157             = robust_relative_extent (me->get_bound (d),
158                                       common[X_AXIS],
159                                       X_AXIS)[-d]
160             - gap * d;
161         }
162       while (flip (&d) != LEFT);
163   
164       if (attachments.length () < 0.6 * staff_space)
165         {
166           /*
167             Let short ties start over note heads, instead of between.
168           */
169           Drul_array<bool> allow (true, true);
170
171           Direction d = LEFT;
172           do {
173             if (Note_head::has_interface (me->get_bound (d)))
174               {
175                 Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
176                 if (get_grob_direction (stem) == dir
177                     && -d == dir)
178                   allow[d] = false;
179               }
180           } while (flip (&d) != LEFT);
181
182           if (allow[LEFT] && allow[RIGHT])
183             {
184               staff_position += dir;
185               do
186                 {
187                   if (Note_head::has_interface (me->get_bound (d)))
188                     {
189                       Interval extent
190                         = robust_relative_extent (me->get_bound (d),
191                                                   common[X_AXIS], X_AXIS);
192
193                       attachments[d] = extent.linear_combination (- 0.5 * d);
194                       in_between = false;
195                     }
196                 }
197               while (flip (&d) != LEFT);
198             }
199         }
200     }
201   SCM details = me->get_property ("details");
202
203   SCM limit
204     = scm_assq (ly_symbol2scm ("height-limit"), details);
205
206   Real h_inf = robust_scm2double (scm_cdr (limit), 0.75) * staff_space;
207   Real r_0 = robust_scm2double (scm_cdr (scm_assq (ly_symbol2scm ("ratio"), details)),
208                                 .333);
209
210   Bezier b = slur_shape (attachments.length(),
211                          h_inf, r_0);
212   b.scale (1, dir);
213   
214   Offset middle = b.curve_point (0.5);
215   Offset edge = b.curve_point (0.0);
216
217   staff_position = int (rint (staff_position));
218   
219   Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
220   bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) staff_position));
221   bool fits_in_space =
222     (dy < 0.6 * staff_space);
223   
224   /*
225     Avoid dot
226    */
227   Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
228   if (left_dot && in_space)
229     {
230       if (staff_position == Staff_symbol_referencer::get_position (left_dot))
231         {
232           staff_position += dir;
233           in_space = false;
234         }
235     }
236
237   /*
238     Avoid flag.
239    */
240   Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
241   if (left_stem)
242     {
243       Stencil flag = Stem::get_translated_flag (left_stem);
244       Real y = staff_position * staff_space * 0.5;
245       if (flag.extent (Y_AXIS).contains (y))
246         {
247           staff_position += dir;
248           in_space = !in_space;
249         }
250     }
251
252   if (in_space != fits_in_space)
253     {
254       if (in_space)
255         {
256           staff_position += dir;
257         }
258       else
259         {
260           in_space = true;
261           staff_position += dir;
262         }
263     }
264
265
266   /*
267     Putting larger in-space ties next to the notes forces
268     the edges to be opposite (Y-wise) to the tie direction.
269    */
270   if (staff_position == tie_position
271       && in_space
272       && dy > 0.3 * staff_space)
273     {
274       staff_position += 2 * dir; 
275     }
276
277   if (!in_between
278       && in_space
279       && fabs (staff_position - tie_position) <= 1)
280     staff_position += 2*dir;
281   
282   
283   if (in_space)
284     {
285       if (fabs (dy) < 0.4 * staff_space)
286         {
287           /*
288             vertically center in space.
289           */
290           Offset middle = b.curve_point (0.5);
291           Offset edge = b.curve_point (0.0);
292
293           Real center = (edge[Y_AXIS] + middle[Y_AXIS])/2.0;
294
295           conf->delta_y_ = - center;
296         }
297       else
298         {
299           conf->delta_y_ = 
300             dir * staff_space * (- 0.3);
301         }
302     }
303   else
304     {
305       Real where = 0.5 * dir;
306       
307       Real rounding_dy = (where - middle[Y_AXIS]);
308       conf->delta_y_ = rounding_dy;
309
310       if (dir * b.curve_point (0.0)[Y_AXIS] <
311           dir * tie_position * 0.5 * staff_space)
312         conf->delta_y_ +=  staff_space * dir; 
313     }
314
315   conf->dir_ = dir;
316   conf->position_ = staff_position;
317   conf->attachment_x_ = attachments;
318 }
319
320
321 void
322 Tie::set_default_control_points (Grob *me_grob)
323 {
324   Spanner *me = dynamic_cast<Spanner*> (me_grob);
325   Grob *common[NO_AXES] = {
326     0, 0
327   };
328   for (int a = X_AXIS; a < NO_AXES; a++)
329     {
330       Axis ax ((Axis) a); 
331       common[ax] = me->get_bound (LEFT)->common_refpoint (me, ax); 
332       common[ax] = me->get_bound (RIGHT)->common_refpoint (common[a], ax); 
333     }
334   
335   Tie_configuration conf;
336   if (!get_grob_direction (me))
337     set_grob_direction (me, get_default_dir (me));
338
339   int tie_position = (int) Tie::get_position (me);
340   conf.position_ = tie_position;
341
342   
343   get_configuration (me, common, &conf);
344   set_control_points (me, common, conf);
345 }
346
347 void
348 Tie::set_control_points (Grob *me,
349                          Grob **common,
350                          Tie_configuration const &conf)
351 {
352   SCM details = me->get_property ("details");
353   SCM limit
354     = scm_assq (ly_symbol2scm ("height-limit"), details);
355
356   Real staff_space = Staff_symbol_referencer::staff_space (me);
357   Real h_inf = robust_scm2double (scm_cdr (limit), 0.75) * staff_space;
358   Real r_0 = robust_scm2double (scm_cdr (scm_assq (ly_symbol2scm ("ratio"),
359                                                    details)),
360                                 .333);
361
362   Bezier b = slur_shape (conf.attachment_x_.length(),
363                          h_inf, r_0);
364   b.scale (1, conf.dir_);
365   b.translate (Offset (conf.attachment_x_[LEFT]
366                        - me->relative_coordinate (common[X_AXIS], X_AXIS),
367                        0.5 * conf.position_ * staff_space 
368                        + conf.delta_y_
369                        ));
370   
371   SCM controls = SCM_EOL;
372   for (int i = 4; i--;)
373     controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
374
375   me->set_property ("control-points", controls);
376 }
377
378
379
380 MAKE_SCHEME_CALLBACK (Tie, print, 1);
381 SCM
382 Tie::print (SCM smob)
383 {
384   Grob *me = unsmob_grob (smob);
385
386   if (CENTER == get_grob_direction (me))
387     set_direction (me);
388       
389   SCM cp = me->get_property ("control-points");
390   if (!scm_is_pair (cp))
391     {
392       set_default_control_points (me);
393       cp = me->get_property ("control-points");
394     }
395
396   if (!scm_is_pair (cp))
397     return Stencil ().smobbed_copy ();
398
399   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
400   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
401   Real thick = base_thick * staff_thick;
402
403   Bezier b;
404   int i = 0;
405   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
406     {
407       b.control_[i] = ly_scm2offset (scm_car (s));
408       i++;
409     }
410
411   Stencil a;
412
413   SCM p = me->get_property ("dash-period");
414   SCM f = me->get_property ("dash-fraction");
415   if (scm_is_number (p) && scm_is_number (f))
416     a = Lookup::dashed_slur (b,
417                              thick,
418                              robust_scm2double (p, 1.0),
419                              robust_scm2double (f, 0));
420   else
421     a = Lookup::slur (b,
422                       get_grob_direction (me) * staff_thick,
423                       thick);
424
425   return a.smobbed_copy ();
426 }
427
428 ADD_INTERFACE (Tie,
429                "tie-interface",
430                
431                "A tie connecting two noteheads.\n",
432                
433                "control-points "
434                "dash-fraction "
435                "dash-period "
436                "details "
437                "direction "
438                "thickness "
439                "x-gap ");
440
441 int
442 Tie_configuration::compare (Tie_configuration const &a,
443                             Tie_configuration const &b)
444 {
445   if (a.position_ - b.position_)
446     return sign (a.position_ - b.position_);
447   return sign (a.dir_ - b.dir_);
448 }
449                             
450
451 Tie_configuration::Tie_configuration ()
452 {
453   dir_ = CENTER;
454   position_ = 0;
455   delta_y_ = 0.0;
456 }