]> git.donarmstrong.com Git - lilypond.git/blob - src/texbeam.cc
release: 0.0.9
[lilypond.git] / src / texbeam.cc
1 /*
2
3   Code to generate beams for TeX
4   
5   */
6
7   #include <math.h>
8 #include "symbol.hh"
9 #include "molecule.hh"
10 #include "tex.hh"
11 #include "symtable.hh"
12 #include "dimen.hh"
13 #include "lookup.hh"
14
15 Symbol
16 Lookup::beam_element(int sidx, int widx, Real slope)
17 {
18     Symbol bs=(*symtables_)("beamslopes")->lookup("slope");
19     
20     svec<String> args;
21     args.add(sidx);
22     args.add(widx);
23     bs.tex = substitute_args(bs.tex,args);
24     int w = 2 << widx;
25     Real width = convert_dimen(w,"pt");
26     bs.dim.x = Interval(0,width);
27     bs.dim.y = Interval(0,width*slope);
28     return bs;
29 }
30
31 // ugh.. hard wired tex-code.
32 static int
33 slope_index(Real &s)
34 {
35     assert(ABS(s) < 0.45);
36     int i = int(rint(s *  20.0));
37
38     s = i/20.0;
39     if (s>0)
40         return 6*i +122;
41     else
42         return -6 * i+ 188;
43 }
44
45 Symbol
46 Lookup::rule_symbol(Real height, Real width)
47 {
48     Symbol bs=(*symtables_)("beamslopes")->lookup("horizontal");    
49     svec<String> args;
50     args.add(print_dimen(height));
51     args.add(print_dimen(width));
52     bs.tex = substitute_args(bs.tex,args);
53     bs.dim.x = Interval(0,width);
54     bs.dim.y = Interval(0,height);
55     return bs;
56 }
57
58 Symbol
59 Lookup::beam(Real &slope, Real width)
60 {        
61     int sidx = slope_index(slope);
62     if (!slope)
63         return rule_symbol(convert_dimen(2,"pt"), width);
64         
65     Real w = width;
66     Real elemwidth = convert_dimen(64,"pt");
67     int widx = 5;
68
69     Molecule m;
70     Real dy=0;
71     Real minwid =convert_dimen(2,"pt");
72     assert(w > minwid);
73     while (w > minwid) {
74         while (elemwidth > w) {
75             widx --;
76             elemwidth /= 2.0;
77         }
78         
79         Atom a(beam_element(sidx, widx, slope));
80         a.translate(Offset(0, dy));
81         m.add_right(a);
82         dy += elemwidth*slope;
83         w -= elemwidth;
84     }
85
86     widx = 0;
87     Atom a(beam_element(sidx, widx, slope));
88     a.translate(Offset(width -minwid, (width-minwid) * slope));
89     m.add(a);
90     
91     Symbol ret;
92     ret.tex = m.TeXstring();
93     ret.dim.y = Interval(0,width*slope);
94     ret.dim.x = Interval(0,width);
95     
96     return ret;
97 }
98
99