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