]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
427bab144d3f85ca46a26b34f473021777fa355c
[lilypond.git] / lily / tie.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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              ? Grob::unsmob (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   for (LEFT_and_RIGHT (d))
96     {
97       Grob *h = head (me, d);
98       if (h)
99         return (int) rint (Staff_symbol_referencer::get_position (h));
100     }
101
102   /*
103     TODO: this is theoretically possible for ties across more than 2
104     systems.. We should look at the first broken copy.
105
106   */
107   programming_error ("Tie without heads.  Suicide");
108   me->suicide ();
109   return 0;
110 }
111
112 /*
113   Default:  Put the tie oppositie of the stem [Wanske p231]
114
115   In case of chords: Tie_column takes over
116
117   The direction of the Tie is more complicated (See [Ross] p136 and
118   further).
119
120   (what about linebreaks? )
121 */
122 Direction
123 Tie::get_default_dir (Grob *me)
124 {
125   Drul_array<Grob *> stems;
126   for (LEFT_and_RIGHT (d))
127     {
128       Grob *one_head = head (me, d);
129       if (!one_head && dynamic_cast<Spanner *> (me))
130         one_head = Tie::head (dynamic_cast<Spanner *> (me)->broken_neighbor (d), d);
131
132       Grob *stem = one_head ? Rhythmic_head::get_stem (one_head) : 0;
133       if (stem)
134         stem = Stem::is_invisible (stem) ? 0 : stem;
135
136       stems[d] = stem;
137     }
138
139   if (stems[LEFT] && stems[RIGHT])
140     {
141       if (get_grob_direction (stems[LEFT]) == UP
142           && get_grob_direction (stems[RIGHT]) == UP)
143         return DOWN;
144     }
145   else if (stems[LEFT] || stems[RIGHT])
146     {
147       Grob *s = stems[LEFT] ? stems[LEFT] : stems[RIGHT];
148       return -get_grob_direction (s);
149     }
150   else if (int p = get_position (me))
151     return Direction (sign (p));
152
153   return to_dir (me->get_property ("neutral-direction"));
154 }
155
156 MAKE_SCHEME_CALLBACK (Tie, calc_direction, 1);
157 SCM
158 Tie::calc_direction (SCM smob)
159 {
160   Grob *me = Grob::unsmob (smob);
161   Grob *yparent = me->get_parent (Y_AXIS);
162   if ((Tie_column::has_interface (yparent)
163        || Semi_tie_column::has_interface (yparent))
164       && Grob_array::is_smob (yparent->get_object ("ties"))
165       //      && Grob_array::unsmob (yparent->get_object ("ties"))->size () > 1
166      )
167     {
168       /* trigger positioning. */
169       (void) yparent->get_property ("positioning-done");
170
171       return me->get_property_data ("direction");
172     }
173   else
174     return scm_from_int (Tie::get_default_dir (me));
175 }
176
177 SCM
178 Tie::get_default_control_points (Grob *me_grob)
179 {
180   Spanner *me = dynamic_cast<Spanner *> (me_grob);
181   Grob *common = me;
182   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
183   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
184
185   Tie_formatting_problem problem;
186   problem.from_tie (me);
187
188   if (!me->is_live ())
189     return SCM_EOL;
190
191   Ties_configuration conf
192     = problem.generate_optimal_configuration ();
193
194   return get_control_points (me, problem.common_x_refpoint (),
195                              conf[0], problem.details_);
196 }
197
198 SCM
199 Tie::get_control_points (Grob *me,
200                          Grob *common,
201                          Tie_configuration const &conf,
202                          Tie_details const &details)
203 {
204   Bezier b = conf.get_transformed_bezier (details);
205   b.translate (Offset (- me->relative_coordinate (common, X_AXIS), 0));
206
207   SCM controls = SCM_EOL;
208   for (int i = 4; i--;)
209     {
210       if (!b.control_[i].is_sane ())
211         programming_error ("Insane offset");
212       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
213     }
214   return controls;
215 }
216
217 MAKE_SCHEME_CALLBACK (Tie, calc_control_points, 1);
218 SCM
219 Tie::calc_control_points (SCM smob)
220 {
221   Grob *me = Grob::unsmob (smob);
222
223   Grob *yparent = me->get_parent (Y_AXIS);
224   if ((Tie_column::has_interface (yparent)
225        || Semi_tie_column::has_interface (yparent))
226       && Grob_array::is_smob (yparent->get_object ("ties")))
227     {
228       extract_grob_set (yparent, "ties", ties);
229       if (me->original () && ties.size () == 1
230           && !to_dir (me->get_property_data ("direction")))
231         {
232           assert (ties[0] == me);
233           set_grob_direction (me, Tie::get_default_dir (me));
234         }
235       /* trigger positioning. */
236       (void) yparent->get_property ("positioning-done");
237     }
238
239   SCM cp = me->get_property_data ("control-points");
240   if (!scm_is_pair (cp))
241     cp = get_default_control_points (me);
242
243   return cp;
244 }
245
246 /*
247   TODO: merge with Slur::print.
248 */
249 MAKE_SCHEME_CALLBACK (Tie, print, 1);
250 SCM
251 Tie::print (SCM smob)
252 {
253   Grob *me = Grob::unsmob (smob);
254
255   SCM cp = me->get_property ("control-points");
256
257   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
258   Real base_thick = staff_thick * robust_scm2double (me->get_property ("thickness"), 1);
259   Real line_thick = staff_thick * robust_scm2double (me->get_property ("line-thickness"), 1);
260
261   Bezier b;
262   int i = 0;
263   for (SCM s = cp; scm_is_pair (s); s = scm_cdr (s))
264     {
265       b.control_[i] = ly_scm2offset (scm_car (s));
266       i++;
267     }
268
269   Stencil a;
270
271   SCM dash_definition = me->get_property ("dash-definition");
272   a = Lookup::slur (b,
273                     get_grob_direction (me) * base_thick,
274                     line_thick,
275                     dash_definition);
276
277 #if DEBUG_TIE_SCORING
278   SCM annotation = me->get_property ("annotation");
279   if (scm_is_string (annotation))
280     {
281       string str;
282       SCM properties = Font_interface::text_font_alist_chain (me);
283
284       Stencil tm = *Stencil::unsmob (Text_interface::interpret_markup
285                                     (me->layout ()->self_scm (), properties,
286                                      annotation));
287       tm.translate (Offset (b.control_[3][X_AXIS] + 0.5,
288                             b.control_[0][Y_AXIS] * 2));
289       tm = tm.in_color (1, 0, 0);
290
291       /*
292         It would be nice if we could put this in a different layer,
293         but alas, this must be done with a Tie override.
294       */
295       a.add_stencil (tm);
296     }
297 #endif
298
299   return a.smobbed_copy ();
300 }
301
302 ADD_INTERFACE (Tie,
303                "A tie - a horizontal curve connecting two noteheads.\n"
304                "\n"
305                "The following properties may be set in the @code{details}"
306                " list.\n"
307                "\n"
308                "@table @code\n"
309                "@item height-limit\n"
310                "The maximum height allowed for this tie.\n"
311                "@item ratio\n"
312                "Parameter for tie shape. The higher this number, the"
313                " quicker the slur attains its height-limit.\n"
314                "@item between-length-limit\n"
315                "This detail is currently unused.\n"
316                "@item wrong-direction-offset-penalty\n"
317                "Demerit for ties that are offset in the wrong"
318                " direction.\n"
319                "@item min-length\n"
320                "If the tie is shorter than this amount (in"
321                " staff-spaces) an increasingly large length penalty is"
322                " incurred.\n"
323                "@item min-length-penalty-factor\n"
324                "Demerit factor for tie lengths shorter than"
325                " @code{min-length}.\n"
326                "@item center-staff-line-clearance\n"
327                "If the center of the tie is closer to a staff line"
328                " than this amount, an increasingly large staff line"
329                " collision penalty is incurred.\n"
330                "@item tip-staff-line-clearance\n"
331                "If the tips of the tie are closer to a staff line"
332                " than this amount, an increasingly large staff line"
333                " collision penalty is incurred.\n"
334                "@item staff-line-collision-penalty\n"
335                "Demerit factor for ties whose tips or center come"
336                " close to staff lines.\n"
337                "@item dot-collision-clearance\n"
338                "If the tie comes closer to a dot than this amount, an"
339                " increasingly large dot collision penalty is incurred.\n"
340                "@item dot-collision-penalty\n"
341                "Demerit factor for ties which come close to dots.\n"
342                "@item note-head-gap\n"
343                "The distance (in staff-spaces) by which the ends of"
344                " the tie are offset horizontally from the center"
345                " line through the note head.\n"
346                "@item stem-gap\n"
347                "The distance (in staff-spaces) by which the ends of"
348                " the tie are offset horizontally from a stem which"
349                " is on the same side of the note head as the tie.\n"
350                "@item tie-column-monotonicity-penalty\n"
351                "Demerit if the y-position of this tie in the set of"
352                " ties being considered is less than the y-position"
353                " of the previous tie.\n"
354                "@item tie-tie-collision-distance\n"
355                "If this tie is closer than this amount to the previous"
356                " tie in the set being considered, an increasingly"
357                " large tie-tie collision penalty is incurred.\n"
358                "@item tie-tie-collision-penalty\n"
359                "Demerit factor for a tie in the set being considered"
360                " which is close to the previous one.\n"
361                "@item horizontal-distance-penalty-factor\n"
362                "Demerit factor for ties in the set being considered"
363                " which are horizontally distant from the note heads.\n"
364                "@item vertical-distance-penalty-factor\n"
365                "Demerit factor for ties in the set being considered"
366                " which are vertically distant from the note heads.\n"
367                "@item same-dir-as-stem-penalty\n"
368                "Demerit if tie is on the same side as a stem or on the"
369                " opposite side to the one specified.\n"
370                "@item intra-space-threshold\n"
371                "If the tie's height (in half staff-spaces) is less than"
372                " this it is positioned between two adjacent staff"
373                " lines; otherwise it is positioned to straddle a staff"
374                " line further from the note heads.\n"
375                "@item outer-tie-length-symmetry-penalty-factor\n"
376                "Demerit factor for ties horizontally positioned"
377                " unsymmetrically with respect to the two note heads.\n"
378                "@item outer-tie-vertical-distance-symmetry-penalty-factor\n"
379                "Demerit factor for ties vertically positioned"
380                " unsymmetrically with respect to the two note heads.\n"
381                "@item outer-tie-vertical-gap\n"
382                "Amount (in half staff-spaces) by which a tie is moved"
383                " away from the note heads if it is closer to either"
384                " of them than 0.25 half staff-spaces.\n"
385                "@item skyline-padding\n"
386                "Padding of the skylines around note heads in chords.\n"
387                "@item single-tie-region-size\n"
388                "The number of candidate ties to generate when only a"
389                " single tie is required.  Successive candidates differ"
390                " in their initial vertical position by half a"
391                " staff-space.\n"
392                "@item multi-tie-region-size\n"
393                "The number of variations that are tried for the"
394                " extremal ties in a chord.  Variations differ in their"
395                " initial vertical position by half a staff-space.\n"
396
397                "@end table\n",
398
399                /* properties */
400                "annotation "
401                "avoid-slur "    //  UGH.
402                "control-points "
403                "dash-definition "
404                "details "
405                "direction "
406                "head-direction "
407                "line-thickness "
408                "neutral-direction "
409                "staff-position "
410                "thickness "
411               );