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