]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.3.16
[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 #include <math.h>
14 #include <ctype.h>
15 #include "lookup.hh"
16 #include "debug.hh"
17 #include "dimensions.hh"
18
19 #include "bezier.hh"
20 #include "paper-def.hh"
21 #include "string-convert.hh"
22 #include "file-path.hh"
23 #include "main.hh"
24 #include "lily-guile.hh"
25 #include "all-font-metrics.hh"
26 #include "afm.hh"
27 #include "scope.hh"
28 #include "molecule.hh"
29 #include "atom.hh"
30 #include "lily-guile.hh"
31
32 SCM
33 ly_offset2scm (Offset o)
34 {
35   return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
36                   SCM_UNDEFINED);
37 }
38
39 Lookup::Lookup ()
40 {
41   afm_l_ = 0;  
42 }
43
44 Lookup::Lookup (Lookup const& s)
45 {
46   font_name_ = s.font_name_;
47   afm_l_ = 0;  
48 }
49
50
51 /*
52   build a ledger line for small pieces.
53  */
54 Molecule
55 Lookup::ledger_line (Interval xwid) const
56 {
57   Drul_array<Molecule> endings;
58   endings[LEFT] = afm_find ("noteheads-ledgerending");
59   Molecule * e = &endings[LEFT];
60   endings[RIGHT] = *e;
61   
62   Real thick = e->dim_[Y_AXIS].length();
63   Real len = e->dim_[X_AXIS].length () - thick;
64
65   Molecule total;
66   Direction d = LEFT;
67   do {
68     endings[d].translate_axis (xwid[d] - endings[d].dim_[X_AXIS][d], X_AXIS);
69     total.add_molecule (endings[d]);    
70   } while ((flip(&d)) != LEFT);
71
72   Real xpos = xwid [LEFT] + len;
73
74   while (xpos + len + thick /2 <= xwid[RIGHT])
75     {
76       e->translate_axis (len, X_AXIS);
77       total.add_molecule (*e);
78       xpos += len;
79     }
80
81   return total;
82 }
83
84
85
86
87 Molecule
88 Lookup::afm_find (String s, bool warn) const
89 {
90   if (!afm_l_)      
91     {
92       Lookup * me = (Lookup*)(this);
93       me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
94       if (!me->afm_l_)
95         {
96           warning (_f ("Can't find font: `%s'", font_name_));
97           warning (_f ("(search path: `%s')", global_path.str ().ch_C()));
98           error (_ ("Aborting"));
99         }
100     }
101   Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
102   Molecule m;
103   if (cm.code () < 0)
104     {
105       /*
106         don't want people relying on this kind of dimension. 
107       */
108       m.set_empty (false);
109       return m;
110     }
111   
112   Atom at (gh_list (ly_symbol2scm ("char"),
113                     gh_int2scm (cm.code ()),
114                     SCM_UNDEFINED));
115
116   at.fontify (afm_l_);
117   m.dim_ = cm.dimensions();
118   m.add_atom (&at);
119   return m;
120 }
121
122 Molecule
123 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
124 {
125   SCM thick = ly_symbol2scm (("barthick_" + type).ch_C());
126   Real w = 0.0;
127   
128   if (paper_l->scope_p_->elem_b (thick))
129     {
130       w = paper_l->get_realvar (thick);
131     }
132   else
133     {
134       programming_error ("No bar thickness set ! ");
135       w = 1 PT;
136     }
137   return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
138 }
139
140   
141 Molecule
142 Lookup::bar (String str, Real h, Paper_def *paper_l) const
143 {
144   if (str == "bracket")
145     return staff_bracket (h, paper_l);
146   else if (str == "brace")
147     {
148       Real staffht  = paper_l->get_var ("staffheight");
149       return staff_brace (h,staffht);
150     }
151   Real kern = paper_l->get_var ("bar_kern");
152   Real thinkern = paper_l->get_var ("bar_thinkern");
153
154   Molecule thin = simple_bar ("thin", h, paper_l);
155   Molecule thick = simple_bar ("thick", h, paper_l);
156   Molecule colon = afm_find ("dots-repeatcolon", paper_l);  
157
158   Molecule m;
159   
160   if (str == "")
161     {
162       return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
163     }
164   if (str == "scorepostbreak")
165     {
166       return simple_bar ("score", h, paper_l);
167     }
168   else if (str == "|")
169     {
170       return thin;
171     }
172   else if (str == "|.")
173     {
174       m.add_at_edge (X_AXIS, LEFT, thick, 0);      
175       m.add_at_edge (X_AXIS, LEFT, thin, kern);
176     }
177   else if (str == ".|")
178     {
179       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
180       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
181     }
182   else if (str == ":|")
183     {
184       m.add_at_edge (X_AXIS, LEFT, thick, 0);
185       m.add_at_edge (X_AXIS, LEFT, thin, kern);
186       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
187     }
188   else if (str == "|:")
189     {
190       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
191       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
192       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
193     }
194   else if (str == ":|:")
195     {
196       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
197       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
198       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
199       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
200     }
201   else if (str == ".|.")
202     {
203       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
204       m.add_at_edge (X_AXIS, RIGHT, thick, kern);      
205     }
206   else if (str == "||")
207     {
208       m.add_at_edge (X_AXIS, RIGHT, thin, 0);
209       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);      
210     }
211
212   return m;
213 }
214
215 Molecule 
216 Lookup::beam (Real slope, Real width, Real thick) const
217 {
218   Real height = slope * width; 
219   Real min_y = (0 <? height) - thick/2;
220   Real max_y = (0 >? height) + thick/2;
221
222   
223   Molecule m;
224   Atom at
225     (gh_list (ly_symbol2scm ("beam"),
226               gh_double2scm (width),
227               gh_double2scm (slope),
228               gh_double2scm (thick),
229               SCM_UNDEFINED));
230
231   m.dim_[X_AXIS] = Interval (0, width);
232   m.dim_[Y_AXIS] = Interval (min_y, max_y);
233   m.add_atom (&at);
234   return m;
235 }
236
237
238
239 /*
240   FIXME.
241  */
242 Molecule
243 Lookup::dashed_slur (Bezier b, Real thick, Real dash) const
244 {
245   SCM l = SCM_EOL;
246   for (int i= 4; i -- ;)
247     {
248       l = gh_cons (ly_offset2scm (b.control_[i]), l);
249     }
250
251   Atom at (gh_list (ly_symbol2scm ("dashed-slur"),
252                     gh_double2scm (thick), 
253                     gh_double2scm (dash),
254                     ly_quote_scm (l),
255                     SCM_UNDEFINED));
256   Molecule m;
257   m.add_atom (&at);
258   return m;
259 }
260
261
262
263
264 Molecule
265 Lookup::fill (Box b) const
266 {
267   Molecule m;
268   m.dim_ = b;
269   return m;
270 }
271
272
273
274 Molecule
275 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
276 {
277   // First guess: s contains only the signature style
278   String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
279   
280   Molecule m = afm_find (symbolname, false);
281   if (!m.empty_b()) 
282     return m;
283
284   // Second guess: s contains the full signature name
285   m = afm_find ("timesig-"+s, false);
286   if (!m.empty_b ()) 
287     return m;
288
289   // Resort to default layout with numbers
290   return time_signature (n,d,pap);
291 }
292
293 Molecule
294 Lookup::filledbox (Box b ) const
295 {
296   Molecule m;
297   
298   Atom at  (gh_list (ly_symbol2scm ("filledbox"),
299                      gh_double2scm (-b[X_AXIS][LEFT]),
300                      gh_double2scm (b[X_AXIS][RIGHT]),                 
301                      gh_double2scm (-b[Y_AXIS][DOWN]),
302                      gh_double2scm (b[Y_AXIS][UP]),                    
303                      SCM_UNDEFINED));
304
305   m.dim_ = b;
306   m.add_atom (&at);
307   return m;
308 }
309
310 /*
311    TODO: THIS IS UGLY.  Since the user has direct access to TeX
312    strings, we try some halfbaked attempt to detect TeX trickery.
313  */
314 String
315 sanitise_TeX_string (String text)
316 {
317   int brace_count =0;
318   for (int i= 0; i < text.length_i (); i++)
319     {
320       if (text[i] == '\\')
321         continue;
322       
323       if (text[i] == '{')
324         brace_count ++;
325       else if (text[i] == '}')
326         brace_count --;
327     }
328   
329   if(brace_count)
330     {
331       warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
332
333       if (brace_count < 0)
334         {
335           text = to_str ('{', -brace_count) + text;
336         }
337       else 
338         {
339           text = text + to_str ('}', brace_count);
340         }
341     }
342     
343   return text;
344 }
345
346 /**
347    TODO!
348  */
349 String
350 sanitise_PS_string (String t)
351 {
352   return t;
353 }
354
355 /**
356
357 */
358 Molecule
359 Lookup::text (String style, String text, Paper_def *paper_l) const
360 {
361   Molecule m;
362   if (style.empty_b ())
363     style = "roman";
364   
365   int font_mag = 0;
366   Real font_h = paper_l->get_var ("font_normal");
367   if (paper_l->scope_p_->elem_b ("font_" + style))
368     {
369       font_h = paper_l->get_var ("font_" + style);
370     }
371
372
373   if (paper_l->scope_p_->elem_b ("magnification_" + style))
374     {
375       font_mag = (int)paper_l->get_var ("magnification_" + style);
376     }
377
378   /*
379     UGH.
380   */
381   SCM l = ly_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
382   if (l != SCM_BOOL_F)
383     {
384       style = ly_scm2string (gh_cdr(l)) +to_str  ((int)font_h);
385     }
386
387   
388
389   Font_metric* metric_l = 0;
390
391   if (font_mag)
392     metric_l = all_fonts_global_p->find_scaled (style, font_mag);
393   else
394     metric_l = all_fonts_global_p->find_font (style);
395   
396   
397   if (output_global_ch == "tex")
398     text = sanitise_TeX_string  (text);
399   else if (output_global_ch == "ps")
400     text = sanitise_PS_string (text);
401     
402   m.dim_ = metric_l->text_dimension (text);
403   
404   Atom at  (gh_list (ly_symbol2scm ("text"),
405                      ly_str02scm (text.ch_C()),
406                      SCM_UNDEFINED));
407   at.fontify (metric_l);
408   
409   m.add_atom (&at);
410   return m;
411 }
412   
413
414
415
416 Molecule
417 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
418 {
419   String sty = "number";
420   Molecule n (text (sty, to_str (num), paper_l));
421   Molecule d (text (sty, to_str (den), paper_l));
422   n.align_to (X_AXIS, CENTER);
423   d.align_to (X_AXIS, CENTER);
424   Molecule m;
425   if (den)
426     {
427       m.add_at_edge (Y_AXIS, UP, n, 0.0);
428       m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
429     }
430   else
431     {
432       m = n;
433       m.align_to (Y_AXIS, CENTER);
434     }
435   return m;
436 }
437
438 Molecule
439 Lookup::staff_brace (Real y, int staff_size) const
440 {
441   Molecule m;
442
443   Real step  = 1.0;
444   int minht  = 2 * staff_size;
445   int maxht = 7 *  minht;
446   int idx = int (((maxht - step) <? y - minht) / step);
447   idx = idx >? 0;
448
449
450   String nm = String ("feta-braces" + to_str (staff_size));
451   SCM e =gh_list (ly_symbol2scm ("char"), gh_int2scm (idx), SCM_UNDEFINED);
452   Atom at  (e);
453
454   at.fontify (all_fonts_global_p->find_font (nm));
455   
456   m.dim_[Y_AXIS] = Interval (-y/2,y/2);
457   m.dim_[X_AXIS] = Interval (0,0);
458   m.add_atom (&at);
459   return m;
460 }
461
462
463
464 Molecule
465 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap,
466                         Real height, Direction dir) const
467 {
468   Molecule m;
469
470   Atom at  (gh_list(ly_symbol2scm ("tuplet"),
471                     gh_double2scm (height),
472                     gh_double2scm (gap),
473                     gh_double2scm (dx),
474                     gh_double2scm (dy),
475                     gh_double2scm (thick),
476                     gh_int2scm (dir),
477                     SCM_UNDEFINED));
478   m.add_atom (&at);
479
480   return m;
481 }
482
483 /*
484   Make a smooth curve along the points 
485  */
486 Molecule
487 Lookup::slur (Bezier curve, Real curvethick, Real linethick) const
488 {
489   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
490   Bezier back = curve;
491
492   back.reverse ();
493   back.control_[1] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
494   back.control_[2] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));  
495
496   SCM scontrols[8];
497   for (int i=4; i--;)
498     scontrols[ i ] = ly_offset2scm (back.control_[i]);
499   for (int i=4 ; i--;)
500     scontrols[i+4] = ly_offset2scm (curve.control_[i]);
501
502   /*
503     Need the weird order b.o. the way PS want its arguments  
504    */
505   int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
506   SCM list = SCM_EOL;
507   for (int i= 8; i--;  )
508     {
509       list = gh_cons (scontrols[indices[i]], list);
510     }
511   
512   
513   Atom at  (gh_list (ly_symbol2scm ("bezier-sandwich"),
514                      ly_quote_scm (list),
515                      gh_double2scm (linethick),
516                      SCM_UNDEFINED));
517
518   Molecule m; 
519   m.dim_[X_AXIS] = curve.extent (X_AXIS);
520   m.dim_[Y_AXIS] = curve.extent (Y_AXIS);
521   m.add_atom (&at);
522   return m;
523 }
524
525 Molecule
526 Lookup::staff_bracket (Real height, Paper_def* paper_l) const
527 {
528   Molecule m;
529   Atom at  ( gh_list (ly_symbol2scm ("bracket"),
530                       gh_double2scm (paper_l->get_var("bracket_arch_angle")),
531                       gh_double2scm (paper_l->get_var("bracket_arch_width")),
532                       gh_double2scm (paper_l->get_var("bracket_arch_height")),
533                       gh_double2scm (paper_l->get_var("bracket_width")),
534                       gh_double2scm (height),
535                       gh_double2scm (paper_l->get_var("bracket_arch_thick")),
536                       gh_double2scm (paper_l->get_var("bracket_thick")),
537                       SCM_UNDEFINED));
538   
539   m.add_atom (&at);                              
540   m.dim_[Y_AXIS] = Interval (-height/2,height/2);
541   m.dim_[X_AXIS] = Interval (0,4 PT);
542
543   m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
544   return m;
545 }
546
547 Molecule
548 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
549 {
550   Molecule m; 
551
552   Atom at  (gh_list (ly_symbol2scm ("volta"),
553                      gh_double2scm (h),
554                      gh_double2scm (w),
555                      gh_double2scm (thick),
556                      gh_int2scm (vert_start),
557                      gh_int2scm (vert_end),
558                      SCM_UNDEFINED));
559
560   m.dim_[Y_AXIS] = Interval (- h/2, h/2);
561   m.dim_[X_AXIS] = Interval (0, w);
562
563   m.add_atom (&at);
564   return m;
565 }
566
567 Molecule
568 Lookup::accordion (SCM s, Real staff_space) const
569 {
570   Molecule m;
571   String sym = ly_scm2string(gh_car (s));
572   String reg = ly_scm2string(gh_car (gh_cdr(s)));
573
574   if (sym == "Discant")
575     {
576       Molecule r = afm_find("scripts-accDiscant");
577       m.add_molecule(r);
578       if (reg.left_str(1) == "F")
579         {
580           Molecule d = afm_find("scripts-accDot");
581           d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
582           m.add_molecule(d);
583           reg = reg.right_str(reg.length_i()-1);
584         }
585       int eflag = 0x00;
586       if (reg.left_str(3) == "EEE")
587         {
588           eflag = 0x07;
589           reg = reg.right_str(reg.length_i()-3);
590         }
591       else if (reg.left_str(2) == "EE")
592         {
593           eflag = 0x05;
594           reg = reg.right_str(reg.length_i()-2);
595         }
596       else if (reg.left_str(2) == "Eh")
597         {
598           eflag = 0x04;
599           reg = reg.right_str(reg.length_i()-2);
600         }
601       else if (reg.left_str(1) == "E")
602         {
603           eflag = 0x02;
604           reg = reg.right_str(reg.length_i()-1);
605         }
606       if (eflag & 0x02)
607         {
608           Molecule d = afm_find("scripts-accDot");
609           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
610           m.add_molecule(d);
611         }
612       if (eflag & 0x04)
613         {
614           Molecule d = afm_find("scripts-accDot");
615           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
616           d.translate_axis(0.8 * staff_space PT, X_AXIS);
617           m.add_molecule(d);
618         }
619       if (eflag & 0x01)
620         {
621           Molecule d = afm_find("scripts-accDot");
622           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
623           d.translate_axis(-0.8 * staff_space PT, X_AXIS);
624           m.add_molecule(d);
625         }
626       if (reg.left_str(2) == "SS")
627         {
628           Molecule d = afm_find("scripts-accDot");
629           d.translate_axis(0.5 * staff_space PT, Y_AXIS);
630           d.translate_axis(0.4 * staff_space PT, X_AXIS);
631           m.add_molecule(d);
632           d.translate_axis(-0.8 * staff_space PT, X_AXIS);
633           m.add_molecule(d);
634           reg = reg.right_str(reg.length_i()-2);
635         }
636       if (reg.left_str(1) == "S")
637         {
638           Molecule d = afm_find("scripts-accDot");
639           d.translate_axis(0.5 * staff_space PT, Y_AXIS);
640           m.add_molecule(d);
641           reg = reg.right_str(reg.length_i()-1);
642         }
643     }
644   else if (sym == "Freebase")
645     {
646       Molecule r = afm_find("scripts-accFreebase");
647       m.add_molecule(r);
648       if (reg.left_str(1) == "F")
649         {
650           Molecule d = afm_find("scripts-accDot");
651           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
652           m.add_molecule(d);
653           reg = reg.right_str(reg.length_i()-1);
654         }
655       if (reg == "E")
656         {
657           Molecule d = afm_find("scripts-accDot");
658           d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
659           m.add_molecule(d);
660         }
661     }
662   else if (sym == "Bayanbase")
663     {
664       Molecule r = afm_find("scripts-accBayanbase");
665       m.add_molecule(r);
666       if (reg.left_str(1) == "T")
667         {
668           Molecule d = afm_find("scripts-accDot");
669           d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
670           m.add_molecule(d);
671           reg = reg.right_str(reg.length_i()-1);
672         }
673       /* include 4' reed just for completeness. You don't want to use this. */
674       if (reg.left_str(1) == "F")
675         {
676           Molecule d = afm_find("scripts-accDot");
677           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
678           m.add_molecule(d);
679           reg = reg.right_str(reg.length_i()-1);
680         }
681       if (reg.left_str(2) == "EE")
682         {
683           Molecule d = afm_find("scripts-accDot");
684           d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
685           d.translate_axis(0.4 * staff_space PT, X_AXIS);
686           m.add_molecule(d);
687           d.translate_axis(-0.8 * staff_space PT, X_AXIS);
688           m.add_molecule(d);
689           reg = reg.right_str(reg.length_i()-2);
690         }
691       if (reg.left_str(1) == "E")
692         {
693           Molecule d = afm_find("scripts-accDot");
694           d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
695           m.add_molecule(d);
696           reg = reg.right_str(reg.length_i()-1);
697         }
698     }
699   else if (sym == "Stdbase")
700     {
701       Molecule r = afm_find("scripts-accStdbase");
702       m.add_molecule(r);
703       if (reg.left_str(1) == "T")
704         {
705           Molecule d = afm_find("scripts-accDot");
706           d.translate_axis(staff_space * 3.5 PT, Y_AXIS);
707           m.add_molecule(d);
708           reg = reg.right_str(reg.length_i()-1);
709         }
710       if (reg.left_str(1) == "F")
711         {
712           Molecule d = afm_find("scripts-accDot");
713           d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
714           m.add_molecule(d);
715           reg = reg.right_str(reg.length_i()-1);
716         }
717       if (reg.left_str(1) == "M")
718         {
719           Molecule d = afm_find("scripts-accDot");
720           d.translate_axis(staff_space * 2 PT, Y_AXIS);
721           d.translate_axis(staff_space PT, X_AXIS);
722           m.add_molecule(d);
723           reg = reg.right_str(reg.length_i()-1);
724         }
725       if (reg.left_str(1) == "E")
726         {
727           Molecule d = afm_find("scripts-accDot");
728           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
729           m.add_molecule(d);
730           reg = reg.right_str(reg.length_i()-1);
731         }
732       if (reg.left_str(1) == "S")
733         {
734           Molecule d = afm_find("scripts-accDot");
735           d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
736           m.add_molecule(d);
737           reg = reg.right_str(reg.length_i()-1);
738         }
739     }
740   /* ugh maybe try to use regular font for S.B. and B.B and only use one font
741      for the rectangle */
742   else if (sym == "SB")
743     {
744       Molecule r = afm_find("scripts-accSB");
745       m.add_molecule(r);
746     }
747   else if (sym == "BB")
748     {
749       Molecule r = afm_find("scripts-accBB");
750       m.add_molecule(r);
751     }
752   else if (sym == "OldEE")
753     {
754       Molecule r = afm_find("scripts-accOldEE");
755       m.add_molecule(r);
756     }
757   else if (sym == "OldEES")
758     {
759       Molecule r = afm_find("scripts-accOldEES");
760       m.add_molecule(r);
761     }
762   return m;  
763 }
764