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