]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
* lily/lookup.cc (LY_DEFINE): new function
[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--2003 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 #include "interval.hh"
27
28 Molecule
29 Lookup::dot (Offset p, Real radius)
30 {
31   SCM at = (scm_list_n (ly_symbol2scm ("dot"),
32                         gh_double2scm (p[X_AXIS]),
33                         gh_double2scm (p[Y_AXIS]),
34                         gh_double2scm (radius),
35                         SCM_UNDEFINED));
36   Box box;
37   box.add_point (p - Offset (radius, radius));
38   box.add_point (p + Offset (radius, radius));
39   return Molecule (box, at);
40 }
41
42 Molecule 
43 Lookup::beam (Real slope, Real width, Real thick) 
44 {
45   Real height = slope * width; 
46   Real min_y = (0 <? height) - thick/2;
47   Real max_y = (0 >? height) + thick/2;
48
49   
50
51   Box b (Interval (0, width),
52          Interval (min_y, max_y));
53
54   
55   SCM at = scm_list_n (ly_symbol2scm ("beam"),
56                     gh_double2scm (width),
57                     gh_double2scm (slope),
58                     gh_double2scm (thick),
59                     SCM_UNDEFINED);
60   return Molecule (b, at);
61 }
62
63 Molecule
64 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
65 {
66   SCM l = SCM_EOL;
67
68   for (int i= 4; i -- ;)
69     {
70       l = gh_cons (ly_offset2scm (b.control_[i]), l);
71     }
72
73   SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
74                                gh_double2scm (thick), 
75                                gh_double2scm (dash),
76                                ly_quote_scm (l),
77                                SCM_UNDEFINED));
78
79   Box box (Interval (0,0),Interval (0,0));
80   return   Molecule (box, at);
81 }
82
83 Molecule
84 Lookup::line (Real th, Offset f, Offset t)
85 {
86   SCM at = (scm_list_n (ly_symbol2scm ("draw-line"),
87                         gh_double2scm (th), 
88                         gh_double2scm (f[X_AXIS]),
89                         gh_double2scm (f[Y_AXIS]),
90                         gh_double2scm (t[X_AXIS]),
91                         gh_double2scm (t[Y_AXIS]),
92                         SCM_UNDEFINED));
93
94   Box box;
95   box.add_point (f);
96   box.add_point (t);
97
98   box[X_AXIS].widen (th/2);
99   box[Y_AXIS].widen (th/2);  
100
101   return Molecule (box, at);
102 }
103
104
105 Molecule
106 Lookup::blank (Box b) 
107 {
108   return Molecule (b, scm_makfrom0str (""));
109 }
110
111 Molecule
112 Lookup::filledbox (Box b) 
113 {
114   SCM  at  = (scm_list_n (ly_symbol2scm ("filledbox"),
115                      gh_double2scm (-b[X_AXIS][LEFT]),
116                      gh_double2scm (b[X_AXIS][RIGHT]),                 
117                      gh_double2scm (-b[Y_AXIS][DOWN]),
118                      gh_double2scm (b[Y_AXIS][UP]),                    
119                      SCM_UNDEFINED));
120
121   return Molecule (b,at);
122 }
123
124 /*
125  * round filled box:
126  *
127  *   __________________________________
128  *  /     \  ^           /     \      ^
129  * |         |blot              |     |
130  * |       | |dia       |       |     |
131  * |         |meter             |     |
132  * |\ _ _ /  v           \ _ _ /|     |
133  * |                            |     |
134  * |                            |     | Box
135  * |                    <------>|     | extent
136  * |                      blot  |     | (Y_AXIS)
137  * |                    diameter|     |
138  * |                            |     |
139  * |  _ _                  _ _  |     |
140  * |/     \              /     \|     |
141  * |                            |     |
142  * |       |            |       |     |
143  * |                            |     |
144  * x\_____/______________\_____/|_____v
145  * |(0,0)                       |
146  * |                            |
147  * |                            |
148  * |<-------------------------->|
149  *       Box extent(X_AXIS)
150  */
151 Molecule
152 Lookup::roundfilledbox (Box b, Real blotdiameter)
153 {
154   if (b.x ().length () < blotdiameter)
155     {
156       programming_error (_f ("round filled box horizontal extent smaller than blot; decreasing blot"));
157       blotdiameter = b.x ().length ();
158     }
159   if (b.y ().length () < blotdiameter)
160     {
161       programming_error (_f ("round filled box vertical extent smaller than blot; decreasing blot"));
162       blotdiameter = b.y ().length ();
163     }
164
165   SCM at = (scm_list_n (ly_symbol2scm ("roundfilledbox"),
166                         gh_double2scm (-b[X_AXIS][LEFT]),
167                         gh_double2scm (b[X_AXIS][RIGHT]),
168                         gh_double2scm (-b[Y_AXIS][DOWN]),
169                         gh_double2scm (b[Y_AXIS][UP]),
170                         gh_double2scm (blotdiameter),
171                         SCM_UNDEFINED));
172
173   return Molecule (b,at);
174 }
175
176 LY_DEFINE(ly_round_filled_box, "ly:round-filled-box",
177           2, 0,0,
178           (SCM b, SCM blot),
179           "Make a box with rounded corners. B is a pair of number-pairs, and BLOT a number")
180 {
181   SCM_ASSERT_TYPE(gh_number_p (blot), blot, SCM_ARG2, __FUNCTION__, "number") ;
182   SCM_ASSERT_TYPE(gh_pair_p (b), b, SCM_ARG4, __FUNCTION__, "pair") ;
183
184   SCM_ASSERT_TYPE(ly_number_pair_p (gh_car (b)), gh_car (b), SCM_ARG1, __FUNCTION__, "number-pair") ;
185   SCM_ASSERT_TYPE(ly_number_pair_p (gh_cdr (b)), gh_cdr (b), SCM_ARG1, __FUNCTION__, "number-pair") ;
186
187   Interval x (ly_scm2interval (gh_car (b)));
188   Interval y (ly_scm2interval (gh_cdr (b)));
189   
190   return Lookup::roundfilledbox (Box (x,y),
191                                  gh_scm2double (blot)).smobbed_copy();
192   
193 }
194           
195
196 /*
197  * Create Molecule that represents a filled polygon with round edges.
198  *
199  * LIMITATIONS:
200  *
201  * (a) Only outer (convex) edges are rounded.
202  *
203  * (b) This algorithm works as expected only for polygons whose edges
204  * do not intersect.  For example, the polygon ((0, 0), (q, 0), (0,
205  * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
206  * will give a strange result.  Even non-adjacent edges that just
207  * touch each other will in general not work as expected for non-null
208  * blotdiameter.
209  *
210  * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x(n-1), y(n-1))),
211  * if there is a natural number k such that blotdiameter is greater
212  * than the maximum of { | (x(k mod n), y(k mod n)) - (x((k+1) mod n),
213  * y((k+1) mod n)) |, | (x(k mod n), y(k mod n)) - (x((k+2) mod n),
214  * y((k+2) mod n)) |, | (x((k+1) mod n), y((k+1) mod n)) - (x((k+2)
215  * mod n), y((k+2) mod n)) | }, then the outline of the rounded
216  * polygon will exceed the outline of the core polygon.  In other
217  * words: Do not draw rounded polygons that have a leg smaller or
218  * thinner than blotdiameter (or set blotdiameter to a sufficiently
219  * small value -- maybe even 0.0)!
220  *
221  * NOTE: Limitations (b) and (c) arise from the fact that round edges
222  * are made by moulding sharp edges to round ones rather than adding
223  * to a core filled polygon.  For details of these two different
224  * approaches, see the thread upon the ledger lines patch that started
225  * on March 25, 2002 on the devel mailing list.  The below version of
226  * round_filled_polygon() sticks to the moulding model, which the
227  * majority of the list participants finally voted for.  This,
228  * however, results in the above limitations and a much increased
229  * complexity of the algorithm, since it has to compute a shrinked
230  * polygon -- which is not trivial define precisely and unambigously.
231  * With the other approach, one simply could move a circle of size
232  * blotdiameter along all edges of the polygon (which is what the
233  * postscript routine in the backend effectively does, but on the
234  * shrinked polygon). --jr
235  */
236 Molecule
237 Lookup::round_filled_polygon (Array<Offset> points, Real blotdiameter)
238 {
239   /* TODO: Maybe print a warning if one of the above limitations
240      applies to the given polygon.  However, this is quite complicated
241      to check. */
242
243   /* remove consecutive duplicate points */
244   const Real epsilon = 0.01;
245   for (int i = 0; i < points.size ();)
246     {
247       int next_i = (i + 1) % points.size ();
248       Real d = (points[i] - points[next_i]).length ();
249       if (d < epsilon)
250         points.del (next_i);
251       else
252         i++;
253     }
254
255   /* special cases: degenerated polygons */
256   if (points.size () == 0)
257     return Molecule ();
258   if (points.size () == 1)
259     return dot (points[0], 0.5 * blotdiameter);
260   if (points.size () == 2)
261     return line (blotdiameter, points[0], points[1]);
262
263   /* shrink polygon in size by 0.5 * blotdiameter */
264   Array<Offset> shrinked_points;
265   shrinked_points.set_size (points.size ());
266   bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
267   for (int i = 0; i < points.size (); i++)
268     {
269       int i0 = i;
270       int i1 = (i + 1) % points.size ();
271       int i2 = (i + 2) % points.size ();
272       Offset p0 = points[i0];
273       Offset p1 = points[i1];
274       Offset p2 = points[i2];
275       Offset p10 = p0 - p1;
276       Offset p12 = p2 - p1;
277       if (p10.length () != 0.0)
278         { // recompute ccw
279           Real phi = p10.arg ();
280           // rotate (p2 - p0) by (-phi)
281           Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
282
283           if (q[Y_AXIS] > 0)
284             ccw = 1;
285           else if (q[Y_AXIS] < 0)
286             ccw = 0;
287           else {} // keep ccw unchanged
288         }
289       else {} // keep ccw unchanged
290       Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
291       Offset p12n = (1.0 / p12.length ()) * p12;
292       Offset p13n = 0.5 * (p10n + p12n);
293       Offset p14n = 0.5 * (p10n - p12n);
294       Offset p13;
295       Real d = p13n.length () * p14n.length (); // distance p3n to line(p1..p0)
296       if (d < epsilon)
297         // special case: p0, p1, p2 are on a single line => build
298         // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
299         {
300           p13[X_AXIS] = p10[Y_AXIS];
301           p13[Y_AXIS] = -p10[X_AXIS];
302           p13 = (0.5 * blotdiameter / p13.length ()) * p13;
303         }
304       else
305         p13 = (0.5 * blotdiameter / d) * p13n;
306       shrinked_points[i1] = p1 + ((ccw) ? p13 : -p13);
307     }
308
309   /* build scm expression and bounding box */
310   SCM shrinked_points_scm = SCM_EOL;
311   Box box;
312   for (int i = 0; i < shrinked_points.size (); i++)
313     {
314       SCM x = gh_double2scm (shrinked_points[i][X_AXIS]);
315       SCM y = gh_double2scm (shrinked_points[i][Y_AXIS]);
316       shrinked_points_scm = gh_cons (x, gh_cons (y, shrinked_points_scm));
317       box.add_point (points[i]);
318     }
319   SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
320                                 ly_quote_scm (shrinked_points_scm),
321                                 gh_double2scm (blotdiameter),
322                                 SCM_UNDEFINED);
323
324   Molecule polygon = Molecule (box, polygon_scm);
325   shrinked_points.clear ();
326   return polygon;
327 }
328
329 Molecule
330 Lookup::frame (Box b, Real thick)
331 {
332   Molecule m;
333   Direction d = LEFT;
334   for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
335     {
336       Axis o = Axis ((a+1)%NO_AXES);
337       do
338         {
339           Box edges;
340           edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
341           edges[o][DOWN] = b[o][DOWN] - thick/2;
342           edges[o][UP] = b[o][UP] + thick/2;      
343           
344           m.add_molecule (filledbox (edges));
345         }
346       while (flip (&d) != LEFT);
347     }
348   return m;
349   
350 }
351
352 /*
353   Make a smooth curve along the points 
354  */
355 Molecule
356 Lookup::slur (Bezier curve, Real curvethick, Real linethick) 
357 {
358   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
359   Bezier back = curve;
360   Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
361   back.reverse ();
362   back.control_[1] += perp;
363   back.control_[2] += perp;
364
365   curve.control_[1] -= perp;
366   curve.control_[2] -= perp;
367   
368   SCM scontrols[8];
369
370   for (int i=4; i--;)
371     scontrols[ i ] = ly_offset2scm (back.control_[i]);
372   for (int i=4 ; i--;)
373     scontrols[i+4] = ly_offset2scm (curve.control_[i]);
374
375   /*
376     Need the weird order b.o. the way PS want its arguments  
377    */
378   int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
379   SCM list = SCM_EOL;
380   for (int i= 8; i--;)
381     {
382       list = gh_cons (scontrols[indices[i]], list);
383     }
384   
385   
386   SCM at = (scm_list_n (ly_symbol2scm ("bezier-bow"),
387                      ly_quote_scm (list),
388                      gh_double2scm (linethick),
389                      SCM_UNDEFINED));
390   Box b(curve.extent (X_AXIS),
391         curve.extent (Y_AXIS));
392
393   b[X_AXIS].unite (back.extent (X_AXIS));
394   b[Y_AXIS].unite (back.extent (Y_AXIS));
395
396   return Molecule (b, at);
397 }
398
399 /*
400  * Bezier Sandwich:
401  *
402  *                               .|
403  *                        .       |
404  *              top .             |
405  *              . curve           |
406  *          .                     |
407  *       .                        |
408  *     .                          |
409  *    |                           |
410  *    |                          .|
411  *    |                     .
412  *    |         bottom .
413  *    |            . curve
414  *    |         .
415  *    |      .
416  *    |   .
417  *    | .
418  *    |.
419  *    |
420  *
421  */
422 Molecule
423 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
424 {
425   /*
426     Need the weird order b.o. the way PS want its arguments  
427    */
428   SCM list = SCM_EOL;
429   list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
430   list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
431   list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
432   list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
433   list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
434   list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
435   list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
436   list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
437
438   SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
439                                     ly_quote_scm (list),
440                                     gh_double2scm (0.0),
441                                     SCM_UNDEFINED);
442
443   Interval x_extent = top_curve.extent (X_AXIS);
444   x_extent.unite (bottom_curve.extent (X_AXIS));
445   Interval y_extent = top_curve.extent (Y_AXIS);
446   y_extent.unite (bottom_curve.extent (Y_AXIS));
447   Box b (x_extent, y_extent);
448
449   return Molecule (b, horizontal_bend);
450 }
451
452 /*
453  * Horizontal Slope:
454  *
455  *            /|   ^
456  *           / |   |
457  *          /  |   | height
458  *         /   |   |
459  *        /    |   v
460  *       |    /
461  *       |   /
462  * (0,0) x  /slope=dy/dx
463  *       | /
464  *       |/
465  *
466  *       <----->
467  *        width
468  */
469 Molecule
470 Lookup::horizontal_slope (Real width, Real slope, Real height)
471 {
472   SCM width_scm = gh_double2scm (width);
473   SCM slope_scm = gh_double2scm (slope);
474   SCM height_scm = gh_double2scm (height);
475   SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
476                                      width_scm, slope_scm,
477                                      height_scm, SCM_UNDEFINED);
478   Box b (Interval (0, width),
479          Interval (-height/2, height/2 + width*slope));
480   return Molecule (b, horizontal_slope);
481 }
482
483 /*
484   TODO: junk me.
485  */
486 Molecule
487 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm) 
488 {
489   Molecule m;
490   String sym = ly_scm2string (ly_car (s));
491   String reg = ly_scm2string (ly_car (ly_cdr (s)));
492
493   if (sym == "Discant")
494     {
495       Molecule r = fm->find_by_name ("accordion-accDiscant");
496       m.add_molecule (r);
497       if (reg.left_string (1) == "F")
498         {
499           Molecule d = fm->find_by_name ("accordion-accDot");
500           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
501           m.add_molecule (d);
502           reg = reg.right_string (reg.length ()-1);
503         }
504       int eflag = 0x00;
505       if (reg.left_string (3) == "EEE")
506         {
507           eflag = 0x07;
508           reg = reg.right_string (reg.length ()-3);
509         }
510       else if (reg.left_string (2) == "EE")
511         {
512           eflag = 0x05;
513           reg = reg.right_string (reg.length ()-2);
514         }
515       else if (reg.left_string (2) == "Eh")
516         {
517           eflag = 0x04;
518           reg = reg.right_string (reg.length ()-2);
519         }
520       else if (reg.left_string (1) == "E")
521         {
522           eflag = 0x02;
523           reg = reg.right_string (reg.length ()-1);
524         }
525       if (eflag & 0x02)
526         {
527           Molecule d = fm->find_by_name ("accordion-accDot");
528           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
529           m.add_molecule (d);
530         }
531       if (eflag & 0x04)
532         {
533           Molecule d = fm->find_by_name ("accordion-accDot");
534           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
535           d.translate_axis (0.8 * staff_space PT, X_AXIS);
536           m.add_molecule (d);
537         }
538       if (eflag & 0x01)
539         {
540           Molecule d = fm->find_by_name ("accordion-accDot");
541           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
542           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
543           m.add_molecule (d);
544         }
545       if (reg.left_string (2) == "SS")
546         {
547           Molecule d = fm->find_by_name ("accordion-accDot");
548           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
549           d.translate_axis (0.4 * staff_space PT, X_AXIS);
550           m.add_molecule (d);
551           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
552           m.add_molecule (d);
553           reg = reg.right_string (reg.length ()-2);
554         }
555       if (reg.left_string (1) == "S")
556         {
557           Molecule d = fm->find_by_name ("accordion-accDot");
558           d.translate_axis (0.5 * staff_space PT, Y_AXIS);
559           m.add_molecule (d);
560           reg = reg.right_string (reg.length ()-1);
561         }
562     }
563   else if (sym == "Freebase")
564     {
565       Molecule r = fm->find_by_name ("accordion-accFreebase");
566       m.add_molecule (r);
567       if (reg.left_string (1) == "F")
568         {
569           Molecule d = fm->find_by_name ("accordion-accDot");
570           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
571           m.add_molecule (d);
572           reg = reg.right_string (reg.length ()-1);
573         }
574       if (reg == "E")
575         {
576           Molecule d = fm->find_by_name ("accordion-accDot");
577           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
578           m.add_molecule (d);
579         }
580     }
581   else if (sym == "Bayanbase")
582     {
583       Molecule r = fm->find_by_name ("accordion-accBayanbase");
584       m.add_molecule (r);
585       if (reg.left_string (1) == "T")
586         {
587           Molecule d = fm->find_by_name ("accordion-accDot");
588           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
589           m.add_molecule (d);
590           reg = reg.right_string (reg.length ()-1);
591         }
592       /* include 4' reed just for completeness. You don't want to use this. */
593       if (reg.left_string (1) == "F")
594         {
595           Molecule d = fm->find_by_name ("accordion-accDot");
596           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
597           m.add_molecule (d);
598           reg = reg.right_string (reg.length ()-1);
599         }
600       if (reg.left_string (2) == "EE")
601         {
602           Molecule d = fm->find_by_name ("accordion-accDot");
603           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
604           d.translate_axis (0.4 * staff_space PT, X_AXIS);
605           m.add_molecule (d);
606           d.translate_axis (-0.8 * staff_space PT, X_AXIS);
607           m.add_molecule (d);
608           reg = reg.right_string (reg.length ()-2);
609         }
610       if (reg.left_string (1) == "E")
611         {
612           Molecule d = fm->find_by_name ("accordion-accDot");
613           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
614           m.add_molecule (d);
615           reg = reg.right_string (reg.length ()-1);
616         }
617     }
618   else if (sym == "Stdbase")
619     {
620       Molecule r = fm->find_by_name ("accordion-accStdbase");
621       m.add_molecule (r);
622       if (reg.left_string (1) == "T")
623         {
624           Molecule d = fm->find_by_name ("accordion-accDot");
625           d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
626           m.add_molecule (d);
627           reg = reg.right_string (reg.length ()-1);
628         }
629       if (reg.left_string (1) == "F")
630         {
631           Molecule d = fm->find_by_name ("accordion-accDot");
632           d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
633           m.add_molecule (d);
634           reg = reg.right_string (reg.length ()-1);
635         }
636       if (reg.left_string (1) == "M")
637         {
638           Molecule d = fm->find_by_name ("accordion-accDot");
639           d.translate_axis (staff_space * 2 PT, Y_AXIS);
640           d.translate_axis (staff_space PT, X_AXIS);
641           m.add_molecule (d);
642           reg = reg.right_string (reg.length ()-1);
643         }
644       if (reg.left_string (1) == "E")
645         {
646           Molecule d = fm->find_by_name ("accordion-accDot");
647           d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
648           m.add_molecule (d);
649           reg = reg.right_string (reg.length ()-1);
650         }
651       if (reg.left_string (1) == "S")
652         {
653           Molecule d = fm->find_by_name ("accordion-accDot");
654           d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
655           m.add_molecule (d);
656           reg = reg.right_string (reg.length ()-1);
657         }
658     }
659   /* ugh maybe try to use regular font for S.B. and B.B and only use one font
660      for the rectangle */
661   else if (sym == "SB")
662     {
663       Molecule r = fm->find_by_name ("accordion-accSB");
664       m.add_molecule (r);
665     }
666   else if (sym == "BB")
667     {
668       Molecule r = fm->find_by_name ("accordion-accBB");
669       m.add_molecule (r);
670     }
671   else if (sym == "OldEE")
672     {
673       Molecule r = fm->find_by_name ("accordion-accOldEE");
674       m.add_molecule (r);
675     }
676   else if (sym == "OldEES")
677     {
678       Molecule r = fm->find_by_name ("accordion-accOldEES");
679       m.add_molecule (r);
680     }
681   return m;  
682 }
683
684 Molecule
685 Lookup::repeat_slash (Real w, Real s, Real t)
686 {
687   SCM wid = gh_double2scm (w);
688   SCM sl = gh_double2scm (s);
689   SCM thick = gh_double2scm (t);
690   SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
691                             wid, sl, thick, SCM_UNDEFINED);
692
693   Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
694          Interval (0, w * s));
695
696   return Molecule (b, slashnodot); //  http://slashnodot.org
697 }
698
699 Molecule
700 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude)
701 {
702   Box b;
703   Axis other = Axis((a+1)%2);
704   b[a] = iv;
705   b[other] = Interval(-1, 1) * thick * 0.5;
706   
707   Molecule m =  filledbox (b);
708
709   b[a] = Interval (iv[UP] - thick, iv[UP]);
710   Interval oi = Interval (-thick/2, thick/2 + fabs (protude)) ;
711   oi *=  sign (protude);
712   b[other] = oi;
713   m.add_molecule (filledbox (b));
714   b[a] = Interval (iv[DOWN], iv[DOWN]  +thick);
715   m.add_molecule (filledbox(b));
716
717   return m;
718 }
719
720 Molecule
721 Lookup::triangle (Interval iv, Real thick, Real protude)
722 {
723   Box b ;
724   b[X_AXIS] = iv;
725   b[Y_AXIS] = Interval (0 <? protude , 0 >? protude);
726
727   SCM s = scm_list_n (ly_symbol2scm ("symmetric-x-triangle"),
728                       gh_double2scm (thick),
729                       gh_double2scm (iv.length()), 
730                       gh_double2scm (protude), SCM_UNDEFINED);
731
732   return Molecule (b, s);
733 }
734
735
736 /*
737   TODO: use rounded boxes.
738  */
739 LY_DEFINE(ly_bracket ,"ly:bracket",
740           4, 0, 0,
741           (SCM a, SCM iv, SCM t, SCM p),
742           "Make a bracket in direction @var{a}. The extent of the bracket is " 
743           "given by @var{iv}. The wings protude by an amount of @var{p}, which "
744           "may be negative. The thickness is given by @var{t}.")
745 {
746   SCM_ASSERT_TYPE(ly_axis_p (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
747   SCM_ASSERT_TYPE(ly_number_pair_p (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
748   SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG3, __FUNCTION__, "number") ;
749   SCM_ASSERT_TYPE(gh_number_p (p), a, SCM_ARG4, __FUNCTION__, "number") ;
750
751
752   return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
753                           gh_scm2double (t),
754                           gh_scm2double (p)).smobbed_copy ();
755 }
756