]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
f777dee42aa1a0f0aaf1407ad4c6edd79d9bf550
[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::special_ball (int j, String kind_of_ball) const
127 {
128   if (j > 2)
129     j = 2;
130
131   return afm_find (String ("balls") + String ("-") + kind_of_ball);
132 }
133
134 Atom
135 Lookup::bar (String str, Real h) const
136 {
137
138   Atom a = (*symtables_p_) ("bars")->lookup (str);
139   
140   
141   a.lambda_ = gh_list (ly_symbol (a.str_.ch_C()),
142                        gh_double2scm (h),
143                        SCM_UNDEFINED);
144
145
146   a.dim_.y () = Interval (-h/2, h/2);
147   a.font_ = font_name_;
148   return a;
149 }
150
151 Atom 
152 Lookup::beam (Real slope, Real width, Real thick) const
153 {
154   Real height = slope * width; 
155   Real min_y = (0 <? height) - thick/2;
156   Real max_y = (0 >? height) + thick/2;
157
158   Atom a;
159   a.lambda_ =   gh_list (ly_symbol ("beam"),
160            gh_double2scm (width),
161            gh_double2scm (slope),
162            gh_double2scm (thick),
163            SCM_UNDEFINED);
164
165   a.dim_[X_AXIS] = Interval (0, width);
166   a.dim_[Y_AXIS] = Interval (min_y, max_y);
167   return a;
168 }
169
170 Atom
171 Lookup::clef (String st) const
172 {
173   return afm_find (String ("clefs") + String ("-") + st);
174 }
175
176 SCM
177 offset2scm (Offset o)
178 {
179   return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
180                   SCM_UNDEFINED);
181 }
182
183 Atom
184 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
185 {
186   assert (controls.size () == 8);
187   Offset d = controls[3] - controls[0];
188   
189   Real dx = d[X_AXIS];
190   Real dy = d[Y_AXIS];
191
192   Atom a;
193   a.font_ = font_name_;
194   a.dim_[X_AXIS] = Interval (0, dx);
195   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
196
197   SCM sc[4];
198   for (int i=0; i<  4; i++)
199     {
200       sc[i] =  offset2scm (controls[i]);
201     }
202
203   a.lambda_ = 
204     gh_list (ly_symbol ("dashed-slur"),
205              gh_double2scm (thick), 
206              gh_double2scm (dash),
207              ly_quote_scm (array_to_list (sc, 4)),
208              SCM_UNDEFINED);
209
210   a.str_ = "dashed_slur";
211   return a;
212 }
213
214 Atom
215 Lookup::dots () const
216 {
217   return afm_find (String ("dots") + String ("-") + String ("dot"));
218 }
219
220 Atom
221 Lookup::dynamic (String st) const
222 {
223   return (*symtables_p_) ("dynamics")->lookup (st);
224 }
225
226 Atom
227 Lookup::extender (Real width) const
228 {
229   Atom a = (*symtables_p_) ("param")->lookup ("extender");
230   a.lambda_ = gh_list (ly_symbol (a.str_),
231                        gh_double2scm (width),
232                        SCM_UNDEFINED);
233   a.str_ = "extender";
234   a.font_ = font_name_;
235   return a;
236 }
237
238 Atom
239 Lookup::fill (Box b) const
240 {
241   Atom a;
242   a.dim_ = b;
243   return a;
244 }
245
246 Atom
247 Lookup::flag (int j, Direction d) const
248 {
249   char c = (d == UP) ? 'u' : 'd';
250   Atom a = afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
251   return a;
252 }
253
254 void
255 Lookup::print () const
256 {
257 #ifndef NPRINT
258   DOUT << "Lookup {\n";
259   symtables_p_->print ();
260   DOUT << "}\n";
261 #endif
262 }
263
264 Atom
265 Lookup::rest (int j, bool o) const
266 {
267    return afm_find (String ("rests")
268                     + String ("-") + to_str (j) + (o ? "o" : ""));
269 }
270
271 Atom
272 Lookup::rule_symbol (Real height, Real width) const
273 {
274   Atom a;
275   a.lambda_ = gh_list (ly_symbol ("rulesym"),
276                        gh_double2scm (height),
277                        gh_double2scm (width),
278                        SCM_UNDEFINED);
279   a.dim_.x () = Interval (0, width);
280   a.dim_.y () = Interval (0, height);
281   return a;
282 }
283
284 Atom
285 Lookup::script (String str) const
286 {
287   return afm_find (String ("scripts") + String ("-") + str);
288 }
289
290 Atom
291 Lookup::special_time_signature (String s, Array<int> arr) const
292 {
293   // First guess: s contains only the signature style
294   assert (arr.size () >1);
295   String symbolname = "timesig-" + s + to_str (arr[0]) + "/" + to_str (arr[1]);
296   
297   Atom a = afm_find (symbolname, false);
298   if (!a.empty ()) 
299     return a;
300
301   // Second guess: s contains the full signature name
302   a = afm_find ("timesig-"+s, false);
303   if (!a.empty ()) 
304     return a;
305
306   // Resort to default layout with numbers
307   return time_signature (arr);
308 }
309
310 Atom
311 Lookup::stem (Real y1, Real y2) const
312 {
313   if (y1 > y2)
314     {
315       Real t = y1;
316       y1 = y2;
317       y2 = t;
318     }
319   Atom a;
320
321   a.dim_.x () = Interval (0,0);
322   a.dim_.y () = Interval (y1,y2);
323
324   Real stem_width = paper_l_->get_var ("stemthickness");
325
326   a.lambda_ = gh_list (ly_symbol ("stem"),
327                        gh_double2scm(-stem_width /2),
328                        gh_double2scm(stem_width),
329                        gh_double2scm(y2),
330                        gh_double2scm(-y1),
331                        SCM_UNDEFINED);
332
333   a.font_ = font_name_;
334   return a;
335 }
336
337 Atom
338 Lookup::streepje (int type) const
339 {
340   if (type > 2)
341     type = 2;
342
343   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
344 }
345
346 static Dict_initialiser<char const*> cmr_init[] = {
347   {"bold", "cmbx"},
348   {"dynamic", "feta-din"},
349   {"finger", "feta-nummer"},
350   {"italic", "cmti"},
351   {"roman", "cmr"},
352   {"large", "cmbx"},
353   {"Large", "cmbx"},
354   {"mark", "feta-nummer"},
355   {"nummer", "feta-nummer"},
356   {0,0}
357 };
358
359 static Dictionary<char const *> cmr_dict (cmr_init);
360
361 Atom
362 Lookup::text (String style, String text) const
363 {
364   Atom a =  (*symtables_p_) ("style")->lookup (style);
365
366   a.lambda_ = gh_list(ly_symbol (a.str_),
367                       gh_str02scm (text.ch_C()),
368                       SCM_UNDEFINED);
369   
370   Real font_w = a.dim_.x ().length ();
371   Real font_h = a.dim_.y ().length ();
372
373   if (cmr_dict.elem_b (style))
374     {
375       style = String (cmr_dict [style]) + to_str  ((int)font_h); // ugh
376     }
377   Real w = 0;
378   Adobe_font_metric* afm_l = all_fonts_global_p->find_font (style);
379   DOUT << "\nChars: ";
380   
381   for (int i = 0; i < text.length_i (); i++) 
382     {
383       if (text[i]=='\\')
384         for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
385           ;
386       else
387         {
388           int c = text[i];
389           int code = afm_l->ascii_to_metric_idx_[c];
390           if (code >=0)
391             {
392               Adobe_font_char_metric m = afm_l->char_metrics_[code];
393               w += m.B_.x ().length ();
394               DOUT << to_str (m.B_.x ().length ()) << " ";
395             }
396         }
397     }
398   DOUT << "\n" << to_str (w) << "\n";
399   a.dim_.x () = Interval (0, w);
400   a.font_ = font_name_;
401   return a;
402 }
403   
404
405 Atom
406 Lookup::time_signature (Array<int> a) const
407 {
408   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
409   s.lambda_ = gh_list (ly_symbol (s.str_),
410                        gh_int2scm (a[0]),
411                        gh_int2scm (a[1]),
412                        SCM_UNDEFINED);
413   return s;
414 }
415
416 Atom
417 Lookup::vbrace (Real &y) const
418 {
419   Atom a;
420   a.lambda_ = gh_list (ly_symbol ("pianobrace"),
421                        gh_double2scm (y),
422                        SCM_UNDEFINED
423                        );
424   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
425   a.font_ = font_name_;
426   return a;
427 }
428
429 Atom
430 Lookup::hairpin (Real width, bool decresc, bool continued) const
431 {
432   Atom a;  
433   Real height = paper_l_->staffheight_f () / 6;
434
435   String hairpin = String (decresc ? "de" : "") + "crescendo";
436   a.lambda_ = gh_list (ly_symbol (hairpin),
437                        gh_double2scm (width),
438                        gh_double2scm (height),
439                        gh_double2scm (continued ? height/2 : 0.0),
440                        SCM_UNDEFINED);
441   a.dim_.x () = Interval (0, width);
442   a.dim_.y () = Interval (-2*height, 2*height);
443   a.font_ = font_name_;
444   return a;
445 }
446
447 Atom
448 Lookup::plet (Real dy , Real dx, Direction dir) const
449 {
450   Atom a;
451   a.lambda_ = gh_list(ly_symbol ("tuplet"),
452                       gh_double2scm (dx),
453                       gh_double2scm (dy),
454                       gh_int2scm (dir), SCM_UNDEFINED);
455   return a;
456 }
457
458
459 Atom
460 Lookup::slur (Array<Offset> controls) const
461 {
462   assert (controls.size () == 8);
463   Real dx = controls[3].x () - controls[0].x ();
464   Real dy = controls[3].y () - controls[0].y ();
465   Atom a;
466
467   SCM scontrols [8];
468   int indices[] = {5,6,7,4,1,2,3,0};
469
470   for (int i= 0; i < 8; i++)
471     scontrols[i] = offset2scm (controls[indices[i]]);
472
473
474   a.lambda_ =gh_list (ly_symbol ("slur"),
475                       ly_quote_scm (array_to_list (scontrols, 8)),
476                       SCM_UNDEFINED);
477
478   a.dim_[X_AXIS] = Interval (0, dx);
479   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
480   a.font_ = font_name_;
481   return a;
482 }
483
484 Atom
485 Lookup::vbracket (Real &y) const
486 {
487   Atom a;
488   a.lambda_ =  gh_list (ly_symbol ("bracket"),
489                         gh_double2scm (y),
490                         SCM_UNDEFINED);
491   a.str_ = "vbracket";
492   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
493   a.dim_[X_AXIS] = Interval (0,4 PT);
494   return a;
495 }
496
497 Atom
498 Lookup::volta (Real w, bool last_b) const
499 {
500   Atom a;
501   a.lambda_ = gh_list (ly_symbol ("volta"),
502                        gh_double2scm (w),
503                        gh_int2scm (last_b),
504                        SCM_UNDEFINED);
505   a.str_ = "volta";
506   Real interline_f = paper_l_->interline_f ();
507
508   a.dim_[Y_AXIS] = Interval (-interline_f, interline_f);
509   a.dim_[X_AXIS] = Interval (0, w);
510   return a;
511 }
512