]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie.cc
Issue 4503 (1/4) Clean up Tie::less and Semi_Tie::less
[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 (Grob *me)
123 {
124   Drul_array<Grob *> stems;
125   for (LEFT_and_RIGHT (d))
126     {
127       Grob *one_head = 0;
128       if (Spanner *spanner = dynamic_cast<Spanner *> (me))
129         {
130           one_head = head (spanner, d);
131           if (!one_head)
132             one_head = head (spanner->broken_neighbor (d), d);
133         }
134       else if (Item *item = dynamic_cast<Item *> (me))
135         one_head = Semi_tie::head (item);
136
137       Grob *stem = one_head ? Rhythmic_head::get_stem (one_head) : 0;
138       stems[d] = (stem && !Stem::is_invisible (stem)) ? stem : 0;
139     }
140
141   if (stems[LEFT] && stems[RIGHT])
142     {
143       if (get_grob_direction (stems[LEFT]) == UP
144           && get_grob_direction (stems[RIGHT]) == UP)
145         return DOWN;
146
147       // And why not return UP if both stems are DOWN?
148
149       // And when stems conflict, why fall directly through to using
150       // neutral-direction without considering get_position (me)?
151     }
152   else if (stems[LEFT])
153     return -get_grob_direction (stems[LEFT]);
154   else if (stems[RIGHT])
155     return -get_grob_direction (stems[RIGHT]);
156   else if (int p = get_position_generic (me))
157     return Direction (sign (p));
158
159   return to_dir (me->get_property ("neutral-direction"));
160 }
161
162 MAKE_SCHEME_CALLBACK (Tie, calc_direction, 1);
163 SCM
164 Tie::calc_direction (SCM smob)
165 {
166   Grob *me = unsmob<Grob> (smob);
167   Grob *yparent = me->get_parent (Y_AXIS);
168   if ((Tie_column::has_interface (yparent)
169        || Semi_tie_column::has_interface (yparent))
170       && unsmob<Grob_array> (yparent->get_object ("ties"))
171       //      && unsmob<Grob_array> (yparent->get_object ("ties"))->size () > 1
172      )
173     {
174       /* trigger positioning. */
175       (void) yparent->get_property ("positioning-done");
176
177       return me->get_property_data ("direction");
178     }
179   else
180     return scm_from_int (Tie::get_default_dir (me));
181 }
182
183 SCM
184 Tie::get_default_control_points (Spanner *me)
185 {
186   Grob *common = me;
187   common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
188   common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
189
190   Tie_formatting_problem problem;
191   problem.from_tie (me);
192
193   if (!me->is_live ())
194     return SCM_EOL;
195
196   Ties_configuration conf
197     = problem.generate_optimal_configuration ();
198
199   return get_control_points (me, problem.common_x_refpoint (),
200                              conf[0], problem.details_);
201 }
202
203 SCM
204 Tie::get_control_points (Grob *me,
205                          Grob *common,
206                          Tie_configuration const &conf,
207                          Tie_details const &details)
208 {
209   Bezier b = conf.get_transformed_bezier (details);
210   b.translate (Offset (- me->relative_coordinate (common, X_AXIS), 0));
211
212   SCM controls = SCM_EOL;
213   for (int i = 4; i--;)
214     {
215       if (!b.control_[i].is_sane ())
216         programming_error ("Insane offset");
217       controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
218     }
219   return controls;
220 }
221
222 MAKE_SCHEME_CALLBACK (Tie, calc_control_points, 1);
223 SCM
224 Tie::calc_control_points (SCM smob)
225 {
226   Spanner *me = LY_ASSERT_SMOB(Spanner, smob, 1);
227
228   Grob *yparent = me->get_parent (Y_AXIS);
229   if ((Tie_column::has_interface (yparent)
230        || Semi_tie_column::has_interface (yparent))
231       && unsmob<Grob_array> (yparent->get_object ("ties")))
232     {
233       extract_grob_set (yparent, "ties", ties);
234       if (me->original () && ties.size () == 1
235           && !to_dir (me->get_property_data ("direction")))
236         {
237           assert (ties[0] == me);
238           set_grob_direction (me, Tie::get_default_dir (me));
239         }
240       /* trigger positioning. */
241       (void) yparent->get_property ("positioning-done");
242     }
243
244   SCM cp = me->get_property_data ("control-points");
245   if (!scm_is_pair (cp))
246     cp = get_default_control_points (me);
247
248   return cp;
249 }
250
251 /*
252   TODO: merge with Slur::print.
253 */
254 MAKE_SCHEME_CALLBACK (Tie, print, 1);
255 SCM
256 Tie::print (SCM smob)
257 {
258   Grob *me = unsmob<Grob> (smob);
259
260   SCM cp = me->get_property ("control-points");
261
262   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
263   Real base_thick = staff_thick * robust_scm2double (me->get_property ("thickness"), 1);
264   Real line_thick = staff_thick * robust_scm2double (me->get_property ("line-thickness"), 1);
265
266   Bezier b;
267   int i = 0;
268   for (SCM s = cp; scm_is_pair (s); s = scm_cdr (s))
269     {
270       b.control_[i] = ly_scm2offset (scm_car (s));
271       i++;
272     }
273
274   Stencil a;
275
276   SCM dash_definition = me->get_property ("dash-definition");
277   a = Lookup::slur (b,
278                     get_grob_direction (me) * base_thick,
279                     line_thick,
280                     dash_definition);
281
282 #if DEBUG_TIE_SCORING
283   SCM annotation = me->get_property ("annotation");
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 tie - a horizontal curve connecting two noteheads.\n"
309                "\n"
310                "The following properties may be set in the @code{details}"
311                " list.\n"
312                "\n"
313                "@table @code\n"
314                "@item height-limit\n"
315                "The maximum height allowed for this tie.\n"
316                "@item ratio\n"
317                "Parameter for tie shape. The higher this number, the"
318                " quicker the slur attains its height-limit.\n"
319                "@item between-length-limit\n"
320                "This detail is currently unused.\n"
321                "@item wrong-direction-offset-penalty\n"
322                "Demerit for ties that are offset in the wrong"
323                " direction.\n"
324                "@item min-length\n"
325                "If the tie is shorter than this amount (in"
326                " staff-spaces) an increasingly large length penalty is"
327                " incurred.\n"
328                "@item min-length-penalty-factor\n"
329                "Demerit factor for tie lengths shorter than"
330                " @code{min-length}.\n"
331                "@item center-staff-line-clearance\n"
332                "If the center of the tie is closer to a staff line"
333                " than this amount, an increasingly large staff line"
334                " collision penalty is incurred.\n"
335                "@item tip-staff-line-clearance\n"
336                "If the tips of the tie are closer to a staff line"
337                " than this amount, an increasingly large staff line"
338                " collision penalty is incurred.\n"
339                "@item staff-line-collision-penalty\n"
340                "Demerit factor for ties whose tips or center come"
341                " close to staff lines.\n"
342                "@item dot-collision-clearance\n"
343                "If the tie comes closer to a dot than this amount, an"
344                " increasingly large dot collision penalty is incurred.\n"
345                "@item dot-collision-penalty\n"
346                "Demerit factor for ties which come close to dots.\n"
347                "@item note-head-gap\n"
348                "The distance (in staff-spaces) by which the ends of"
349                " the tie are offset horizontally from the center"
350                " line through the note head.\n"
351                "@item stem-gap\n"
352                "The distance (in staff-spaces) by which the ends of"
353                " the tie are offset horizontally from a stem which"
354                " is on the same side of the note head as the tie.\n"
355                "@item tie-column-monotonicity-penalty\n"
356                "Demerit if the y-position of this tie in the set of"
357                " ties being considered is less than the y-position"
358                " of the previous tie.\n"
359                "@item tie-tie-collision-distance\n"
360                "If this tie is closer than this amount to the previous"
361                " tie in the set being considered, an increasingly"
362                " large tie-tie collision penalty is incurred.\n"
363                "@item tie-tie-collision-penalty\n"
364                "Demerit factor for a tie in the set being considered"
365                " which is close to the previous one.\n"
366                "@item horizontal-distance-penalty-factor\n"
367                "Demerit factor for ties in the set being considered"
368                " which are horizontally distant from the note heads.\n"
369                "@item vertical-distance-penalty-factor\n"
370                "Demerit factor for ties in the set being considered"
371                " which are vertically distant from the note heads.\n"
372                "@item same-dir-as-stem-penalty\n"
373                "Demerit if tie is on the same side as a stem or on the"
374                " opposite side to the one specified.\n"
375                "@item intra-space-threshold\n"
376                "If the tie's height (in half staff-spaces) is less than"
377                " this it is positioned between two adjacent staff"
378                " lines; otherwise it is positioned to straddle a staff"
379                " line further from the note heads.\n"
380                "@item outer-tie-length-symmetry-penalty-factor\n"
381                "Demerit factor for ties horizontally positioned"
382                " unsymmetrically with respect to the two note heads.\n"
383                "@item outer-tie-vertical-distance-symmetry-penalty-factor\n"
384                "Demerit factor for ties vertically positioned"
385                " unsymmetrically with respect to the two note heads.\n"
386                "@item outer-tie-vertical-gap\n"
387                "Amount (in half staff-spaces) by which a tie is moved"
388                " away from the note heads if it is closer to either"
389                " of them than 0.25 half staff-spaces.\n"
390                "@item skyline-padding\n"
391                "Padding of the skylines around note heads in chords.\n"
392                "@item single-tie-region-size\n"
393                "The number of candidate ties to generate when only a"
394                " single tie is required.  Successive candidates differ"
395                " in their initial vertical position by half a"
396                " staff-space.\n"
397                "@item multi-tie-region-size\n"
398                "The number of variations that are tried for the"
399                " extremal ties in a chord.  Variations differ in their"
400                " initial vertical position by half a staff-space.\n"
401
402                "@end table\n",
403
404                /* properties */
405                "annotation "
406                "avoid-slur "    //  UGH.
407                "control-points "
408                "dash-definition "
409                "details "
410                "direction "
411                "head-direction "
412                "line-thickness "
413                "neutral-direction "
414                "staff-position "
415                "thickness "
416               );