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