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