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