]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
Remove last vestiges of SeparationItem.
[lilypond.git] / lily / tie.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 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   Spanner *span = dynamic_cast<Spanner *> (me);
78   Grob *h = head (me, d);
79   if (!h)
80     h = span->get_bound (d);
81
82   Grob *col = dynamic_cast<Item *> (h)->get_column ();
83   return Paper_column::get_rank (col);
84 }
85
86 int
87 Tie::get_position (Grob *me)
88 {
89   Direction d = LEFT;
90   do
91     {
92       Grob *h = head (me, d);
93       if (h)
94         return (int) rint (Staff_symbol_referencer::get_position (h));
95     }
96   while (flip (&d) != LEFT);
97
98   /*
99     TODO: this is theoretically possible for ties across more than 2
100     systems.. We should look at the first broken copy.
101
102   */
103   programming_error ("Tie without heads. Suicide");
104   me->suicide ();
105   return 0;
106 }
107
108 /*
109   Default:  Put the tie oppositie of the stem [Wanske p231]
110
111   In case of chords: Tie_column takes over
112
113   The direction of the Tie is more complicated (See [Ross] p136 and
114   further).
115
116   (what about linebreaks? )
117 */
118 Direction
119 Tie::get_default_dir (Grob *me)
120 {
121   Drul_array<Grob *> stems;
122   Direction d = LEFT;
123   do
124     {
125       Grob *one_head = head (me, d);
126       if (!one_head && dynamic_cast<Spanner *> (me))
127         one_head = Tie::head (dynamic_cast<Spanner *> (me)->broken_neighbor (d), d);
128
129       Grob *stem = one_head ? Rhythmic_head::get_stem (one_head) : 0;
130       if (stem)
131         stem = Stem::is_invisible (stem) ? 0 : stem;
132
133       stems[d] = stem;
134     }
135   while (flip (&d) != LEFT);
136
137   if (stems[LEFT] && stems[RIGHT])
138     {
139       if (get_grob_direction (stems[LEFT]) == UP
140           && get_grob_direction (stems[RIGHT]) == UP)
141         return DOWN;
142     }
143   else if (stems[LEFT] || stems[RIGHT])
144     {
145       Grob *s = stems[LEFT] ? stems[LEFT] : stems[RIGHT];
146       return -get_grob_direction (s);
147     }
148   else if (int p = get_position (me))
149     return Direction (sign (p));
150
151   return to_dir (me->get_property ("neutral-direction"));
152 }
153
154 MAKE_SCHEME_CALLBACK (Tie, calc_direction, 1);
155 SCM
156 Tie::calc_direction (SCM smob)
157 {
158   Grob *me = unsmob_grob (smob);
159   Grob *yparent = me->get_parent (Y_AXIS);
160   if ((Tie_column::has_interface (yparent)
161        || Semi_tie_column::has_interface (yparent))
162       && unsmob_grob_array (yparent->get_object ("ties"))
163       //      && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1
164       )
165     {
166       /* trigger positioning. */
167       (void) yparent->get_property ("positioning-done");
168
169       return me->get_property_data ("direction");
170     }
171   else
172     return scm_from_int (Tie::get_default_dir (me));
173 }
174
175 SCM
176 Tie::get_default_control_points (Grob *me_grob)
177 {
178   Spanner *me = dynamic_cast<Spanner *> (me_grob);
179   Grob *common = me;
180   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
181   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
182
183   Tie_formatting_problem problem;
184   problem.from_tie (me);
185
186   Tie_specification spec = problem.get_tie_specification (0);
187   if (!me->is_live ())
188     return SCM_EOL;
189
190   Ties_configuration conf
191     = problem.generate_optimal_configuration ();
192
193   return get_control_points (me, problem.common_x_refpoint (),
194                              conf[0], problem.details_);
195 }
196
197 SCM
198 Tie::get_control_points (Grob *me,
199                          Grob *common,
200                          Tie_configuration const &conf,
201                          Tie_details const &details)
202 {
203   Bezier b = conf.get_transformed_bezier (details);
204   b.translate (Offset (- me->relative_coordinate (common, X_AXIS), 0));
205
206   SCM controls = SCM_EOL;
207   for (int i = 4; i--;)
208     {
209       if (!b.control_[i].is_sane ())
210         programming_error ("Insane offset");
211       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
212     }
213   return controls;
214 }
215
216 MAKE_SCHEME_CALLBACK (Tie, calc_control_points, 1);
217 SCM
218 Tie::calc_control_points (SCM smob)
219 {
220   Grob *me = unsmob_grob (smob);
221
222   Grob *yparent = me->get_parent (Y_AXIS);
223   if ((Tie_column::has_interface (yparent)
224        || Semi_tie_column::has_interface (yparent))
225       && unsmob_grob_array (yparent->get_object ("ties")))
226     {
227       extract_grob_set (yparent, "ties", ties);
228       if (me->original () && ties.size () == 1
229           && !to_dir (me->get_property_data ("direction")))
230         {
231           assert (ties[0] == me);
232           set_grob_direction (me, Tie::get_default_dir (me));
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     cp = get_default_control_points (me);
241
242   return cp;
243 }
244
245 /*
246   TODO: merge with Slur::print.
247 */
248 MAKE_SCHEME_CALLBACK (Tie, print, 1);
249 SCM
250 Tie::print (SCM smob)
251 {
252   Grob *me = unsmob_grob (smob);
253
254   SCM cp = me->get_property ("control-points");
255
256   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
257   Real base_thick = staff_thick * robust_scm2double (me->get_property ("thickness"), 1);
258   Real line_thick = staff_thick * robust_scm2double (me->get_property ("line-thickness"), 1);
259
260   Bezier b;
261   int i = 0;
262   for (SCM s = cp; scm_is_pair (s); s = scm_cdr (s))
263     {
264       b.control_[i] = ly_scm2offset (scm_car (s));
265       i++;
266     }
267
268   Stencil a;
269
270   SCM dash_definition = me->get_property ("dash-definition");
271   a = Lookup::slur (b,
272                     get_grob_direction (me) * base_thick,
273                     line_thick,
274                     dash_definition);
275
276 #if DEBUG_TIE_SCORING
277   SCM annotation = me->get_property ("annotation");
278   if (!scm_is_string (annotation))
279     {
280       SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-tie-scoring"));
281       if (to_boolean (debug))
282         annotation = me->get_property ("quant-score");
283     }
284   if (scm_is_string (annotation))
285     {
286       string str;
287       SCM properties = Font_interface::text_font_alist_chain (me);
288
289       Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
290                                     (me->layout ()->self_scm (), properties,
291                                      annotation));
292       tm.translate (Offset (b.control_[3][X_AXIS] + 0.5,
293                             b.control_[0][Y_AXIS] * 2));
294       tm = tm.in_color (1, 0, 0);
295
296       /*
297         It would be nice if we could put this in a different layer,
298         but alas, this must be done with a Tie override.
299       */
300       a.add_stencil (tm);
301     }
302 #endif
303
304   return a.smobbed_copy ();
305 }
306
307 ADD_INTERFACE (Tie,
308                "A horizontal curve connecting two noteheads.",
309
310                /* properties */
311                "annotation "
312                "avoid-slur "    //  UGH.
313                "control-points "
314                "dash-definition "
315                "details "
316                "direction "
317                "head-direction "
318                "line-thickness "
319                "neutral-direction "
320                "quant-score "
321                "staff-position "
322                "thickness "
323                );