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