]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 0.1.61
[lilypond.git] / lily / lookup.cc
1 /*
2   lookup.cc -- implement simple Lookup methods.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1998 Han-Wen Nienhuys <hanwen@stack.nl>
7
8   TODO
9   This doth suck. We should have PS output, and read spacing info from TFMs
10
11   Glissando, 
12
13 */
14
15 #include "lookup.hh"
16 #include "debug.hh"
17 #include "symtable.hh"
18 #include "dimen.hh"
19 #include "tex.hh"
20 #include "scalar.hh"
21 #include "paper-def.hh"
22 #include "string-convert.hh"
23 #include "main.hh"
24
25 Lookup::Lookup()
26 {
27   paper_l_ = 0;
28   texsetting = "\\unknowntexsetting";
29   symtables_p_ = new Symtables;
30 }
31
32 Lookup::Lookup (Lookup const &s)
33 {
34   paper_l_ = s.paper_l_;
35   texsetting = s.texsetting;
36   symtables_p_ = new Symtables (*s.symtables_p_);
37 }
38 Lookup::~Lookup()
39 {
40   delete symtables_p_;
41 }
42
43 void
44 Lookup::add (String s, Symtable*p)
45 {
46   symtables_p_->add (s, p);
47 }
48
49 void
50 Lookup::print() const
51 {
52 #ifndef NPRINT
53   DOUT << "Lookup: " << texsetting << " {\n";
54   symtables_p_->print();
55   DOUT << "}\n";
56 #endif
57 }
58
59 Atom
60 Lookup::text (String style, String text, int dir) const
61 {
62   Array<String> a;
63
64   a.push (text);
65   Atom tsym =  (*symtables_p_)("style")->lookup (style);
66   a[0] = substitute_args (tsym.tex_,a);
67
68   Atom s = (*symtables_p_)("align")->lookup (dir);
69   s.tex_ = substitute_args (s.tex_,a);
70   s.dim_ = tsym.dim_;
71   return s;
72 }
73
74
75
76 Atom
77 Lookup::ball (int j) const
78 {
79   if (j > 2)
80     j = 2;
81
82   Symtable * st = (*symtables_p_)("balls");
83   return st->lookup (String (j));
84 }
85
86 Atom
87 Lookup::rest (int j, bool o) const
88 {
89   return (*symtables_p_)("rests")->lookup (String (j) + (o ? "o" : ""));
90 }
91
92 Atom
93 Lookup::fill (Box b) const
94 {
95   Atom s ((*symtables_p_)("param")->lookup ("fill"));
96   s.dim_ = b;
97   return s;
98 }
99
100 Atom
101 Lookup::accidental (int j) const
102 {
103   return (*symtables_p_)("accidentals")->lookup (String (j));
104 }
105
106
107 Atom
108 Lookup::bar (String s, Real h) const
109 {
110   Array<String> a;
111   a.push (print_dimen (h));
112   Atom ret=(*symtables_p_)("bars")->lookup (s);
113   ret.tex_ = substitute_args (ret.tex_, a);
114   ret.dim_.y() = Interval (-h/2, h/2);
115   return ret;
116 }
117
118 Atom
119 Lookup::script (String s) const
120 {
121   return (*symtables_p_)("scripts")->lookup (s);
122 }
123
124 Atom
125 Lookup::dynamic (String s) const
126 {
127   return (*symtables_p_)("dynamics")->lookup (s);
128 }
129
130 Atom
131 Lookup::clef (String s) const
132 {
133   return (*symtables_p_)("clefs")->lookup (s);
134 }
135
136 Atom
137 Lookup::dots () const
138 {
139   return (*symtables_p_)("dots")->lookup ("dot");
140 }
141
142 Atom
143 Lookup::flag (int j, Direction d) const
144 {
145   char c = (d == UP) ? 'u' : 'd';
146   return (*symtables_p_)("flags")->lookup (c + String (j));
147 }
148
149 Atom
150 Lookup::slur (Array<Offset> controls) const
151 {
152   assert (controls.size () == 8);
153
154   String ps = "\\embeddedps{\n";
155   
156   Real dx = controls[3].x () - controls[0].x ();
157   Real dy = controls[3].y () - controls[0].y ();
158
159   for (int i = 5; i < 8; i++)
160     ps += String_convert::double_str (controls[i].x ()) + " "
161       + String_convert::double_str (controls[i].y ()) + " ";
162
163   ps += String_convert::double_str (controls[4].x ()) + " "
164     + String_convert::double_str (controls[4].y ()) + " ";
165
166   for (int i = 1; i < 4; i++)
167     ps += String_convert::double_str (controls[i].x ()) + " "
168       + String_convert::double_str (controls[i].y ()) + " ";
169
170   ps += String_convert::double_str (controls[0].x ()) + " "
171     + String_convert::double_str (controls[0].y ()) + " ";
172
173   ps += " draw_slur}";
174
175   Atom s;
176   s.tex_ = ps;
177   
178   s.dim_[X_AXIS] = Interval (0, dx);
179   s.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
180   return s;
181 }
182
183 Atom
184 Lookup::streepje (int type) const
185 {
186   if (type > 2)
187     type = 2;
188
189   Symtable * st = (*symtables_p_)("balls");
190   
191   return st->lookup (String (type) + 'l');
192 }
193
194 Atom
195 Lookup::hairpin (Real width, bool decresc, bool continued) const
196 {
197   String embed;
198   Atom ret;  
199   Real height = paper_l_->get_var ("barsize") / 6;
200   embed = "\\embeddedps{\n" ;
201   embed += String (width) + " " 
202         + String (height) + " " 
203     + String (continued ? height/2 : 0) + 
204     + " draw_"  + String(decresc ? "de" : "") + "cresc}\n";
205   ret.tex_ = embed;
206
207
208   ret.dim_.x () = Interval (0, width);
209   ret.dim_.y () = Interval (-2*height, 2*height);
210
211   return ret;
212 }
213
214 Atom
215 Lookup::meter (Array<Scalar> a) const
216 {
217   Atom s((*symtables_p_)("param")->lookup ("meter"));
218   s.tex_ = substitute_args (s.tex_,a);
219   return s;
220 }
221
222
223 Atom
224 Lookup::stem (Real y1,Real y2) const
225 {
226   if (y1 > y2)
227     {
228       Real t = y1;
229       y1 = y2;
230       y2 = t;
231     }
232   Atom s;
233
234   s.dim_.x() = Interval (0,0);
235   s.dim_.y() = Interval (y1,y2);
236
237   Array<String> a;
238   a.push (print_dimen (y1));
239   a.push (print_dimen (y2));
240
241   String src = (*symtables_p_)("param")->lookup ("stem").tex_;
242   s.tex_ = substitute_args (src,a);
243   return s;
244 }
245
246 /*
247   should be handled via Tex_ code and Lookup::bar()
248  */
249 Atom
250 Lookup::vbrace (Real &y) const
251 {
252   Atom brace = (*symtables_p_)("param")->lookup ("brace");
253   Interval ydims = brace.dim_[Y_AXIS];
254   Real min_y = ydims[LEFT];
255   Real max_y = ydims[RIGHT];
256   Real step = 1.0 PT;
257  
258   if (y < min_y)
259     {
260       warning (_("piano brace too small (") + print_dimen (y)+ ")");
261       y = min_y;
262     }
263   if (y > max_y)
264     {
265       warning (_("piano brace too big (") + print_dimen (y)+ ")");
266       y = max_y;
267     }
268
269   
270   int idx = int (rint ((y- min_y)/step)) + 1;
271   
272   {
273     Array<String> a;
274     a.push (idx);
275     brace.tex_ = substitute_args (brace.tex_,a);
276     brace.dim_[Y_AXIS] = Interval (-y/2,y/2);
277   }
278
279   return brace;
280 }
281
282 Atom
283 Lookup::vbracket (Real &y) const
284 {
285   Atom psbracket;
286   psbracket.tex_ = String ("\\embeddedps{ ") + y + " draw_bracket}";
287   psbracket.dim_[Y_AXIS] = Interval (-y/2,y/2);
288   psbracket.dim_[X_AXIS] = Interval (0,4 PT);
289   return psbracket;
290   Atom bracket = (*symtables_p_)("param")->lookup ("bracket");
291   Interval ydims = bracket.dim_[Y_AXIS];
292
293   Real min_y = ydims[LEFT];
294   Real max_y = ydims[RIGHT];
295   Real step = 1.0 PT;
296  
297   if (y < min_y)
298     {
299       warning (_("bracket too small (") + print_dimen (y)+ ")");
300       y = min_y;
301     }
302   if (y > max_y)
303     {
304       warning (_("bracket too big (") + print_dimen (y)+ ")");
305       y = max_y;
306     }
307
308   
309   int idx = int (rint ((y- min_y)/step)) + 1;
310   
311   {
312     Array<String> a;
313     a.push (idx);
314     bracket.tex_ = substitute_args (bracket.tex_,a);
315     bracket.dim_[Y_AXIS] = Interval (-y/2,y/2);
316   }
317
318   return bracket;
319 }
320