]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.3.19
[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--2000 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
33 Lookup::Lookup ()
34 {
35   afm_l_ = 0;  
36 }
37
38 Lookup::Lookup (Lookup const& s)
39 {
40   font_name_ = s.font_name_;
41   afm_l_ = 0;  
42 }
43
44
45 /*
46   build a ledger line for small pieces.
47  */
48 Molecule
49 Lookup::ledger_line (Interval xwid) const
50 {
51   Drul_array<Molecule> endings;
52   endings[LEFT] = afm_find ("noteheads-ledgerending");
53   Molecule * e = &endings[LEFT];
54   endings[RIGHT] = *e;
55   
56   Real thick = e->dim_[Y_AXIS].length();
57   Real len = e->dim_[X_AXIS].length () - thick;
58
59   Molecule total;
60   Direction d = LEFT;
61   do {
62     endings[d].translate_axis (xwid[d] - endings[d].dim_[X_AXIS][d], X_AXIS);
63     total.add_molecule (endings[d]);    
64   } while ((flip(&d)) != LEFT);
65
66   Real xpos = xwid [LEFT] + len;
67
68   while (xpos + len + thick /2 <= xwid[RIGHT])
69     {
70       e->translate_axis (len, X_AXIS);
71       total.add_molecule (*e);
72       xpos += len;
73     }
74
75   return total;
76 }
77
78
79
80
81 Molecule
82 Lookup::afm_find (String s, bool warn) const
83 {
84   if (!afm_l_)      
85     {
86       Lookup * me = (Lookup*)(this);
87       me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
88       if (!me->afm_l_)
89         {
90           warning (_f ("Can't find font: `%s'", font_name_));
91           warning (_f ("(search path `%s')", global_path.str ().ch_C()));
92           error (_ ("Aborting"));
93         }
94     }
95   Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
96   Molecule m;
97   if (cm.code () < 0)
98     {
99       /*
100         don't want people relying on this kind of dimension. 
101       */
102       m.set_empty (false);
103       return m;
104     }
105   
106   Atom at (gh_list (ly_symbol2scm ("char"),
107                     gh_int2scm (cm.code ()),
108                     SCM_UNDEFINED));
109
110   at.fontify (afm_l_);
111   m.dim_ = cm.dimensions();
112   m.add_atom (&at);
113   return m;
114 }
115
116 Molecule
117 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
118 {
119   SCM thick = ly_symbol2scm (("barthick_" + type).ch_C());
120   Real w = 0.0;
121   
122   if (paper_l->scope_p_->elem_b (thick))
123     {
124       w = paper_l->get_realvar (thick);
125     }
126   else
127     {
128       programming_error ("No bar thickness set ! ");
129       w = 1 PT;
130     }
131   return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
132 }
133
134   
135 Molecule
136 Lookup::bar (String str, Real h, Paper_def *paper_l) const
137 {
138   if (str == "bracket")
139     return staff_bracket (h, paper_l);
140   else if (str == "brace")
141     {
142       Real staffht  = paper_l->get_var ("staffheight");
143       return staff_brace (h,staffht);
144     }
145   Real kern = paper_l->get_var ("bar_kern");
146   Real thinkern = paper_l->get_var ("bar_thinkern");
147
148   Molecule thin = simple_bar ("thin", h, paper_l);
149   Molecule thick = simple_bar ("thick", h, paper_l);
150   Molecule colon = afm_find ("dots-repeatcolon", paper_l);  
151
152   Molecule m;
153   
154   if (str == "")
155     {
156       return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
157     }
158   if (str == "scorepostbreak")
159     {
160       return simple_bar ("score", h, paper_l);
161     }
162   else if (str == "|")
163     {
164       return thin;
165     }
166   else if (str == "|.")
167     {
168       m.add_at_edge (X_AXIS, LEFT, thick, 0);      
169       m.add_at_edge (X_AXIS, LEFT, thin, kern);
170     }
171   else if (str == ".|")
172     {
173       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
174       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
175     }
176   else if (str == ":|")
177     {
178       m.add_at_edge (X_AXIS, LEFT, thick, 0);
179       m.add_at_edge (X_AXIS, LEFT, thin, kern);
180       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
181     }
182   else if (str == "|:")
183     {
184       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
185       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
186       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
187     }
188   else if (str == ":|:")
189     {
190       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
191       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
192       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
193       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
194     }
195   else if (str == ".|.")
196     {
197       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
198       m.add_at_edge (X_AXIS, RIGHT, thick, kern);      
199     }
200   else if (str == "||")
201     {
202       m.add_at_edge (X_AXIS, RIGHT, thin, 0);
203       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);      
204     }
205
206   return m;
207 }
208
209 Molecule 
210 Lookup::beam (Real slope, Real width, Real thick) const
211 {
212   Real height = slope * width; 
213   Real min_y = (0 <? height) - thick/2;
214   Real max_y = (0 >? height) + thick/2;
215
216   
217   Molecule m;
218   Atom at
219     (gh_list (ly_symbol2scm ("beam"),
220               gh_double2scm (width),
221               gh_double2scm (slope),
222               gh_double2scm (thick),
223               SCM_UNDEFINED));
224
225   m.dim_[X_AXIS] = Interval (0, width);
226   m.dim_[Y_AXIS] = Interval (min_y, max_y);
227   m.add_atom (&at);
228   return m;
229 }
230
231
232
233 /*
234   FIXME.
235  */
236 Molecule
237 Lookup::dashed_slur (Bezier b, Real thick, Real dash) const
238 {
239   SCM l = SCM_EOL;
240   for (int i= 4; i -- ;)
241     {
242       l = gh_cons (ly_offset2scm (b.control_[i]), l);
243     }
244
245   Atom at (gh_list (ly_symbol2scm ("dashed-slur"),
246                     gh_double2scm (thick), 
247                     gh_double2scm (dash),
248                     ly_quote_scm (l),
249                     SCM_UNDEFINED));
250   Molecule m;
251   m.add_atom (&at);
252   return m;
253 }
254
255
256
257
258 Molecule
259 Lookup::fill (Box b) const
260 {
261   Molecule m;
262   m.dim_ = b;
263   return m;
264 }
265
266
267
268 Molecule
269 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
270 {
271   // First guess: s contains only the signature style
272   String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
273   
274   Molecule m = afm_find (symbolname, false);
275   if (!m.empty_b()) 
276     return m;
277
278   // Second guess: s contains the full signature name
279   m = afm_find ("timesig-"+s, false);
280   if (!m.empty_b ()) 
281     return m;
282
283   // Resort to default layout with numbers
284   return time_signature (n,d,pap);
285 }
286
287 Molecule
288 Lookup::filledbox (Box b ) const
289 {
290   Molecule m;
291   
292   Atom at  (gh_list (ly_symbol2scm ("filledbox"),
293                      gh_double2scm (-b[X_AXIS][LEFT]),
294                      gh_double2scm (b[X_AXIS][RIGHT]),                 
295                      gh_double2scm (-b[Y_AXIS][DOWN]),
296                      gh_double2scm (b[Y_AXIS][UP]),                    
297                      SCM_UNDEFINED));
298
299   m.dim_ = b;
300   m.add_atom (&at);
301   return m;
302 }
303
304 /*
305    TODO: THIS IS UGLY.  Since the user has direct access to TeX
306    strings, we try some halfbaked attempt to detect TeX trickery.
307  */
308 String
309 sanitise_TeX_string (String text)
310 {
311   int brace_count =0;
312   for (int i= 0; i < text.length_i (); i++)
313     {
314       if (text[i] == '\\')
315         continue;
316       
317       if (text[i] == '{')
318         brace_count ++;
319       else if (text[i] == '}')
320         brace_count --;
321     }
322   
323   if(brace_count)
324     {
325       warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
326
327       if (brace_count < 0)
328         {
329           text = to_str ('{', -brace_count) + text;
330         }
331       else 
332         {
333           text = text + to_str ('}', brace_count);
334         }
335     }
336     
337   return text;
338 }
339
340 /**
341    TODO!
342  */
343 String
344 sanitise_PS_string (String t)
345 {
346   return t;
347 }
348
349 /**
350
351 */
352 Molecule
353 Lookup::text (String style, String text, Paper_def *paper_l) const
354 {
355   Molecule m;
356   if (style.empty_b ())
357     style = "roman";
358   
359   int font_mag = 0;
360   Real font_h = paper_l->get_var ("font_normal");
361   if (paper_l->scope_p_->elem_b ("font_" + style))
362     {
363       font_h = paper_l->get_var ("font_" + style);
364     }
365
366
367   if (paper_l->scope_p_->elem_b ("magnification_" + style))
368     {
369       font_mag = (int)paper_l->get_var ("magnification_" + style);
370     }
371
372   /*
373     UGH.
374   */
375   SCM l = ly_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
376   if (l != SCM_BOOL_F)
377     {
378       style = ly_scm2string (gh_cdr(l)) +to_str  ((int)font_h);
379     }
380
381   
382
383   Font_metric* metric_l = 0;
384
385   if (font_mag)
386     metric_l = all_fonts_global_p->find_scaled (style, font_mag);
387   else
388     metric_l = all_fonts_global_p->find_font (style);
389   
390   
391   if (output_global_ch == "tex")
392     text = sanitise_TeX_string  (text);
393   else if (output_global_ch == "ps")
394     text = sanitise_PS_string (text);
395     
396   m.dim_ = metric_l->text_dimension (text);
397   
398   Atom at  (gh_list (ly_symbol2scm ("text"),
399                      ly_str02scm (text.ch_C()),
400                      SCM_UNDEFINED));
401   at.fontify (metric_l);
402   
403   m.add_atom (&at);
404   return m;
405 }
406   
407
408
409
410 Molecule
411 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
412 {
413   String sty = "number";
414   Molecule n (text (sty, to_str (num), paper_l));
415   Molecule d (text (sty, to_str (den), paper_l));
416   n.align_to (X_AXIS, CENTER);
417   d.align_to (X_AXIS, CENTER);
418   Molecule m;
419   if (den)
420     {
421       m.add_at_edge (Y_AXIS, UP, n, 0.0);
422       m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
423     }
424   else
425     {
426       m = n;
427       m.align_to (Y_AXIS, CENTER);
428     }
429   return m;
430 }
431
432 Molecule
433 Lookup::staff_brace (Real y, int staff_size) const
434 {
435   Molecule m;
436
437   Real step  = 1.0;
438   int minht  = 2 * staff_size;
439   int maxht = 7 *  minht;
440   int idx = int (((maxht - step) <? y - minht) / step);
441   idx = idx >? 0;
442
443
444   String nm = String ("feta-braces" + to_str (staff_size));
445   SCM e =gh_list (ly_symbol2scm ("char"), gh_int2scm (idx), SCM_UNDEFINED);
446   Atom at  (e);
447
448   at.fontify (all_fonts_global_p->find_font (nm));
449   
450   m.dim_[Y_AXIS] = Interval (-y/2,y/2);
451   m.dim_[X_AXIS] = Interval (0,0);
452   m.add_atom (&at);
453   return m;
454 }
455
456
457
458 Molecule
459 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap,
460                         Real height, Direction dir) const
461 {
462   Molecule m;
463
464   Atom at  (gh_list(ly_symbol2scm ("tuplet"),
465                     gh_double2scm (height),
466                     gh_double2scm (gap),
467                     gh_double2scm (dx),
468                     gh_double2scm (dy),
469                     gh_double2scm (thick),
470                     gh_int2scm (dir),
471                     SCM_UNDEFINED));
472   m.add_atom (&at);
473
474   return m;
475 }
476
477 /*
478   Make a smooth curve along the points 
479  */
480 Molecule
481 Lookup::slur (Bezier curve, Real curvethick, Real linethick) const
482 {
483   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
484   Bezier back = curve;
485
486   back.reverse ();
487   back.control_[1] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
488   back.control_[2] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));  
489
490   SCM scontrols[8];
491   for (int i=4; i--;)
492     scontrols[ i ] = ly_offset2scm (back.control_[i]);
493   for (int i=4 ; i--;)
494     scontrols[i+4] = ly_offset2scm (curve.control_[i]);
495
496   /*
497     Need the weird order b.o. the way PS want its arguments  
498    */
499   int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
500   SCM list = SCM_EOL;
501   for (int i= 8; i--;  )
502     {
503       list = gh_cons (scontrols[indices[i]], list);
504     }
505   
506   
507   Atom at  (gh_list (ly_symbol2scm ("bezier-sandwich"),
508                      ly_quote_scm (list),
509                      gh_double2scm (linethick),
510                      SCM_UNDEFINED));
511
512   Molecule m; 
513   m.dim_[X_AXIS] = curve.extent (X_AXIS);
514   m.dim_[Y_AXIS] = curve.extent (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 staff_space) 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(staff_space * 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(staff_space * 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(staff_space * 1.5 PT, Y_AXIS);
610           d.translate_axis(0.8 * staff_space 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(staff_space * 1.5 PT, Y_AXIS);
617           d.translate_axis(-0.8 * staff_space 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 * staff_space PT, Y_AXIS);
624           d.translate_axis(0.4 * staff_space PT, X_AXIS);
625           m.add_molecule(d);
626           d.translate_axis(-0.8 * staff_space 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 * staff_space 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(staff_space * 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(staff_space * 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(staff_space * 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(staff_space * 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(staff_space * 0.5 PT, Y_AXIS);
679           d.translate_axis(0.4 * staff_space PT, X_AXIS);
680           m.add_molecule(d);
681           d.translate_axis(-0.8 * staff_space 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(staff_space * 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(staff_space * 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(staff_space * 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(staff_space * 2 PT, Y_AXIS);
715           d.translate_axis(staff_space 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(staff_space * 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(staff_space * 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