]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
* lily/tie-column.cc (set_manual_tie_configuration): new function.
[lilypond.git] / lily / slur.cc
1 /*
2   slur.cc -- implement external interface for Slur
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 #include "slur.hh"
11
12 #include <cmath>
13 using namespace std;
14
15 #include "beam.hh"
16 #include "bezier.hh"
17 #include "directional-element-interface.hh"
18 #include "font-interface.hh"
19 #include "pointer-group-interface.hh"
20 #include "lookup.hh"
21 #include "main.hh"              // DEBUG_SLUR_SCORING
22 #include "note-column.hh"
23 #include "output-def.hh"
24 #include "spanner.hh"
25 #include "staff-symbol-referencer.hh"
26 #include "staff-symbol.hh"
27 #include "stem.hh"
28 #include "text-interface.hh"
29 #include "warn.hh"
30 #include "slur-scoring.hh"
31
32 static Direction
33 get_default_dir (Grob *me)
34 {
35   extract_grob_set (me, "note-columns", encompasses);
36
37   Direction d = DOWN;
38   for (int i = 0; i < encompasses.size (); i++)
39     {
40       if (Note_column::dir (encompasses[i]) < 0)
41         {
42           d = UP;
43           break;
44         }
45     }
46   return d;
47 }
48
49 MAKE_SCHEME_CALLBACK (Slur, height, 2);
50 SCM
51 Slur::height (SCM smob, SCM ax)
52 {
53   Axis a = (Axis)scm_to_int (ax);
54   Grob *me = unsmob_grob (smob);
55   assert (a == Y_AXIS);
56
57   SCM mol = me->get_uncached_stencil ();
58   Interval ext;
59   if (Stencil *m = unsmob_stencil (mol))
60     ext = m->extent (a);
61   return ly_interval2scm (ext);
62 }
63
64 /*
65   Ugh should have dash-length + dash-period
66 */
67 MAKE_SCHEME_CALLBACK (Slur, print, 1);
68 SCM
69 Slur::print (SCM smob)
70 {
71   Grob *me = unsmob_grob (smob);
72   extract_grob_set (me, "note-columns", encompasses);
73   if (encompasses.is_empty ())
74     {
75       me->suicide ();
76       return SCM_EOL;
77     }
78
79   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
80   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
81   Real thick = base_thick * staff_thick;
82   Bezier one = get_curve (me);
83   Stencil a;
84
85   /*
86     TODO: replace dashed with generic property.
87   */
88   SCM p = me->get_property ("dash-period");
89   SCM f = me->get_property ("dash-fraction");
90   if (scm_is_number (p) && scm_is_number (f))
91     a = Lookup::dashed_slur (one, thick, robust_scm2double (p, 1.0),
92                              robust_scm2double (f, 0));
93   else
94     a = Lookup::slur (one,
95                       get_grob_direction (me) * staff_thick * 1.0,
96                       thick);
97
98 #if DEBUG_SLUR_SCORING
99   SCM quant_score = me->get_property ("quant-score");
100
101   if (to_boolean (me->get_layout ()
102                   ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
103       && scm_is_string (quant_score))
104     {
105       String str;
106       SCM properties = Font_interface::text_font_alist_chain (me);
107
108       Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
109                                     (me->get_layout ()->self_scm (), properties,
110                                      quant_score));
111       a.add_at_edge (Y_AXIS, get_grob_direction (me), tm, 1.0, 0);
112     }
113 #endif
114
115   return a.smobbed_copy ();
116 }
117
118 Bezier
119 Slur::get_curve (Grob *me)
120 {
121   Bezier b;
122   int i = 0;
123   for (SCM s = me->get_property ("control-points"); s != SCM_EOL;
124        s = scm_cdr (s))
125     b.control_[i++] = ly_scm2offset (scm_car (s));
126
127   return b;
128 }
129
130 void
131 Slur::add_column (Grob *me, Grob *n)
132 {
133   Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
134   add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (n));
135 }
136
137 void
138 Slur::add_extra_encompass (Grob *me, Grob *n)
139 {
140   Pointer_group_interface::add_grob (me, ly_symbol2scm ("encompass-objects"), n);
141   me->add_dependency (n);
142 }
143
144 #include "script-interface.hh"
145 MAKE_SCHEME_CALLBACK (Slur, outside_slur_callback, 2);
146 SCM
147 Slur::outside_slur_callback (SCM grob, SCM axis)
148 {
149   Grob *script = unsmob_grob (grob);
150   Axis a = Axis (scm_to_int (axis));
151   (void) a;
152   assert (a == Y_AXIS);
153
154   Grob *slur = unsmob_grob (script->get_object ("slur"));
155
156   if (!slur)
157     return scm_from_int (0);
158
159   Direction dir = get_grob_direction (script);
160   if (dir == CENTER)
161     return scm_from_int (0);
162
163   /* FIXME: this dependency should be automatic.  */
164   if (scm_ilength (slur->get_property ("control-points")) < 4)
165     Slur::after_line_breaking (slur->self_scm ());
166
167   Grob *cx = script->common_refpoint (slur, X_AXIS);
168   Grob *cy = script->common_refpoint (slur, Y_AXIS);
169
170   Bezier curve = Slur::get_curve (slur);
171
172   curve.translate (Offset (slur->relative_coordinate (cx, X_AXIS),
173                            slur->relative_coordinate (cy, Y_AXIS)));
174
175   Interval yext = robust_relative_extent (script, cy, Y_AXIS);
176   Interval xext = robust_relative_extent (script, cx, X_AXIS);
177
178   /* FIXME: slur property, script property?  */
179   Real slur_padding = robust_scm2double (script->get_property ("slur-padding"),
180                                          0.0);
181   yext.widen (slur_padding);
182
183   Real EPS = 1e-3;
184   Interval bezext (curve.control_[0][X_AXIS], curve.control_[3][X_AXIS]);
185   bool consider[] = { false, false, false };
186   Real ys[] = {0, 0, 0};
187   bool do_shift = false;
188   SCM avoid = script->get_property ("avoid-slur");
189
190   for (int d = LEFT, k = 0; d <= RIGHT; d++, k++)
191     {
192       Real x = xext.linear_combination ((Direction) d);
193       consider[k] = bezext.contains (x);
194
195       if (consider[k])
196         {
197           ys[k]
198             = (fabs (bezext[LEFT] - x) < EPS)
199             ? curve.control_[0][Y_AXIS]
200             : ((fabs (bezext[RIGHT] - x) < EPS)
201                ? curve.control_[3][Y_AXIS]
202                : curve.get_other_coordinate (X_AXIS, x));
203           consider[k] = true;
204
205           /* Request shift if slur is contained script's Y, or if
206              script is inside slur and avoid == outside.  */
207           if (yext.contains (ys[k])
208               || (avoid == ly_symbol2scm ("outside")
209                   && dir * ys[k] > dir * yext[-dir]))
210             do_shift = true;
211         }
212     }
213   Real offset = 0.0;
214   if (do_shift)
215     {
216       for (int d = LEFT, k = 0; d <= RIGHT; d++, k++)
217         offset = dir * (max (dir * offset,
218                              dir * (ys[k] - yext[-dir] + dir * slur_padding)));
219     }
220
221   return scm_from_double (offset);
222 }
223
224 MAKE_SCHEME_CALLBACK (Slur, after_line_breaking, 1);
225 SCM
226 Slur::after_line_breaking (SCM smob)
227 {
228   Spanner *me = dynamic_cast<Spanner *> (unsmob_grob (smob));
229   extract_grob_set (me, "note-columns", encompasses);
230   if (encompasses.is_empty ())
231     {
232       me->suicide ();
233       return SCM_UNSPECIFIED;
234     }
235
236   if (!get_grob_direction (me))
237     set_grob_direction (me, get_default_dir (me));
238
239   if (scm_ilength (me->get_property ("control-points")) < 4)
240     set_slur_control_points (me);
241
242   return SCM_UNSPECIFIED;
243 }
244
245 ADD_INTERFACE (Slur, "slur-interface",
246                "A slur",
247                "positions quant-score eccentricity encompass-objects control-points dash-period dash-fraction slur-details direction height-limit note-columns ratio thickness");
248