]> git.donarmstrong.com Git - lilypond.git/blob - src/texbeam.cc
release: 0.0.23
[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 "debug.hh"
14 #include "lookup.hh"
15
16 Symbol
17 Lookup::beam_element(int sidx, int widx, Real slope)
18 {
19     Symbol bs=(*symtables_)("beamslopes")->lookup("slope");
20     
21     Array<String> args;
22     args.add(sidx);
23     args.add(widx);
24     bs.tex = substitute_args(bs.tex,args);
25     int w = 2 << widx;
26     Real width = convert_dimen(w,"pt");
27     bs.dim.x = Interval(0,width);
28     bs.dim.y = Interval(0,width*slope);
29     return bs;
30 }
31
32 // ugh.. hard wired tex-code.
33 static int
34 slope_index(Real &s)
35 {
36     if (abs(s) > 0.5) {
37         WARN << "beam steeper than 0.5";
38         s = sign(s) * 0.5;
39     }
40
41     int i = int(rint(s *  20.0));
42
43     s = i/20.0;
44     if (s>0)
45         return 6*i +122;
46     else
47         return -6 * i+ 186;
48 }
49
50 Symbol
51 Lookup::rule_symbol(Real height, Real width)
52 {
53     Symbol bs=(*symtables_)("beamslopes")->lookup("horizontal");    
54     Array<String> args;
55     args.add(print_dimen(height));
56     args.add(print_dimen(width));
57     bs.tex = substitute_args(bs.tex,args);
58     bs.dim.x = Interval(0,width);
59     bs.dim.y = Interval(0,height);
60     return bs;
61 }
62
63 Symbol
64 Lookup::beam(Real &slope, Real width)
65 {        
66     int sidx = slope_index(slope);
67     if (!slope)
68         return rule_symbol(convert_dimen(2,"pt"), width);
69     if (width < convert_dimen(2,"pt")) {
70         WARN<<"Beam too narrow.\n";
71         width = convert_dimen(2,"pt");
72     }
73     Real elemwidth = convert_dimen(64,"pt");
74     int widx = 5;
75
76     Molecule m;
77     
78     while (elemwidth > width) {
79         widx --;
80         elemwidth /= 2.0;
81     }
82     Real overlap = elemwidth/4;
83     Real last_x = width - elemwidth;
84     Real x = overlap;
85     Atom elem(beam_element(sidx, widx, slope));
86     Atom a(elem);
87     m.add(a);
88     while (x < last_x) {
89         a=elem;
90         a.translate(Offset(x-overlap, (x-overlap)*slope));
91         m.add(a);
92         x += elemwidth - overlap;
93     }
94     a=elem;
95     a.translate(Offset(last_x, (last_x) * slope));
96     m.add(a);
97     
98     Symbol ret;
99     ret.tex = m.TeXstring();
100     ret.dim.y = Interval(0,width*slope);
101     ret.dim.x = Interval(0,width);
102     
103     return ret;
104 }
105
106