]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
patch::: 1.0.13.mb1: Re: 1.0.13
[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--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8   Jan Nieuwenhuizen <janneke@gnu.org>
9
10   TODO
11       Read spacing info from AFMs
12       Glissando
13 */
14
15 #include "lookup.hh"
16 #include "debug.hh"
17 #include "symtable.hh"
18 #include "dimension.hh"
19 #include "tex.hh"
20 #include "scalar.hh"
21 #include "paper-def.hh"
22 #include "string-convert.hh"
23 #include "main.hh"
24
25 Lookup::Lookup ()
26 {
27   paper_l_ = 0;
28   symtables_p_ = new Symtables;
29   afm_p_ =0;
30 }
31
32 Lookup::Lookup (Lookup const& s)
33 {
34   font_ = s.font_;
35   font_path_ = s.font_path_;
36   paper_l_ = s.paper_l_;
37   symtables_p_ = new Symtables (*s.symtables_p_);
38   afm_p_ = 0;
39 }
40
41 Lookup::Lookup (Symtables const& s)
42 {
43   font_ = s.font_;
44   font_path_ = s.font_path_;
45   paper_l_ = 0;
46   symtables_p_ = new Symtables (s);
47   afm_p_ = 0;
48 }
49
50 Lookup::~Lookup ()
51 {
52   delete afm_p_;
53   delete symtables_p_;
54 }
55
56 Atom
57 Lookup::accidental (int j) const
58 {
59   return afm_find (String ("accidentals") + String ("-") + to_str (j));
60 }
61
62 void
63 Lookup::add (String s, Symtable*p)
64 {
65   symtables_p_->add (s, p);
66 }
67
68 Atom
69 Lookup::afm_find (String s, String str) const
70 {
71   if (!afm_p_)
72     {
73       *mlog << "[" << font_path_;
74       ( (Lookup*)this)->afm_p_ = new Adobe_font_metric (read_afm (font_path_));
75       *mlog << "]" << flush ;
76       DOUT << this->afm_p_->str ();
77     }
78   Adobe_font_char_metric m = afm_p_->find_char (s);
79
80   Atom a;
81   if (m.code () < 0)
82     return a;
83   
84   a.dim_ = m.B_;
85   a.dim_[X_AXIS] *= 1 / 1000.0;
86   a.dim_[Y_AXIS] *= 1 / 1000.0;
87   a.str_ = String_convert::form_str (str.ch_C (), m.code ());
88   a.font_ = font_;
89   return a;
90 }
91
92 Atom
93 Lookup::ball (int j) const
94 {
95   if (j > 2)
96     j = 2;
97
98   return afm_find (String ("balls") + String ("-") + to_str (j));
99 }
100
101 Atom
102 Lookup::bar (String str, Real h) const
103 {
104   Array<String> a;
105   a.push (print_dimen (h));
106   Atom s = (*symtables_p_) ("bars")->lookup (str);
107   s.str_ = substitute_args (s.str_, a);
108   s.dim_.y () = Interval (-h/2, h/2);
109   s.font_ = font_;
110   return s;
111 }
112
113 Atom 
114 Lookup::beam (Real slope, Real width, Real thick) const
115 {
116   Atom a (ps_beam (slope, width, thick));
117   Real height = slope * width; 
118   Real min_y = (0 <? height) - thick/2;
119   Real max_y = (0 >? height) + thick/2;
120   
121   a.dim_[X_AXIS] = Interval (0, width);
122   a.dim_[Y_AXIS] = Interval (min_y, max_y);
123   return a;
124 }
125
126 Atom
127 Lookup::clef (String st) const
128 {
129   return afm_find (String ("clefs") + String ("-") + st);
130 }
131
132 Atom
133 Lookup::dots () const
134 {
135   return afm_find (String ("dots") + String ("-") + String ("dot"));
136 }
137
138 Atom
139 Lookup::dynamic (String st) const
140 {
141   return (*symtables_p_) ("dynamics")->lookup (st);
142 }
143
144 Atom
145 Lookup::fill (Box b) const
146 {
147   Atom a;
148   a.dim_ = b;
149   return a;
150 }
151
152 Atom
153 Lookup::flag (int j, Direction d) const
154 {
155   char c = (d == UP) ? 'u' : 'd';
156   return afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
157 }
158
159 void
160 Lookup::print () const
161 {
162 #ifndef NPRINT
163   DOUT << "Lookup {\n";
164   symtables_p_->print ();
165   DOUT << "}\n";
166 #endif
167 }
168
169 Atom
170 Lookup::rest (int j, bool o) const
171 {
172    return afm_find (String ("rests")
173                     + String ("-") + to_str (j) + (o ? "o" : ""));
174 }
175
176 Atom
177 Lookup::rule_symbol (Real height, Real width) const
178 {
179   Atom bs= (*symtables_p_) ("param")->lookup ("rule");
180   Array<String> args;
181   args.push (print_dimen (height));
182   args.push (print_dimen (width));
183   bs.str_ = substitute_args (bs.str_,args);
184   bs.dim_.x () = Interval (0,width);
185   bs.dim_.y () = Interval (0,height);
186   return bs;
187 }
188
189 Atom
190 Lookup::script (String str) const
191 {
192   return afm_find (String ("scripts") + String ("-") + str);
193 }
194
195 Atom
196 Lookup::special_time_signature (String s, Array<Scalar> arr) const
197 {
198   String symbolname="timesig-"+s+"%/%";
199   Atom a (afm_find (substitute_args(symbolname,arr)));
200   if (!a.empty()) 
201     return a;
202   // Try if the full name was given
203   a=afm_find ("timesig-"+s);
204   if (!a.empty()) 
205     return a;
206   // Resort to default layout with numbers
207   return time_signature(arr);
208 }
209
210 Atom
211 Lookup::stem (Real y1, Real y2, String str) const
212 {
213   if (y1 > y2)
214     {
215       Real t = y1;
216       y1 = y2;
217       y2 = t;
218     }
219   Atom s;
220
221   s.dim_.x () = Interval (0,0);
222   s.dim_.y () = Interval (y1,y2);
223
224   Array<String> a;
225
226   Real stem_width = paper_l_->get_var ("stemthickness");
227   a.push (print_dimen (-stem_width /2));
228   a.push (print_dimen (stem_width));
229   a.push (print_dimen (y2));
230   a.push (print_dimen (-y1));
231
232   s.str_ = substitute_args (str, a);
233   s.font_ = font_;
234   return s;
235 }
236
237 Atom
238 Lookup::streepje (int type) const
239 {
240   if (type > 2)
241     type = 2;
242
243   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
244 }
245
246 Atom
247 Lookup::text (String style, String text) const
248 {
249   Array<String> a;
250
251   a.push (text);
252   Atom s =  (*symtables_p_) ("style")->lookup (style);
253   s.str_ = substitute_args (s.str_,a);
254   s.font_ = font_;
255
256   return s;
257 }
258
259 Atom
260 Lookup::time_signature (Array<Scalar> a) const
261 {
262   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
263   s.str_ = substitute_args (s.str_, a);
264
265   return s;
266 }
267
268 /*
269   should be handled via Tex_ code and Lookup::bar ()
270  */
271 Atom
272 Lookup::vbrace (Real &y) const
273 {
274   Atom brace = (*symtables_p_) ("param")->lookup ( "brace");
275   Interval ydims = brace.dim_[Y_AXIS];
276   Real min_y = ydims[LEFT];
277   Real max_y = ydims[RIGHT];
278   Real step = 1.0 PT;
279  
280   if (y < min_y)
281     {
282       warning (_ ("piano brace") 
283         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
284       y = min_y;
285     }
286   if (y > max_y)
287     {
288       warning (_ ("piano brace")
289        + " " + _ ("too big") + " (" + print_dimen (y) + ")");
290       y = max_y;
291     }
292
293   
294   int idx = int (rint ( (y- min_y)/step)) + 1;
295   
296   {
297     Array<String> a;
298     a.push (to_str (idx));
299     brace.str_ = substitute_args (brace.str_,a);
300     brace.dim_[Y_AXIS] = Interval (-y/2,y/2);
301   }
302
303   brace.font_ = font_;
304
305   return brace;
306 }
307
308