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