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