]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
patch::: 1.1.1.jcn1: Re: geil
[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   Jan Nieuwenhuizen <janneke@gnu.org>
9
10   TODO
11       Read spacing info from AFMs
12       Glissando
13 */
14
15 #include "lookup.hh"
16 #include "debug.hh"
17 #include "dimensions.hh"
18 #include "symtable.hh"
19 #include "scalar.hh"
20 #include "paper-def.hh"
21 #include "string-convert.hh"
22 #include "main.hh"
23 #include "lily-guile.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 (Symtables const& s)
42 {
43   font_ = s.font_;
44   font_path_ = s.font_path_;
45   paper_l_ = 0;
46   symtables_p_ = new Symtables (s);
47   afm_p_ = 0;
48 }
49
50 Lookup::~Lookup ()
51 {
52   delete afm_p_;
53   delete symtables_p_;
54 }
55
56 Atom
57 Lookup::accidental (int j) const
58 {
59   Atom a = afm_find (String ("accidentals") + String ("-") + to_str (j));
60   return a;
61 }
62
63 void
64 Lookup::add (String s, Symtable*p)
65 {
66   symtables_p_->add (s, p);
67 }
68
69 Atom
70 Lookup::afm_find (String s) const
71 {
72   if (!afm_p_)
73     {
74       *mlog << "[" << font_path_;
75       ( (Lookup*)this)->afm_p_ = new Adobe_font_metric (read_afm (font_path_));
76       *mlog << "]" << flush ;
77       DOUT << this->afm_p_->str ();
78     }
79   Adobe_font_char_metric m = afm_p_->find_char (s);
80
81   Atom a;
82   if (m.code () < 0)
83     return a;
84   
85   a.dim_ = m.B_;
86   a.dim_[X_AXIS] *= 1 / 1000.0;
87   a.dim_[Y_AXIS] *= 1 / 1000.0;
88   Array<Real> arr;
89   arr.push (m.code ());
90   a.lambda_ =  (lambda_scm ("char", arr));
91   a.str_ = "afm_find: " + s;
92   a.font_ = font_;
93   return a;
94 }
95
96 Atom
97 Lookup::ball (int j) const
98 {
99   if (j > 2)
100     j = 2;
101
102   return afm_find (String ("balls") + String ("-") + to_str (j));
103 }
104
105 Atom
106 Lookup::bar (String str, Real h) const
107 {
108   Array<Real> arr;
109   arr.push (h);
110   Atom a = (*symtables_p_) ("bars")->lookup (str);
111   a.lambda_ =  (lambda_scm (a.str_, arr));
112   a.str_ = "bar";
113   a.dim_.y () = Interval (-h/2, h/2);
114   a.font_ = font_;
115   return a;
116 }
117
118 Atom 
119 Lookup::beam (Real slope, Real width, Real thick) const
120 {
121   Real height = slope * width; 
122   Real min_y = (0 <? height) - thick/2;
123   Real max_y = (0 >? height) + thick/2;
124
125   Array<Real> arr;
126   arr.push (width);
127   arr.push (slope);
128   arr.push (thick);
129
130   Atom a;
131   a.lambda_ =  (lambda_scm ("beam", arr));
132   a.str_ = "beam";
133   a.dim_[X_AXIS] = Interval (0, width);
134   a.dim_[Y_AXIS] = Interval (min_y, max_y);
135   return a;
136 }
137
138 Atom
139 Lookup::clef (String st) const
140 {
141   return afm_find (String ("clefs") + String ("-") + st);
142 }
143
144 Atom
145 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
146 {
147   assert (controls.size () == 8);
148
149   Real dx = controls[3].x () - controls[0].x ();
150   Real dy = controls[3].y () - controls[0].y ();
151
152   Atom a;
153   a.font_ = font_;
154   a.dim_[X_AXIS] = Interval (0, dx);
155   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
156
157   // (lambda (o) (dashed-slur o '((0.1 0.2) (1.1 1.2) (2.1 2.2) (3.1 3.2))))
158   a.lambda_ =  (
159     ly_append (ly_lambda_o (), 
160     ly_list1 (ly_append (ly_func_o ("dashed-slur"),
161     gh_cons (gh_double2scm (thick), gh_cons (gh_double2scm (dash),
162     ly_list1 (ly_list2 (ly_quote (),
163     gh_cons (ly_list2 (gh_double2scm (controls[1].x ()), gh_double2scm (controls[1].y ())),
164     gh_cons (ly_list2 (gh_double2scm (controls[2].x ()), gh_double2scm (controls[2].y ())),
165     gh_cons (ly_list2 (gh_double2scm (controls[3].x ()), gh_double2scm (controls[3].y ())),
166     gh_cons (ly_list2 (gh_double2scm (controls[0].x ()), gh_double2scm (controls[0].y ())),
167     SCM_EOL))))))))))));
168
169   a.str_ = "dashed_slur";
170   return a;
171 }
172
173 Atom
174 Lookup::dots () const
175 {
176   return afm_find (String ("dots") + String ("-") + String ("dot"));
177 }
178
179 Atom
180 Lookup::dynamic (String st) const
181 {
182   return (*symtables_p_) ("dynamics")->lookup (st);
183 }
184
185 Atom
186 Lookup::fill (Box b) const
187 {
188   Atom a;
189   a.dim_ = b;
190   return a;
191 }
192
193 Atom
194 Lookup::flag (int j, Direction d) const
195 {
196   char c = (d == UP) ? 'u' : 'd';
197   Atom a = afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
198   return a;
199 }
200
201 void
202 Lookup::print () const
203 {
204 #ifndef NPRINT
205   DOUT << "Lookup {\n";
206   symtables_p_->print ();
207   DOUT << "}\n";
208 #endif
209 }
210
211 Atom
212 Lookup::rest (int j, bool o) const
213 {
214    return afm_find (String ("rests")
215                     + String ("-") + to_str (j) + (o ? "o" : ""));
216 }
217
218 Atom
219 Lookup::rule_symbol (Real height, Real width) const
220 {
221   Atom a = (*symtables_p_) ("param")->lookup ("rule");
222   Array<Real> arr;
223   arr.push (height);
224   arr.push (width);
225   a.lambda_ = lambda_scm (a.str_, arr);
226   a.str_ = "rule_symbol";
227   a.dim_.x () = Interval (0, width);
228   a.dim_.y () = Interval (0, height);
229 }
230
231 Atom
232 Lookup::script (String str) const
233 {
234   return afm_find (String ("scripts") + String ("-") + str);
235 }
236
237 Atom
238 Lookup::special_time_signature (String s, Array<Real> arr) const
239 {
240   String symbolname = "timesig-" + s;
241   if (!arr.empty ())
242     symbolname += to_str (arr[0]);
243   if (arr.size () >1)
244     symbolname += "/" + to_str (arr[1]);
245   
246   Atom a = afm_find (symbolname);
247   if (!a.empty ()) 
248     return a;
249
250 #if 0 //guess we covered this
251   // Try if the full name was given
252   a = afm_find ("timesig-"+s);
253   if (!a.empty ()) 
254     return a;
255 #endif
256
257   // Resort to default layout with numbers
258   return time_signature (arr);
259 }
260
261 Atom
262 Lookup::stem (Real y1, Real y2) const
263 {
264   if (y1 > y2)
265     {
266       Real t = y1;
267       y1 = y2;
268       y2 = t;
269     }
270   Atom a;
271
272   a.dim_.x () = Interval (0,0);
273   a.dim_.y () = Interval (y1,y2);
274
275   Array<Real> arr;
276
277   Real stem_width = paper_l_->get_var ("stemthickness");
278   arr.push (-stem_width /2);
279   arr.push (stem_width);
280   arr.push (y2);
281   arr.push (-y1);
282
283   a.lambda_ = lambda_scm ("stem", arr);
284   a.str_ = "stem";
285   a.font_ = font_;
286 }
287
288 Atom
289 Lookup::streepje (int type) const
290 {
291   if (type > 2)
292     type = 2;
293
294   Atom a = afm_find ("balls" + String ("-") + to_str (type) + "l");
295   return a;
296 }
297
298 Atom
299 Lookup::text (String style, String text) const
300 {
301   Array<Scalar> arr;
302
303   arr.push (text);
304   Atom a =  (*symtables_p_) ("style")->lookup (style);
305   a.lambda_ = lambda_scm (a.str_, arr);
306   a.str_ = "text";
307   a.font_ = font_;
308 }
309
310 Atom
311 Lookup::time_signature (Array<Real> a) const
312 {
313   Atom a ((*symtables_p_) ("param")->lookup ("time_signature"));
314   a.lambda_ = lambda_scm (a.str_, arr);
315   a.str_ = "time_signature";
316 }
317
318 /*
319   should be handled via Tex_ code and Lookup::bar ()
320  */
321 Atom
322 Lookup::vbrace (Real &y) const
323 {
324   Atom a = (*symtables_p_) ("param")->lookup ( "brace");
325   Interval ydims = a.dim_[Y_AXIS];
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 (_ ("piano brace") 
333         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
334       y = min_y;
335     }
336   if (y > max_y)
337     {
338       warning (_ ("piano brace")
339        + " " + _ ("too big") + " (" + print_dimen (y) + ")");
340       y = max_y;
341     }
342
343   
344   int idx = int (rint ( (y- min_y)/step)) + 1;
345   
346   Array<Real> arr;
347   arr.push (idx);
348   a.lambda_ = lambda_scm (a.str_, arr);
349   a.str_ = "brace";
350   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
351   a.font_ = font_;
352   return a;
353 }
354
355 Atom
356 Lookup::hairpin (Real width, bool decresc, bool continued) const
357 {
358   Atom a;  
359   Real height = paper_l_->staffheight_f () / 6;
360   String ps;
361   ps += to_str (width) + " " 
362         + to_str (height) + " " 
363     + to_str (continued ? height/2 : 0) + 
364     + " draw_"  + String (decresc ? "de" : "") + "cresc\n";
365   a.str_ = ps;
366
367
368   a.dim_.x () = Interval (0, width);
369   a.dim_.y () = Interval (-2*height, 2*height);
370   a.font_ = font_;
371   return a;
372 }
373
374 Atom
375 Lookup::plet (Real dy , Real dx, Direction dir) const
376 {
377   String ps;
378   
379   
380   ps += String_convert::double_str (dx) + " " 
381     + String_convert::double_str (dy) + " "
382     + String_convert::int_str ( (int)dir) +
383     " draw_plet ";
384
385   Atom a;
386   a.str_ = ps;
387   return a;
388 }
389
390 Atom
391 Lookup::slur (Array<Offset> controls) const
392 {
393   assert (controls.size () == 8);
394
395   String ps;
396   
397   Real dx = controls[3].x () - controls[0].x ();
398   Real dy = controls[3].y () - controls[0].y ();
399   Atom a;
400  
401   // (lambda (o) (slur o '((0.1 0.2) (1.1 1.2) (2.1 2.2) (3.1 3.2) .. )))
402   a.lambda_ =  (
403     ly_append (ly_lambda_o (), 
404     ly_list1 (ly_append (ly_func_o ("slur"),
405     ly_list1 (ly_list2 (ly_quote (),
406     gh_cons (ly_list2 (gh_double2scm (controls[5].x ()), gh_double2scm (controls[5].y ())),
407     gh_cons (ly_list2 (gh_double2scm (controls[6].x ()), gh_double2scm (controls[6].y ())),
408     gh_cons (ly_list2 (gh_double2scm (controls[7].x ()), gh_double2scm (controls[7].y ())),
409     gh_cons (ly_list2 (gh_double2scm (controls[4].x ()), gh_double2scm (controls[4].y ())),
410     gh_cons (ly_list2 (gh_double2scm (controls[1].x ()), gh_double2scm (controls[1].y ())),
411     gh_cons (ly_list2 (gh_double2scm (controls[2].x ()), gh_double2scm (controls[2].y ())),
412     gh_cons (ly_list2 (gh_double2scm (controls[3].x ()), gh_double2scm (controls[3].y ())),
413     gh_cons (ly_list2 (gh_double2scm (controls[0].x ()), gh_double2scm (controls[0].y ())),
414     SCM_EOL))))))))))))));
415   a.str_ = "slur";
416
417   a.dim_[X_AXIS] = Interval (0, dx);
418   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
419   a.font_ = font_;
420   return a;
421 }
422
423 Atom
424 Lookup::vbracket (Real &y) const
425 {
426   Atom a;
427   Real min_y = paper_l_->staffheight_f ();
428   if (y < min_y)
429     {
430       warning (_ ("bracket")
431         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
432 //      y = min_y;
433     }
434   Array<Real> arr;
435   arr.push (y);
436   a.lambda_ =  (lambda_scm ("bracket", arr));
437   a.str_ = "vbracket";
438   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
439   a.dim_[X_AXIS] = Interval (0,4 PT);
440   return a;
441 }
442
443