]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
(print): try Tie_column::set_directions () if
[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 int
69 Tie::get_position (Grob *me)
70 {
71   Direction d = head (me, LEFT) ? LEFT : RIGHT;
72   return (int) 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 Interval
118 get_default_attachments (Spanner *me, Grob *common, Real gap,
119                          int *staff_position,
120                          bool *in_between
121                          )
122 {
123   Real staff_space = Staff_symbol_referencer::staff_space (me);
124   Direction dir = get_grob_direction (me);
125   Interval attachments;
126   Direction d = LEFT;
127   do
128     {
129       attachments[d]
130         = robust_relative_extent (me->get_bound (d),
131                                   common,
132                                   X_AXIS)[-d]
133         - gap * d;
134     }
135   while (flip (&d) != LEFT);
136
137   if (attachments.length () < 0.6 * staff_space)
138     {
139       /*
140         Let short ties start over note heads, instead of between.
141       */
142       Drul_array<bool> allow (true, true);
143
144       Direction d = LEFT;
145       do {
146         if (Note_head::has_interface (me->get_bound (d)))
147           {
148             Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
149             if (get_grob_direction (stem) == dir
150                 && -d == dir)
151               allow[d] = false;
152           }
153       } while (flip (&d) != LEFT);
154
155       if (allow[LEFT] && allow[RIGHT])
156         {
157           *staff_position += dir;
158           do
159             {
160               if (Note_head::has_interface (me->get_bound (d)))
161                 {
162                   Interval extent
163                     = robust_relative_extent (me->get_bound (d),
164                                               common, X_AXIS);
165
166                   attachments[d] = extent.linear_combination (- 0.5 * d);
167                   *in_between = false;
168                 }
169             }
170           while (flip (&d) != LEFT);
171         }
172     }
173
174   return attachments;
175 }  
176
177 Interval
178 get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
179                         Real y)
180 {
181   Interval attachments;
182   Direction d = LEFT;
183   do
184     {
185       attachments[d] = skyline_height (skylines[d], y, -d);
186     }
187   while (flip (&d) != LEFT);
188   
189   return attachments;
190 }
191                         
192 void
193 Tie::get_configuration (Grob *me_grob, Grob *common,
194                         Tie_configuration *conf,
195                         Drul_array< Array < Skyline_entry > > const *skylines,
196                         Tie_details const &details
197                         )
198 {
199   Spanner *me = dynamic_cast<Spanner*> (me_grob);
200   if (!head (me, LEFT) && !head (me, RIGHT))
201     {
202       programming_error ("tie without heads");
203       me->suicide ();
204       return ;
205     }
206
207   Direction dir = CENTER;
208   
209   int tie_position = (int) Tie::get_position (me);
210   int staff_position = (int) conf->position_;
211
212   if (conf->dir_)
213     {
214       dir = conf->dir_;
215     }
216   else
217     {
218       dir = get_grob_direction (me);
219       if (!dir)
220         dir = get_default_dir (me);
221     }
222
223   Real staff_space = details.staff_space_;
224
225   bool in_between = true;
226   Interval attachments = conf->attachment_x_;
227   Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
228   if (attachments.is_empty())
229     {
230       if (!skylines)
231         attachments = get_default_attachments (me, common, gap,
232                                                &staff_position,
233                                                &in_between);
234       else
235         {
236           Real y = staff_space * 0.5 * staff_position;
237           attachments = get_skyline_attachment (*skylines, y);
238           attachments.widen (-gap);
239         }
240     }
241
242   Bezier b = slur_shape (attachments.length(),
243                          details.height_limit_,
244                          details.ratio_);
245   b.scale (1, dir);
246   
247   Offset middle = b.curve_point (0.5);
248   Offset edge = b.curve_point (0.0);
249
250   staff_position = int (rint (staff_position));
251   
252   Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
253   bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) staff_position));
254   bool fits_in_space =
255     (dy < 0.6 * staff_space);
256   
257   /*
258     Avoid dot
259    */
260   Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
261   if (left_dot && in_space)
262     {
263       if (staff_position == Staff_symbol_referencer::get_position (left_dot))
264         {
265           staff_position += dir;
266           in_space = false;
267         }
268     }
269
270   /*
271     Avoid flag.
272    */
273   Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
274   if (left_stem)
275     {
276       Stencil flag = Stem::get_translated_flag (left_stem);
277       Real y = staff_position * staff_space * 0.5;
278       if (flag.extent (Y_AXIS).contains (y))
279         {
280           staff_position += dir;
281           in_space = !in_space;
282         }
283     }
284
285   if (in_space != fits_in_space)
286     {
287       if (in_space)
288         {
289           staff_position += dir;
290         }
291       else
292         {
293           in_space = true;
294           staff_position += dir;
295         }
296     }
297
298
299   /*
300     Putting larger in-space ties next to the notes forces
301     the edges to be opposite (Y-wise) to the tie direction.
302    */
303   if (staff_position == tie_position
304       && in_space
305       && dy > 0.3 * staff_space)
306     {
307       staff_position += 2 * dir; 
308     }
309
310   if (!in_between
311       && in_space
312       && fabs (staff_position - tie_position) <= 1)
313     staff_position += 2*dir;
314   
315   
316   if (in_space)
317     {
318       if (fabs (dy) < 0.45 * staff_space)
319         {
320           /*
321             vertically center in space.
322           */
323           conf->dir_ = dir;        
324           conf->attachment_x_ = attachments;
325           conf->center_tie_vertically(details);
326         }
327       else
328         {
329           conf->delta_y_ = 
330             dir * staff_space * (- 0.3);
331         }
332     }
333   else
334     {
335       Real where = 0.5 * dir;
336       
337       Real rounding_dy = (where - middle[Y_AXIS]);
338       conf->delta_y_ = rounding_dy;
339
340       if (dir * b.curve_point (0.0)[Y_AXIS] <
341           dir * tie_position * 0.5 * staff_space)
342         conf->delta_y_ +=  staff_space * dir; 
343     }
344
345   conf->dir_ = dir;
346   conf->position_ = staff_position;
347
348   if (skylines)
349     {
350       Real half_space = 0.5 * staff_space;
351       Real y = staff_position * half_space;
352       
353       attachments = get_skyline_attachment (*skylines, y);
354       
355       attachments.widen (-gap);
356     }
357   conf->attachment_x_ = attachments;
358 }
359
360
361 void
362 Tie::set_default_control_points (Grob *me_grob)
363 {
364   Spanner *me = dynamic_cast<Spanner*> (me_grob);
365   Grob *common  = me;
366   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
367   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
368   
369   Tie_configuration conf;
370   if (!get_grob_direction (me))
371     set_grob_direction (me, get_default_dir (me));
372
373   int tie_position = (int) Tie::get_position (me);
374   conf.position_ = tie_position;
375   
376   Tie_details details;
377   details.init (me);
378   get_configuration (me, common, &conf, 0, details);
379   set_control_points (me, common, conf, details);
380 }
381
382 void
383 Tie::set_control_points (Grob *me,
384                          Grob *common,
385                          Tie_configuration const &conf,
386                          Tie_details const &details
387                          )
388 {
389   Bezier b = conf.get_bezier (details);
390   b.scale (1, conf.dir_);
391   b.translate (Offset (conf.attachment_x_[LEFT]
392                        - me->relative_coordinate (common, X_AXIS),
393                        0.5 * conf.position_ * details.staff_space_
394                        + conf.delta_y_
395                        ));
396   
397   SCM controls = SCM_EOL;
398   for (int i = 4; i--;)
399     controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
400
401   me->set_property ("control-points", controls);
402 }
403
404
405
406 MAKE_SCHEME_CALLBACK (Tie, print, 1);
407 SCM
408 Tie::print (SCM smob)
409 {
410   Grob *me = unsmob_grob (smob);
411
412   if (CENTER == get_grob_direction (me))
413     set_direction (me);
414       
415   SCM cp = me->get_property ("control-points");
416   if (!scm_is_pair (cp))
417     {
418       /*
419         UGH.  dependency tracking!
420        */
421       if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
422         {
423           Tie_column::set_directions (me->get_parent (Y_AXIS));
424         }
425
426       cp = me->get_property ("control-points");
427     }
428   
429   if (!scm_is_pair (cp))
430     {
431       set_default_control_points (me);
432       cp = me->get_property ("control-points");
433     }
434
435   if (!scm_is_pair (cp))
436     return Stencil ().smobbed_copy ();
437
438   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
439   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
440   Real thick = base_thick * staff_thick;
441
442   Bezier b;
443   int i = 0;
444   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
445     {
446       b.control_[i] = ly_scm2offset (scm_car (s));
447       i++;
448     }
449
450   Stencil a;
451
452   SCM p = me->get_property ("dash-period");
453   SCM f = me->get_property ("dash-fraction");
454   if (scm_is_number (p) && scm_is_number (f))
455     a = Lookup::dashed_slur (b,
456                              thick,
457                              robust_scm2double (p, 1.0),
458                              robust_scm2double (f, 0));
459   else
460     a = Lookup::slur (b,
461                       get_grob_direction (me) * staff_thick,
462                       thick);
463
464   return a.smobbed_copy ();
465 }
466
467 ADD_INTERFACE (Tie,
468                "tie-interface",
469                
470                "A tie connecting two noteheads.\n",
471                
472                "control-points "
473                "dash-fraction "
474                "dash-period "
475                "details "
476                "direction "
477                "thickness "
478                "x-gap ");