]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
''
[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 Molecule
49 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
50 {
51   SCM l = SCM_EOL;
52
53   for (int i= 4; i -- ;)
54     {
55       l = gh_cons (ly_offset2scm (b.control_[i]), l);
56     }
57
58   SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
59                                gh_double2scm (thick), 
60                                gh_double2scm (dash),
61                                ly_quote_scm (l),
62                                SCM_UNDEFINED));
63
64   Box box (Interval (0,0),Interval (0,0));
65   return   Molecule (box, at);
66 }
67
68 Molecule
69 Lookup::blank (Box b) 
70 {
71   return Molecule (b, SCM_EOL);
72 }
73
74 Molecule
75 Lookup::filledbox (Box b) 
76 {
77   SCM  at  = (scm_list_n (ly_symbol2scm ("filledbox"),
78                      gh_double2scm (-b[X_AXIS][LEFT]),
79                      gh_double2scm (b[X_AXIS][RIGHT]),                 
80                      gh_double2scm (-b[Y_AXIS][DOWN]),
81                      gh_double2scm (b[Y_AXIS][UP]),                    
82                      SCM_UNDEFINED));
83
84   return Molecule (b,at);
85 }
86
87 /*
88  * round filled box:
89  *
90  *   __________________________________
91  *  /     \  ^           /     \      ^
92  * |         |blot              |     |
93  * |       | |dia       |       |     |
94  * |         |meter             |     |
95  * |\ _ _ /  v           \ _ _ /|     |
96  * |                            |     |
97  * |                            |     | Box
98  * |                    <------>|     | extent
99  * |                      blot  |     | (Y_AXIS)
100  * |                    diameter|     |
101  * |                            |     |
102  * |  _ _                  _ _  |     |
103  * |/     \              /     \|     |
104  * |                            |     |
105  * |       |            |       |     |
106  * |                            |     |
107  * x\_____/______________\_____/|_____v
108  * |(0,0)                       |
109  * |                            |
110  * |                            |
111  * |<-------------------------->|
112  *       Box extent(X_AXIS)
113  */
114 Molecule
115 Lookup::roundfilledbox (Box b, Real blotdiameter)
116 {
117   if (b.x ().length () < blotdiameter)
118     {
119       programming_error (_f ("round filled box horizontal extent smaller than blot; decreasing blot"));
120       blotdiameter = b.x ().length ();
121     }
122   if (b.y ().length () < blotdiameter)
123     {
124       programming_error (_f ("round filled box vertical extent smaller than blot; decreasing blot"));
125       blotdiameter = b.y ().length ();
126     }
127
128   SCM at = (scm_list_n (ly_symbol2scm ("roundfilledbox"),
129                         gh_double2scm (-b[X_AXIS][LEFT]),
130                         gh_double2scm (b[X_AXIS][RIGHT]),
131                         gh_double2scm (-b[Y_AXIS][DOWN]),
132                         gh_double2scm (b[Y_AXIS][UP]),
133                         gh_double2scm (blotdiameter),
134                         SCM_UNDEFINED));
135
136   return Molecule (b,at);
137 }
138
139 Molecule
140 Lookup::frame (Box b, Real thick)
141 {
142   Molecule m;
143   Direction d = LEFT;
144   Axis a = X_AXIS;
145   while (a < NO_AXES)
146     {
147       do
148         {
149           Axis o = Axis ((a+1)%NO_AXES);
150
151           Box edges;
152           edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
153           edges[o][DOWN] = b[o][DOWN] - thick/2;
154           edges[o][UP] = b[o][UP] + thick/2;      
155           
156           m.add_molecule (filledbox (edges));
157         }
158       while (flip (&d) != LEFT);
159     }
160   return m;
161   
162 }
163
164 /*
165   Make a smooth curve along the points 
166  */
167 Molecule
168 Lookup::slur (Bezier curve, Real curvethick, Real linethick) 
169 {
170   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
171   Bezier back = curve;
172   Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
173   back.reverse ();
174   back.control_[1] += perp;
175   back.control_[2] += perp;
176
177   curve.control_[1] -= perp;
178   curve.control_[2] -= perp;
179   
180   SCM scontrols[8];
181
182   for (int i=4; i--;)
183     scontrols[ i ] = ly_offset2scm (back.control_[i]);
184   for (int i=4 ; i--;)
185     scontrols[i+4] = ly_offset2scm (curve.control_[i]);
186
187   /*
188     Need the weird order b.o. the way PS want its arguments  
189    */
190   int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
191   SCM list = SCM_EOL;
192   for (int i= 8; i--;)
193     {
194       list = gh_cons (scontrols[indices[i]], list);
195     }
196   
197   
198   SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
199                      ly_quote_scm (list),
200                      gh_double2scm (linethick),
201                      SCM_UNDEFINED));
202   Box b(curve.extent (X_AXIS),
203         curve.extent (Y_AXIS));
204
205   b[X_AXIS].unite (back.extent (X_AXIS));
206   b[Y_AXIS].unite (back.extent (Y_AXIS));
207
208   return Molecule (b, at);
209 }
210
211 /*
212  * Bezier Sandwich:
213  *
214  *                               .|
215  *                        .       |
216  *              top .             |
217  *              . curve           |
218  *          .                     |
219  *       .                        |
220  *     .                          |
221  *    |                           |
222  *    |                          .|
223  *    |                     .
224  *    |         bottom .
225  *    |            . curve
226  *    |         .
227  *    |      .
228  *    |   .
229  *    | .
230  *    |.
231  *    |
232  *
233  */
234 Molecule
235 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
236 {
237   /*
238     Need the weird order b.o. the way PS want its arguments  
239    */
240   SCM list = SCM_EOL;
241   list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
242   list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
243   list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
244   list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
245   list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
246   list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
247   list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
248   list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
249
250   SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
251                                     ly_quote_scm (list),
252                                     gh_double2scm (0.0),
253                                     SCM_UNDEFINED);
254
255   Interval x_extent = top_curve.extent (X_AXIS);
256   x_extent.unite (bottom_curve.extent (X_AXIS));
257   Interval y_extent = top_curve.extent (Y_AXIS);
258   y_extent.unite (bottom_curve.extent (Y_AXIS));
259   Box b (x_extent, y_extent);
260
261   return Molecule (b, horizontal_bend);
262 }
263
264 /*
265  * Horizontal Slope:
266  *
267  *            /|   ^
268  *           / |   |
269  *          /  |   | height
270  *         /   |   |
271  *        /    |   v
272  *       |    /
273  *       |   /
274  * (0,0) x  /slope=dy/dx
275  *       | /
276  *       |/
277  *
278  *       <----->
279  *        width
280  */
281 Molecule
282 Lookup::horizontal_slope (Real width, Real slope, Real height)
283 {
284   SCM width_scm = gh_double2scm (width);
285   SCM slope_scm = gh_double2scm (slope);
286   SCM height_scm = gh_double2scm (height);
287   SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
288                                      width_scm, slope_scm,
289                                      height_scm, SCM_UNDEFINED);
290   Box b (Interval (0, width),
291          Interval (-height/2, height/2 + width*slope));
292   return Molecule (b, horizontal_slope);
293 }
294
295 /*
296   TODO: junk me.
297  */
298 Molecule
299 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm) 
300 {
301   Molecule m;
302   String sym = ly_scm2string (ly_car (s));
303   String reg = ly_scm2string (ly_car (ly_cdr (s)));
304
305   if (sym == "Discant")
306     {
307       Molecule r = fm->find_by_name ("accordion-accDiscant");
308       m.add_molecule (r);
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       int eflag = 0x00;
317       if (reg.left_str (3) == "EEE")
318         {
319           eflag = 0x07;
320           reg = reg.right_str (reg.length_i ()-3);
321         }
322       else if (reg.left_str (2) == "EE")
323         {
324           eflag = 0x05;
325           reg = reg.right_str (reg.length_i ()-2);
326         }
327       else if (reg.left_str (2) == "Eh")
328         {
329           eflag = 0x04;
330           reg = reg.right_str (reg.length_i ()-2);
331         }
332       else if (reg.left_str (1) == "E")
333         {
334           eflag = 0x02;
335           reg = reg.right_str (reg.length_i ()-1);
336         }
337       if (eflag & 0x02)
338         {
339           Molecule d = fm->find_by_name ("accordion-accDot");
340           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
341           m.add_molecule (d);
342         }
343       if (eflag & 0x04)
344         {
345           Molecule d = fm->find_by_name ("accordion-accDot");
346           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
347           d.translate_axis (0.8 * staff_space PT, X_AXIS);
348           m.add_molecule (d);
349         }
350       if (eflag & 0x01)
351         {
352           Molecule d = fm->find_by_name ("accordion-accDot");
353           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
354           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
355           m.add_molecule (d);
356         }
357       if (reg.left_str (2) == "SS")
358         {
359           Molecule d = fm->find_by_name ("accordion-accDot");
360           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
361           d.translate_axis (0.4 * staff_space PT, X_AXIS);
362           m.add_molecule (d);
363           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
364           m.add_molecule (d);
365           reg = reg.right_str (reg.length_i ()-2);
366         }
367       if (reg.left_str (1) == "S")
368         {
369           Molecule d = fm->find_by_name ("accordion-accDot");
370           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
371           m.add_molecule (d);
372           reg = reg.right_str (reg.length_i ()-1);
373         }
374     }
375   else if (sym == "Freebase")
376     {
377       Molecule r = fm->find_by_name ("accordion-accFreebase");
378       m.add_molecule (r);
379       if (reg.left_str (1) == "F")
380         {
381           Molecule d = fm->find_by_name ("accordion-accDot");
382           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
383           m.add_molecule (d);
384           reg = reg.right_str (reg.length_i ()-1);
385         }
386       if (reg == "E")
387         {
388           Molecule d = fm->find_by_name ("accordion-accDot");
389           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
390           m.add_molecule (d);
391         }
392     }
393   else if (sym == "Bayanbase")
394     {
395       Molecule r = fm->find_by_name ("accordion-accBayanbase");
396       m.add_molecule (r);
397       if (reg.left_str (1) == "T")
398         {
399           Molecule d = fm->find_by_name ("accordion-accDot");
400           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
401           m.add_molecule (d);
402           reg = reg.right_str (reg.length_i ()-1);
403         }
404       /* include 4' reed just for completeness. You don't want to use this. */
405       if (reg.left_str (1) == "F")
406         {
407           Molecule d = fm->find_by_name ("accordion-accDot");
408           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
409           m.add_molecule (d);
410           reg = reg.right_str (reg.length_i ()-1);
411         }
412       if (reg.left_str (2) == "EE")
413         {
414           Molecule d = fm->find_by_name ("accordion-accDot");
415           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
416           d.translate_axis (0.4 * staff_space PT, X_AXIS);
417           m.add_molecule (d);
418           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
419           m.add_molecule (d);
420           reg = reg.right_str (reg.length_i ()-2);
421         }
422       if (reg.left_str (1) == "E")
423         {
424           Molecule d = fm->find_by_name ("accordion-accDot");
425           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
426           m.add_molecule (d);
427           reg = reg.right_str (reg.length_i ()-1);
428         }
429     }
430   else if (sym == "Stdbase")
431     {
432       Molecule r = fm->find_by_name ("accordion-accStdbase");
433       m.add_molecule (r);
434       if (reg.left_str (1) == "T")
435         {
436           Molecule d = fm->find_by_name ("accordion-accDot");
437           d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
438           m.add_molecule (d);
439           reg = reg.right_str (reg.length_i ()-1);
440         }
441       if (reg.left_str (1) == "F")
442         {
443           Molecule d = fm->find_by_name ("accordion-accDot");
444           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
445           m.add_molecule (d);
446           reg = reg.right_str (reg.length_i ()-1);
447         }
448       if (reg.left_str (1) == "M")
449         {
450           Molecule d = fm->find_by_name ("accordion-accDot");
451           d.translate_axis (staff_space * 2 PT, Y_AXIS);
452           d.translate_axis (staff_space PT, X_AXIS);
453           m.add_molecule (d);
454           reg = reg.right_str (reg.length_i ()-1);
455         }
456       if (reg.left_str (1) == "E")
457         {
458           Molecule d = fm->find_by_name ("accordion-accDot");
459           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
460           m.add_molecule (d);
461           reg = reg.right_str (reg.length_i ()-1);
462         }
463       if (reg.left_str (1) == "S")
464         {
465           Molecule d = fm->find_by_name ("accordion-accDot");
466           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
467           m.add_molecule (d);
468           reg = reg.right_str (reg.length_i ()-1);
469         }
470     }
471   /* ugh maybe try to use regular font for S.B. and B.B and only use one font
472      for the rectangle */
473   else if (sym == "SB")
474     {
475       Molecule r = fm->find_by_name ("accordion-accSB");
476       m.add_molecule (r);
477     }
478   else if (sym == "BB")
479     {
480       Molecule r = fm->find_by_name ("accordion-accBB");
481       m.add_molecule (r);
482     }
483   else if (sym == "OldEE")
484     {
485       Molecule r = fm->find_by_name ("accordion-accOldEE");
486       m.add_molecule (r);
487     }
488   else if (sym == "OldEES")
489     {
490       Molecule r = fm->find_by_name ("accordion-accOldEES");
491       m.add_molecule (r);
492     }
493   return m;  
494 }
495
496 Molecule
497 Lookup::repeat_slash (Real w, Real s, Real t)
498 {
499   SCM wid = gh_double2scm (w);
500   SCM sl = gh_double2scm (s);
501   SCM thick = gh_double2scm (t);
502   SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
503                             wid, sl, thick, SCM_UNDEFINED);
504
505   Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
506          Interval (0, w * s));
507
508   return Molecule (b, slashnodot); //  http://slashnodot.org
509 }
510
511 Molecule
512 Lookup::bracket (Axis a, Interval iv, Direction d, Real thick, Real protude)
513 {
514   Box b;
515   Axis other = Axis((a+1)%2);
516   b[a] = iv;
517   b[other] = Interval(-1, 1) * thick * 0.5;
518   
519   Molecule m =  filledbox (b);
520
521   b[a] = Interval (iv[UP] - thick, iv[UP]);
522   Interval oi = Interval (-thick/2, thick/2 + protude) ;
523   oi *=  d;
524   b[other] = oi;
525   m.add_molecule (filledbox (b));
526   b[a] = Interval (iv[DOWN], iv[DOWN]  +thick);
527   m.add_molecule (filledbox(b));
528
529   return m;
530 }
531
532 SCM
533 ly_bracket (SCM a, SCM iv, SCM d, SCM t, SCM p)
534 {
535   SCM_ASSERT_TYPE(ly_axis_p (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
536   SCM_ASSERT_TYPE(ly_number_pair_p (iv), iv, SCM_ARG1, __FUNCTION__, "number pair") ;
537   SCM_ASSERT_TYPE(ly_dir_p (d), a, SCM_ARG1, __FUNCTION__, "direction") ;
538   SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG1, __FUNCTION__, "number") ;
539   SCM_ASSERT_TYPE(gh_number_p(p), a, SCM_ARG1, __FUNCTION__, "number") ;
540
541
542   return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
543                   (Direction)gh_scm2int (d), gh_scm2double (t), gh_scm2double (p)).smobbed_copy ();
544 }
545   
546 static void
547 lookup_init ()
548 {
549   scm_c_define_gsubr ("ly-bracket", 5, 0, 0, (Scheme_function_unknown) ly_bracket);
550 }
551
552 ADD_SCM_INIT_FUNC (lookup,lookup_init);
553