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