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