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