]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
7c015019ffd3fb1d1dd2ca842023896c61a42728
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.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.empty ())
42     {
43       me->suicide ();
44       return SCM_BOOL_F;
45     }
46
47   Direction d = DOWN;
48   for (vsize 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, 1);
60 SCM
61 Slur::height (SCM smob)
62 {
63   Grob *me = unsmob_grob (smob);
64
65   // FIXME uncached
66   Stencil *m = me->get_stencil ();
67   return m ? ly_interval2scm (m->extent (Y_AXIS))
68     : ly_interval2scm (Interval ());
69 }
70
71 /*
72   Ugh should have dash-length + dash-period
73 */
74 MAKE_SCHEME_CALLBACK (Slur, print, 1);
75 SCM
76 Slur::print (SCM smob)
77 {
78   Grob *me = unsmob_grob (smob);
79   extract_grob_set (me, "note-columns", encompasses);
80   if (encompasses.empty ())
81     {
82       me->suicide ();
83       return SCM_EOL;
84     }
85
86   Real staff_thick = Staff_symbol_referencer::line_thickness (me);
87   Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
88   Real thick = base_thick * staff_thick;
89   Bezier one = get_curve (me);
90   Stencil a;
91
92   /*
93     TODO: replace dashed with generic property.
94   */
95   SCM p = me->get_property ("dash-period");
96   SCM f = me->get_property ("dash-fraction");
97   if (scm_is_number (p) && scm_is_number (f))
98     a = Lookup::dashed_slur (one, thick, robust_scm2double (p, 1.0),
99                              robust_scm2double (f, 0));
100   else
101     a = Lookup::slur (one,
102                       get_grob_direction (me) * staff_thick * 1.0,
103                       thick);
104
105 #if DEBUG_SLUR_SCORING
106   SCM quant_score = me->get_property ("quant-score");
107
108   if (to_boolean (me->layout ()
109                   ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
110       && scm_is_string (quant_score))
111     {
112       std::string str;
113       SCM properties = Font_interface::text_font_alist_chain (me);
114
115       Stencil tm = *unsmob_stencil (Text_interface::interpret_markup
116                                     (me->layout ()->self_scm (), properties,
117                                      quant_score));
118       a.add_at_edge (Y_AXIS, get_grob_direction (me), tm, 1.0, 0);
119     }
120 #endif
121
122   return a.smobbed_copy ();
123 }
124
125 Bezier
126 Slur::get_curve (Grob *me)
127 {
128   Bezier b;
129   int i = 0;
130   for (SCM s = me->get_property ("control-points"); s != SCM_EOL;
131        s = scm_cdr (s))
132     b.control_[i++] = ly_scm2offset (scm_car (s));
133
134   return b;
135 }
136
137 void
138 Slur::add_column (Grob *me, Grob *n)
139 {
140   Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
141   add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (n));
142 }
143
144 void
145 Slur::add_extra_encompass (Grob *me, Grob *n)
146 {
147   Pointer_group_interface::add_grob (me, ly_symbol2scm ("encompass-objects"), n);
148 }
149
150
151 MAKE_SCHEME_CALLBACK (Slur, outside_slur_callback, 2);
152 SCM
153 Slur::outside_slur_callback (SCM grob, SCM offset_scm)
154 {
155   Grob *script = unsmob_grob (grob);
156   Grob *slur = unsmob_grob (script->get_object ("slur")); 
157
158   if (!slur)
159     return offset_scm;
160
161   SCM avoid = script->get_property ("avoid-slur");
162   if (avoid != ly_symbol2scm ("outside")
163       && avoid != ly_symbol2scm ("around"))
164     return offset_scm;
165   
166   Direction dir = get_grob_direction (script);
167   if (dir == CENTER)
168     return offset_scm;
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   yext.translate (robust_scm2double (offset_scm, 0));
182   
183   
184   /* FIXME: slur property, script property?  */
185   Real slur_padding = robust_scm2double (script->get_property ("slur-padding"),
186                                          0.0);
187   yext.widen (slur_padding);
188
189   Real EPS = 1e-3;
190   Interval bezext (curve.control_[0][X_AXIS], curve.control_[3][X_AXIS]);
191   bool consider[] = { false, false, false };
192   Real ys[] = {0, 0, 0};
193   bool do_shift = false;
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               || (dir * ys[k] > dir * yext[-dir] && avoid == ly_symbol2scm ("outside")))
214             do_shift = true;
215         }
216     }
217
218   Real avoidance_offset = 0.0;
219   if (do_shift)
220     {
221       for (int d = LEFT, k = 0; d <= RIGHT; d++, k++)
222         avoidance_offset = dir * (max (dir * avoidance_offset,
223                              dir * (ys[k] - yext[-dir] + dir * slur_padding)));
224     }
225
226   return scm_from_double (scm_to_double (offset_scm) + avoidance_offset);
227 }
228
229
230
231 ADD_INTERFACE (Slur, "slur-interface",
232                
233                "A slur",
234                
235                /* properties */
236                "control-points "
237                "dash-fraction "
238                "dash-period "
239                "direction "
240                "eccentricity "
241                "encompass-objects "
242                "height-limit "
243                "note-columns "
244                "positions "
245                "quant-score "
246                "ratio "
247                "details "
248                "thickness ");
249