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