]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
Fix #434, #440
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "tie.hh"
10
11 #include "main.hh"
12 #include "bezier.hh"
13 #include "directional-element-interface.hh"
14 #include "font-interface.hh"
15 #include "grob-array.hh"
16 #include "lookup.hh"
17 #include "note-head.hh"
18 #include "output-def.hh"
19 #include "paper-column.hh"
20 #include "pointer-group-interface.hh"
21 #include "rhythmic-head.hh"
22 #include "spanner.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "stem.hh"
25 #include "text-interface.hh"
26 #include "tie-column.hh"
27 #include "tie-configuration.hh"
28 #include "tie-formatting-problem.hh"
29 #include "warn.hh"
30 #include "semi-tie-column.hh"
31
32
33 bool
34 Tie::less (Grob *const &s1,
35            Grob *const &s2)
36 {
37   return Tie::get_position (s1) < Tie::get_position (s2);
38 }
39
40 void
41 Tie::set_head (Grob *me, Direction d, Grob *h)
42 {
43   dynamic_cast<Spanner *> (me)->set_bound (d, h);
44 }
45
46 Grob *
47 Tie::head (Grob *me, Direction d)
48 {
49   if (is_direction (me->get_property ("head-direction")))
50      {
51        Direction hd = to_dir (me->get_property ("head-direction"));
52  
53        return (hd == d)
54          ? unsmob_grob (me->get_object ("note-head"))
55          : 0;
56      }
57   
58   Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
59   if (Note_head::has_interface (it))
60     return it;
61   else
62     return 0;
63 }
64
65 int
66 Tie::get_column_rank (Grob *me, Direction d)
67 {
68   Spanner *span = dynamic_cast<Spanner *> (me);
69   Grob *h = head (me, d);
70   if (!h)
71     h = span->get_bound (d);
72
73   Grob *col = dynamic_cast<Item *> (h)->get_column ();
74   return Paper_column::get_rank (col);
75 }
76
77 int
78 Tie::get_position (Grob *me)
79 {
80   Direction d = LEFT;
81   do
82     {
83       Grob *h = head (me, d);
84       if (h)
85         return (int) rint (Staff_symbol_referencer::get_position (h));
86     }
87   while (flip (&d) != LEFT);
88
89   /*
90
91   TODO: this is theoretically possible for ties across more than 2
92   systems.. We should look at the first broken copy.
93   
94   */
95   programming_error ("Tie without heads. Suicide");
96   me->suicide ();
97   return 0;
98 }
99
100 /*
101   Default:  Put the tie oppositie of the stem [Wanske p231]
102
103   In case of chords: Tie_column takes over
104
105   The direction of the Tie is more complicated (See [Ross] p136 and
106   further).
107
108   (what about linebreaks? )
109 */
110 Direction
111 Tie::get_default_dir (Grob *me)
112 {
113   Drul_array<Grob*> stems;
114   Direction d = LEFT;
115   do
116     {
117       Grob *one_head = head (me, d);
118       if (!one_head && dynamic_cast<Spanner*> (me)) 
119         one_head = Tie::head (dynamic_cast<Spanner*> (me)->broken_neighbor (d), d);
120       
121       Grob *stem = one_head ? Rhythmic_head::get_stem (one_head) : 0;
122       if (stem)
123         stem = Stem::is_invisible (stem) ? 0 : stem;
124
125       stems[d] = stem;
126     }
127   while (flip (&d)!= LEFT);
128   
129   if (stems[LEFT] && stems[RIGHT])
130     {
131       if (get_grob_direction (stems[LEFT]) == UP
132           && get_grob_direction (stems[RIGHT]) == UP)
133         return DOWN;
134     }
135   else if (stems[LEFT] || stems[RIGHT])
136     {
137       Grob *s = stems[LEFT] ? stems[LEFT] : stems[RIGHT];
138       return -get_grob_direction (s);
139     }
140   else if (int p = get_position (me))
141     return Direction (sign (p));
142   
143   return to_dir (me->get_property("neutral-direction"));
144 }
145
146
147 MAKE_SCHEME_CALLBACK (Tie, calc_direction, 1);
148 SCM
149 Tie::calc_direction (SCM smob)
150 {
151   Grob *me = unsmob_grob (smob);
152   Grob *yparent = me->get_parent (Y_AXIS);
153   if ((Tie_column::has_interface (yparent)
154        || Semi_tie_column::has_interface (yparent)) 
155       && unsmob_grob_array (yparent->get_object ("ties"))
156       //      && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1
157       )
158     {
159       /* trigger positioning. */
160       (void) yparent->get_property ("positioning-done");
161
162       return me->get_property_data ("direction");
163     }
164   else
165     return scm_from_int (Tie::get_default_dir (me));
166 }
167
168
169 SCM
170 Tie::get_default_control_points (Grob *me_grob)
171 {
172   Spanner *me = dynamic_cast<Spanner*> (me_grob);
173   Grob *common  = me;
174   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS); 
175   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS); 
176   
177   Tie_formatting_problem problem;
178   problem.from_tie (me);
179   
180   Tie_specification spec = problem.get_tie_specification (0);
181   if (!me->is_live ())
182     return SCM_EOL;
183
184   
185   Ties_configuration conf
186     = problem.generate_optimal_configuration ();
187   
188   return get_control_points (me, problem.common_x_refpoint (),
189                              conf[0], problem.details_);
190 }
191
192 SCM
193 Tie::get_control_points (Grob *me,
194                          Grob *common,
195                          Tie_configuration const &conf,
196                          Tie_details const &details
197                          )
198 {
199   Bezier b = conf.get_transformed_bezier (details);
200   b.translate (Offset (- me->relative_coordinate (common, X_AXIS), 0));
201
202   SCM controls = SCM_EOL;
203   for (int i = 4; i--;)
204     {
205       if (!b.control_[i].is_sane ())
206         programming_error ("Insane offset");
207       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
208     }
209   return controls;
210 }
211
212
213 MAKE_SCHEME_CALLBACK (Tie, calc_control_points, 1);
214 SCM
215 Tie::calc_control_points (SCM smob)
216 {
217   Grob *me = unsmob_grob (smob);
218
219   Grob *yparent = me->get_parent (Y_AXIS);
220   if ((Tie_column::has_interface (yparent)
221        || Semi_tie_column::has_interface (yparent)) 
222       && unsmob_grob_array (yparent->get_object ("ties"))
223       //      && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1
224       )
225     {
226       extract_grob_set (yparent, "ties", ties);
227       if (ties.size() == 1
228           && !to_dir (me->get_property_data ("direction")))
229         {
230           assert (ties[0] == me);
231           set_grob_direction (me, Tie::get_default_dir (me));
232         }
233       
234       /* trigger positioning. */
235       (void) yparent->get_property ("positioning-done");
236     }
237
238   SCM cp = me->get_property_data ("control-points");
239   if (!scm_is_pair (cp))
240     {
241       cp = get_default_control_points (me);
242     }
243
244   return cp;
245 }
246
247 /*
248   TODO: merge with Slur::print.
249  */
250 MAKE_SCHEME_CALLBACK (Tie, print, 1);
251 SCM
252 Tie::print (SCM smob)
253 {
254   Grob *me = unsmob_grob (smob);
255   
256   SCM cp = me->get_property ("control-points");
257
258   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
259   Real base_thick = staff_thick * robust_scm2double (me->get_property ("thickness"), 1);
260   Real line_thick = staff_thick * robust_scm2double (me->get_property ("line-thickness"), 1);
261
262   Bezier b;
263   int i = 0;
264   for (SCM s = cp; scm_is_pair (s); s = scm_cdr (s))
265     {
266       b.control_[i] = ly_scm2offset (scm_car (s));
267       i++;
268     }
269
270   Stencil a;
271
272   SCM p = me->get_property ("dash-period");
273   SCM f = me->get_property ("dash-fraction");
274   if (scm_is_number (p) && scm_is_number (f))
275     a = Lookup::dashed_slur (b,
276                              line_thick,
277                              robust_scm2double (p, 1.0),
278                              robust_scm2double (f, 0));
279   else
280     a = Lookup::slur (b,
281                       get_grob_direction (me) * base_thick,
282                       line_thick);
283
284 #if DEBUG_TIE_SCORING
285   SCM annotation = me->get_property ("annotation");
286   if (!scm_is_string (annotation))
287     {
288       SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-tie-scoring"));
289       if (to_boolean (debug))
290         annotation = me->get_property ("quant-score");
291     }
292   if (scm_is_string (annotation))
293     {
294       string str;
295       SCM properties = Font_interface::text_font_alist_chain (me);
296
297       Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
298                                     (me->layout ()->self_scm (), properties,
299                                      annotation));
300       tm.translate (Offset (b.control_[3][X_AXIS] + 0.5,
301                             b.control_[0][Y_AXIS] * 2));
302       tm = tm.in_color (1, 0, 0);
303
304       /*
305         It would be nice if we could put this in a different layer,
306         but alas, this must be done with a Tie override.
307        */
308       a.add_stencil (tm);
309     }
310 #endif
311
312   return a.smobbed_copy ();
313 }
314
315 ADD_INTERFACE (Tie,
316                "A horizontal curve connecting two noteheads.",
317                
318                /* properties */
319                "annotation "
320                "avoid-slur "    //  UGH.
321                "control-points "
322                "dash-fraction "
323                "dash-period "
324                "details "
325                "direction "
326                "head-direction "
327                "line-thickness "
328                "neutral-direction "
329                "quant-score "
330                "separation-item "
331                "staff-position "
332                "thickness "
333                );
334
335
336
337
338