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