lilypond-0.1.27
[lilypond.git] / lily / tex-beam.cc
blob6553e17868fa321ca5e9f49c02b88688e8aec9ae
1 /*
2 tex-beam.cc -- implement Lookup::{beam_element, beam, rule_symbol}
4 source file of the GNU LilyPond music typesetter
6 (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 /*
10 Code to generate beams for TeX
13 #include <math.h>
14 #include "atom.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 #include "misc.hh"
23 Atom
24 Lookup::beam_element (int sidx, int widx, Real slope) const
26 String name = String("slope");
27 Atom bs=(*symtables_)("beamslopes")->lookup (name);
29 Array<String> args;
30 args.push (sidx);
31 args.push (widx);
32 bs.tex_ = substitute_args (bs.tex_,args);
33 int w = 2 << widx;
34 Real width = w PT;
35 bs.dim_.x() = Interval (0,width);
36 bs.dim_.y() = Interval (0,width*slope);
37 return bs;
41 Atom
42 Lookup::rule_symbol (Real height, Real width) const
44 Atom bs=(*symtables_)("beamslopes")->lookup ("horizontal");
45 Array<String> args;
46 args.push (print_dimen (height));
47 args.push (print_dimen (width));
48 bs.tex_ = substitute_args (bs.tex_,args);
49 bs.dim_.x() = Interval (0,width);
50 bs.dim_.y() = Interval (0,height);
51 return bs;
54 Atom
55 Lookup::beam (Real &slope, Real width) const
57 int sidx = 0;
58 if (abs (slope) > 1.0)
60 WARN << _("beam steeper than 1.0 (") << slope << ")\n";
61 slope = sign (slope);
64 sidx = int (rint (slope * 20.0));
65 slope = sidx / 20.0;
67 Interval xdims = (*symtables_)("beamslopes")->lookup ("slope").dim_[X_AXIS];
68 Real min_wid = xdims[LEFT];
69 Real max_wid = xdims[RIGHT];
70 assert(max_wid > 0);
71 int widths = intlog2 (int (max_wid/min_wid)) + 1;
73 if (width < min_wid)
75 WARN<<_("Beam too narrow. (") << print_dimen (width) <<")\n";
76 width = min_wid;
79 Real elemwidth = max_wid;
81 int widx =widths - 1;
82 Molecule m;
83 while (elemwidth > width)
85 widx --;
86 elemwidth /= 2.0;
88 Real overlap = elemwidth/4;
89 Real last_x = width - elemwidth;
90 Real x = overlap;
91 Atom elem (beam_element (sidx * widths, widx, slope));
92 m.add (elem);
93 while (x < last_x)
95 Atom a(elem);
96 a.translate (Offset (x-overlap, (x-overlap)*slope));
97 m.add (a);
98 x += elemwidth - overlap;
100 Atom a(elem);
101 a.translate (Offset (last_x, (last_x) * slope));
102 m.add (a);
104 Atom ret;
105 ret.tex_ = m.TeX_string();
106 ret.dim_.y() = Interval (0,width*slope);
107 ret.dim_.x() = Interval (0,width);
109 return ret;