]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.5.36
[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--2002 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 Molecule
50 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
51 {
52   SCM l = SCM_EOL;
53
54   for (int i= 4; i -- ;)
55     {
56       l = gh_cons (ly_offset2scm (b.control_[i]), l);
57     }
58
59   SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
60                                gh_double2scm (thick), 
61                                gh_double2scm (dash),
62                                ly_quote_scm (l),
63                                SCM_UNDEFINED));
64
65   Box box (Interval (0,0),Interval (0,0));
66   return   Molecule (box, at);
67 }
68
69 Molecule
70 Lookup::blank (Box b) 
71 {
72   return Molecule (b, SCM_EOL);
73 }
74
75
76 Molecule
77 Lookup::filledbox (Box b) 
78 {
79   SCM  at  = (scm_list_n (ly_symbol2scm ("filledbox"),
80                      gh_double2scm (-b[X_AXIS][LEFT]),
81                      gh_double2scm (b[X_AXIS][RIGHT]),                 
82                      gh_double2scm (-b[Y_AXIS][DOWN]),
83                      gh_double2scm (b[Y_AXIS][UP]),                    
84                      SCM_UNDEFINED));
85
86   return Molecule (b,at);
87 }
88
89
90 Molecule
91 Lookup::frame (Box b, Real thick)
92 {
93   Molecule m;
94   Direction d = LEFT;
95   Axis a = X_AXIS;
96   while (a < NO_AXES)
97     {
98       do
99         {
100           Axis o = Axis ((a+1)%NO_AXES);
101
102           Box edges;
103           edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
104           edges[o][DOWN] = b[o][DOWN] - thick/2;
105           edges[o][UP] = b[o][UP] + thick/2;      
106           
107           m.add_molecule (filledbox (edges));
108         }
109       while (flip (&d) != LEFT);
110     }
111   return m;
112   
113 }
114
115
116 /*
117   Make a smooth curve along the points 
118  */
119 Molecule
120 Lookup::slur (Bezier curve, Real curvethick, Real linethick) 
121 {
122   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
123   Bezier back = curve;
124   Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
125   back.reverse ();
126   back.control_[1] += perp;
127   back.control_[2] += perp;
128
129   curve.control_[1] -= perp;
130   curve.control_[2] -= perp;
131   
132   SCM scontrols[8];
133
134   for (int i=4; i--;)
135     scontrols[ i ] = ly_offset2scm (back.control_[i]);
136   for (int i=4 ; i--;)
137     scontrols[i+4] = ly_offset2scm (curve.control_[i]);
138
139   /*
140     Need the weird order b.o. the way PS want its arguments  
141    */
142   int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
143   SCM list = SCM_EOL;
144   for (int i= 8; i--;)
145     {
146       list = gh_cons (scontrols[indices[i]], list);
147     }
148   
149   
150   SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
151                      ly_quote_scm (list),
152                      gh_double2scm (linethick),
153                      SCM_UNDEFINED));
154   Box b(curve.extent (X_AXIS),
155         curve.extent (Y_AXIS));
156
157   b[X_AXIS].unite (back.extent (X_AXIS));
158   b[Y_AXIS].unite (back.extent (Y_AXIS));
159
160   return Molecule (b, at);
161 }
162
163 /*
164   TODO: junk me.
165  */
166 Molecule
167 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm) 
168 {
169   Molecule m;
170   String sym = ly_scm2string (ly_car (s));
171   String reg = ly_scm2string (ly_car (ly_cdr (s)));
172
173   if (sym == "Discant")
174     {
175       Molecule r = fm->find_by_name ("accordion-accDiscant");
176       m.add_molecule (r);
177       if (reg.left_str (1) == "F")
178         {
179           Molecule d = fm->find_by_name ("accordion-accDot");
180           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
181           m.add_molecule (d);
182           reg = reg.right_str (reg.length_i ()-1);
183         }
184       int eflag = 0x00;
185       if (reg.left_str (3) == "EEE")
186         {
187           eflag = 0x07;
188           reg = reg.right_str (reg.length_i ()-3);
189         }
190       else if (reg.left_str (2) == "EE")
191         {
192           eflag = 0x05;
193           reg = reg.right_str (reg.length_i ()-2);
194         }
195       else if (reg.left_str (2) == "Eh")
196         {
197           eflag = 0x04;
198           reg = reg.right_str (reg.length_i ()-2);
199         }
200       else if (reg.left_str (1) == "E")
201         {
202           eflag = 0x02;
203           reg = reg.right_str (reg.length_i ()-1);
204         }
205       if (eflag & 0x02)
206         {
207           Molecule d = fm->find_by_name ("accordion-accDot");
208           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
209           m.add_molecule (d);
210         }
211       if (eflag & 0x04)
212         {
213           Molecule d = fm->find_by_name ("accordion-accDot");
214           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
215           d.translate_axis (0.8 * staff_space PT, X_AXIS);
216           m.add_molecule (d);
217         }
218       if (eflag & 0x01)
219         {
220           Molecule d = fm->find_by_name ("accordion-accDot");
221           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
222           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
223           m.add_molecule (d);
224         }
225       if (reg.left_str (2) == "SS")
226         {
227           Molecule d = fm->find_by_name ("accordion-accDot");
228           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
229           d.translate_axis (0.4 * staff_space PT, X_AXIS);
230           m.add_molecule (d);
231           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
232           m.add_molecule (d);
233           reg = reg.right_str (reg.length_i ()-2);
234         }
235       if (reg.left_str (1) == "S")
236         {
237           Molecule d = fm->find_by_name ("accordion-accDot");
238           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
239           m.add_molecule (d);
240           reg = reg.right_str (reg.length_i ()-1);
241         }
242     }
243   else if (sym == "Freebase")
244     {
245       Molecule r = fm->find_by_name ("accordion-accFreebase");
246       m.add_molecule (r);
247       if (reg.left_str (1) == "F")
248         {
249           Molecule d = fm->find_by_name ("accordion-accDot");
250           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
251           m.add_molecule (d);
252           reg = reg.right_str (reg.length_i ()-1);
253         }
254       if (reg == "E")
255         {
256           Molecule d = fm->find_by_name ("accordion-accDot");
257           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
258           m.add_molecule (d);
259         }
260     }
261   else if (sym == "Bayanbase")
262     {
263       Molecule r = fm->find_by_name ("accordion-accBayanbase");
264       m.add_molecule (r);
265       if (reg.left_str (1) == "T")
266         {
267           Molecule d = fm->find_by_name ("accordion-accDot");
268           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
269           m.add_molecule (d);
270           reg = reg.right_str (reg.length_i ()-1);
271         }
272       /* include 4' reed just for completeness. You don't want to use this. */
273       if (reg.left_str (1) == "F")
274         {
275           Molecule d = fm->find_by_name ("accordion-accDot");
276           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
277           m.add_molecule (d);
278           reg = reg.right_str (reg.length_i ()-1);
279         }
280       if (reg.left_str (2) == "EE")
281         {
282           Molecule d = fm->find_by_name ("accordion-accDot");
283           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
284           d.translate_axis (0.4 * staff_space PT, X_AXIS);
285           m.add_molecule (d);
286           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
287           m.add_molecule (d);
288           reg = reg.right_str (reg.length_i ()-2);
289         }
290       if (reg.left_str (1) == "E")
291         {
292           Molecule d = fm->find_by_name ("accordion-accDot");
293           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
294           m.add_molecule (d);
295           reg = reg.right_str (reg.length_i ()-1);
296         }
297     }
298   else if (sym == "Stdbase")
299     {
300       Molecule r = fm->find_by_name ("accordion-accStdbase");
301       m.add_molecule (r);
302       if (reg.left_str (1) == "T")
303         {
304           Molecule d = fm->find_by_name ("accordion-accDot");
305           d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
306           m.add_molecule (d);
307           reg = reg.right_str (reg.length_i ()-1);
308         }
309       if (reg.left_str (1) == "F")
310         {
311           Molecule d = fm->find_by_name ("accordion-accDot");
312           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
313           m.add_molecule (d);
314           reg = reg.right_str (reg.length_i ()-1);
315         }
316       if (reg.left_str (1) == "M")
317         {
318           Molecule d = fm->find_by_name ("accordion-accDot");
319           d.translate_axis (staff_space * 2 PT, Y_AXIS);
320           d.translate_axis (staff_space PT, X_AXIS);
321           m.add_molecule (d);
322           reg = reg.right_str (reg.length_i ()-1);
323         }
324       if (reg.left_str (1) == "E")
325         {
326           Molecule d = fm->find_by_name ("accordion-accDot");
327           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
328           m.add_molecule (d);
329           reg = reg.right_str (reg.length_i ()-1);
330         }
331       if (reg.left_str (1) == "S")
332         {
333           Molecule d = fm->find_by_name ("accordion-accDot");
334           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
335           m.add_molecule (d);
336           reg = reg.right_str (reg.length_i ()-1);
337         }
338     }
339   /* ugh maybe try to use regular font for S.B. and B.B and only use one font
340      for the rectangle */
341   else if (sym == "SB")
342     {
343       Molecule r = fm->find_by_name ("accordion-accSB");
344       m.add_molecule (r);
345     }
346   else if (sym == "BB")
347     {
348       Molecule r = fm->find_by_name ("accordion-accBB");
349       m.add_molecule (r);
350     }
351   else if (sym == "OldEE")
352     {
353       Molecule r = fm->find_by_name ("accordion-accOldEE");
354       m.add_molecule (r);
355     }
356   else if (sym == "OldEES")
357     {
358       Molecule r = fm->find_by_name ("accordion-accOldEES");
359       m.add_molecule (r);
360     }
361   return m;  
362 }
363
364 /*
365   TODO: should use slope instead?  Angle gives nasty rad <-> degree
366   conversions.
367 */
368 Molecule
369 Lookup::repeat_slash (Real w, Real s, Real t)
370 {
371   SCM wid = gh_double2scm (w);
372   SCM sl = gh_double2scm (s);
373   SCM thick = gh_double2scm (t);
374   SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
375                             wid, sl, thick, SCM_UNDEFINED);
376
377   Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
378          Interval (0, w * s));
379
380   return Molecule (b, slashnodot); //  http://slashnodot.org
381 }
382
383
384
385 Molecule
386 Lookup::bracket (Axis a, Interval iv, Direction d, Real thick, Real protude)
387 {
388   Box b;
389   Axis other = Axis((a+1)%2);
390   b[a] = iv;
391   b[other] = Interval(-1, 1) * thick * 0.5;
392   
393   Molecule m =  filledbox (b);
394
395   b[a] = Interval (iv[UP] - thick, iv[UP]);
396   Interval oi = Interval (-thick/2, thick/2 + protude) ;
397   oi *=  d;
398   b[other] = oi;
399   m.add_molecule (filledbox (b));
400   b[a] = Interval (iv[DOWN], iv[DOWN]  +thick);
401   m.add_molecule (filledbox(b));
402
403   return m;
404 }
405
406 SCM
407 ly_bracket (SCM a, SCM iv, SCM d, SCM t, SCM p)
408 {
409   SCM_ASSERT_TYPE(ly_axis_p (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
410   SCM_ASSERT_TYPE(ly_number_pair_p (iv), iv, SCM_ARG1, __FUNCTION__, "number pair") ;
411   SCM_ASSERT_TYPE(ly_dir_p (d), a, SCM_ARG1, __FUNCTION__, "direction") ;
412   SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG1, __FUNCTION__, "number") ;
413   SCM_ASSERT_TYPE(gh_number_p(p), a, SCM_ARG1, __FUNCTION__, "number") ;
414
415
416   return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
417                   (Direction)gh_scm2int (d), gh_scm2double (t), gh_scm2double (p)).smobbed_copy ();
418 }
419   
420 static void
421 lookup_init ()
422 {
423   scm_c_define_gsubr ("ly-bracket", 5, 0, 0, (Scheme_function_unknown) ly_bracket);
424 }
425
426 ADD_SCM_INIT_FUNC (lookup,lookup_init);
427