]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
patch::: 1.1.4.jcn1
[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 <ctype.h>
16 #include "lookup.hh"
17 #include "debug.hh"
18 #include "dimensions.hh"
19 #include "symtable.hh"
20 #include "scalar.hh"
21 #include "paper-def.hh"
22 #include "string-convert.hh"
23 #include "file-path.hh"
24 #include "main.hh"
25 #include "lily-guile.hh"
26
27
28 Lookup::Lookup ()
29 {
30   paper_l_ = 0;
31   symtables_p_ = new Symtables;
32   afm_p_ =0;
33 }
34
35 Lookup::Lookup (Lookup const& s)
36 {
37   font_ = s.font_;
38   font_path_ = s.font_path_;
39   paper_l_ = s.paper_l_;
40   symtables_p_ = new Symtables (*s.symtables_p_);
41   afm_p_ = 0;
42 }
43
44 Lookup::Lookup (Symtables const& s)
45 {
46   font_ = s.font_;
47   font_path_ = s.font_path_;
48   paper_l_ = 0;
49   symtables_p_ = new Symtables (s);
50   afm_p_ = 0;
51 }
52
53 Lookup::~Lookup ()
54 {
55   delete afm_p_;
56   delete symtables_p_;
57 }
58
59 Molecule
60 Lookup::accidental (int j, bool cautionary) const
61 {
62   Molecule m(afm_find (String ("accidentals") + String ("-") + to_str (j)));
63   if (cautionary) 
64     {
65       m.add_at_edge(X_AXIS, LEFT, 
66                     Molecule(afm_find (String ("accidentals") + String ("-("))))
67 ;
68       m.add_at_edge(X_AXIS, RIGHT, 
69                     Molecule(afm_find (String ("accidentals") + String ("-)"))))
70 ;
71     }
72   return m;
73 }
74
75 void
76 Lookup::add (String s, Symtable*p)
77 {
78   symtables_p_->add (s, p);
79 }
80
81 Atom
82 Lookup::afm_find (String s, bool warn) const
83 {
84   if (!afm_p_)
85     {
86       *mlog << "[" << font_path_;
87       ( (Lookup*)this)->afm_p_ = new Adobe_font_metric (read_afm (font_path_));
88       *mlog << "]" << flush ;
89       DOUT << this->afm_p_->str ();
90     }
91   Adobe_font_char_metric m = afm_p_->find_char (s, warn);
92
93   Atom a;
94   if (m.code () < 0)
95     return a;
96   
97   a.dim_ = m.B_;
98   a.dim_[X_AXIS] *= 1 / 1000.0;
99   a.dim_[Y_AXIS] *= 1 / 1000.0;
100   Array<Real> arr;
101   arr.push (m.code ());
102   a.lambda_ =  (lambda_scm ("char", arr));
103   a.str_ = "afm_find: " + s;
104   a.font_ = font_;
105   return a;
106 }
107
108 Atom
109 Lookup::ball (int j) const
110 {
111   if (j > 2)
112     j = 2;
113
114   return afm_find (String ("balls") + String ("-") + to_str (j));
115 }
116
117 Atom
118 Lookup::bar (String str, Real h) const
119 {
120   Array<Real> arr;
121   arr.push (h);
122   Atom a = (*symtables_p_) ("bars")->lookup (str);
123   a.lambda_ =  (lambda_scm (a.str_, arr));
124   a.str_ = "bar";
125   a.dim_.y () = Interval (-h/2, h/2);
126   a.font_ = font_;
127   return a;
128 }
129
130 Atom 
131 Lookup::beam (Real slope, Real width, Real thick) const
132 {
133   Real height = slope * width; 
134   Real min_y = (0 <? height) - thick/2;
135   Real max_y = (0 >? height) + thick/2;
136
137   Array<Real> arr;
138   arr.push (width);
139   arr.push (slope);
140   arr.push (thick);
141
142   Atom a;
143   a.lambda_ =  (lambda_scm ("beam", arr));
144   a.str_ = "beam";
145   a.dim_[X_AXIS] = Interval (0, width);
146   a.dim_[Y_AXIS] = Interval (min_y, max_y);
147   return a;
148 }
149
150 Atom
151 Lookup::clef (String st) const
152 {
153   return afm_find (String ("clefs") + String ("-") + st);
154 }
155
156 SCM
157 offset2scm (Offset o)
158 {
159   return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]), SCM_UNDEFINED);
160 }
161
162 Atom
163 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
164 {
165   assert (controls.size () == 8);
166   Offset d = controls[3] - controls[0];
167   
168   Real dx = d[X_AXIS];
169   Real dy = d[Y_AXIS];
170
171   Atom a;
172   a.font_ = font_;
173   a.dim_[X_AXIS] = Interval (0, dx);
174   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
175
176   SCM sc[4];
177   for (int i=0; i<  4; i++)
178     {
179       sc[i] =  offset2scm (controls[i]);
180     }
181
182   // (lambda (o) (dashed-slur o thick dash '(stuff))
183   a.lambda_ = 
184     ly_append (ly_lambda_o (),
185     ly_list1 (ly_append (ly_func_o ("dashed-slur"),
186     gh_cons (gh_double2scm (thick), 
187     gh_cons (gh_double2scm (dash),
188     ly_list1 (ly_quote_scm (gh_list (sc[0], sc[1], sc[2], sc[3], SCM_UNDEFINED))))))));
189
190   a.str_ = "dashed_slur";
191   return a;
192 }
193
194 Atom
195 Lookup::dots () const
196 {
197   return afm_find (String ("dots") + String ("-") + String ("dot"));
198 }
199
200 Atom
201 Lookup::dynamic (String st) const
202 {
203   return (*symtables_p_) ("dynamics")->lookup (st);
204 }
205
206 Atom
207 Lookup::fill (Box b) const
208 {
209   Atom a;
210   a.dim_ = b;
211   return a;
212 }
213
214 Atom
215 Lookup::flag (int j, Direction d) const
216 {
217   char c = (d == UP) ? 'u' : 'd';
218   Atom a = afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
219   return a;
220 }
221
222 void
223 Lookup::print () const
224 {
225 #ifndef NPRINT
226   DOUT << "Lookup {\n";
227   symtables_p_->print ();
228   DOUT << "}\n";
229 #endif
230 }
231
232 Atom
233 Lookup::rest (int j, bool o) const
234 {
235    return afm_find (String ("rests")
236                     + String ("-") + to_str (j) + (o ? "o" : ""));
237 }
238
239 Atom
240 Lookup::rule_symbol (Real height, Real width) const
241 {
242   Atom a = (*symtables_p_) ("param")->lookup ("rule");
243   Array<Real> arr;
244   arr.push (height);
245   arr.push (width);
246   a.lambda_ = (lambda_scm (a.str_, arr));
247   a.str_ = "rule_symbol";
248   a.dim_.x () = Interval (0, width);
249   a.dim_.y () = Interval (0, height);
250   return a;
251 }
252
253 Atom
254 Lookup::script (String str) const
255 {
256   return afm_find (String ("scripts") + String ("-") + str);
257 }
258
259 Atom
260 Lookup::special_time_signature (String s, Array<int> arr) const
261 {
262   // First guess: s contains only the signature style
263   assert (arr.size () >1);
264   String symbolname = "timesig-" + s + to_str (arr[0]) + "/" + to_str (arr[1]);
265   
266   Atom a = afm_find (symbolname, false);
267   if (!a.empty ()) 
268     return a;
269
270   // Second guess: s contains the full signature name
271   a = afm_find ("timesig-"+s, false);
272   if (!a.empty ()) 
273     return a;
274
275   // Resort to default layout with numbers
276   return time_signature (arr);
277 }
278
279 Atom
280 Lookup::stem (Real y1, Real y2) const
281 {
282   if (y1 > y2)
283     {
284       Real t = y1;
285       y1 = y2;
286       y2 = t;
287     }
288   Atom a;
289
290   a.dim_.x () = Interval (0,0);
291   a.dim_.y () = Interval (y1,y2);
292
293   Array<Real> arr;
294
295   Real stem_width = paper_l_->get_var ("stemthickness");
296   arr.push (-stem_width /2);
297   arr.push (stem_width);
298   arr.push (y2);
299   arr.push (-y1);
300
301   a.lambda_ = (lambda_scm ("stem", arr));
302   a.str_ = "stem";
303   a.font_ = font_;
304   return a;
305 }
306
307 Atom
308 Lookup::streepje (int type) const
309 {
310   if (type > 2)
311     type = 2;
312
313   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
314 }
315
316 Dictionary<String> cmr_dict;
317 Dictionary<Adobe_font_metric*> afm_p_dict;
318
319 Atom
320 Lookup::text (String style, String text) const
321 {
322   Array<Scalar> arr;
323
324   arr.push (text);
325   Atom a =  (*symtables_p_) ("style")->lookup (style);
326   a.lambda_ = lambda_scm (a.str_, arr);
327
328 // urg
329 //  if (!cmr_dict.length_i ())
330   if (!cmr_dict.elem_b ("roman"))
331     {
332       cmr_dict.elem ("italic") = "cmri12.afm";
333       cmr_dict.elem ("roman") = "cmr12.afm";
334     }
335
336   if (!afm_p_dict.elem_b (style))
337     {
338       String cmr_str = cmr_dict.elem (style);
339       String font_path = global_path.find (cmr_str);
340       if (!font_path.length_i ())
341         error (_f("can't open file: `%s'", cmr_str.ch_C ()));
342       *mlog << "[" << font_path;
343       Adobe_font_metric* afm_p = new Adobe_font_metric (read_afm (font_path));
344       DOUT << afm_p->str ();
345       *mlog << "]" << flush ;
346       afm_p_dict.elem (style) = afm_p;
347     }
348   Real w = 0;
349   Real guess_w = a.dim_.x ().length ();
350   Adobe_font_metric* afm_p = afm_p_dict.elem (style);
351   DOUT << "\nChars: ";
352   for (int i = 0; i < text.length_i (); i++) 
353     {
354       if (text[i]=='\\')
355         for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
356           ;
357       else
358         {
359           if (afm_p)
360             {
361               Adobe_font_char_metric m = afm_p->char_metrics_[(int)text[i]];
362               w += m.B_.x ().length ();
363               DOUT << to_str (m.B_.x ().length ()) << " ";
364             }
365           else
366               w += guess_w;
367         }
368     }
369   DOUT << "\n" << to_str (w) << "\n";
370   a.dim_.x () = Interval (0, w);
371   a.str_ = "text";
372   a.font_ = font_;
373   return a;
374 }
375   
376
377 Atom
378 Lookup::time_signature (Array<int> a) const
379 {
380   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
381   s.lambda_ =  (lambda_scm (s.str_, a));
382
383   return s;
384 }
385
386 /*
387   should be handled via Tex_ code and Lookup::bar ()
388  */
389 Atom
390 Lookup::vbrace (Real &y) const
391 {
392   Atom a = (*symtables_p_) ("param")->lookup ( "brace");
393   Interval ydims = a.dim_[Y_AXIS];
394   Real min_y = ydims[LEFT];
395   Real max_y = ydims[RIGHT];
396   Real step = 1.0 PT;
397  
398   if (y < min_y)
399     {
400       warning (_ ("piano brace") 
401                + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
402       y = min_y;
403     }
404   if (y > max_y)
405     {
406       warning (_ ("piano brace")
407                + " " + _ ("too big") + " (" + print_dimen (y) + ")");
408       y = max_y;
409     }
410
411   
412   int idx = int (rint ( (y- min_y)/step)) + 1;
413   
414   Array<Real> arr;
415   arr.push (idx);
416   a.lambda_ = (lambda_scm (a.str_, arr));
417   a.str_ = "brace";
418   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
419   a.font_ = font_;
420   return a;
421 }
422
423 Atom
424 Lookup::hairpin (Real width, bool decresc, bool continued) const
425 {
426   Atom a;  
427   Real height = paper_l_->staffheight_f () / 6;
428   String ps;
429   ps += to_str (width) + " " 
430     + to_str (height) + " " 
431     + to_str (continued ? height/2 : 0) + 
432     + " draw_"  + String (decresc ? "de" : "") + "cresc\n";
433   a.str_ = ps;
434   a.dim_.x () = Interval (0, width);
435   a.dim_.y () = Interval (-2*height, 2*height);
436   a.font_ = font_;
437   return a;
438 }
439
440 Atom
441 Lookup::plet (Real dy , Real dx, Direction dir) const
442 {
443   String ps;
444     
445   ps += String_convert::double_str (dx) + " " 
446     + String_convert::double_str (dy) + " "
447     + String_convert::int_str ( (int)dir) +
448     " draw_plet ";
449
450   Atom a;
451   a.str_ = ps;
452   return a;
453 }
454
455 Atom
456 Lookup::slur (Array<Offset> controls) const
457 {
458   assert (controls.size () == 8);
459
460   String ps;
461   
462   Real dx = controls[3].x () - controls[0].x ();
463   Real dy = controls[3].y () - controls[0].y ();
464   Atom a;
465
466   SCM scontrols [8];
467   int indices[] = {5,6,7,4,1,2,3,0};
468
469   for (int i= 0; i < 8; i++)
470     scontrols[i] = offset2scm (controls[indices[i]]);
471
472
473   a.lambda_ =
474     gh_append2 (ly_lambda_o (),
475                 gh_list (gh_append2 (ly_func_o ("slur"),
476                                      gh_list (ly_quote_scm (gh_list (scontrols[0],
477                                                                      scontrols[1],
478                                                                      scontrols[2],
479                                                                      scontrols[3],
480                                                                      scontrols[4],
481                                                                      scontrols[5],
482                                                                      scontrols[6],
483                                                                      scontrols[7],
484                                                                      SCM_UNDEFINED)),
485                                               SCM_UNDEFINED)
486                                      ),
487                          SCM_UNDEFINED)
488                 );
489
490
491   a.str_ = "slur";
492
493   a.dim_[X_AXIS] = Interval (0, dx);
494   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
495   a.font_ = font_;
496   return a;
497 }
498
499 Atom
500 Lookup::vbracket (Real &y) const
501 {
502   Atom a;
503   Real min_y = paper_l_->staffheight_f ();
504   if (y < min_y)
505     {
506       warning (_ ("bracket")
507                + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
508       //      y = min_y;
509     }
510   Array<Real> arr;
511   arr.push (y);
512   a.lambda_ =  (lambda_scm ("bracket", arr));
513   a.str_ = "vbracket";
514   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
515   a.dim_[X_AXIS] = Interval (0,4 PT);
516   return a;
517 }
518
519