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