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