lilypond-1.1.44
[lilypond.git] / lily / lookup.cc
blob846e2614eb46ba298e8a21001fa845091cdd131c
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
14 #include <ctype.h>
15 #include "lookup.hh"
16 #include "debug.hh"
17 #include "dimensions.hh"
18 #include "scalar.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-fonts.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 paper_l_ = 0;
35 afm_l_ = 0;
38 Lookup::Lookup (Lookup const& s)
40 font_name_ = s.font_name_;
41 paper_l_ = 0;
42 afm_l_ = 0;
47 build a ledger line for small pieces.
49 Molecule
50 Lookup::ledger_line (Interval xwid) const
52 Drul_array<Molecule> endings;
53 endings[LEFT] = afm_find ("noteheads-ledgerending");
54 Molecule * e = &endings[LEFT];
55 endings[RIGHT] = *e;
57 Real thick = e->dim_[Y_AXIS].length();
58 Real len = e->dim_[X_AXIS].length () - thick;
60 Molecule total;
61 Direction d = LEFT;
62 do {
63 endings[d].translate_axis (xwid[d] - endings[d].dim_[X_AXIS][d], X_AXIS);
64 total.add_molecule (endings[d]);
65 } while ((flip(&d)) != LEFT);
67 Real xpos = xwid [LEFT] + len;
69 while (xpos + len + thick /2 <= xwid[RIGHT])
71 e->translate_axis (len, X_AXIS);
72 total.add_molecule (*e);
73 xpos += len;
76 return total;
80 Molecule
81 Lookup::accidental (int j, bool cautionary) const
83 Molecule m(afm_find (String ("accidentals-") + to_str (j)));
84 if (cautionary)
86 Molecule open = afm_find (String ("accidentals-("));
87 Molecule close = afm_find (String ("accidentals-)"));
88 m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
89 m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
91 return m;
96 Molecule
97 Lookup::afm_find (String s, bool warn) const
99 if (!afm_l_)
101 Lookup * me = (Lookup*)(this);
102 me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
103 if (!me->afm_l_)
105 warning (_f("Can't open `%s'\n", font_name_));
106 warning (_f("Search path %s\n", global_path.str ().ch_C()));
107 error (_f("Aborting"));
110 Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
111 Molecule m;
112 if (cm.code () < 0)
115 don't want people relying on this kind of dimension.
117 m.set_empty (false);
118 return m;
121 Atom at (gh_list (char_scm_sym,
122 gh_int2scm (cm.code ()),
123 SCM_UNDEFINED));
124 at.font_ = ly_symbol (font_name_.ch_C());
125 m.dim_ = cm.dimensions();
126 m.add_atom (&at);
127 return m;
130 Molecule
131 Lookup::notehead (int j, String type) const
133 if (j > 2)
134 j = 2;
136 return afm_find (String ("noteheads-") + to_str (j) + type);
139 Molecule
140 Lookup::simple_bar (String type, Real h) const
142 SCM thick = ly_symbol ("barthick_" + type);
143 Real w = 0.0;
145 if (paper_l_->scope_p_->elem_b (thick))
147 w = paper_l_->get_realvar (thick);
149 else
151 programming_error ("No bar thickness set ! ");
152 w = 1 PT;
154 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
158 Molecule
159 Lookup::bar (String str, Real h) const
161 if (str == "[")
162 return staff_bracket (h);
163 else if (str == "{")
164 return staff_brace (h);
166 Real kern = paper_l_->get_var ("bar_kern");
167 Real thinkern = paper_l_->get_var ("bar_thinkern");
168 Molecule thin = simple_bar ("thin", h);
169 Molecule thick = simple_bar ("thick", h);
170 Molecule colon = afm_find ("dots-repeatcolon");
172 Molecule m;
174 if (str == "")
176 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
178 if (str == "scorepostbreak")
180 return simple_bar ("score", h);
182 else if (str == "|")
184 return thin;
186 else if (str == "|.")
188 m.add_at_edge (X_AXIS, LEFT, thick, 0);
189 m.add_at_edge (X_AXIS, LEFT, thin, kern);
191 else if (str == ".|")
193 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
194 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
196 else if (str == ":|")
198 m.add_at_edge (X_AXIS, LEFT, thick, 0);
199 m.add_at_edge (X_AXIS, LEFT, thin, kern);
200 m.add_at_edge (X_AXIS, LEFT, colon, kern);
202 else if (str == "|:")
204 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
205 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
206 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
208 else if (str == ":|:")
210 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
211 m.add_at_edge (X_AXIS, LEFT, colon, kern);
212 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
213 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
215 else if (str == ".|.")
217 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
218 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
220 else if (str == "||")
222 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
223 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
226 return m;
229 Molecule
230 Lookup::beam (Real slope, Real width, Real thick) const
232 Real height = slope * width;
233 Real min_y = (0 <? height) - thick/2;
234 Real max_y = (0 >? height) + thick/2;
237 Molecule m;
238 Atom at
239 (gh_list (beam_scm_sym,
240 gh_double2scm (width),
241 gh_double2scm (slope),
242 gh_double2scm (thick),
243 SCM_UNDEFINED));
245 m.dim_[X_AXIS] = Interval (0, width);
246 m.dim_[Y_AXIS] = Interval (min_y, max_y);
247 m.add_atom (&at);
248 return m;
251 Molecule
252 Lookup::clef (String st) const
254 return afm_find (String ("clefs-" + st));
258 offset2scm (Offset o)
260 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
261 SCM_UNDEFINED);
264 Molecule
265 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
267 assert (controls.size () == 8);
268 Offset d = controls[3] - controls[0];
270 Real dx = d[X_AXIS];
271 Real dy = d[Y_AXIS];
273 Molecule m;
276 m.dim_[X_AXIS] = Interval (0, dx);
277 m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
279 SCM sc[4];
280 for (int i=0; i< 4; i++)
282 sc[i] = offset2scm (controls[i]);
285 Atom at
286 (gh_list (ly_symbol ("dashed-slur"),
287 gh_double2scm (thick),
288 gh_double2scm (dash),
289 ly_quote_scm (array_to_list (sc, 4)),
290 SCM_UNDEFINED));
293 m.add_atom (&at);
294 return m;
297 Molecule
298 Lookup::dots () const
300 return afm_find (String ("dots-dot"));
305 Molecule
306 Lookup::fill (Box b) const
308 Molecule m;
309 m.dim_ = b;
310 return m;
313 Molecule
314 Lookup::flag (int j, Direction d) const
316 char c = (d == UP) ? 'u' : 'd';
317 return afm_find (String ("flags-") + to_str (c) + to_str (j));
320 Molecule
321 Lookup::rest (int j, bool o, String style) const
323 return afm_find (String ("rests-") + to_str (j) + (o ? "o" : "") + style);
327 Molecule
328 Lookup::special_time_signature (String s, int n, int d) const
330 // First guess: s contains only the signature style
331 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
333 Molecule m = afm_find (symbolname, false);
334 if (!m.empty_b())
335 return m;
337 // Second guess: s contains the full signature name
338 m = afm_find ("timesig-"+s, false);
339 if (!m.empty_b ())
340 return m;
342 // Resort to default layout with numbers
343 return time_signature (n,d);
346 Molecule
347 Lookup::filledbox (Box b ) const
349 Molecule m;
351 Atom at (gh_list (filledbox_scm_sym,
352 gh_double2scm (-b[X_AXIS][LEFT]),
353 gh_double2scm (b[X_AXIS][RIGHT]),
354 gh_double2scm (-b[Y_AXIS][DOWN]),
355 gh_double2scm (b[Y_AXIS][UP]),
356 SCM_UNDEFINED));
358 m.dim_ = b;
359 m.add_atom (&at);
360 return m;
363 Molecule
364 Lookup::stem (Real y1, Real y2) const
366 if (y1 > y2)
368 Real t = y1;
369 y1 = y2;
370 y2 = t;
372 Real stem_width = paper_l_->get_var ("stemthickness");
373 return filledbox (Box (Interval (-stem_width/2,stem_width/2),
374 Interval (y1, y2)));
380 Magnification steps. These are powers of 1.2. The numbers are
381 taken from Knuth's plain.tex: */
382 static Real mag_steps[] = {1, 1, 1.200, 1.440, 1.7280, 2.074, 2.488};
386 Molecule
387 Lookup::text (String style, String text) const
389 Molecule m;
390 if (style.empty_b ())
391 style = "roman";
393 int font_mag = 1;
394 Real font_h = paper_l_->get_var ("font_normal");
395 if (paper_l_->scope_p_->elem_b ("font_" + style))
397 font_h = paper_l_->get_var ("font_" + style);
400 if (paper_l_->scope_p_->elem_b ("magnification_" + style))
402 font_mag = (int)paper_l_->get_var ("magnification_" + style);
406 UGH.
408 SCM l = gh_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
409 if (l != SCM_BOOL_F)
411 style = ly_scm2string (SCM_CDR(l)) +to_str ((int)font_h);
414 Real w = 0;
415 Interval ydims (0,0);
417 Font_metric* afm_l = all_fonts_global_p->find_font (style);
418 DOUT << "\nChars: ";
420 for (int i = 0; i < text.length_i (); i++)
422 if (text[i]=='\\')
423 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
425 else
427 Character_metric *c = afm_l->get_char ((unsigned char)text[i],false);
429 w += c->dimensions()[X_AXIS].length ();
430 ydims.unite (c->dimensions()[Y_AXIS]);
434 if (font_mag > 1 && font_mag < 7 )
436 /* UGH */
437 style = style + String(" scaled \\magstep ") + to_str (font_mag);
438 w *= mag_steps[font_mag];
439 ydims *= mag_steps[font_mag];
442 DOUT << "\n" << to_str (w) << "\n";
443 m.dim_.x () = Interval (0, w);
444 m.dim_.y () = ydims;
447 Atom at (gh_list (text_scm_sym,
448 gh_str02scm (text.ch_C()),
449 SCM_UNDEFINED));
450 at.font_ = ly_symbol (style);
452 m.add_atom (&at);
453 return m;
457 Molecule
458 Lookup::time_signature (int num, int den) const
460 String sty = "number";
461 Molecule n (text (sty, to_str (num)));
462 Molecule d (text (sty, to_str (den)));
463 n.align_to (X_AXIS, CENTER);
464 d.align_to (X_AXIS, CENTER);
465 Molecule m;
466 if (den)
468 m.add_at_edge (Y_AXIS, UP, n, 0.0);
469 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
471 else
473 m = n;
474 m.align_to (Y_AXIS, CENTER);
476 return m;
479 Molecule
480 Lookup::staff_brace (Real y) const
482 Molecule m;
484 Atom at (gh_list (pianobrace_scm_sym,
485 gh_double2scm (y),
486 SCM_UNDEFINED
489 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
490 m.dim_[X_AXIS] = Interval (0,0);
491 m.add_atom (&at);
492 return m;
495 Molecule
496 Lookup::hairpin (Real width, bool decresc, bool continued) const
498 Molecule m;
499 Real height = paper_l_->staffheight_f () / 6;
501 String hairpin = String (decresc ? "de" : "") + "crescendo";
502 Atom at (gh_list (ly_symbol (hairpin),
503 gh_double2scm (width),
504 gh_double2scm (height),
505 gh_double2scm (continued ? height/2 : 0.0),
506 SCM_UNDEFINED));
507 m.dim_.x () = Interval (0, width);
508 m.dim_.y () = Interval (-2*height, 2*height);
510 m.add_atom (&at);
511 return m;
514 Molecule
515 Lookup::plet (Real dy , Real dx, Direction dir) const
517 Molecule m;
518 SCM thick = tuplet_thick_scm_sym;
519 Real t = 0.1 PT;
520 if (paper_l_->scope_p_->elem_b (thick))
522 t = paper_l_->get_realvar (thick);
525 Real interline_f = paper_l_->get_realvar (interline_scm_sym);
526 Atom at (gh_list(tuplet_scm_sym,
527 gh_double2scm (interline_f),
528 gh_double2scm (dx),
529 gh_double2scm (dy),
530 gh_double2scm (t),
531 gh_int2scm (dir),
532 SCM_UNDEFINED));
533 m.add_atom (&at);
535 return m;
539 Make a smooth curve along the points
541 Molecule
542 Lookup::slur (Array<Offset> controls) const
544 Offset delta_off = controls[3]- controls[0];
545 Molecule m;
547 SCM scontrols [8];
548 int indices[] = {5,6,7,4,1,2,3,0};
550 for (int i= 0; i < 8; i++)
551 scontrols[i] = offset2scm (controls[indices[i]]);
554 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
555 ly_quote_scm (array_to_list (scontrols, 8)),
556 SCM_UNDEFINED));
558 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
559 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
560 m.add_atom (&at);
561 return m;
564 Molecule
565 Lookup::staff_bracket (Real y) const
567 Molecule m;
568 Atom at ( gh_list (bracket_scm_sym,
569 gh_double2scm (y),
570 SCM_UNDEFINED));
571 m.add_atom (&at);
572 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
573 m.dim_[X_AXIS] = Interval (0,4 PT);
575 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
576 return m;
579 Molecule
580 Lookup::volta (Real w, bool last_b) const
582 Molecule m;
583 SCM thick = volta_thick_scm_sym;
584 Real t = 0.1 PT;
585 if (paper_l_->scope_p_->elem_b (thick))
587 t = paper_l_->get_realvar (thick);
589 Atom at (gh_list (volta_scm_sym,
590 gh_double2scm (w),
591 gh_double2scm (t),
592 gh_int2scm (last_b),
593 SCM_UNDEFINED));
595 Real interline_f = paper_l_->get_realvar (interline_scm_sym);
597 m.dim_[Y_AXIS] = Interval (-interline_f, interline_f);
598 m.dim_[X_AXIS] = Interval (0, w);
600 m.add_atom (&at);
601 return m;
604 Molecule
605 Lookup::accordion (SCM s) const
607 Molecule m;
608 String sym = ly_scm2string(SCM_CAR(s));
609 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
610 Real interline_f = paper_l_->get_realvar(interline_scm_sym);
611 if (sym == "Discant")
613 Molecule r = afm_find("scripts-accDiscant");
614 m.add_molecule(r);
615 if (reg.left_str(1) == "F")
617 Molecule d = afm_find("scripts-accDot");
618 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
619 m.add_molecule(d);
620 reg = reg.right_str(reg.length_i()-1);
622 int eflag = 0x00;
623 if (reg.left_str(3) == "EEE")
625 eflag = 0x07;
626 reg = reg.right_str(reg.length_i()-3);
628 else if (reg.left_str(2) == "EE")
630 eflag = 0x05;
631 reg = reg.right_str(reg.length_i()-2);
633 else if (reg.left_str(2) == "Eh")
635 eflag = 0x04;
636 reg = reg.right_str(reg.length_i()-2);
638 else if (reg.left_str(1) == "E")
640 eflag = 0x02;
641 reg = reg.right_str(reg.length_i()-1);
643 if (eflag & 0x02)
645 Molecule d = afm_find("scripts-accDot");
646 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
647 m.add_molecule(d);
649 if (eflag & 0x04)
651 Molecule d = afm_find("scripts-accDot");
652 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
653 d.translate_axis(0.8 * interline_f PT, X_AXIS);
654 m.add_molecule(d);
656 if (eflag & 0x01)
658 Molecule d = afm_find("scripts-accDot");
659 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
660 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
661 m.add_molecule(d);
663 if (reg.left_str(2) == "SS")
665 Molecule d = afm_find("scripts-accDot");
666 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
667 d.translate_axis(0.4 * interline_f PT, X_AXIS);
668 m.add_molecule(d);
669 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
670 m.add_molecule(d);
671 reg = reg.right_str(reg.length_i()-2);
673 if (reg.left_str(1) == "S")
675 Molecule d = afm_find("scripts-accDot");
676 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
677 m.add_molecule(d);
678 reg = reg.right_str(reg.length_i()-1);
681 else if (sym == "Freebase")
683 Molecule r = afm_find("scripts-accFreebase");
684 m.add_molecule(r);
685 if (reg.left_str(1) == "F")
687 Molecule d = afm_find("scripts-accDot");
688 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
689 m.add_molecule(d);
690 reg = reg.right_str(reg.length_i()-1);
692 if (reg == "E")
694 Molecule d = afm_find("scripts-accDot");
695 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
696 m.add_molecule(d);
699 else if (sym == "Bayanbase")
701 Molecule r = afm_find("scripts-accBayanbase");
702 m.add_molecule(r);
703 if (reg.left_str(1) == "T")
705 Molecule d = afm_find("scripts-accDot");
706 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
707 m.add_molecule(d);
708 reg = reg.right_str(reg.length_i()-1);
710 /* include 4' reed just for completeness. You don't want to use this. */
711 if (reg.left_str(1) == "F")
713 Molecule d = afm_find("scripts-accDot");
714 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
715 m.add_molecule(d);
716 reg = reg.right_str(reg.length_i()-1);
718 if (reg.left_str(2) == "EE")
720 Molecule d = afm_find("scripts-accDot");
721 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
722 d.translate_axis(0.4 * interline_f PT, X_AXIS);
723 m.add_molecule(d);
724 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
725 m.add_molecule(d);
726 reg = reg.right_str(reg.length_i()-2);
728 if (reg.left_str(1) == "E")
730 Molecule d = afm_find("scripts-accDot");
731 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
732 m.add_molecule(d);
733 reg = reg.right_str(reg.length_i()-1);
736 else if (sym == "Stdbase")
738 Molecule r = afm_find("scripts-accStdbase");
739 m.add_molecule(r);
740 if (reg.left_str(1) == "T")
742 Molecule d = afm_find("scripts-accDot");
743 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
744 m.add_molecule(d);
745 reg = reg.right_str(reg.length_i()-1);
747 if (reg.left_str(1) == "F")
749 Molecule d = afm_find("scripts-accDot");
750 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
751 m.add_molecule(d);
752 reg = reg.right_str(reg.length_i()-1);
754 if (reg.left_str(1) == "M")
756 Molecule d = afm_find("scripts-accDot");
757 d.translate_axis(interline_f * 2 PT, Y_AXIS);
758 d.translate_axis(interline_f PT, X_AXIS);
759 m.add_molecule(d);
760 reg = reg.right_str(reg.length_i()-1);
762 if (reg.left_str(1) == "E")
764 Molecule d = afm_find("scripts-accDot");
765 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
766 m.add_molecule(d);
767 reg = reg.right_str(reg.length_i()-1);
769 if (reg.left_str(1) == "S")
771 Molecule d = afm_find("scripts-accDot");
772 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
773 m.add_molecule(d);
774 reg = reg.right_str(reg.length_i()-1);
777 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
778 for the rectangle */
779 else if (sym == "SB")
781 Molecule r = afm_find("scripts-accSB");
782 m.add_molecule(r);
784 else if (sym == "BB")
786 Molecule r = afm_find("scripts-accBB");
787 m.add_molecule(r);
789 else if (sym == "OldEE")
791 Molecule r = afm_find("scripts-accOldEE");
792 m.add_molecule(r);
794 else if (sym == "OldEES")
796 Molecule r = afm_find("scripts-accOldEES");
797 m.add_molecule(r);
799 return m;