lilypond-1.4.2
[lilypond.git] / lily / lookup.cc
blobb01db45413ca14b3f931ec242ae3804dacf7dc76
1 /*
2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
10 TODO
11 Glissando
13 #include <math.h>
14 #include <ctype.h>
16 #include "warn.hh"
17 #include "dimensions.hh"
18 #include "bezier.hh"
19 #include "string-convert.hh"
20 #include "file-path.hh"
21 #include "main.hh"
22 #include "lily-guile.hh"
23 #include "molecule.hh"
24 #include "lookup.hh"
25 #include "font-metric.hh"
27 Molecule
28 Lookup::beam (Real slope, Real width, Real thick)
30 Real height = slope * width;
31 Real min_y = (0 <? height) - thick/2;
32 Real max_y = (0 >? height) + thick/2;
36 Box b (Interval (0, width),
37 Interval (min_y, max_y));
40 SCM at = gh_list (ly_symbol2scm ("beam"),
41 gh_double2scm (width),
42 gh_double2scm (slope),
43 gh_double2scm (thick),
44 SCM_UNDEFINED);
45 return Molecule (b, at);
50 Molecule
51 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
53 SCM l = SCM_EOL;
55 for (int i= 4; i -- ;)
57 l = gh_cons (ly_offset2scm (b.control_[i]), l);
60 SCM at = (gh_list (ly_symbol2scm ("dashed-slur"),
61 gh_double2scm (thick),
62 gh_double2scm (dash),
63 ly_quote_scm (l),
64 SCM_UNDEFINED));
66 Box box (Interval (0,0),Interval (0,0));
67 return Molecule (box, at);
73 Molecule
74 Lookup::blank (Box b)
76 return Molecule (b, SCM_EOL);
80 Molecule
81 Lookup::filledbox (Box b)
83 SCM at = (gh_list (ly_symbol2scm ("filledbox"),
84 gh_double2scm (-b[X_AXIS][LEFT]),
85 gh_double2scm (b[X_AXIS][RIGHT]),
86 gh_double2scm (-b[Y_AXIS][DOWN]),
87 gh_double2scm (b[Y_AXIS][UP]),
88 SCM_UNDEFINED));
90 return Molecule (b,at);
93 Molecule
94 Lookup::frame (Box b, Real thick)
96 Molecule m;
97 Direction d = LEFT;
98 Axis a = X_AXIS;
99 while (a < NO_AXES)
103 Axis o = Axis ((a+1)%NO_AXES);
105 Box edges;
106 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
107 edges[o][DOWN] = b[o][DOWN] - thick/2;
108 edges[o][UP] = b[o][UP] + thick/2;
111 m.add_molecule (filledbox (edges));
113 while (flip (&d) != LEFT);
115 return m;
121 Make a smooth curve along the points
123 Molecule
124 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
126 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
127 Bezier back = curve;
129 back.reverse ();
130 back.control_[1] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
131 back.control_[2] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
133 SCM scontrols[8];
135 for (int i=4; i--;)
136 scontrols[ i ] = ly_offset2scm (back.control_[i]);
137 for (int i=4 ; i--;)
138 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
141 Need the weird order b.o. the way PS want its arguments
143 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
144 SCM list = SCM_EOL;
145 for (int i= 8; i--;)
147 list = gh_cons (scontrols[indices[i]], list);
151 SCM at = (gh_list (ly_symbol2scm ("bezier-sandwich"),
152 ly_quote_scm (list),
153 gh_double2scm (linethick),
154 SCM_UNDEFINED));
156 Box b (curve.extent (X_AXIS), curve.extent (Y_AXIS));
157 return Molecule (b, at);
160 Molecule
161 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
163 Molecule m;
164 String sym = ly_scm2string (gh_car (s));
165 String reg = ly_scm2string (gh_car (gh_cdr (s)));
167 if (sym == "Discant")
169 Molecule r = fm->find_by_name ("accordion-accDiscant");
170 m.add_molecule (r);
171 if (reg.left_str (1) == "F")
173 Molecule d = fm->find_by_name ("accordion-accDot");
174 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
175 m.add_molecule (d);
176 reg = reg.right_str (reg.length_i ()-1);
178 int eflag = 0x00;
179 if (reg.left_str (3) == "EEE")
181 eflag = 0x07;
182 reg = reg.right_str (reg.length_i ()-3);
184 else if (reg.left_str (2) == "EE")
186 eflag = 0x05;
187 reg = reg.right_str (reg.length_i ()-2);
189 else if (reg.left_str (2) == "Eh")
191 eflag = 0x04;
192 reg = reg.right_str (reg.length_i ()-2);
194 else if (reg.left_str (1) == "E")
196 eflag = 0x02;
197 reg = reg.right_str (reg.length_i ()-1);
199 if (eflag & 0x02)
201 Molecule d = fm->find_by_name ("accordion-accDot");
202 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
203 m.add_molecule (d);
205 if (eflag & 0x04)
207 Molecule d = fm->find_by_name ("accordion-accDot");
208 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
209 d.translate_axis (0.8 * staff_space PT, X_AXIS);
210 m.add_molecule (d);
212 if (eflag & 0x01)
214 Molecule d = fm->find_by_name ("accordion-accDot");
215 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
216 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
217 m.add_molecule (d);
219 if (reg.left_str (2) == "SS")
221 Molecule d = fm->find_by_name ("accordion-accDot");
222 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
223 d.translate_axis (0.4 * staff_space PT, X_AXIS);
224 m.add_molecule (d);
225 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
226 m.add_molecule (d);
227 reg = reg.right_str (reg.length_i ()-2);
229 if (reg.left_str (1) == "S")
231 Molecule d = fm->find_by_name ("accordion-accDot");
232 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
233 m.add_molecule (d);
234 reg = reg.right_str (reg.length_i ()-1);
237 else if (sym == "Freebase")
239 Molecule r = fm->find_by_name ("accordion-accFreebase");
240 m.add_molecule (r);
241 if (reg.left_str (1) == "F")
243 Molecule d = fm->find_by_name ("accordion-accDot");
244 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
245 m.add_molecule (d);
246 reg = reg.right_str (reg.length_i ()-1);
248 if (reg == "E")
250 Molecule d = fm->find_by_name ("accordion-accDot");
251 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
252 m.add_molecule (d);
255 else if (sym == "Bayanbase")
257 Molecule r = fm->find_by_name ("accordion-accBayanbase");
258 m.add_molecule (r);
259 if (reg.left_str (1) == "T")
261 Molecule d = fm->find_by_name ("accordion-accDot");
262 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
263 m.add_molecule (d);
264 reg = reg.right_str (reg.length_i ()-1);
266 /* include 4' reed just for completeness. You don't want to use this. */
267 if (reg.left_str (1) == "F")
269 Molecule d = fm->find_by_name ("accordion-accDot");
270 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
271 m.add_molecule (d);
272 reg = reg.right_str (reg.length_i ()-1);
274 if (reg.left_str (2) == "EE")
276 Molecule d = fm->find_by_name ("accordion-accDot");
277 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
278 d.translate_axis (0.4 * staff_space PT, X_AXIS);
279 m.add_molecule (d);
280 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
281 m.add_molecule (d);
282 reg = reg.right_str (reg.length_i ()-2);
284 if (reg.left_str (1) == "E")
286 Molecule d = fm->find_by_name ("accordion-accDot");
287 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
288 m.add_molecule (d);
289 reg = reg.right_str (reg.length_i ()-1);
292 else if (sym == "Stdbase")
294 Molecule r = fm->find_by_name ("accordion-accStdbase");
295 m.add_molecule (r);
296 if (reg.left_str (1) == "T")
298 Molecule d = fm->find_by_name ("accordion-accDot");
299 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
300 m.add_molecule (d);
301 reg = reg.right_str (reg.length_i ()-1);
303 if (reg.left_str (1) == "F")
305 Molecule d = fm->find_by_name ("accordion-accDot");
306 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
307 m.add_molecule (d);
308 reg = reg.right_str (reg.length_i ()-1);
310 if (reg.left_str (1) == "M")
312 Molecule d = fm->find_by_name ("accordion-accDot");
313 d.translate_axis (staff_space * 2 PT, Y_AXIS);
314 d.translate_axis (staff_space PT, X_AXIS);
315 m.add_molecule (d);
316 reg = reg.right_str (reg.length_i ()-1);
318 if (reg.left_str (1) == "E")
320 Molecule d = fm->find_by_name ("accordion-accDot");
321 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
322 m.add_molecule (d);
323 reg = reg.right_str (reg.length_i ()-1);
325 if (reg.left_str (1) == "S")
327 Molecule d = fm->find_by_name ("accordion-accDot");
328 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
329 m.add_molecule (d);
330 reg = reg.right_str (reg.length_i ()-1);
333 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
334 for the rectangle */
335 else if (sym == "SB")
337 Molecule r = fm->find_by_name ("accordion-accSB");
338 m.add_molecule (r);
340 else if (sym == "BB")
342 Molecule r = fm->find_by_name ("accordion-accBB");
343 m.add_molecule (r);
345 else if (sym == "OldEE")
347 Molecule r = fm->find_by_name ("accordion-accOldEE");
348 m.add_molecule (r);
350 else if (sym == "OldEES")
352 Molecule r = fm->find_by_name ("accordion-accOldEES");
353 m.add_molecule (r);
355 return m;
359 TODO: should use slope instead? Angle gives nasty rad <-> degree
360 conversions.
362 Molecule
363 Lookup::repeat_slash (Real w, Real s, Real t)
365 SCM wid = gh_double2scm (w);
366 SCM sl = gh_double2scm (s);
367 SCM thick = gh_double2scm (t);
368 SCM slashnodot = gh_list (ly_symbol2scm ("repeat-slash"),
369 wid, sl, thick, SCM_UNDEFINED);
371 Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
372 Interval (0, w * s));
374 return Molecule (b, slashnodot);