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