]> git.donarmstrong.com Git - lilypond.git/blob - symtable.cc
release: 0.0.3
[lilypond.git] / symtable.cc
1 #include "misc.hh"
2 #include "debug.hh"
3 #include "real.hh"
4 #include "symbol.hh"
5 #include "assoc.hh"
6 #include "symtable.hh"
7 #include "const.hh"
8
9 static Symbol unknown;
10
11
12 // scary! What if Symtable resizes on the fly...?
13 const Symbol *
14 Symtable::lookup(String s) const
15 {
16     if (elt_query(s))
17         return &(*this)[s];
18     else {
19         WARN<<"Unknown symbol " << s <<'\n';
20         return &unknown;
21     }
22 }
23
24 Symtable* 
25 Symtables::operator()(String s) 
26 {
27     if (!done_reading){ // read on demand
28         *mlog << '(' << fname ;
29         read();
30         done_reading = true;
31         *mlog << ")\n";
32     }
33     return Assoc<String, Symtable*>::operator[](s);
34
35
36 void
37 Symtables::read()
38 {
39      Text_db symini(fname);
40      while (!symini.eof()) {
41          Text_record  r(  symini++);
42          assert (r[0] == "table");
43          
44          String tabnam = r[1];
45          Symtable * sp = new Symtable;
46          while (!symini.eof()){
47              r = symini++;
48              if (r[0] == "end")
49                  break;
50              
51              assert(r.sz() == 6);
52              int i=0;
53              String id=r[i++];
54              String tex=r[i++];
55              svec<Real> dims;
56              for (int j=0; j < 4; j++)
57                  dims.add( r[i++].fvalue() *1.0/CM_TO_PT);
58              
59              Symbol s(tex, Box(dims));
60              (*sp)[id] = s;
61          }
62          (*this)[tabnam] = sp;           
63      }
64 }
65
66 Symtables the_sym_tables("symbol.ini");
67
68
69 const Symbol*
70 Symbol::find_ball(int j)
71 {
72     if (j > 4) j = 4;
73     Symtable * st = the_sym_tables("balls");
74     return st->lookup(String(j));
75 }
76
77 const Symbol*
78 Symbol::find_rest(int j)
79 {
80     return the_sym_tables("rests")->lookup(String(j));
81 }
82 const Symbol*
83 Symbol::find_bar(String s)
84 {
85     return the_sym_tables("bars")->lookup(s);
86 }
87 /****************************************************************/
88 // bare bones.
89
90 struct Linestaf_symbol : Stretchable_symbol {
91     int lines;
92     String operator ()(Real w);
93     Linestaf_symbol(int n) { lines = n;}
94     Interval height(Real) const { return Interval(0,lines*1/CM_TO_PT); }
95 };
96
97
98
99 // should be done in TeX
100 String
101 Linestaf_symbol::operator()(Real w)
102 {
103     String s;
104     s += "\\hbox to 0pt{";
105     s+= "\\vbox to 0pt{";
106     for (int i=0; i<lines; i++) {
107         if (i) s+= "\\vskip1pt";
108         s+= "\\hrule width " + String(w* HOR_TO_PT) +"pt";
109     }
110     s+="\\vss}\\hss}";
111     return s;
112 }
113
114 const Stretchable_symbol *
115 Stretchable_symbol::get_linestaff(int n)
116 {
117     return new Linestaf_symbol(n);
118 }