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