]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.5.47
[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  * Bezier Sandwich:
165  *
166  *                               .|
167  *                        .       |
168  *              top .             |
169  *              . curve           |
170  *          .                     |
171  *       .                        |
172  *     .                          |
173  *    |                           |
174  *    |                          .|
175  *    |                     .
176  *    |         bottom .
177  *    |            . curve
178  *    |         .
179  *    |      .
180  *    |   .
181  *    | .
182  *    |.
183  *    |
184  *
185  */
186 Molecule
187 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
188 {
189   /*
190     Need the weird order b.o. the way PS want its arguments  
191    */
192   SCM list = SCM_EOL;
193   list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
194   list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
195   list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
196   list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
197   list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
198   list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
199   list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
200   list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
201
202   SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
203                                     ly_quote_scm (list),
204                                     gh_double2scm (0.0),
205                                     SCM_UNDEFINED);
206
207   Interval x_extent = top_curve.extent (X_AXIS);
208   x_extent.unite (bottom_curve.extent (X_AXIS));
209   Interval y_extent = top_curve.extent (Y_AXIS);
210   y_extent.unite (bottom_curve.extent (Y_AXIS));
211   Box b (x_extent, y_extent);
212
213   return Molecule (b, horizontal_bend);
214 }
215
216 /*
217  * Horizontal Slope:
218  *
219  *            /|   ^
220  *           / |   |
221  *          /  |   | height
222  *         /   |   |
223  *        /    |   v
224  *       |    /
225  *       |   /
226  * (0,0) x  /slope=dy/dx
227  *       | /
228  *       |/
229  *
230  *       <----->
231  *        width
232  */
233 Molecule
234 Lookup::horizontal_slope (Real width, Real slope, Real height)
235 {
236   SCM width_scm = gh_double2scm (width);
237   SCM slope_scm = gh_double2scm (slope);
238   SCM height_scm = gh_double2scm (height);
239   SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
240                                      width_scm, slope_scm,
241                                      height_scm, SCM_UNDEFINED);
242   Box b (Interval (0, width),
243          Interval (-height/2, height/2 + width*slope));
244   return Molecule (b, horizontal_slope);
245 }
246
247 /*
248   TODO: junk me.
249  */
250 Molecule
251 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm) 
252 {
253   Molecule m;
254   String sym = ly_scm2string (ly_car (s));
255   String reg = ly_scm2string (ly_car (ly_cdr (s)));
256
257   if (sym == "Discant")
258     {
259       Molecule r = fm->find_by_name ("accordion-accDiscant");
260       m.add_molecule (r);
261       if (reg.left_str (1) == "F")
262         {
263           Molecule d = fm->find_by_name ("accordion-accDot");
264           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
265           m.add_molecule (d);
266           reg = reg.right_str (reg.length_i ()-1);
267         }
268       int eflag = 0x00;
269       if (reg.left_str (3) == "EEE")
270         {
271           eflag = 0x07;
272           reg = reg.right_str (reg.length_i ()-3);
273         }
274       else if (reg.left_str (2) == "EE")
275         {
276           eflag = 0x05;
277           reg = reg.right_str (reg.length_i ()-2);
278         }
279       else if (reg.left_str (2) == "Eh")
280         {
281           eflag = 0x04;
282           reg = reg.right_str (reg.length_i ()-2);
283         }
284       else if (reg.left_str (1) == "E")
285         {
286           eflag = 0x02;
287           reg = reg.right_str (reg.length_i ()-1);
288         }
289       if (eflag & 0x02)
290         {
291           Molecule d = fm->find_by_name ("accordion-accDot");
292           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
293           m.add_molecule (d);
294         }
295       if (eflag & 0x04)
296         {
297           Molecule d = fm->find_by_name ("accordion-accDot");
298           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
299           d.translate_axis (0.8 * staff_space PT, X_AXIS);
300           m.add_molecule (d);
301         }
302       if (eflag & 0x01)
303         {
304           Molecule d = fm->find_by_name ("accordion-accDot");
305           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
306           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
307           m.add_molecule (d);
308         }
309       if (reg.left_str (2) == "SS")
310         {
311           Molecule d = fm->find_by_name ("accordion-accDot");
312           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
313           d.translate_axis (0.4 * staff_space PT, X_AXIS);
314           m.add_molecule (d);
315           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
316           m.add_molecule (d);
317           reg = reg.right_str (reg.length_i ()-2);
318         }
319       if (reg.left_str (1) == "S")
320         {
321           Molecule d = fm->find_by_name ("accordion-accDot");
322           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
323           m.add_molecule (d);
324           reg = reg.right_str (reg.length_i ()-1);
325         }
326     }
327   else if (sym == "Freebase")
328     {
329       Molecule r = fm->find_by_name ("accordion-accFreebase");
330       m.add_molecule (r);
331       if (reg.left_str (1) == "F")
332         {
333           Molecule d = fm->find_by_name ("accordion-accDot");
334           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
335           m.add_molecule (d);
336           reg = reg.right_str (reg.length_i ()-1);
337         }
338       if (reg == "E")
339         {
340           Molecule d = fm->find_by_name ("accordion-accDot");
341           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
342           m.add_molecule (d);
343         }
344     }
345   else if (sym == "Bayanbase")
346     {
347       Molecule r = fm->find_by_name ("accordion-accBayanbase");
348       m.add_molecule (r);
349       if (reg.left_str (1) == "T")
350         {
351           Molecule d = fm->find_by_name ("accordion-accDot");
352           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
353           m.add_molecule (d);
354           reg = reg.right_str (reg.length_i ()-1);
355         }
356       /* include 4' reed just for completeness. You don't want to use this. */
357       if (reg.left_str (1) == "F")
358         {
359           Molecule d = fm->find_by_name ("accordion-accDot");
360           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
361           m.add_molecule (d);
362           reg = reg.right_str (reg.length_i ()-1);
363         }
364       if (reg.left_str (2) == "EE")
365         {
366           Molecule d = fm->find_by_name ("accordion-accDot");
367           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
368           d.translate_axis (0.4 * staff_space PT, X_AXIS);
369           m.add_molecule (d);
370           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
371           m.add_molecule (d);
372           reg = reg.right_str (reg.length_i ()-2);
373         }
374       if (reg.left_str (1) == "E")
375         {
376           Molecule d = fm->find_by_name ("accordion-accDot");
377           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
378           m.add_molecule (d);
379           reg = reg.right_str (reg.length_i ()-1);
380         }
381     }
382   else if (sym == "Stdbase")
383     {
384       Molecule r = fm->find_by_name ("accordion-accStdbase");
385       m.add_molecule (r);
386       if (reg.left_str (1) == "T")
387         {
388           Molecule d = fm->find_by_name ("accordion-accDot");
389           d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
390           m.add_molecule (d);
391           reg = reg.right_str (reg.length_i ()-1);
392         }
393       if (reg.left_str (1) == "F")
394         {
395           Molecule d = fm->find_by_name ("accordion-accDot");
396           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
397           m.add_molecule (d);
398           reg = reg.right_str (reg.length_i ()-1);
399         }
400       if (reg.left_str (1) == "M")
401         {
402           Molecule d = fm->find_by_name ("accordion-accDot");
403           d.translate_axis (staff_space * 2 PT, Y_AXIS);
404           d.translate_axis (staff_space PT, X_AXIS);
405           m.add_molecule (d);
406           reg = reg.right_str (reg.length_i ()-1);
407         }
408       if (reg.left_str (1) == "E")
409         {
410           Molecule d = fm->find_by_name ("accordion-accDot");
411           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
412           m.add_molecule (d);
413           reg = reg.right_str (reg.length_i ()-1);
414         }
415       if (reg.left_str (1) == "S")
416         {
417           Molecule d = fm->find_by_name ("accordion-accDot");
418           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
419           m.add_molecule (d);
420           reg = reg.right_str (reg.length_i ()-1);
421         }
422     }
423   /* ugh maybe try to use regular font for S.B. and B.B and only use one font
424      for the rectangle */
425   else if (sym == "SB")
426     {
427       Molecule r = fm->find_by_name ("accordion-accSB");
428       m.add_molecule (r);
429     }
430   else if (sym == "BB")
431     {
432       Molecule r = fm->find_by_name ("accordion-accBB");
433       m.add_molecule (r);
434     }
435   else if (sym == "OldEE")
436     {
437       Molecule r = fm->find_by_name ("accordion-accOldEE");
438       m.add_molecule (r);
439     }
440   else if (sym == "OldEES")
441     {
442       Molecule r = fm->find_by_name ("accordion-accOldEES");
443       m.add_molecule (r);
444     }
445   return m;  
446 }
447
448 Molecule
449 Lookup::repeat_slash (Real w, Real s, Real t)
450 {
451   SCM wid = gh_double2scm (w);
452   SCM sl = gh_double2scm (s);
453   SCM thick = gh_double2scm (t);
454   SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
455                             wid, sl, thick, SCM_UNDEFINED);
456
457   Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
458          Interval (0, w * s));
459
460   return Molecule (b, slashnodot); //  http://slashnodot.org
461 }
462
463
464
465 Molecule
466 Lookup::bracket (Axis a, Interval iv, Direction d, Real thick, Real protude)
467 {
468   Box b;
469   Axis other = Axis((a+1)%2);
470   b[a] = iv;
471   b[other] = Interval(-1, 1) * thick * 0.5;
472   
473   Molecule m =  filledbox (b);
474
475   b[a] = Interval (iv[UP] - thick, iv[UP]);
476   Interval oi = Interval (-thick/2, thick/2 + protude) ;
477   oi *=  d;
478   b[other] = oi;
479   m.add_molecule (filledbox (b));
480   b[a] = Interval (iv[DOWN], iv[DOWN]  +thick);
481   m.add_molecule (filledbox(b));
482
483   return m;
484 }
485
486 SCM
487 ly_bracket (SCM a, SCM iv, SCM d, SCM t, SCM p)
488 {
489   SCM_ASSERT_TYPE(ly_axis_p (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
490   SCM_ASSERT_TYPE(ly_number_pair_p (iv), iv, SCM_ARG1, __FUNCTION__, "number pair") ;
491   SCM_ASSERT_TYPE(ly_dir_p (d), a, SCM_ARG1, __FUNCTION__, "direction") ;
492   SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG1, __FUNCTION__, "number") ;
493   SCM_ASSERT_TYPE(gh_number_p(p), a, SCM_ARG1, __FUNCTION__, "number") ;
494
495
496   return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
497                   (Direction)gh_scm2int (d), gh_scm2double (t), gh_scm2double (p)).smobbed_copy ();
498 }
499   
500 static void
501 lookup_init ()
502 {
503   scm_c_define_gsubr ("ly-bracket", 5, 0, 0, (Scheme_function_unknown) ly_bracket);
504 }
505
506 ADD_SCM_INIT_FUNC (lookup,lookup_init);
507