]> git.donarmstrong.com Git - lilypond.git/blob - lily/tex-slur.cc
release: 0.1.50
[lilypond.git] / lily / tex-slur.cc
1 /*
2   tex-slur.cc -- implement Lookup::*slur
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
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"
18
19
20 static char
21 direction_char (Direction y_sign)
22 {
23   char c='#';
24   switch (y_sign)
25     {
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);
37     }
38   return c;
39 }
40
41 Atom
42 Lookup::half_slur_middlepart (Real &dx, Direction dir) const
43 {
44   // todo
45   if (dx >= 400 PT)
46     {
47       WARN<<_("halfslur too large") <<print_dimen (dx)<< _("shrinking (ugh)\n");
48       dx = 400 PT;
49     }
50   int widx = int (floor (dx / 4.0));
51   dx = widx * 4.0;
52   if (widx) widx --;
53   else
54     {
55       WARN <<  _("slur too narrow\n");
56     }
57
58   Atom s;
59
60   s.dim_[Y_AXIS] = Interval (min (0, 0), max (0, 0)); // todo
61   s.dim_[X_AXIS] = Interval (-dx/2, dx/2);
62
63   String f =  String ("\\hslurchar");
64   f += direction_char (CENTER);
65
66   int idx = widx;
67   if (dir < 0)
68     idx += 128;
69
70   assert (idx < 256);
71
72   f +=String ("{") + String (idx) + "}";
73   s.tex_ = f;
74   s.translate_axis (dx/2, X_AXIS);
75
76   return s;
77 }
78
79 /*
80    The halfslurs have their center at the end pointing away from the notehead.
81    This lookup translates so that width() == [0, w]
82  */
83
84 Atom
85 Lookup::half_slur (int dy, Real &dx, Direction dir, int xpart) const
86 {
87   Real orig_dx = dx;
88   if (!xpart)
89     return half_slur_middlepart (dx, dir);
90
91   int widx;
92
93   if (dx >= 96 PT)
94     {
95       WARN << _("Slur half too wide.") << print_dimen (orig_dx) << _(" shrinking (ugh)\n");
96       dx =  96 PT;
97     }
98
99   widx = int (rint (dx/12.0));
100   dx = widx*12.0;
101   if (widx)
102     widx --;
103   else
104     {
105       WARN <<  _("slur too narrow ") << print_dimen (orig_dx)<<"\n";
106     }
107
108   Atom s;
109   s.dim_[X_AXIS] = Interval (0, dx);
110   s.dim_[Y_AXIS] = Interval (min (0, dy), max (0, dy));
111
112
113   String f = String ("\\hslurchar");
114
115   f+= direction_char (dir);
116
117   int hidx = dy;
118   if (hidx <0)
119     hidx = -hidx;
120   hidx --;
121   int idx =-1;
122
123   idx = widx * 16 + hidx;
124   if (xpart < 0)
125     idx += 128;
126
127   assert (idx < 256);
128   f+=String ("{") + String (idx) + "}";
129
130   s.tex_ = f;
131
132   return s;
133 }
134
135 Atom
136 Lookup::ps_slur (Real dy , Real dx, Real ht, Real dir) const
137 {
138   String ps = "\\embeddedps{\n";
139   
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}";
145
146   /*
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
150    */
151   String dx_str = String_convert::precision_str (dx, 4);
152   String dy_str = String_convert::precision_str (dy, 3);
153   String dir_str = String_convert::int_str ((int)dir);
154   String name = "feta-sleur-" + dx_str + "-" + dy_str + "-" + dir_str;
155   int i = name.index_i ('.');
156   while (i != -1)
157     {
158       *(name.ch_l () + i) = 'x';
159       i = name.index_i ('.');
160     }
161
162   String mf = "\\embeddedmf{" + name + "}{\n";
163   mf += "mode_setup;\n";
164   mf += "staffsize\\#:=" 
165     + String_convert::int_str ((int)paper_l_->get_var ("barsize"), "%d")
166     + "pt\\#;\n";
167   mf += "interline#:=staffsize#/4;\n";
168   mf += "stafflinethickness#:=0.1interline#;\n";
169   mf += "input feta-sleur;\n";
170   mf += "slurchar(" + dx_str + "," + dy_str + "," + dir_str + ");\n";
171   mf += "end.\n";
172   mf += "}\n";
173
174   Atom s;
175   s.tex_ = ps + mf;
176   return s;
177 }
178
179 Atom
180 Lookup::tex_slur (int dy , Real &dx, Direction dir) const
181 {
182   assert (abs (dir) <= 1);
183   Atom s;
184   Direction y_sign = (Direction) sign (dy);
185
186   bool large = abs (dy) > 8;
187
188   if (y_sign)
189     {
190       large |= dx>= 4*16 PT;
191     }
192   else
193     large |= dx>= 4*54 PT;
194
195   if (large)
196     {
197       s = big_slur (dy, dx, dir);
198       return s;
199     }
200   Real orig_dx = dx;
201   int widx = int (floor (dx/4.0)); // slurs better too small..
202   dx = 4.0 * widx;
203   if (widx)
204     widx --;
205   else
206     {
207       WARN <<  _("slur too narrow: ") << print_dimen (orig_dx) << "\n";
208     }
209
210   int hidx = dy;
211   if (hidx <0)
212     hidx = -hidx;
213   hidx --;
214   if (hidx > 8)
215     {
216       WARN<<_("slur to steep: ") << dy << _(" shrinking (ugh)\n");
217     }
218
219   String f = String ("\\slurchar") + String (direction_char (y_sign));
220
221   int idx=-1;
222   if (y_sign)
223     {
224       idx = hidx * 16 + widx;
225       if (dir < 0)
226         idx += 128;
227     }
228   else
229     {
230       if (dx >= 4*54 PT)
231         {
232           WARN << _("slur too wide: ") << print_dimen (dx) <<
233             _(" shrinking (ugh)\n");
234           dx = 4*54 PT;
235         }
236       idx = widx;
237       if (dir < 0)
238         idx += 54;
239     }
240
241   assert (idx < 256);
242   f+=String ("{") + String (idx) + "}";
243   s.tex_ = f;
244   s.translate_axis (dx/2, X_AXIS);
245   return s;
246 }
247
248 Atom
249 Lookup::big_slur (int dy , Real &dx, Direction dir) const
250 {
251   if (dx < 24 PT)
252     {
253       warning (_("big_slur too small ") + print_dimen (dx) + _(" (stretching)"));
254       dx = 24 PT;
255     }
256
257   Real slur_extra =abs (dy)  /2.0 + 2;
258   int l_dy = int (Real (dy)/2 + slur_extra*dir);
259   int r_dy =  dy - l_dy;
260
261   Real internote_f = paper_l_->internote_f();
262   Real left_wid = dx/4.0;
263   Real right_wid = left_wid;
264
265   Atom l = half_slur (l_dy, left_wid, dir, -1);
266
267
268   Atom r = half_slur (r_dy, right_wid, dir, 1);
269   Real mid_wid = dx - left_wid - right_wid;
270
271   Molecule mol;
272   mol.add (l);
273   Atom a (half_slur (0, mid_wid, dir, 0));
274   mol.add_at_edge (X_AXIS, RIGHT, a);
275   mol.add_at_edge (X_AXIS, RIGHT, r);
276
277   mol.translate_axis (l_dy * internote_f, Y_AXIS);
278   Atom s;
279   s.tex_ = mol.TeX_string();
280   s.dim_ = mol.extent();
281   return s;
282 }
283
284
285 Atom
286 Lookup::slur  (Real &dy_f , Real &dx, Real ht, Direction dir) const
287 {
288   if  (dx < 0)
289     {
290       warning (_("Negative slur/tie length: ") + print_dimen (dx));
291       dx = 4.0 PT;
292     }
293   Atom s;
294   
295   if (postscript_global_b)
296     s = ps_slur (dy_f, dx, ht, dir);
297   else
298     {
299       Real nh =  paper_l_->internote_f ();
300       int dy_i = (int) rint(dy_f / nh);
301
302       s = tex_slur (dy_i, dx, dir);
303       dy_f = dy_i *  nh;
304     }
305   
306   s.dim_[X_AXIS] = Interval (0, dx);
307   s.dim_[Y_AXIS] = Interval (0 <? dy_f,  0 >? dy_f);
308   return s;
309 }