]> git.donarmstrong.com Git - lilypond.git/blob - lily/tex-beam.cc
release: 0.1.11
[lilypond.git] / lily / tex-beam.cc
1 /*
2   tex-beam.cc -- implement Lookup::{beam_element, beam, rule_symbol}
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 /*
10   Code to generate beams for TeX
11   */
12
13 #include <math.h>
14 #include "symbol.hh"
15 #include "molecule.hh"
16 #include "tex.hh"
17 #include "symtable.hh"
18 #include "dimen.hh"
19 #include "debug.hh"
20 #include "lookup.hh"
21
22 Symbol
23 Lookup::beam_element (int sidx, int widx, Real slope) const
24 {
25   Symbol bs=(*symtables_)("beamslopes")->lookup ("slope");
26   
27   Array<String> args;
28   args.push (sidx);
29   args.push (widx);
30   bs.tex = substitute_args (bs.tex,args);
31   int w = 2 << widx;
32   Real width = w PT;
33   bs.dim.x() = Interval (0,width);
34   bs.dim.y() = Interval (0,width*slope);
35   return bs;
36 }
37
38 // ugh.. hard wired tex-code.
39 static int
40 slope_index (Real &s)
41 {
42   if (abs (s) > 0.5) 
43     {
44       WARN << "beam steeper than 0.5 (" << s << ")\n";
45       s = sign (s) * 0.5;
46     }
47
48   int i = int (rint (s *  20.0));
49
50   s = i/20.0;
51   if (s>0)
52     return 6*i +122;
53   else
54     return -6 * i+ 186;
55 }
56
57 Symbol
58 Lookup::rule_symbol (Real height, Real width) const
59 {
60   Symbol bs=(*symtables_)("beamslopes")->lookup ("horizontal");    
61   Array<String> args;
62   args.push (print_dimen (height));
63   args.push (print_dimen (width));
64   bs.tex = substitute_args (bs.tex,args);
65   bs.dim.x() = Interval (0,width);
66   bs.dim.y() = Interval (0,height);
67   return bs;
68 }
69
70 Symbol
71 Lookup::beam (Real &slope, Real width) const
72 {        
73   int sidx = slope_index (slope);
74   if (!slope)
75     return rule_symbol (2 PT, width);
76   if (width < 2 PT) 
77     {
78       WARN<<"Beam too narrow. (" << print_dimen (width) <<")\n";
79       width = 2 PT;
80     }
81   Real elemwidth = 64 PT;
82   int widx = 5;
83
84   Molecule m;
85   
86   while (elemwidth > width) 
87     {
88       widx --;
89       elemwidth /= 2.0;
90     }
91   Real overlap = elemwidth/4;
92   Real last_x = width - elemwidth;
93   Real x = overlap;
94   Atom elem (beam_element (sidx, widx, slope));
95   Atom a (elem);
96   m.add (a);
97   while (x < last_x) 
98     {
99       a=elem;
100       a.translate (Offset (x-overlap, (x-overlap)*slope));
101       m.add (a);
102       x += elemwidth - overlap;
103     }
104   a=elem;
105   a.translate (Offset (last_x, (last_x) * slope));
106   m.add (a);
107   
108   Symbol ret;
109   ret.tex = m.TeX_string();
110   ret.dim.y() = Interval (0,width*slope);
111   ret.dim.x() = Interval (0,width);
112   
113   return ret;
114 }
115
116