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