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