lilypond-0.1.57
[lilypond.git] / lily / tex-slur.cc
blob90f8860c718db53ef84bc355208861fa44985d4b
1 /*
2 tex-slur.cc -- implement Lookup::*slur
4 source file of the GNU LilyPond music typesetter
6 (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include <math.h>
10 #include "main.hh"
11 #include "misc.hh"
12 #include "lookup.hh"
13 #include "molecule.hh"
14 #include "dimen.hh"
15 #include "debug.hh"
16 #include "paper-def.hh"
17 #include "string-convert.hh"
20 static char
21 direction_char (Direction y_sign)
23 char c='#';
24 switch (y_sign)
26 case DOWN:
27 c = 'd';
28 break;
29 case CENTER:
30 c = 'h';
31 break;
32 case UP:
33 c = 'u';
34 break;
35 default:
36 assert (false);
38 return c;
41 Atom
42 Lookup::half_slur_middlepart (Real &dx, Direction dir) const
44 // todo
45 if (dx >= 400 PT)
47 WARN<<_("halfslur too large") <<print_dimen (dx)<< _("shrinking (ugh)\n");
48 dx = 400 PT;
50 int widx = int (floor (dx / 4.0));
51 dx = widx * 4.0;
52 if (widx) widx --;
53 else
55 WARN << _("slur too narrow\n");
58 Atom s;
60 s.dim_[Y_AXIS] = Interval (min (0, 0), max (0, 0)); // todo
61 s.dim_[X_AXIS] = Interval (-dx/2, dx/2);
63 String f = String ("\\hslurchar");
64 f += direction_char (CENTER);
66 int idx = widx;
67 if (dir < 0)
68 idx += 128;
70 assert (idx < 256);
72 f +=String ("{") + String (idx) + "}";
73 s.tex_ = f;
74 s.translate_axis (dx/2, X_AXIS);
76 return s;
80 The halfslurs have their center at the end pointing away from the notehead.
81 This lookup translates so that width() == [0, w]
84 Atom
85 Lookup::half_slur (int dy, Real &dx, Direction dir, int xpart) const
87 Real orig_dx = dx;
88 if (!xpart)
89 return half_slur_middlepart (dx, dir);
91 int widx;
93 if (dx >= 96 PT)
95 WARN << _("Slur half too wide.") << print_dimen (orig_dx) << _(" shrinking (ugh)\n");
96 dx = 96 PT;
99 widx = int (rint (dx/12.0));
100 dx = widx*12.0;
101 if (widx)
102 widx --;
103 else
105 WARN << _("slur too narrow ") << print_dimen (orig_dx)<<"\n";
108 Atom s;
109 s.dim_[X_AXIS] = Interval (0, dx);
110 s.dim_[Y_AXIS] = Interval (min (0, dy), max (0, dy));
113 String f = String ("\\hslurchar");
115 f+= direction_char (dir);
117 int hidx = dy;
118 if (hidx <0)
119 hidx = -hidx;
120 hidx --;
121 int idx =-1;
123 idx = widx * 16 + hidx;
124 if (xpart < 0)
125 idx += 128;
127 assert (idx < 256);
128 f+=String ("{") + String (idx) + "}";
130 s.tex_ = f;
132 return s;
135 Atom
136 Lookup::ps_slur (Real dy , Real dx, Real ht, Real dir) const
138 String ps = "\\embeddedps{\n";
140 ps += String_convert::double_str (dx) + " "
141 + String_convert::double_str (dy) + " "
142 + String_convert::double_str (ht) + " "
143 + String_convert::double_str (dir) +
144 " draw_slur}";
147 slurs are rarely wider than 100pt:
148 precision of 3 yields maximum (slur spanning page)
149 error of: 1%% * 6*72pt === 0.4pt = 0.14 mm
151 String dx_str = String_convert::precision_str (dx, 4);
152 String dy_str = String_convert::precision_str (dy, 3);
153 String ht_str = String_convert::precision_str (ht, 3);
154 String dir_str = String_convert::int_str ((int)dir);
155 String name = "feta-sleur-" + dx_str + "-" + dy_str + "-" + ht_str + "-" + dir_str;
156 int i = name.index_i ('.');
157 while (i != -1)
159 *(name.ch_l () + i) = 'x';
160 i = name.index_i ('.');
163 String mf = "\\embeddedmf{" + name + "}{\n";
164 mf += "mode_setup;\n";
165 mf += "staffsize\\#:="
166 + String_convert::int_str ((int)paper_l_->get_var ("barsize"), "%d")
167 + "pt\\#;\n";
168 mf += "interline#:=staffsize#/4;\n";
169 mf += "stafflinethickness#:=0.1interline#;\n";
170 mf += "input feta-sleur;\n";
171 mf += "slurchar(" + dx_str + "," + dy_str + "," + ht_str + "," + dir_str + ");\n";
172 mf += "end.\n";
173 mf += "}\n";
175 Atom s;
176 s.tex_ = ps + mf;
177 return s;
180 Atom
181 Lookup::tex_slur (int dy , Real &dx, Direction dir) const
183 assert (abs ((int)dir) <= 1);
184 Atom s;
185 Direction y_sign = (Direction) sign (dy);
187 bool large = abs (dy) > 8;
189 if (y_sign)
191 large |= dx>= 4*16 PT;
193 else
194 large |= dx>= 4*54 PT;
196 if (large)
198 s = big_slur (dy, dx, dir);
199 return s;
201 Real orig_dx = dx;
202 int widx = int (floor (dx/4.0)); // slurs better too small..
203 dx = 4.0 * widx;
204 if (widx)
205 widx --;
206 else
208 WARN << _("slur too narrow: ") << print_dimen (orig_dx) << "\n";
211 int hidx = dy;
212 if (hidx <0)
213 hidx = -hidx;
214 hidx --;
215 if (hidx > 8)
217 WARN<<_("slur to steep: ") << dy << _(" shrinking (ugh)\n");
220 String f = String ("\\slurchar") + String (direction_char (y_sign));
222 int idx=-1;
223 if (y_sign)
225 idx = hidx * 16 + widx;
226 if (dir < 0)
227 idx += 128;
229 else
231 if (dx >= 4*54 PT)
233 WARN << _("slur too wide: ") << print_dimen (dx) <<
234 _(" shrinking (ugh)\n");
235 dx = 4*54 PT;
237 idx = widx;
238 if (dir < 0)
239 idx += 54;
242 assert (idx < 256);
243 f+=String ("{") + String (idx) + "}";
244 s.tex_ = f;
245 s.translate_axis (dx/2, X_AXIS);
246 return s;
249 Atom
250 Lookup::big_slur (int dy , Real &dx, Direction dir) const
252 if (dx < 24 PT)
254 warning (_("big_slur too small ") + print_dimen (dx) + _(" (stretching)"));
255 dx = 24 PT;
258 Real slur_extra =abs (dy) /2.0 + 2;
259 int l_dy = int (Real (dy)/2 + slur_extra*dir);
260 int r_dy = dy - l_dy;
262 Real internote_f = paper_l_->internote_f();
263 Real left_wid = dx/4.0;
264 Real right_wid = left_wid;
266 Atom l = half_slur (l_dy, left_wid, dir, -1);
269 Atom r = half_slur (r_dy, right_wid, dir, 1);
270 Real mid_wid = dx - left_wid - right_wid;
272 Molecule mol;
273 mol.add (l);
274 Atom a (half_slur (0, mid_wid, dir, 0));
275 mol.add_at_edge (X_AXIS, RIGHT, a);
276 mol.add_at_edge (X_AXIS, RIGHT, r);
278 mol.translate_axis (l_dy * internote_f, Y_AXIS);
279 Atom s;
280 s.tex_ = mol.TeX_string();
281 s.dim_ = mol.extent();
282 return s;
286 Atom
287 Lookup::slur (Real &dy_f , Real &dx, Real ht, Direction dir) const
289 if (dx < 0)
291 warning (_("Negative slur/tie length: ") + print_dimen (dx));
292 dx = 4.0 PT;
294 Atom s;
296 if (postscript_global_b)
297 s = ps_slur (dy_f, dx, ht, dir);
298 else
300 Real nh = paper_l_->internote_f ();
301 int dy_i = (int) rint(dy_f / nh);
303 s = tex_slur (dy_i, dx, dir);
304 dy_f = dy_i * nh;
307 s.dim_[X_AXIS] = Interval (0, dx);
308 s.dim_[Y_AXIS] = Interval (0 <? dy_f, 0 >? dy_f);
309 return s;