]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
40af656ee39e06c0b55361966a02d799a869e349
[lilypond.git] / lily / lookup.cc
1 /*
2   lookup.cc -- implement simple Lookup methods.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8   Jan Nieuwenhuizen <janneke@gnu.org>
9
10   TODO
11       Glissando
12 */
13 #include <math.h>
14 #include <ctype.h>
15
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"
26
27 Molecule 
28 Lookup::beam (Real slope, Real width, Real thick) 
29 {
30   Real height = slope * width; 
31   Real min_y = (0 <? height) - thick/2;
32   Real max_y = (0 >? height) + thick/2;
33
34   
35
36   Box b (Interval (0, width),
37          Interval (min_y, max_y));
38
39   
40   SCM at = scm_list_n (ly_symbol2scm ("beam"),
41                     gh_double2scm (width),
42                     gh_double2scm (slope),
43                     gh_double2scm (thick),
44                     SCM_UNDEFINED);
45   return Molecule (b, at);
46 }
47
48
49
50 Molecule
51 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
52 {
53   SCM l = SCM_EOL;
54
55   for (int i= 4; i -- ;)
56     {
57       l = gh_cons (ly_offset2scm (b.control_[i]), l);
58     }
59
60   SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
61                                gh_double2scm (thick), 
62                                gh_double2scm (dash),
63                                ly_quote_scm (l),
64                                SCM_UNDEFINED));
65
66   Box box (Interval (0,0),Interval (0,0));
67   return   Molecule (box, at);
68 }
69
70
71
72
73 Molecule
74 Lookup::blank (Box b) 
75 {
76   return Molecule (b, SCM_EOL);
77 }
78
79
80 Molecule
81 Lookup::filledbox (Box b) 
82 {
83   SCM  at  = (scm_list_n (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));
89
90   return Molecule (b,at);
91 }
92
93 Molecule
94 Lookup::frame (Box b, Real thick)
95 {
96   Molecule m;
97   Direction d = LEFT;
98   Axis a = X_AXIS;
99   while (a < NO_AXES)
100     {
101       do
102         {
103           Axis o = Axis ((a+1)%NO_AXES);
104
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;      
109           
110           
111           m.add_molecule (filledbox (edges));
112         }
113       while (flip (&d) != LEFT);
114     }
115   return m;
116   
117 }
118
119
120 /*
121   Make a smooth curve along the points 
122  */
123 Molecule
124 Lookup::slur (Bezier curve, Real curvethick, Real linethick) 
125 {
126   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
127   Bezier back = curve;
128   Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
129   back.reverse ();
130   back.control_[1] += perp;
131   back.control_[2] += perp;
132
133   curve.control_[1] -= perp;
134   curve.control_[2] -= perp;
135   
136   SCM scontrols[8];
137
138   for (int i=4; i--;)
139     scontrols[ i ] = ly_offset2scm (back.control_[i]);
140   for (int i=4 ; i--;)
141     scontrols[i+4] = ly_offset2scm (curve.control_[i]);
142
143   /*
144     Need the weird order b.o. the way PS want its arguments  
145    */
146   int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
147   SCM list = SCM_EOL;
148   for (int i= 8; i--;)
149     {
150       list = gh_cons (scontrols[indices[i]], list);
151     }
152   
153   
154   SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
155                      ly_quote_scm (list),
156                      gh_double2scm (linethick),
157                      SCM_UNDEFINED));
158   Box b(curve.extent (X_AXIS),
159         curve.extent (Y_AXIS));
160
161   b[X_AXIS].unite (back.extent (X_AXIS));
162   b[Y_AXIS].unite (back.extent (Y_AXIS));
163
164   return Molecule (b, at);
165 }
166
167 Molecule
168 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm) 
169 {
170   Molecule m;
171   String sym = ly_scm2string (ly_car (s));
172   String reg = ly_scm2string (ly_car (ly_cdr (s)));
173
174   if (sym == "Discant")
175     {
176       Molecule r = fm->find_by_name ("accordion-accDiscant");
177       m.add_molecule (r);
178       if (reg.left_str (1) == "F")
179         {
180           Molecule d = fm->find_by_name ("accordion-accDot");
181           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
182           m.add_molecule (d);
183           reg = reg.right_str (reg.length_i ()-1);
184         }
185       int eflag = 0x00;
186       if (reg.left_str (3) == "EEE")
187         {
188           eflag = 0x07;
189           reg = reg.right_str (reg.length_i ()-3);
190         }
191       else if (reg.left_str (2) == "EE")
192         {
193           eflag = 0x05;
194           reg = reg.right_str (reg.length_i ()-2);
195         }
196       else if (reg.left_str (2) == "Eh")
197         {
198           eflag = 0x04;
199           reg = reg.right_str (reg.length_i ()-2);
200         }
201       else if (reg.left_str (1) == "E")
202         {
203           eflag = 0x02;
204           reg = reg.right_str (reg.length_i ()-1);
205         }
206       if (eflag & 0x02)
207         {
208           Molecule d = fm->find_by_name ("accordion-accDot");
209           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
210           m.add_molecule (d);
211         }
212       if (eflag & 0x04)
213         {
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);
218         }
219       if (eflag & 0x01)
220         {
221           Molecule d = fm->find_by_name ("accordion-accDot");
222           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
223           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
224           m.add_molecule (d);
225         }
226       if (reg.left_str (2) == "SS")
227         {
228           Molecule d = fm->find_by_name ("accordion-accDot");
229           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
230           d.translate_axis (0.4 * staff_space PT, X_AXIS);
231           m.add_molecule (d);
232           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
233           m.add_molecule (d);
234           reg = reg.right_str (reg.length_i ()-2);
235         }
236       if (reg.left_str (1) == "S")
237         {
238           Molecule d = fm->find_by_name ("accordion-accDot");
239           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
240           m.add_molecule (d);
241           reg = reg.right_str (reg.length_i ()-1);
242         }
243     }
244   else if (sym == "Freebase")
245     {
246       Molecule r = fm->find_by_name ("accordion-accFreebase");
247       m.add_molecule (r);
248       if (reg.left_str (1) == "F")
249         {
250           Molecule d = fm->find_by_name ("accordion-accDot");
251           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
252           m.add_molecule (d);
253           reg = reg.right_str (reg.length_i ()-1);
254         }
255       if (reg == "E")
256         {
257           Molecule d = fm->find_by_name ("accordion-accDot");
258           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
259           m.add_molecule (d);
260         }
261     }
262   else if (sym == "Bayanbase")
263     {
264       Molecule r = fm->find_by_name ("accordion-accBayanbase");
265       m.add_molecule (r);
266       if (reg.left_str (1) == "T")
267         {
268           Molecule d = fm->find_by_name ("accordion-accDot");
269           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
270           m.add_molecule (d);
271           reg = reg.right_str (reg.length_i ()-1);
272         }
273       /* include 4' reed just for completeness. You don't want to use this. */
274       if (reg.left_str (1) == "F")
275         {
276           Molecule d = fm->find_by_name ("accordion-accDot");
277           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
278           m.add_molecule (d);
279           reg = reg.right_str (reg.length_i ()-1);
280         }
281       if (reg.left_str (2) == "EE")
282         {
283           Molecule d = fm->find_by_name ("accordion-accDot");
284           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
285           d.translate_axis (0.4 * staff_space PT, X_AXIS);
286           m.add_molecule (d);
287           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
288           m.add_molecule (d);
289           reg = reg.right_str (reg.length_i ()-2);
290         }
291       if (reg.left_str (1) == "E")
292         {
293           Molecule d = fm->find_by_name ("accordion-accDot");
294           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
295           m.add_molecule (d);
296           reg = reg.right_str (reg.length_i ()-1);
297         }
298     }
299   else if (sym == "Stdbase")
300     {
301       Molecule r = fm->find_by_name ("accordion-accStdbase");
302       m.add_molecule (r);
303       if (reg.left_str (1) == "T")
304         {
305           Molecule d = fm->find_by_name ("accordion-accDot");
306           d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
307           m.add_molecule (d);
308           reg = reg.right_str (reg.length_i ()-1);
309         }
310       if (reg.left_str (1) == "F")
311         {
312           Molecule d = fm->find_by_name ("accordion-accDot");
313           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
314           m.add_molecule (d);
315           reg = reg.right_str (reg.length_i ()-1);
316         }
317       if (reg.left_str (1) == "M")
318         {
319           Molecule d = fm->find_by_name ("accordion-accDot");
320           d.translate_axis (staff_space * 2 PT, Y_AXIS);
321           d.translate_axis (staff_space PT, X_AXIS);
322           m.add_molecule (d);
323           reg = reg.right_str (reg.length_i ()-1);
324         }
325       if (reg.left_str (1) == "E")
326         {
327           Molecule d = fm->find_by_name ("accordion-accDot");
328           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
329           m.add_molecule (d);
330           reg = reg.right_str (reg.length_i ()-1);
331         }
332       if (reg.left_str (1) == "S")
333         {
334           Molecule d = fm->find_by_name ("accordion-accDot");
335           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
336           m.add_molecule (d);
337           reg = reg.right_str (reg.length_i ()-1);
338         }
339     }
340   /* ugh maybe try to use regular font for S.B. and B.B and only use one font
341      for the rectangle */
342   else if (sym == "SB")
343     {
344       Molecule r = fm->find_by_name ("accordion-accSB");
345       m.add_molecule (r);
346     }
347   else if (sym == "BB")
348     {
349       Molecule r = fm->find_by_name ("accordion-accBB");
350       m.add_molecule (r);
351     }
352   else if (sym == "OldEE")
353     {
354       Molecule r = fm->find_by_name ("accordion-accOldEE");
355       m.add_molecule (r);
356     }
357   else if (sym == "OldEES")
358     {
359       Molecule r = fm->find_by_name ("accordion-accOldEES");
360       m.add_molecule (r);
361     }
362   return m;  
363 }
364
365 /*
366   TODO: should use slope instead?  Angle gives nasty rad <-> degree
367   conversions.
368 */
369 Molecule
370 Lookup::repeat_slash (Real w, Real s, Real t)
371 {
372   SCM wid = gh_double2scm (w);
373   SCM sl = gh_double2scm (s);
374   SCM thick = gh_double2scm (t);
375   SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
376                             wid, sl, thick, SCM_UNDEFINED);
377
378   Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
379          Interval (0, w * s));
380
381   return Molecule (b, slashnodot); //  http://slashnodot.org
382 }