]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
036ba8895944abbbd2dd12bed515371f0d2aa64b
[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
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));  
132
133   SCM scontrols[8];
134
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]);
139
140   /*
141     Need the weird order b.o. the way PS want its arguments  
142    */
143   int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
144   SCM list = SCM_EOL;
145   for (int i= 8; i--;)
146     {
147       list = gh_cons (scontrols[indices[i]], list);
148     }
149   
150   
151   SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
152                      ly_quote_scm (list),
153                      gh_double2scm (linethick),
154                      SCM_UNDEFINED));
155
156   Box b (curve.extent (X_AXIS), curve.extent (Y_AXIS));
157   return Molecule (b, at);
158 }
159
160 Molecule
161 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm) 
162 {
163   Molecule m;
164   String sym = ly_scm2string (ly_car (s));
165   String reg = ly_scm2string (ly_car (ly_cdr (s)));
166
167   if (sym == "Discant")
168     {
169       Molecule r = fm->find_by_name ("accordion-accDiscant");
170       m.add_molecule (r);
171       if (reg.left_str (1) == "F")
172         {
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);
177         }
178       int eflag = 0x00;
179       if (reg.left_str (3) == "EEE")
180         {
181           eflag = 0x07;
182           reg = reg.right_str (reg.length_i ()-3);
183         }
184       else if (reg.left_str (2) == "EE")
185         {
186           eflag = 0x05;
187           reg = reg.right_str (reg.length_i ()-2);
188         }
189       else if (reg.left_str (2) == "Eh")
190         {
191           eflag = 0x04;
192           reg = reg.right_str (reg.length_i ()-2);
193         }
194       else if (reg.left_str (1) == "E")
195         {
196           eflag = 0x02;
197           reg = reg.right_str (reg.length_i ()-1);
198         }
199       if (eflag & 0x02)
200         {
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);
204         }
205       if (eflag & 0x04)
206         {
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);
211         }
212       if (eflag & 0x01)
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 (reg.left_str (2) == "SS")
220         {
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);
228         }
229       if (reg.left_str (1) == "S")
230         {
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);
235         }
236     }
237   else if (sym == "Freebase")
238     {
239       Molecule r = fm->find_by_name ("accordion-accFreebase");
240       m.add_molecule (r);
241       if (reg.left_str (1) == "F")
242         {
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);
247         }
248       if (reg == "E")
249         {
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);
253         }
254     }
255   else if (sym == "Bayanbase")
256     {
257       Molecule r = fm->find_by_name ("accordion-accBayanbase");
258       m.add_molecule (r);
259       if (reg.left_str (1) == "T")
260         {
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);
265         }
266       /* include 4' reed just for completeness. You don't want to use this. */
267       if (reg.left_str (1) == "F")
268         {
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);
273         }
274       if (reg.left_str (2) == "EE")
275         {
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);
283         }
284       if (reg.left_str (1) == "E")
285         {
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);
290         }
291     }
292   else if (sym == "Stdbase")
293     {
294       Molecule r = fm->find_by_name ("accordion-accStdbase");
295       m.add_molecule (r);
296       if (reg.left_str (1) == "T")
297         {
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);
302         }
303       if (reg.left_str (1) == "F")
304         {
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);
309         }
310       if (reg.left_str (1) == "M")
311         {
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);
317         }
318       if (reg.left_str (1) == "E")
319         {
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);
324         }
325       if (reg.left_str (1) == "S")
326         {
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);
331         }
332     }
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")
336     {
337       Molecule r = fm->find_by_name ("accordion-accSB");
338       m.add_molecule (r);
339     }
340   else if (sym == "BB")
341     {
342       Molecule r = fm->find_by_name ("accordion-accBB");
343       m.add_molecule (r);
344     }
345   else if (sym == "OldEE")
346     {
347       Molecule r = fm->find_by_name ("accordion-accOldEE");
348       m.add_molecule (r);
349     }
350   else if (sym == "OldEES")
351     {
352       Molecule r = fm->find_by_name ("accordion-accOldEES");
353       m.add_molecule (r);
354     }
355   return m;  
356 }
357
358 /*
359   TODO: should use slope instead?  Angle gives nasty rad <-> degree
360   conversions.
361 */
362 Molecule
363 Lookup::repeat_slash (Real w, Real s, Real t)
364 {
365   SCM wid = gh_double2scm (w);
366   SCM sl = gh_double2scm (s);
367   SCM thick = gh_double2scm (t);
368   SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
369                             wid, sl, thick, SCM_UNDEFINED);
370
371   Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
372          Interval (0, w * s));
373
374   return Molecule (b, slashnodot); //  http://slashnodot.org
375 }