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