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