]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
* Documentation/topdocs/NEWS.tely (Top): document new feature.
[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@xs4all.nl>
7 */
8
9 #include "tie.hh"
10 #include "spanner.hh"
11 #include "lookup.hh"
12 #include "output-def.hh"
13 #include "rhythmic-head.hh"
14 #include "bezier.hh"
15 #include "paper-column.hh"
16 #include "warn.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "directional-element-interface.hh"
19 #include "bezier.hh"
20 #include "stem.hh"
21 #include "note-head.hh"
22 #include "tie-column.hh"
23 #include "grob-array.hh"
24 #include "tie-formatting-problem.hh"
25 #include "tie-configuration.hh"
26
27
28 int
29 Tie::compare (Grob *const &s1,
30               Grob *const &s2)
31 {
32   return sign (Tie::get_position (s1) - Tie::get_position (s2));
33 }
34
35 void
36 Tie::set_head (Grob *me, Direction d, Grob *h)
37 {
38   dynamic_cast<Spanner *> (me)->set_bound (d, h);
39 }
40
41 Grob *
42 Tie::head (Grob *me, Direction d)
43 {
44   Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
45   if (Note_head::has_interface (it))
46     return it;
47   else
48     return 0;
49 }
50
51 int
52 Tie::get_column_rank (Grob *me, Direction d)
53 {
54   Spanner *span = dynamic_cast<Spanner *> (me);
55   Grob *h = head (me, d);
56   if (!h)
57     h = span->get_bound (d);
58
59   Grob *col = dynamic_cast<Item *> (h)->get_column ();
60   return Paper_column::get_rank (col);
61 }
62
63 int
64 Tie::get_position (Grob *me)
65 {
66   Direction d = LEFT;
67   do
68     {
69       Grob *h = head (me, d);
70       if (h)
71         return (int) Staff_symbol_referencer::get_position (h);
72     }
73   while (flip (&d) != LEFT);
74
75   /*
76
77   TODO: this is theoretically possible for ties across more than 2
78   systems.. We should look at the first broken copy.
79   
80   */
81   programming_error ("Tie without heads. Suicide");
82   me->suicide ();
83   return 0;
84 }
85
86 /*
87   Default:  Put the tie oppositie of the stem [Wanske p231]
88
89   In case of chords: Tie_column takes over
90
91   The direction of the Tie is more complicated (See [Ross] p136 and
92   further).
93
94   (what about linebreaks? )
95 */
96 Direction
97 Tie::get_default_dir (Grob *me)
98 {
99   Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
100   Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
101   if (sl && sr)
102     {
103       if (get_grob_direction (sl) == UP
104           && get_grob_direction (sr) == UP)
105         return DOWN;
106     }
107   else if (sl || sr)
108     {
109       Item *s = sl ? sl : sr;
110       return -get_grob_direction (s);
111     }
112
113   return UP;
114 }
115
116
117 MAKE_SCHEME_CALLBACK(Tie, calc_direction, 1);
118 SCM
119 Tie::calc_direction (SCM smob)
120 {
121   Grob *me = unsmob_grob (smob);
122   Grob *yparent = me->get_parent (Y_AXIS);
123   if (Tie_column::has_interface (yparent)
124       && unsmob_grob_array (yparent->get_object ("ties"))
125       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
126     {
127       /* trigger positioning. */
128       (void) yparent->get_property ("positioning-done");
129     }
130   else
131     set_grob_direction (me, Tie::get_default_dir (me));
132
133   return SCM_UNSPECIFIED;
134 }
135
136 Interval
137 Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
138                               int *staff_position,
139                               bool *in_between,
140                               Tie_details const &details
141                               )
142 {
143   Real staff_space = Staff_symbol_referencer::staff_space (me);
144   Direction dir = get_grob_direction (me);
145   Interval attachments;
146   Direction d = LEFT;
147   do
148     {
149       attachments[d]
150         = robust_relative_extent (me->get_bound (d),
151                                   common,
152                                   X_AXIS)[-d]
153         - gap * d;
154     }
155   while (flip (&d) != LEFT);
156
157   if (attachments.length () < details.between_length_limit_ * staff_space)
158     {
159       /*
160         Let short ties start over note heads, instead of between.
161       */
162       Drul_array<bool> allow (true, true);
163
164       Direction d = LEFT;
165       do {
166         if (Note_head::has_interface (me->get_bound (d)))
167           {
168             Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
169             if (get_grob_direction (stem) == dir
170                 && -d == dir)
171               allow[d] = false;
172           }
173       } while (flip (&d) != LEFT);
174
175       if (allow[LEFT] && allow[RIGHT])
176         {
177           *staff_position += dir;
178           do
179             {
180               if (Note_head::has_interface (me->get_bound (d)))
181                 {
182                   Interval extent
183                     = robust_relative_extent (me->get_bound (d),
184                                               common, X_AXIS);
185
186                   attachments[d] = extent.linear_combination (- 0.5 * d);
187                   *in_between = false;
188                 }
189             }
190           while (flip (&d) != LEFT);
191         }
192     }
193
194   return attachments;
195 }  
196
197 void
198 Tie::set_default_control_points (Grob *me_grob)
199 {
200   Spanner *me = dynamic_cast<Spanner*> (me_grob);
201   Grob *common  = me;
202   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
203   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
204   
205   Tie_formatting_problem problem;
206   problem.from_tie (me);
207   
208   // get_configuration (me,  &conf, problem);
209   int tie_position = (int) Tie::get_position (me);
210   Tie_configuration conf
211     = problem.find_optimal_tie_configuration (tie_position, get_grob_direction (me));
212   set_control_points (me, problem.common_x_refpoint (),
213                       conf, problem.details_);
214 }
215
216 void
217 Tie::set_control_points (Grob *me,
218                          Grob *common,
219                          Tie_configuration const &conf,
220                          Tie_details const &details
221                          )
222 {
223   Bezier b = conf.get_transformed_bezier (details);
224   b.translate (Offset (- me->relative_coordinate (common, X_AXIS), 0));
225
226   SCM controls = SCM_EOL;
227   for (int i = 4; i--;)
228     {
229       if (!b.control_[i].is_sane ())
230         programming_error ("Insane offset");
231       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
232     }
233   me->set_property ("control-points", controls);
234 }
235
236 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
237 SCM
238 Tie::calc_control_points (SCM smob)
239 {
240   Grob *me = unsmob_grob (smob);
241
242   // trigger Tie-column
243   (void)  get_grob_direction (me);
244
245   Grob *yparent = me->get_parent (Y_AXIS);
246   if (Tie_column::has_interface (yparent)
247       && unsmob_grob_array (yparent->get_object ("ties"))
248       && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
249     {
250       /* trigger positioning. */
251       (void) yparent->get_property ("positioning-done");
252     }
253
254   if (!scm_is_pair (me->get_property ("control-points")))
255     {
256       set_default_control_points (me);
257     }
258
259   return SCM_UNSPECIFIED;
260 }
261
262
263 MAKE_SCHEME_CALLBACK (Tie, print, 1);
264 SCM
265 Tie::print (SCM smob)
266 {
267   Grob *me = unsmob_grob (smob);
268   
269   SCM cp = me->get_property ("control-points");
270
271   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
272   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
273   Real thick = base_thick * staff_thick;
274
275   Bezier b;
276   int i = 0;
277   for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
278     {
279       b.control_[i] = ly_scm2offset (scm_car (s));
280       i++;
281     }
282
283   Stencil a;
284
285   SCM p = me->get_property ("dash-period");
286   SCM f = me->get_property ("dash-fraction");
287   if (scm_is_number (p) && scm_is_number (f))
288     a = Lookup::dashed_slur (b,
289                              thick,
290                              robust_scm2double (p, 1.0),
291                              robust_scm2double (f, 0));
292   else
293     a = Lookup::slur (b,
294                       get_grob_direction (me) * staff_thick,
295                       thick);
296
297   return a.smobbed_copy ();
298 }
299
300 ADD_INTERFACE (Tie,
301                "tie-interface",
302                
303                "A tie connecting two noteheads. \n\n"
304                ,
305                
306
307                /* properties */
308                "control-points "
309                "dash-fraction "
310                "dash-period "
311                "details "
312                "direction "
313                "thickness "
314                "x-gap ");
315
316
317