lilypond-1.3.7
[lilypond.git] / lily / lookup.cc
blob35bdc323a3883c39e9e3a3d7f8123725dbb4f4b5
1 /*
2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
10 TODO
11 Glissando
13 #include <math.h>
14 #include <ctype.h>
15 #include "lookup.hh"
16 #include "debug.hh"
17 #include "dimensions.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-font-metrics.hh"
25 #include "afm.hh"
26 #include "scope.hh"
27 #include "molecule.hh"
28 #include "atom.hh"
29 #include "lily-guile.hh"
32 Lookup::Lookup ()
34 afm_l_ = 0;
37 Lookup::Lookup (Lookup const& s)
39 font_name_ = s.font_name_;
40 afm_l_ = 0;
45 build a ledger line for small pieces.
47 Molecule
48 Lookup::ledger_line (Interval xwid) const
50 Drul_array<Molecule> endings;
51 endings[LEFT] = afm_find ("noteheads-ledgerending");
52 Molecule * e = &endings[LEFT];
53 endings[RIGHT] = *e;
55 Real thick = e->dim_[Y_AXIS].length();
56 Real len = e->dim_[X_AXIS].length () - thick;
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);
65 Real xpos = xwid [LEFT] + len;
67 while (xpos + len + thick /2 <= xwid[RIGHT])
69 e->translate_axis (len, X_AXIS);
70 total.add_molecule (*e);
71 xpos += len;
74 return total;
80 Molecule
81 Lookup::afm_find (String s, bool warn) const
83 if (!afm_l_)
85 Lookup * me = (Lookup*)(this);
86 me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
87 if (!me->afm_l_)
89 warning (_f ("Can't find font: `%s'", font_name_));
90 warning (_f ("(search path: `%s')", global_path.str ().ch_C()));
91 error (_ ("Aborting"));
94 Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
95 Molecule m;
96 if (cm.code () < 0)
99 don't want people relying on this kind of dimension.
101 m.set_empty (false);
102 return m;
105 Atom at (gh_list (ly_symbol2scm ("char"),
106 gh_int2scm (cm.code ()),
107 SCM_UNDEFINED));
109 at.fontify (afm_l_);
110 m.dim_ = cm.dimensions();
111 m.add_atom (&at);
112 return m;
115 Molecule
116 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
118 SCM thick = ly_symbol2scm (("barthick_" + type).ch_C());
119 Real w = 0.0;
121 if (paper_l->scope_p_->elem_b (thick))
123 w = paper_l->get_realvar (thick);
125 else
127 programming_error ("No bar thickness set ! ");
128 w = 1 PT;
130 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
134 Molecule
135 Lookup::bar (String str, Real h, Paper_def *paper_l) const
137 if (str == "bracket")
138 return staff_bracket (h, paper_l);
139 else if (str == "brace")
141 Real staffht = paper_l->get_var ("staffheight");
142 return staff_brace (h,staffht);
144 Real kern = paper_l->get_var ("bar_kern");
145 Real thinkern = paper_l->get_var ("bar_thinkern");
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);
151 Molecule m;
153 if (str == "")
155 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
157 if (str == "scorepostbreak")
159 return simple_bar ("score", h, paper_l);
161 else if (str == "|")
163 return thin;
165 else if (str == "|.")
167 m.add_at_edge (X_AXIS, LEFT, thick, 0);
168 m.add_at_edge (X_AXIS, LEFT, thin, kern);
170 else if (str == ".|")
172 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
173 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
175 else if (str == ":|")
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);
181 else if (str == "|:")
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);
187 else if (str == ":|:")
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);
194 else if (str == ".|.")
196 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
197 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
199 else if (str == "||")
201 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
202 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
205 return m;
208 Molecule
209 Lookup::beam (Real slope, Real width, Real thick) const
211 Real height = slope * width;
212 Real min_y = (0 <? height) - thick/2;
213 Real max_y = (0 >? height) + thick/2;
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));
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;
231 offset2scm (Offset o)
233 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
234 SCM_UNDEFINED);
237 Molecule
238 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
240 assert (controls.size () == 8);
241 Offset d = controls[3] - controls[0];
243 Real dx = d[X_AXIS];
244 Real dy = d[Y_AXIS];
246 Molecule m;
249 m.dim_[X_AXIS] = Interval (0, dx);
250 m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
252 SCM sc[4];
253 for (int i=0; i< 4; i++)
255 sc[i] = offset2scm (controls[i]);
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));
266 m.add_atom (&at);
267 return m;
273 Molecule
274 Lookup::fill (Box b) const
276 Molecule m;
277 m.dim_ = b;
278 return m;
283 Molecule
284 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
286 // First guess: s contains only the signature style
287 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
289 Molecule m = afm_find (symbolname, false);
290 if (!m.empty_b())
291 return m;
293 // Second guess: s contains the full signature name
294 m = afm_find ("timesig-"+s, false);
295 if (!m.empty_b ())
296 return m;
298 // Resort to default layout with numbers
299 return time_signature (n,d,pap);
302 Molecule
303 Lookup::filledbox (Box b ) const
305 Molecule m;
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));
314 m.dim_ = b;
315 m.add_atom (&at);
316 return m;
320 TODO: THIS IS UGLY. Since the user has direct access to TeX
321 strings, we try some halfbaked attempt to detect TeX trickery.
323 String
324 sanitise_TeX_string (String text)
326 int brace_count =0;
327 for (int i= 0; i < text.length_i (); i++)
329 if (text[i] == '\\')
330 continue;
332 if (text[i] == '{')
333 brace_count ++;
334 else if (text[i] == '}')
335 brace_count --;
338 if(brace_count)
340 warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
342 if (brace_count < 0)
344 text = to_str ('{', -brace_count) + text;
346 else
348 text = text + to_str ('}', brace_count);
352 return text;
356 TODO!
358 String
359 sanitise_PS_string (String t)
361 return t;
367 Molecule
368 Lookup::text (String style, String text, Paper_def *paper_l) const
370 Molecule m;
371 if (style.empty_b ())
372 style = "roman";
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))
378 font_h = paper_l->get_var ("font_" + style);
382 if (paper_l->scope_p_->elem_b ("magnification_" + style))
384 font_mag = (int)paper_l->get_var ("magnification_" + style);
388 UGH.
390 SCM l = ly_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
391 if (l != SCM_BOOL_F)
393 style = ly_scm2string (gh_cdr(l)) +to_str ((int)font_h);
398 Font_metric* metric_l = 0;
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);
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);
411 m.dim_ = metric_l->text_dimension (text);
413 Atom at (gh_list (ly_symbol2scm ("text"),
414 ly_str02scm (text.ch_C()),
415 SCM_UNDEFINED));
416 at.fontify (metric_l);
418 m.add_atom (&at);
419 return m;
425 Molecule
426 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
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)
436 m.add_at_edge (Y_AXIS, UP, n, 0.0);
437 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
439 else
441 m = n;
442 m.align_to (Y_AXIS, CENTER);
444 return m;
447 Molecule
448 Lookup::staff_brace (Real y, int staff_size) const
450 Molecule m;
452 (define (pianobrace y staffht)
453 (let* ((step 1.0)
454 (minht (* 2 staffht))
455 (maxht (* 7 minht))
457 (string-append
458 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
459 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
464 Real step = 1.0;
465 int minht = 2 * staff_size;
466 int maxht = 7 * minht;
467 int idx = int (((maxht - step) <? y - minht) / step);
468 idx = idx >? 0;
471 String nm = String ("feta-braces" + to_str (staff_size));
472 SCM e =gh_list (ly_symbol2scm ("char"), gh_int2scm (idx), SCM_UNDEFINED);
473 Atom at (e);
475 at.fontify (all_fonts_global_p->find_font (nm));
477 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
478 m.dim_[X_AXIS] = Interval (0,0);
479 m.add_atom (&at);
480 return m;
483 Molecule
484 Lookup::hairpin (Real width, Real height, Real thick, bool decresc, bool continued) const
486 Molecule m;
488 const char* hairpin = decresc ? "decrescendo" : "crescendo";
489 Atom at (gh_list (ly_symbol2scm (hairpin),
490 gh_double2scm (thick),
491 gh_double2scm (width),
492 gh_double2scm (height),
493 gh_double2scm (continued ? height/2 : 0.0),
494 SCM_UNDEFINED));
495 m.dim_.x () = Interval (0, width);
496 m.dim_.y () = Interval (-2*height, 2*height);
498 m.add_atom (&at);
499 return m;
502 Molecule
503 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap,
504 Real height, Direction dir) const
506 Molecule m;
508 Atom at (gh_list(ly_symbol2scm ("tuplet"),
509 gh_double2scm (height),
510 gh_double2scm (gap),
511 gh_double2scm (dx),
512 gh_double2scm (dy),
513 gh_double2scm (thick),
514 gh_int2scm (dir),
515 SCM_UNDEFINED));
516 m.add_atom (&at);
518 return m;
522 Make a smooth curve along the points
524 Molecule
525 Lookup::slur (Array<Offset> controls, Real linethick) const
527 Offset delta_off = controls[3]- controls[0];
528 Molecule m;
530 SCM scontrols [8];
531 int indices[] = {5,6,7,4,1,2,3,0};
533 for (int i= 0; i < 8; i++)
534 scontrols[i] = offset2scm (controls[indices[i]]);
537 Atom at (gh_list (ly_symbol2scm ("bezier-sandwich"),
538 ly_quote_scm (array_to_list (scontrols, 8)),
539 gh_double2scm (linethick),
540 SCM_UNDEFINED));
542 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
543 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
544 m.add_atom (&at);
545 return m;
548 Molecule
549 Lookup::staff_bracket (Real height, Paper_def* paper_l) const
551 Molecule m;
552 Atom at ( gh_list (ly_symbol2scm ("bracket"),
553 gh_double2scm (paper_l->get_var("bracket_arch_angle")),
554 gh_double2scm (paper_l->get_var("bracket_arch_width")),
555 gh_double2scm (paper_l->get_var("bracket_arch_height")),
556 gh_double2scm (paper_l->get_var("bracket_width")),
557 gh_double2scm (height),
558 gh_double2scm (paper_l->get_var("bracket_arch_thick")),
559 gh_double2scm (paper_l->get_var("bracket_thick")),
560 SCM_UNDEFINED));
562 m.add_atom (&at);
563 m.dim_[Y_AXIS] = Interval (-height/2,height/2);
564 m.dim_[X_AXIS] = Interval (0,4 PT);
566 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
567 return m;
570 Molecule
571 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
573 Molecule m;
575 Atom at (gh_list (ly_symbol2scm ("volta"),
576 gh_double2scm (h),
577 gh_double2scm (w),
578 gh_double2scm (thick),
579 gh_int2scm (vert_start),
580 gh_int2scm (vert_end),
581 SCM_UNDEFINED));
583 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
584 m.dim_[X_AXIS] = Interval (0, w);
586 m.add_atom (&at);
587 return m;
590 Molecule
591 Lookup::accordion (SCM s, Real interline_f) const
593 Molecule m;
594 String sym = ly_scm2string(SCM_CAR(s));
595 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
597 if (sym == "Discant")
599 Molecule r = afm_find("scripts-accDiscant");
600 m.add_molecule(r);
601 if (reg.left_str(1) == "F")
603 Molecule d = afm_find("scripts-accDot");
604 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
605 m.add_molecule(d);
606 reg = reg.right_str(reg.length_i()-1);
608 int eflag = 0x00;
609 if (reg.left_str(3) == "EEE")
611 eflag = 0x07;
612 reg = reg.right_str(reg.length_i()-3);
614 else if (reg.left_str(2) == "EE")
616 eflag = 0x05;
617 reg = reg.right_str(reg.length_i()-2);
619 else if (reg.left_str(2) == "Eh")
621 eflag = 0x04;
622 reg = reg.right_str(reg.length_i()-2);
624 else if (reg.left_str(1) == "E")
626 eflag = 0x02;
627 reg = reg.right_str(reg.length_i()-1);
629 if (eflag & 0x02)
631 Molecule d = afm_find("scripts-accDot");
632 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
633 m.add_molecule(d);
635 if (eflag & 0x04)
637 Molecule d = afm_find("scripts-accDot");
638 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
639 d.translate_axis(0.8 * interline_f PT, X_AXIS);
640 m.add_molecule(d);
642 if (eflag & 0x01)
644 Molecule d = afm_find("scripts-accDot");
645 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
646 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
647 m.add_molecule(d);
649 if (reg.left_str(2) == "SS")
651 Molecule d = afm_find("scripts-accDot");
652 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
653 d.translate_axis(0.4 * interline_f PT, X_AXIS);
654 m.add_molecule(d);
655 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
656 m.add_molecule(d);
657 reg = reg.right_str(reg.length_i()-2);
659 if (reg.left_str(1) == "S")
661 Molecule d = afm_find("scripts-accDot");
662 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
663 m.add_molecule(d);
664 reg = reg.right_str(reg.length_i()-1);
667 else if (sym == "Freebase")
669 Molecule r = afm_find("scripts-accFreebase");
670 m.add_molecule(r);
671 if (reg.left_str(1) == "F")
673 Molecule d = afm_find("scripts-accDot");
674 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
675 m.add_molecule(d);
676 reg = reg.right_str(reg.length_i()-1);
678 if (reg == "E")
680 Molecule d = afm_find("scripts-accDot");
681 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
682 m.add_molecule(d);
685 else if (sym == "Bayanbase")
687 Molecule r = afm_find("scripts-accBayanbase");
688 m.add_molecule(r);
689 if (reg.left_str(1) == "T")
691 Molecule d = afm_find("scripts-accDot");
692 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
693 m.add_molecule(d);
694 reg = reg.right_str(reg.length_i()-1);
696 /* include 4' reed just for completeness. You don't want to use this. */
697 if (reg.left_str(1) == "F")
699 Molecule d = afm_find("scripts-accDot");
700 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
701 m.add_molecule(d);
702 reg = reg.right_str(reg.length_i()-1);
704 if (reg.left_str(2) == "EE")
706 Molecule d = afm_find("scripts-accDot");
707 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
708 d.translate_axis(0.4 * interline_f PT, X_AXIS);
709 m.add_molecule(d);
710 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
711 m.add_molecule(d);
712 reg = reg.right_str(reg.length_i()-2);
714 if (reg.left_str(1) == "E")
716 Molecule d = afm_find("scripts-accDot");
717 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
718 m.add_molecule(d);
719 reg = reg.right_str(reg.length_i()-1);
722 else if (sym == "Stdbase")
724 Molecule r = afm_find("scripts-accStdbase");
725 m.add_molecule(r);
726 if (reg.left_str(1) == "T")
728 Molecule d = afm_find("scripts-accDot");
729 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
730 m.add_molecule(d);
731 reg = reg.right_str(reg.length_i()-1);
733 if (reg.left_str(1) == "F")
735 Molecule d = afm_find("scripts-accDot");
736 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
737 m.add_molecule(d);
738 reg = reg.right_str(reg.length_i()-1);
740 if (reg.left_str(1) == "M")
742 Molecule d = afm_find("scripts-accDot");
743 d.translate_axis(interline_f * 2 PT, Y_AXIS);
744 d.translate_axis(interline_f PT, X_AXIS);
745 m.add_molecule(d);
746 reg = reg.right_str(reg.length_i()-1);
748 if (reg.left_str(1) == "E")
750 Molecule d = afm_find("scripts-accDot");
751 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
752 m.add_molecule(d);
753 reg = reg.right_str(reg.length_i()-1);
755 if (reg.left_str(1) == "S")
757 Molecule d = afm_find("scripts-accDot");
758 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
759 m.add_molecule(d);
760 reg = reg.right_str(reg.length_i()-1);
763 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
764 for the rectangle */
765 else if (sym == "SB")
767 Molecule r = afm_find("scripts-accSB");
768 m.add_molecule(r);
770 else if (sym == "BB")
772 Molecule r = afm_find("scripts-accBB");
773 m.add_molecule(r);
775 else if (sym == "OldEE")
777 Molecule r = afm_find("scripts-accOldEE");
778 m.add_molecule(r);
780 else if (sym == "OldEES")
782 Molecule r = afm_find("scripts-accOldEES");
783 m.add_molecule(r);
785 return m;