]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 0.1.62
[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::dashed_slur (Array<Offset> controls, Real thick, Real dash) 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 = 1; i < 4; 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[0].x ()) + " "
164     + String_convert::double_str (controls[0].y ()) + " ";
165
166   ps += String_convert::double_str (thick) + " ";
167   Real on = dash > 1? thick * dash - thick : 0;
168   Real off = 2 * thick;
169   ps += "[" + String_convert::double_str (on) + " ";
170   ps += String_convert::double_str (off) + "] ";
171   ps += String_convert::int_str (0) + " ";
172   ps += " draw_dashed_slur}";
173
174   Atom s;
175   s.tex_ = ps;
176   
177   s.dim_[X_AXIS] = Interval (0, dx);
178   s.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
179   return s;
180 }
181
182 Atom
183 Lookup::slur (Array<Offset> controls) const
184 {
185   assert (controls.size () == 8);
186
187   String ps = "\\embeddedps{\n";
188   
189   Real dx = controls[3].x () - controls[0].x ();
190   Real dy = controls[3].y () - controls[0].y ();
191
192   for (int i = 5; i < 8; i++)
193     ps += String_convert::double_str (controls[i].x ()) + " "
194       + String_convert::double_str (controls[i].y ()) + " ";
195
196   ps += String_convert::double_str (controls[4].x ()) + " "
197     + String_convert::double_str (controls[4].y ()) + " ";
198
199   for (int i = 1; i < 4; i++)
200     ps += String_convert::double_str (controls[i].x ()) + " "
201       + String_convert::double_str (controls[i].y ()) + " ";
202
203   ps += String_convert::double_str (controls[0].x ()) + " "
204     + String_convert::double_str (controls[0].y ()) + " ";
205
206   ps += " draw_slur}";
207
208   Atom s;
209   s.tex_ = ps;
210   
211   s.dim_[X_AXIS] = Interval (0, dx);
212   s.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
213   return s;
214 }
215
216 Atom
217 Lookup::streepje (int type) const
218 {
219   if (type > 2)
220     type = 2;
221
222   Symtable * st = (*symtables_p_)("balls");
223   
224   return st->lookup (String (type) + 'l');
225 }
226
227 Atom
228 Lookup::hairpin (Real width, bool decresc, bool continued) const
229 {
230   String embed;
231   Atom ret;  
232   Real height = paper_l_->staffheight_f () / 6;
233   embed = "\\embeddedps{\n" ;
234   embed += String (width) + " " 
235         + String (height) + " " 
236     + String (continued ? height/2 : 0) + 
237     + " draw_"  + String(decresc ? "de" : "") + "cresc}\n";
238   ret.tex_ = embed;
239
240
241   ret.dim_.x () = Interval (0, width);
242   ret.dim_.y () = Interval (-2*height, 2*height);
243
244   return ret;
245 }
246
247 Atom
248 Lookup::meter (Array<Scalar> a) const
249 {
250   Atom s((*symtables_p_)("param")->lookup ("meter"));
251   s.tex_ = substitute_args (s.tex_,a);
252   return s;
253 }
254
255
256 Atom
257 Lookup::stem (Real y1,Real y2) const
258 {
259   if (y1 > y2)
260     {
261       Real t = y1;
262       y1 = y2;
263       y2 = t;
264     }
265   Atom s;
266
267   s.dim_.x() = Interval (0,0);
268   s.dim_.y() = Interval (y1,y2);
269
270   Array<String> a;
271   a.push (print_dimen (y1));
272   a.push (print_dimen (y2));
273
274   String src = (*symtables_p_)("param")->lookup ("stem").tex_;
275   s.tex_ = substitute_args (src,a);
276   return s;
277 }
278
279 /*
280   should be handled via Tex_ code and Lookup::bar()
281  */
282 Atom
283 Lookup::vbrace (Real &y) const
284 {
285   Atom brace = (*symtables_p_)("param")->lookup ("brace");
286   Interval ydims = brace.dim_[Y_AXIS];
287   Real min_y = ydims[LEFT];
288   Real max_y = ydims[RIGHT];
289   Real step = 1.0 PT;
290  
291   if (y < min_y)
292     {
293       warning (_("piano brace too small (") + print_dimen (y)+ ")");
294       y = min_y;
295     }
296   if (y > max_y)
297     {
298       warning (_("piano brace too big (") + print_dimen (y)+ ")");
299       y = max_y;
300     }
301
302   
303   int idx = int (rint ((y- min_y)/step)) + 1;
304   
305   {
306     Array<String> a;
307     a.push (idx);
308     brace.tex_ = substitute_args (brace.tex_,a);
309     brace.dim_[Y_AXIS] = Interval (-y/2,y/2);
310   }
311
312   return brace;
313 }
314
315 Atom
316 Lookup::vbracket (Real &y) const
317 {
318   Atom psbracket;
319   psbracket.tex_ = String ("\\embeddedps{ ") + y + " draw_bracket}";
320   psbracket.dim_[Y_AXIS] = Interval (-y/2,y/2);
321   psbracket.dim_[X_AXIS] = Interval (0,4 PT);
322   return psbracket;
323   Atom bracket = (*symtables_p_)("param")->lookup ("bracket");
324   Interval ydims = bracket.dim_[Y_AXIS];
325
326   Real min_y = ydims[LEFT];
327   Real max_y = ydims[RIGHT];
328   Real step = 1.0 PT;
329  
330   if (y < min_y)
331     {
332       warning (_("bracket too small (") + print_dimen (y)+ ")");
333       y = min_y;
334     }
335   if (y > max_y)
336     {
337       warning (_("bracket too big (") + print_dimen (y)+ ")");
338       y = max_y;
339     }
340
341   
342   int idx = int (rint ((y- min_y)/step)) + 1;
343   
344   {
345     Array<String> a;
346     a.push (idx);
347     bracket.tex_ = substitute_args (bracket.tex_,a);
348     bracket.dim_[Y_AXIS] = Interval (-y/2,y/2);
349   }
350
351   return bracket;
352 }
353