]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.3.30
[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
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
47 Molecule
48 Lookup::afm_find (String s, bool warn) const
49 {
50   if (!afm_l_)      
51     {
52       Lookup * me = (Lookup*)(this);
53       me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
54       if (!me->afm_l_)
55         {
56           warning (_f ("Can't find font: `%s'", font_name_));
57           warning (_f ("(search path `%s')", global_path.str ().ch_C()));
58           error (_ ("Aborting"));
59         }
60     }
61   AFM_CharMetricInfo const *cm = afm_l_->find_char_metric (s, warn);
62
63   if (!cm)
64     {
65       Molecule m;
66       m.set_empty (false);
67       return m;
68     }
69   
70   SCM at =  (gh_list (ly_symbol2scm ("char"),
71                     gh_int2scm (cm->code),
72                     SCM_UNDEFINED));
73
74   at= fontify_atom (afm_l_,at);
75   return Molecule ( afm_bbox_to_box (cm->charBBox), at);
76 }
77
78 Molecule
79 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
80 {
81   SCM thick = ly_symbol2scm (("barthick_" + type).ch_C());
82   Real w = 0.0;
83   
84   if (paper_l->scope_p_->elem_b (thick))
85     {
86       w = paper_l->get_realvar (thick);
87     }
88   else
89     {
90       programming_error ("No bar thickness set ! ");
91       w = 1 PT;
92     }
93   return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
94 }
95
96   
97 Molecule
98 Lookup::bar (String str, Real h, Paper_def *paper_l) const
99 {
100   if (str == "bracket")
101     return staff_bracket (h, paper_l);
102   else if (str == "brace")
103     {
104       Real staffht  = paper_l->get_var ("staffheight");
105       return staff_brace (h,staffht);
106     }
107   Real kern = paper_l->get_var ("bar_kern");
108   Real thinkern = paper_l->get_var ("bar_thinkern");
109
110   Molecule thin = simple_bar ("thin", h, paper_l);
111   Molecule thick = simple_bar ("thick", h, paper_l);
112   Molecule colon = afm_find ("dots-repeatcolon", paper_l);  
113
114   Molecule m;
115   
116   if (str == "")
117     {
118       return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
119     }
120   if (str == "scorepostbreak")
121     {
122       return simple_bar ("score", h, paper_l);
123     }
124   else if (str == "|")
125     {
126       return thin;
127     }
128   else if (str == "|.")
129     {
130       m.add_at_edge (X_AXIS, LEFT, thick, 0);      
131       m.add_at_edge (X_AXIS, LEFT, thin, kern);
132     }
133   else if (str == ".|")
134     {
135       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
136       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
137     }
138   else if (str == ":|")
139     {
140       m.add_at_edge (X_AXIS, LEFT, thick, 0);
141       m.add_at_edge (X_AXIS, LEFT, thin, kern);
142       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
143     }
144   else if (str == "|:")
145     {
146       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
147       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
148       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
149     }
150   else if (str == ":|:")
151     {
152       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
153       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
154       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
155       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
156     }
157   else if (str == ".|.")
158     {
159       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
160       m.add_at_edge (X_AXIS, RIGHT, thick, kern);      
161     }
162   else if (str == "||")
163     {
164       m.add_at_edge (X_AXIS, RIGHT, thin, 0);
165       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);      
166     }
167
168   return m;
169 }
170
171 Molecule 
172 Lookup::beam (Real slope, Real width, Real thick) 
173 {
174   Real height = slope * width; 
175   Real min_y = (0 <? height) - thick/2;
176   Real max_y = (0 >? height) + thick/2;
177
178   
179
180   Box b( Interval (0, width),
181          Interval (min_y, max_y));
182
183   
184   SCM at = gh_list (ly_symbol2scm ("beam"),
185                     gh_double2scm (width),
186                     gh_double2scm (slope),
187                     gh_double2scm (thick),
188                     SCM_UNDEFINED);
189   return Molecule (b, at);
190 }
191
192
193
194 Molecule
195 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
196 {
197   SCM l = SCM_EOL;
198   // this is silly, we have array_to_scm
199   for (int i= 4; i -- ;)
200     {
201       l = gh_cons (to_scm (b.control_[i]), l);
202     }
203
204   SCM at = (gh_list (ly_symbol2scm ("dashed-slur"),
205                                gh_double2scm (thick), 
206                                gh_double2scm (dash),
207                                ly_quote_scm (l),
208                                SCM_UNDEFINED));
209
210   Box box (Interval(0,0),Interval( 0,0));
211   return   Molecule (box, at);
212 }
213
214
215
216
217 Molecule
218 Lookup::fill (Box b) 
219 {
220   Molecule m;
221   m.dim_ = b;
222   return m;
223 }
224
225
226 Molecule
227 Lookup::filledbox (Box b ) 
228 {
229   SCM  at  = (gh_list (ly_symbol2scm ("filledbox"),
230                      gh_double2scm (-b[X_AXIS][LEFT]),
231                      gh_double2scm (b[X_AXIS][RIGHT]),                 
232                      gh_double2scm (-b[Y_AXIS][DOWN]),
233                      gh_double2scm (b[Y_AXIS][UP]),                    
234                      SCM_UNDEFINED));
235
236   return Molecule ( b,at);
237 }
238
239 Molecule
240 Lookup::frame (Box b, Real thick)
241 {
242   Molecule m;
243   Direction d = LEFT;
244   Axis a = X_AXIS;
245   while (a < NO_AXES)
246     {
247       do
248         {
249           Axis o = Axis ((a+1)%NO_AXES);
250
251           Box edges;
252           edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
253           edges[o][DOWN] = b[o][DOWN] - thick/2;
254           edges[o][UP] = b[o][UP] + thick/2;      
255           
256           
257           m.add_molecule (filledbox (edges));
258         }
259       while (flip (&d) != LEFT);
260     }
261   return m;
262   
263 }
264
265
266 /*
267    TODO: THIS IS UGLY.  Since the user has direct access to TeX
268    strings, we try some halfbaked attempt to detect TeX trickery.
269  */
270 String
271 sanitise_TeX_string (String text)
272 {
273   int brace_count =0;
274   for (int i= 0; i < text.length_i (); i++)
275     {
276       if (text[i] == '\\')
277         continue;
278       
279       if (text[i] == '{')
280         brace_count ++;
281       else if (text[i] == '}')
282         brace_count --;
283     }
284   
285   if(brace_count)
286     {
287       warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
288
289       if (brace_count < 0)
290         {
291           text = to_str ('{', -brace_count) + text;
292         }
293       else 
294         {
295           text = text + to_str ('}', brace_count);
296         }
297     }
298     
299   return text;
300 }
301
302 /**
303    TODO!
304  */
305 String
306 sanitise_PS_string (String t)
307 {
308   return t;
309 }
310
311 /**
312
313 */
314 Molecule
315 Lookup::text (String style, String text, Paper_def *paper_l) 
316 {
317   if (style.empty_b ())
318     style = "roman";
319   
320   int font_mag = 0;
321   Real font_h = paper_l->get_var ("font_normal");
322   if (paper_l->scope_p_->elem_b ("font_" + style))
323     {
324       font_h = paper_l->get_var ("font_" + style);
325     }
326
327
328   if (paper_l->scope_p_->elem_b ("magnification_" + style))
329     {
330       font_mag = (int)paper_l->get_var ("magnification_" + style);
331     }
332
333   /*
334     UGH.
335   */
336   SCM l = ly_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
337   if (l != SCM_BOOL_F)
338     {
339       style = ly_scm2string (gh_cdr(l)) +to_str  ((int)font_h);
340     }
341
342   
343
344   Font_metric* metric_l = 0;
345
346   if (font_mag)
347     metric_l = all_fonts_global_p->find_scaled (style, font_mag);
348   else
349     metric_l = all_fonts_global_p->find_font (style);
350   
351   
352   if (output_global_ch == "tex")
353     text = sanitise_TeX_string  (text);
354   else if (output_global_ch == "ps")
355     text = sanitise_PS_string (text);
356     
357
358   
359   SCM at = (gh_list (ly_symbol2scm ("text"),
360                      ly_str02scm (text.ch_C()),
361                      SCM_UNDEFINED));
362   at = fontify_atom (metric_l,at);
363   return Molecule ( metric_l->text_dimension (text),
364                     at);
365 }
366   
367
368
369 Molecule
370 Lookup::staff_brace (Real y, int staff_size) 
371 {
372   // URG
373   Real step  = 1.0;
374   int minht  = 2 * staff_size;
375   int maxht = 7 *  minht;
376   int idx = int (((maxht - step) <? y - minht) / step);
377   idx = idx >? 0;
378
379   SCM l = ly_eval_str ("(style-to-cmr \"brace\")");
380   String nm = "feta-braces";
381   if (l != SCM_BOOL_F)
382     nm = ly_scm2string (gh_cdr (l));
383   nm += to_str (staff_size);
384   SCM e =gh_list (ly_symbol2scm ("char"), gh_int2scm (idx), SCM_UNDEFINED);
385   SCM at = (e);
386
387   at = fontify_atom (all_fonts_global_p->find_font (nm), at);
388   
389   Box b ( Interval (-y/2,y/2),
390            Interval (0,0));
391   return Molecule(b, at);
392 }
393  
394
395 /*
396   Make a smooth curve along the points 
397  */
398 Molecule
399 Lookup::slur (Bezier curve, Real curvethick, Real linethick) 
400 {
401   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
402   Bezier back = curve;
403
404   back.reverse ();
405   back.control_[1] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
406   back.control_[2] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));  
407
408   SCM scontrols[8];
409   // this is silly, we have array_to_scm
410   for (int i=4; i--;)
411     scontrols[ i ] = to_scm (back.control_[i]);
412   for (int i=4 ; i--;)
413     scontrols[i+4] = to_scm (curve.control_[i]);
414
415   /*
416     Need the weird order b.o. the way PS want its arguments  
417    */
418   int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
419   SCM list = SCM_EOL;
420   for (int i= 8; i--;  )
421     {
422       list = gh_cons (scontrols[indices[i]], list);
423     }
424   
425   
426   SCM at = (gh_list (ly_symbol2scm ("bezier-sandwich"),
427                      ly_quote_scm (list),
428                      gh_double2scm (linethick),
429                      SCM_UNDEFINED));
430
431   Box b ( curve.extent (X_AXIS), curve.extent (Y_AXIS));
432   return Molecule (b, at);
433 }
434
435 Molecule
436 Lookup::staff_bracket (Real height, Paper_def* paper_l)
437 {
438   SCM at = ( gh_list (ly_symbol2scm ("bracket"),
439                       gh_double2scm (paper_l->get_var("bracket_arch_angle")),
440                       gh_double2scm (paper_l->get_var("bracket_arch_width")),
441                       gh_double2scm (paper_l->get_var("bracket_arch_height")),
442                       gh_double2scm (paper_l->get_var("bracket_width")),
443                       gh_double2scm (height),
444                       gh_double2scm (paper_l->get_var("bracket_arch_thick")),
445                       gh_double2scm (paper_l->get_var("bracket_thick")),
446                       SCM_UNDEFINED));
447
448   Box b ( Interval (-height/2,height/2), Interval (0,4 PT));
449   Molecule m (b, at);
450
451   m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
452   return m;
453 }
454
455
456 Molecule
457 Lookup::accordion (SCM s, Real staff_space) const
458 {
459   Molecule m;
460   String sym = ly_scm2string(gh_car (s));
461   String reg = ly_scm2string(gh_car (gh_cdr(s)));
462
463   if (sym == "Discant")
464     {
465       Molecule r = afm_find("scripts-accDiscant");
466       m.add_molecule(r);
467       if (reg.left_str(1) == "F")
468         {
469           Molecule d = afm_find("scripts-accDot");
470           d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
471           m.add_molecule(d);
472           reg = reg.right_str(reg.length_i()-1);
473         }
474       int eflag = 0x00;
475       if (reg.left_str(3) == "EEE")
476         {
477           eflag = 0x07;
478           reg = reg.right_str(reg.length_i()-3);
479         }
480       else if (reg.left_str(2) == "EE")
481         {
482           eflag = 0x05;
483           reg = reg.right_str(reg.length_i()-2);
484         }
485       else if (reg.left_str(2) == "Eh")
486         {
487           eflag = 0x04;
488           reg = reg.right_str(reg.length_i()-2);
489         }
490       else if (reg.left_str(1) == "E")
491         {
492           eflag = 0x02;
493           reg = reg.right_str(reg.length_i()-1);
494         }
495       if (eflag & 0x02)
496         {
497           Molecule d = afm_find("scripts-accDot");
498           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
499           m.add_molecule(d);
500         }
501       if (eflag & 0x04)
502         {
503           Molecule d = afm_find("scripts-accDot");
504           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
505           d.translate_axis(0.8 * staff_space PT, X_AXIS);
506           m.add_molecule(d);
507         }
508       if (eflag & 0x01)
509         {
510           Molecule d = afm_find("scripts-accDot");
511           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
512           d.translate_axis(-0.8 * staff_space PT, X_AXIS);
513           m.add_molecule(d);
514         }
515       if (reg.left_str(2) == "SS")
516         {
517           Molecule d = afm_find("scripts-accDot");
518           d.translate_axis(0.5 * staff_space PT, Y_AXIS);
519           d.translate_axis(0.4 * staff_space PT, X_AXIS);
520           m.add_molecule(d);
521           d.translate_axis(-0.8 * staff_space PT, X_AXIS);
522           m.add_molecule(d);
523           reg = reg.right_str(reg.length_i()-2);
524         }
525       if (reg.left_str(1) == "S")
526         {
527           Molecule d = afm_find("scripts-accDot");
528           d.translate_axis(0.5 * staff_space PT, Y_AXIS);
529           m.add_molecule(d);
530           reg = reg.right_str(reg.length_i()-1);
531         }
532     }
533   else if (sym == "Freebase")
534     {
535       Molecule r = afm_find("scripts-accFreebase");
536       m.add_molecule(r);
537       if (reg.left_str(1) == "F")
538         {
539           Molecule d = afm_find("scripts-accDot");
540           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
541           m.add_molecule(d);
542           reg = reg.right_str(reg.length_i()-1);
543         }
544       if (reg == "E")
545         {
546           Molecule d = afm_find("scripts-accDot");
547           d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
548           m.add_molecule(d);
549         }
550     }
551   else if (sym == "Bayanbase")
552     {
553       Molecule r = afm_find("scripts-accBayanbase");
554       m.add_molecule(r);
555       if (reg.left_str(1) == "T")
556         {
557           Molecule d = afm_find("scripts-accDot");
558           d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
559           m.add_molecule(d);
560           reg = reg.right_str(reg.length_i()-1);
561         }
562       /* include 4' reed just for completeness. You don't want to use this. */
563       if (reg.left_str(1) == "F")
564         {
565           Molecule d = afm_find("scripts-accDot");
566           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
567           m.add_molecule(d);
568           reg = reg.right_str(reg.length_i()-1);
569         }
570       if (reg.left_str(2) == "EE")
571         {
572           Molecule d = afm_find("scripts-accDot");
573           d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
574           d.translate_axis(0.4 * staff_space PT, X_AXIS);
575           m.add_molecule(d);
576           d.translate_axis(-0.8 * staff_space PT, X_AXIS);
577           m.add_molecule(d);
578           reg = reg.right_str(reg.length_i()-2);
579         }
580       if (reg.left_str(1) == "E")
581         {
582           Molecule d = afm_find("scripts-accDot");
583           d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
584           m.add_molecule(d);
585           reg = reg.right_str(reg.length_i()-1);
586         }
587     }
588   else if (sym == "Stdbase")
589     {
590       Molecule r = afm_find("scripts-accStdbase");
591       m.add_molecule(r);
592       if (reg.left_str(1) == "T")
593         {
594           Molecule d = afm_find("scripts-accDot");
595           d.translate_axis(staff_space * 3.5 PT, Y_AXIS);
596           m.add_molecule(d);
597           reg = reg.right_str(reg.length_i()-1);
598         }
599       if (reg.left_str(1) == "F")
600         {
601           Molecule d = afm_find("scripts-accDot");
602           d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
603           m.add_molecule(d);
604           reg = reg.right_str(reg.length_i()-1);
605         }
606       if (reg.left_str(1) == "M")
607         {
608           Molecule d = afm_find("scripts-accDot");
609           d.translate_axis(staff_space * 2 PT, Y_AXIS);
610           d.translate_axis(staff_space PT, X_AXIS);
611           m.add_molecule(d);
612           reg = reg.right_str(reg.length_i()-1);
613         }
614       if (reg.left_str(1) == "E")
615         {
616           Molecule d = afm_find("scripts-accDot");
617           d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
618           m.add_molecule(d);
619           reg = reg.right_str(reg.length_i()-1);
620         }
621       if (reg.left_str(1) == "S")
622         {
623           Molecule d = afm_find("scripts-accDot");
624           d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
625           m.add_molecule(d);
626           reg = reg.right_str(reg.length_i()-1);
627         }
628     }
629   /* ugh maybe try to use regular font for S.B. and B.B and only use one font
630      for the rectangle */
631   else if (sym == "SB")
632     {
633       Molecule r = afm_find("scripts-accSB");
634       m.add_molecule(r);
635     }
636   else if (sym == "BB")
637     {
638       Molecule r = afm_find("scripts-accBB");
639       m.add_molecule(r);
640     }
641   else if (sym == "OldEE")
642     {
643       Molecule r = afm_find("scripts-accOldEE");
644       m.add_molecule(r);
645     }
646   else if (sym == "OldEES")
647     {
648       Molecule r = afm_find("scripts-accOldEES");
649       m.add_molecule(r);
650     }
651   return m;  
652 }
653