]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.1.18
[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   {0,0}
344 };
345
346 static Dictionary<char const *> cmr_dict (cmr_init);
347
348 Atom
349 Lookup::text (String style, String text) const
350 {
351   Atom a =  (*symtables_p_) ("style")->lookup (style);
352
353   a.lambda_ = gh_list(ly_symbol (a.str_),
354                       gh_str02scm (text.ch_C()),
355                       SCM_UNDEFINED);
356   
357   Real font_w = a.dim_.x ().length ();
358   Real font_h = a.dim_.y ().length ();
359
360   if (cmr_dict.elem_b (style))
361     {
362       style = String (cmr_dict [style]) + to_str  ((int)font_h); // ugh
363     }
364   Real w = 0;
365   Adobe_font_metric* afm_l = all_fonts_global_p->find_font (style);
366   DOUT << "\nChars: ";
367   
368   for (int i = 0; i < text.length_i (); i++) 
369     {
370       if (text[i]=='\\')
371         for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
372           ;
373       else
374         {
375           int c = text[i];
376           int code = afm_l->ascii_to_metric_idx_[c];
377           if (code >=0)
378             {
379               Adobe_font_char_metric m = afm_l->char_metrics_[code];
380               w += m.B_.x ().length ();
381               DOUT << to_str (m.B_.x ().length ()) << " ";
382             }
383         }
384     }
385   DOUT << "\n" << to_str (w) << "\n";
386   a.dim_.x () = Interval (0, w);
387   a.font_ = font_name_;
388   return a;
389 }
390   
391
392 Atom
393 Lookup::time_signature (Array<int> a) const
394 {
395   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
396   s.lambda_ = gh_list (ly_symbol (s.str_),
397                        gh_int2scm (a[0]),
398                        gh_int2scm (a[1]),
399                        SCM_UNDEFINED);
400   return s;
401 }
402
403 Atom
404 Lookup::vbrace (Real &y) const
405 {
406   Atom a;
407   a.lambda_ = gh_list (ly_symbol ("pianobrace"),
408                        gh_double2scm (y),
409                        SCM_UNDEFINED
410                        );
411   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
412   a.font_ = font_name_;
413   return a;
414 }
415
416 Atom
417 Lookup::hairpin (Real width, bool decresc, bool continued) const
418 {
419   Atom a;  
420   Real height = paper_l_->staffheight_f () / 6;
421
422   String hairpin = String (decresc ? "de" : "") + "crescendo";
423   a.lambda_ = gh_list (ly_symbol (hairpin),
424                        gh_double2scm (width),
425                        gh_double2scm (height),
426                        gh_double2scm (continued ? height/2 : 0.0),
427                        SCM_UNDEFINED);
428   a.dim_.x () = Interval (0, width);
429   a.dim_.y () = Interval (-2*height, 2*height);
430   a.font_ = font_name_;
431   return a;
432 }
433
434 Atom
435 Lookup::plet (Real dy , Real dx, Direction dir) const
436 {
437   Atom a;
438   a.lambda_ = gh_list(ly_symbol ("tuplet"),
439                       gh_double2scm (dx),
440                       gh_double2scm (dy),
441                       gh_int2scm (dir), SCM_UNDEFINED);
442   return a;
443 }
444
445
446 Atom
447 Lookup::slur (Array<Offset> controls) const
448 {
449   assert (controls.size () == 8);
450   Real dx = controls[3].x () - controls[0].x ();
451   Real dy = controls[3].y () - controls[0].y ();
452   Atom a;
453
454   SCM scontrols [8];
455   int indices[] = {5,6,7,4,1,2,3,0};
456
457   for (int i= 0; i < 8; i++)
458     scontrols[i] = offset2scm (controls[indices[i]]);
459
460
461   a.lambda_ =gh_list (ly_symbol ("slur"),
462                       ly_quote_scm (array_to_list (scontrols, 8)),
463                       SCM_UNDEFINED);
464
465   a.dim_[X_AXIS] = Interval (0, dx);
466   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
467   a.font_ = font_name_;
468   return a;
469 }
470
471 Atom
472 Lookup::vbracket (Real &y) const
473 {
474   Atom a;
475   a.lambda_ =  gh_list (ly_symbol ("bracket"),
476                         gh_double2scm (y),
477                         SCM_UNDEFINED);
478   a.str_ = "vbracket";
479   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
480   a.dim_[X_AXIS] = Interval (0,4 PT);
481   return a;
482 }
483
484 Atom
485 Lookup::volta (Real w, bool last_b) const
486 {
487   Atom a;
488   a.lambda_ = gh_list (ly_symbol ("volta"),
489                        gh_double2scm (w),
490                        gh_int2scm (last_b),
491                        SCM_UNDEFINED);
492   a.str_ = "volta";
493   Real interline_f = paper_l_->interline_f ();
494
495   a.dim_[Y_AXIS] = Interval (-interline_f, interline_f);
496   a.dim_[X_AXIS] = Interval (0, w);
497   return a;
498 }
499