]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
1624a42b467f482c1a4b5940f5a2bc166d67fb33
[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@cs.uu.nl>
7
8   TODO
9   This doth suck. We should have PS output, and read spacing info from AFMs
10
11   Glissando, 
12
13 */
14
15 #include "lookup.hh"
16 #include "debug.hh"
17 #include "symtable.hh"
18 #include "dimension.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   symtables_p_ = new Symtables;
29   afm_p_ =0;
30 }
31
32 Lookup::Lookup (Lookup const &s)
33 {
34   font_ = s.font_;
35   font_path_ = s.font_path_;
36   paper_l_ = s.paper_l_;
37   symtables_p_ = new Symtables (*s.symtables_p_);
38   afm_p_ = 0;
39 }
40
41 Lookup::~Lookup()
42 {
43   delete afm_p_;
44   delete symtables_p_;
45 }
46
47 Atom
48 Lookup::afm_find (String s) const
49 {
50   if (!afm_p_)
51     {
52       *mlog << "[" << font_path_;
53       ((Lookup*)this)->afm_p_ = new Adobe_font_metric (read_afm (font_path_));
54       *mlog << "]" << flush ;
55       DOUT << this->afm_p_->str ();
56     }
57   Adobe_font_char_metric m = afm_p_->find_char (s);
58
59   Atom a;
60   a.tex_ = String_convert::form_str ("\\char%d", m.code ());
61   a.dim_ = m.B_;
62   a.dim_[X_AXIS] *= 1 / 1000.0;
63   a.dim_[Y_AXIS] *= 1 / 1000.0;
64   a.font_ = font_;
65   return a;
66 }
67
68 void
69 Lookup::add (String s, Symtable*p)
70 {
71   symtables_p_->add (s, p);
72 }
73
74 void
75 Lookup::print() const
76 {
77 #ifndef NPRINT
78   DOUT << "Lookup {\n";
79   symtables_p_->print();
80   DOUT << "}\n";
81 #endif
82 }
83
84 Atom
85 Lookup::text (String style, String text) const
86 {
87   Array<String> a;
88
89   a.push (text);
90   Atom tsym =  (*symtables_p_)("style")->lookup (style);
91   tsym.tex_ = substitute_args (tsym.tex_,a);
92   tsym.font_ = font_;
93
94   return tsym;
95 }
96
97
98
99 Atom
100 Lookup::ball (int j) const
101 {
102   if (j > 2)
103     j = 2;
104
105   Atom s = afm_find (String ("balls") + String ("-") + to_str (j));
106   return s;
107 }
108
109 Atom
110 Lookup::rest (int j, bool o) const
111 {
112   Atom s =afm_find (String ("rests")
113                     + String ("-") + to_str (j) + (o ? "o" : ""));
114
115   return s;
116 }
117
118 Atom
119 Lookup::fill (Box b) const
120 {
121   Atom s;
122   s.tex_ = "";
123   s.dim_ = b;
124
125   return s;
126 }
127
128 Atom
129 Lookup::accidental (int j) const
130 {
131   Atom s= afm_find (String ("accidentals") + String ("-") + to_str (j));
132   return s;
133 }
134
135
136 Atom
137 Lookup::bar (String s, Real h) const
138 {
139   Array<String> a;
140   a.push (print_dimen (h));
141   Atom ret=(*symtables_p_)("bars")->lookup (s);
142   ret.tex_ = substitute_args (ret.tex_, a);
143   ret.dim_.y() = Interval (-h/2, h/2);
144   ret.font_ = font_;
145   return ret;
146 }
147
148 Atom
149 Lookup::script (String st) const
150 {
151   Atom s= afm_find (String ("scripts") + String ("-") + st);
152
153   return s;
154 }
155
156 Atom
157 Lookup::dynamic (String st) const
158 {
159   return (*symtables_p_) ("dynamics")->lookup (st);
160 }
161
162 Atom
163 Lookup::clef (String st) const
164 {
165   Atom s=afm_find (String ("clefs") + String ("-") + st);
166
167   return s;
168 }
169
170 Atom
171 Lookup::dots () const
172 {
173   Atom s=afm_find (String ("dots") + String ("-") + String("dot"));
174   
175     return s;
176 }
177
178 Atom
179 Lookup::flag (int j, Direction d) const
180 {
181   char c = (d == UP) ? 'u' : 'd';
182   Atom s=afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
183
184   return s;
185 }
186
187 Atom
188 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
189 {
190   assert (controls.size () == 8);
191
192   String ps = "\\embeddedps{\n";
193   
194   Real dx = controls[3].x () - controls[0].x ();
195   Real dy = controls[3].y () - controls[0].y ();
196
197   for (int i = 1; i < 4; i++)
198     ps += String_convert::double_str (controls[i].x ()) + " "
199       + String_convert::double_str (controls[i].y ()) + " ";
200
201   ps += String_convert::double_str (controls[0].x ()) + " "
202     + String_convert::double_str (controls[0].y ()) + " ";
203
204   ps += String_convert::double_str (thick) + " ";
205   Real on = dash > 1? thick * dash - thick : 0;
206   Real off = 2 * thick;
207   ps += "[" + String_convert::double_str (on) + " ";
208   ps += String_convert::double_str (off) + "] ";
209   ps += String_convert::int_str (0) + " ";
210   ps += " draw_dashed_slur}";
211
212   Atom s;
213   s.tex_ = ps;
214   
215   s.dim_[X_AXIS] = Interval (0, dx);
216   s.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
217   s.font_ = font_;
218   return s;
219 }
220
221 Atom
222 Lookup::slur (Array<Offset> controls) const
223 {
224   assert (controls.size () == 8);
225
226   String ps = "\\embeddedps{\n";
227   
228   Real dx = controls[3].x () - controls[0].x ();
229   Real dy = controls[3].y () - controls[0].y ();
230
231   for (int i = 5; i < 8; i++)
232     ps += String_convert::double_str (controls[i].x ()) + " "
233       + String_convert::double_str (controls[i].y ()) + " ";
234
235   ps += String_convert::double_str (controls[4].x ()) + " "
236     + String_convert::double_str (controls[4].y ()) + " ";
237   
238   for (int i = 1; i < 4; i++)
239     ps += String_convert::double_str (controls[i].x ()) + " "
240       + String_convert::double_str (controls[i].y ()) + " ";
241
242   ps += String_convert::double_str (controls[0].x ()) + " "
243     + String_convert::double_str (controls[0].y ()) + " ";
244
245   ps += " draw_slur}";
246
247   Atom s;
248   s.tex_ = ps;
249   
250   s.dim_[X_AXIS] = Interval (0, dx);
251   s.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
252   s.font_ = font_;
253   return s;
254
255 }
256
257 Atom
258 Lookup::streepje (int type) const
259 {
260   if (type > 2)
261     type = 2;
262
263   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
264 }
265
266 Atom
267 Lookup::hairpin (Real width, bool decresc, bool continued) const
268 {
269   String embed;
270   Atom ret;  
271   Real height = paper_l_->staffheight_f () / 6;
272   embed = "\\embeddedps{\n" ;
273   embed += to_str (width) + " " 
274         + to_str (height) + " " 
275     + to_str (continued ? height/2 : 0) + 
276     + " draw_"  + String(decresc ? "de" : "") + "cresc}\n";
277   ret.tex_ = embed;
278
279
280   ret.dim_.x () = Interval (0, width);
281   ret.dim_.y () = Interval (-2*height, 2*height);
282   ret.font_ = font_;
283   return ret;
284 }
285
286 Atom
287 Lookup::time_signature (Array<Scalar> a) const
288 {
289   Atom s((*symtables_p_)("param")->lookup ("time_signature"));
290   s.tex_ = substitute_args (s.tex_,a);
291
292   return s;
293 }
294
295
296 Atom
297 Lookup::stem (Real y1,Real y2) const
298 {
299   if (y1 > y2)
300     {
301       Real t = y1;
302       y1 = y2;
303       y2 = t;
304     }
305   Atom s;
306
307   s.dim_.x() = Interval (0,0);
308   s.dim_.y() = Interval (y1,y2);
309
310   Array<String> a;
311
312   
313   Real stem_width = paper_l_->get_var ("stemthickness");
314   a.push (print_dimen (-stem_width /2));
315   a.push (print_dimen (stem_width));
316   a.push (print_dimen (y2));
317   a.push (print_dimen (-y1));
318
319   String src = "\\kern %\\vrule width % height % depth %";
320   s.tex_ = substitute_args (src,a);
321   s.font_ = font_;
322   return s;
323 }
324
325 /*
326   should be handled via Tex_ code and Lookup::bar()
327  */
328 Atom
329 Lookup::vbrace (Real &y) const
330 {
331   Atom brace = (*symtables_p_) ("param")->lookup ( "brace");
332   Interval ydims = brace.dim_[Y_AXIS];
333   Real min_y = ydims[LEFT];
334   Real max_y = ydims[RIGHT];
335   Real step = 1.0 PT;
336  
337   if (y < min_y)
338     {
339       warning (_ ("piano brace") 
340         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
341       y = min_y;
342     }
343   if (y > max_y)
344     {
345       warning (_ ("piano brace")
346        + " " + _ ("too big") + " (" + print_dimen (y) + ")");
347       y = max_y;
348     }
349
350   
351   int idx = int (rint ((y- min_y)/step)) + 1;
352   
353   {
354     Array<String> a;
355     a.push (to_str (idx));
356     brace.tex_ = substitute_args (brace.tex_,a);
357     brace.dim_[Y_AXIS] = Interval (-y/2,y/2);
358   }
359
360   brace.font_ = font_;
361
362   return brace;
363 }
364
365 Atom
366 Lookup::vbracket (Real &y) const
367 {
368   Atom psbracket;
369   Real min_y = paper_l_->staffheight_f ();
370   if (y < min_y)
371     {
372       warning (_ ("bracket")
373         + " " + _ ("too small") +  "(" + print_dimen (y) + ")");
374 //      y = min_y;
375     }
376   psbracket.tex_ = String ("\\embeddedps{ ") + to_str (y) + " draw_bracket}";
377   psbracket.dim_[Y_AXIS] = Interval (-y/2,y/2);
378   psbracket.dim_[X_AXIS] = Interval (0,4 PT);
379   return psbracket;
380 #if 0
381   Atom bracket = afm_find (String ("param") + "bracket");
382   Interval ydims = bracket.dim_[Y_AXIS];
383
384   Real min_y = ydims[LEFT];
385   Real max_y = ydims[RIGHT];
386   Real step = 1.0 PT;
387  
388   if (y < min_y)
389     {
390       warning (_ ("bracket")
391         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
392       y = min_y;
393     }
394   if (y > max_y)
395     {
396       warning (_ ("bracket")
397        + " " + _ ("too big") + " (" + print_dimen (y) + ")");
398       y = max_y;
399     }
400
401   
402   int idx = int (rint ((y- min_y)/step)) + 1;
403   
404   {
405     Array<String> a;
406     a.push (to_str (idx));
407     bracket.tex_ = substitute_args (bracket.tex_,a);
408     bracket.dim_[Y_AXIS] = Interval (-y/2,y/2);
409   }
410
411   return bracket;
412 #endif
413 }
414