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