]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.1.7
[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     gh_append2 (ly_lambda_o (),
185     gh_list (gh_append2 (ly_func_o ("dashed-slur"),
186     gh_cons (gh_double2scm (thick), 
187     gh_cons (gh_double2scm (dash),
188     gh_list (ly_quote_scm (gh_list (sc[1], sc[2], sc[3], sc[0], SCM_UNDEFINED)), SCM_UNDEFINED)))), 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::extender (Real width) const
208 {
209   Atom a = (*symtables_p_) ("param")->lookup ("extender");
210   Array<Real> arr;
211   arr.push (width);
212   a.lambda_ = (lambda_scm (a.str_, arr));
213   a.str_ = "extender";
214   a.font_ = font_;
215   return a;
216 }
217
218 Atom
219 Lookup::fill (Box b) const
220 {
221   Atom a;
222   a.dim_ = b;
223   return a;
224 }
225
226 Atom
227 Lookup::flag (int j, Direction d) const
228 {
229   char c = (d == UP) ? 'u' : 'd';
230   Atom a = afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
231   return a;
232 }
233
234 void
235 Lookup::print () const
236 {
237 #ifndef NPRINT
238   DOUT << "Lookup {\n";
239   symtables_p_->print ();
240   DOUT << "}\n";
241 #endif
242 }
243
244 Atom
245 Lookup::rest (int j, bool o) const
246 {
247    return afm_find (String ("rests")
248                     + String ("-") + to_str (j) + (o ? "o" : ""));
249 }
250
251 Atom
252 Lookup::rule_symbol (Real height, Real width) const
253 {
254   Atom a = (*symtables_p_) ("param")->lookup ("rule");
255   Array<Real> arr;
256   arr.push (height);
257   arr.push (width);
258   a.lambda_ = (lambda_scm (a.str_, arr));
259   a.str_ = "rule_symbol";
260   a.dim_.x () = Interval (0, width);
261   a.dim_.y () = Interval (0, height);
262   return a;
263 }
264
265 Atom
266 Lookup::script (String str) const
267 {
268   return afm_find (String ("scripts") + String ("-") + str);
269 }
270
271 Atom
272 Lookup::special_time_signature (String s, Array<int> arr) const
273 {
274   // First guess: s contains only the signature style
275   assert (arr.size () >1);
276   String symbolname = "timesig-" + s + to_str (arr[0]) + "/" + to_str (arr[1]);
277   
278   Atom a = afm_find (symbolname, false);
279   if (!a.empty ()) 
280     return a;
281
282   // Second guess: s contains the full signature name
283   a = afm_find ("timesig-"+s, false);
284   if (!a.empty ()) 
285     return a;
286
287   // Resort to default layout with numbers
288   return time_signature (arr);
289 }
290
291 Atom
292 Lookup::stem (Real y1, Real y2) const
293 {
294   if (y1 > y2)
295     {
296       Real t = y1;
297       y1 = y2;
298       y2 = t;
299     }
300   Atom a;
301
302   a.dim_.x () = Interval (0,0);
303   a.dim_.y () = Interval (y1,y2);
304
305   Array<Real> arr;
306
307   Real stem_width = paper_l_->get_var ("stemthickness");
308   arr.push (-stem_width /2);
309   arr.push (stem_width);
310   arr.push (y2);
311   arr.push (-y1);
312
313   a.lambda_ = (lambda_scm ("stem", arr));
314   a.str_ = "stem";
315   a.font_ = font_;
316   return a;
317 }
318
319 Atom
320 Lookup::streepje (int type) const
321 {
322   if (type > 2)
323     type = 2;
324
325   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
326 }
327
328 Dictionary<String> cmr_dict;
329 Dictionary<Adobe_font_metric*> afm_p_dict;
330
331 Atom
332 Lookup::text (String style, String text) const
333 {
334   Array<Scalar> arr;
335
336   arr.push (text);
337   Atom a =  (*symtables_p_) ("style")->lookup (style);
338   a.lambda_ = lambda_scm (a.str_, arr);
339   Real font_w = a.dim_.x ().length ();
340   Real font_h = a.dim_.y ().length ();
341
342 // urg
343 //  if (!cmr_dict.length_i ())
344   if (!cmr_dict.elem_b ("roman"))
345     {
346       //brrrr
347       cmr_dict.elem ("bold") = "cmbx";
348       cmr_dict.elem ("dynamic") = "feta-din";
349       cmr_dict.elem ("finger") = "feta-nummer";
350       cmr_dict.elem ("italic") = "cmti";
351       cmr_dict.elem ("roman") = "cmr";
352     }
353
354   if (!afm_p_dict.elem_b (style))
355     {
356       Adobe_font_metric* afm_p = 0;
357       String cmr_str = cmr_dict.elem (style) + to_str ((int) font_h) + ".afm";
358       String font_path = global_path.find (cmr_str);
359       if (!font_path.length_i ())
360         {
361           warning (_f("can't open file: `%s'", cmr_str.ch_C ()));
362           warning (_f("guessing dimensions for font style: `%s'", style.ch_C ()));
363         }
364       else
365         {
366           *mlog << "[" << font_path;
367           afm_p = new Adobe_font_metric (read_afm (font_path));
368           DOUT << afm_p->str ();
369           *mlog << "]" << flush ;
370         }
371       afm_p_dict.elem (style) = afm_p;
372     }
373   Real w = 0;
374   Adobe_font_metric* afm_p = afm_p_dict.elem (style);
375   DOUT << "\nChars: ";
376   for (int i = 0; i < text.length_i (); i++) 
377     {
378       if (text[i]=='\\')
379         for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
380           ;
381       else
382         {
383           int c = text[i];
384           if (afm_p && ((c >= 0) && (c < afm_p->char_metrics_.size ())))
385             {
386               Adobe_font_char_metric m = afm_p->char_metrics_[c];
387               w += m.B_.x ().length ();
388               DOUT << to_str (m.B_.x ().length ()) << " ";
389             }
390           else
391               w += font_w;
392         }
393     }
394   DOUT << "\n" << to_str (w) << "\n";
395   a.dim_.x () = Interval (0, w);
396   a.str_ = "text";
397   a.font_ = font_;
398   return a;
399 }
400   
401
402 Atom
403 Lookup::time_signature (Array<int> a) const
404 {
405   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
406   s.lambda_ =  (lambda_scm (s.str_, a));
407
408   return s;
409 }
410
411 /*
412   should be handled via Tex_ code and Lookup::bar ()
413  */
414 Atom
415 Lookup::vbrace (Real &y) const
416 {
417   Atom a = (*symtables_p_) ("param")->lookup ( "brace");
418   Interval ydims = a.dim_[Y_AXIS];
419   Real min_y = ydims[LEFT];
420   Real max_y = ydims[RIGHT];
421   Real step = 1.0 PT;
422  
423   if (y < min_y)
424     {
425       warning (_ ("piano brace") 
426                + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
427       y = min_y;
428     }
429   if (y > max_y)
430     {
431       warning (_ ("piano brace")
432                + " " + _ ("too big") + " (" + print_dimen (y) + ")");
433       y = max_y;
434     }
435
436   
437   int idx = int (rint ( (y- min_y)/step)) + 1;
438   
439   Array<Real> arr;
440   arr.push (idx);
441   a.lambda_ = (lambda_scm (a.str_, arr));
442   a.str_ = "brace";
443   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
444   a.font_ = font_;
445   return a;
446 }
447
448 Atom
449 Lookup::hairpin (Real width, bool decresc, bool continued) const
450 {
451   Atom a;  
452   Real height = paper_l_->staffheight_f () / 6;
453   Array<Real> arr;
454   arr.push (width);
455   arr.push (height);
456   arr.push (continued ? height/2 : 0);
457   String hairpin = String (decresc ? "de" : "") + "crescendo\n";
458   a.lambda_ = (lambda_scm (hairpin, arr));
459   a.str_ = "hairpin";
460   a.dim_.x () = Interval (0, width);
461   a.dim_.y () = Interval (-2*height, 2*height);
462   a.font_ = font_;
463   return a;
464 }
465
466 Atom
467 Lookup::plet (Real dy , Real dx, Direction dir) const
468 {
469   Array<Real> arr;
470   arr.push (dx);
471   arr.push (dy);
472   arr.push (dir);
473   Atom a;
474   a.lambda_ = (lambda_scm ("tuplet", arr));
475   a.str_ = "plet";
476   return a;
477 }
478
479 Atom
480 Lookup::slur (Array<Offset> controls) const
481 {
482   assert (controls.size () == 8);
483
484   Real dx = controls[3].x () - controls[0].x ();
485   Real dy = controls[3].y () - controls[0].y ();
486   Atom a;
487
488   SCM scontrols [8];
489   int indices[] = {5,6,7,4,1,2,3,0};
490
491   for (int i= 0; i < 8; i++)
492     scontrols[i] = offset2scm (controls[indices[i]]);
493
494
495   a.lambda_ =
496     gh_append2 (ly_lambda_o (),
497                 gh_list (gh_append2 (ly_func_o ("slur"),
498                                      gh_list (ly_quote_scm (gh_list (scontrols[0],
499                                                                      scontrols[1],
500                                                                      scontrols[2],
501                                                                      scontrols[3],
502                                                                      scontrols[4],
503                                                                      scontrols[5],
504                                                                      scontrols[6],
505                                                                      scontrols[7],
506                                                                      SCM_UNDEFINED)),
507                                               SCM_UNDEFINED)
508                                      ),
509                          SCM_UNDEFINED)
510                 );
511
512
513   a.str_ = "slur";
514
515   a.dim_[X_AXIS] = Interval (0, dx);
516   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
517   a.font_ = font_;
518   return a;
519 }
520
521 Atom
522 Lookup::vbracket (Real &y) const
523 {
524   Atom a;
525   Real min_y = paper_l_->staffheight_f ();
526   if (y < min_y)
527     {
528       warning (_ ("bracket")
529                + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
530       //      y = min_y;
531     }
532   Array<Real> arr;
533   arr.push (y);
534   a.lambda_ =  (lambda_scm ("bracket", arr));
535   a.str_ = "vbracket";
536   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
537   a.dim_[X_AXIS] = Interval (0,4 PT);
538   return a;
539 }
540
541 Atom
542 Lookup::volta (Real w, bool last_b) const
543 {
544   Array<Real> arr;
545   arr.push (w);
546   arr.push (last_b);
547   Atom a;
548   a.lambda_ = (lambda_scm ("volta", arr));
549   a.str_ = "volta";
550   return a;
551 }
552