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