]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.0.17
[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 "dimensions.hh"
18 #include "symtable.hh"
19 #include "scalar.hh"
20 #include "paper-def.hh"
21 #include "string-convert.hh"
22 #include "main.hh"
23
24 Lookup::Lookup ()
25 {
26   paper_l_ = 0;
27   symtables_p_ = new Symtables;
28   afm_p_ =0;
29 }
30
31 Lookup::Lookup (Lookup const& s)
32 {
33   font_ = s.font_;
34   font_path_ = s.font_path_;
35   paper_l_ = s.paper_l_;
36   symtables_p_ = new Symtables (*s.symtables_p_);
37   afm_p_ = 0;
38 }
39
40 Lookup::Lookup (Symtables const& s)
41 {
42   font_ = s.font_;
43   font_path_ = s.font_path_;
44   paper_l_ = 0;
45   symtables_p_ = new Symtables (s);
46   afm_p_ = 0;
47 }
48
49 Lookup::~Lookup ()
50 {
51   delete afm_p_;
52   delete symtables_p_;
53 }
54
55 Atom
56 Lookup::accidental (int j) const
57 {
58   return afm_find (String ("accidentals") + String ("-") + to_str (j));
59 }
60
61 void
62 Lookup::add (String s, Symtable*p)
63 {
64   symtables_p_->add (s, p);
65 }
66
67 Atom
68 Lookup::afm_find (String s, String str) const
69 {
70   if (!afm_p_)
71     {
72       *mlog << "[" << font_path_;
73       ( (Lookup*)this)->afm_p_ = new Adobe_font_metric (read_afm (font_path_));
74       *mlog << "]" << flush ;
75       DOUT << this->afm_p_->str ();
76     }
77   Adobe_font_char_metric m = afm_p_->find_char (s);
78
79   Atom a;
80   if (m.code () < 0)
81     return a;
82   
83   a.dim_ = m.B_;
84   a.dim_[X_AXIS] *= 1 / 1000.0;
85   a.dim_[Y_AXIS] *= 1 / 1000.0;
86   a.str_ = String_convert::form_str (str.ch_C (), m.code ());
87   a.font_ = font_;
88   return a;
89 }
90
91 Atom
92 Lookup::ball (int j) const
93 {
94   if (j > 2)
95     j = 2;
96
97   return afm_find (String ("balls") + String ("-") + to_str (j));
98 }
99
100 Atom
101 Lookup::bar (String str, Real h) const
102 {
103   Array<String> a;
104   a.push (print_dimen (h));
105   Atom s = (*symtables_p_) ("bars")->lookup (str);
106   s.str_ = substitute_args (s.str_, a);
107   s.dim_.y () = Interval (-h/2, h/2);
108   s.font_ = font_;
109   return s;
110 }
111
112 String
113 Lookup::base_output_str () const
114 {
115   assert (paper_l_);
116   String str = paper_l_->get_default_output ();
117
118   if (str.empty_b ())
119     {
120       str = default_outname_base_global;
121       int def = paper_l_->get_next_default_count ();
122       if (def)
123         str += "-" + to_str (def);
124     }
125   return str;
126 }
127
128 Atom 
129 Lookup::beam (Real slope, Real width, Real thick) const
130 {
131   Atom a (ps_beam (slope, width, thick));
132   Real height = slope * width; 
133   Real min_y = (0 <? height) - thick/2;
134   Real max_y = (0 >? height) + thick/2;
135   
136   a.dim_[X_AXIS] = Interval (0, width);
137   a.dim_[Y_AXIS] = Interval (min_y, max_y);
138   return a;
139 }
140
141 String
142 Lookup::character_str (int i) const
143 {
144   return to_str (i);
145 }
146
147 Atom
148 Lookup::clef (String st) const
149 {
150   return afm_find (String ("clefs") + String ("-") + st);
151 }
152
153 Atom
154 Lookup::dots () const
155 {
156   return afm_find (String ("dots") + String ("-") + String ("dot"));
157 }
158
159 Atom
160 Lookup::dynamic (String st) const
161 {
162   return (*symtables_p_) ("dynamics")->lookup (st);
163 }
164
165 Atom
166 Lookup::fill (Box b) const
167 {
168   Atom a;
169   a.dim_ = b;
170   return a;
171 }
172
173 Atom
174 Lookup::flag (int j, Direction d) const
175 {
176   char c = (d == UP) ? 'u' : 'd';
177   return afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
178 }
179
180 void
181 Lookup::print () const
182 {
183 #ifndef NPRINT
184   DOUT << "Lookup {\n";
185   symtables_p_->print ();
186   DOUT << "}\n";
187 #endif
188 }
189
190 String
191 Lookup::print_dimen (Real r) const
192 {
193   String s = to_str (r, "%.3f");
194   if (s.index_i ("NaN") != -1)
195     {
196       warning (_ ("NaN"));
197       s = "0.0";
198     }
199   return s;
200 }
201
202 Atom
203 Lookup::rest (int j, bool o) const
204 {
205    return afm_find (String ("rests")
206                     + String ("-") + to_str (j) + (o ? "o" : ""));
207 }
208
209 Atom
210 Lookup::rule_symbol (Real height, Real width) const
211 {
212   Atom bs= (*symtables_p_) ("param")->lookup ("rule");
213   Array<String> args;
214   args.push (print_dimen (height));
215   args.push (print_dimen (width));
216   bs.str_ = substitute_args (bs.str_, args);
217   bs.dim_.x () = Interval (0, width);
218   bs.dim_.y () = Interval (0, height);
219   return bs;
220 }
221
222 Atom
223 Lookup::script (String str) const
224 {
225   return afm_find (String ("scripts") + String ("-") + str);
226 }
227
228 Atom
229 Lookup::special_time_signature (String s, Array<Scalar> arr) const
230 {
231   String symbolname = "timesig-"+s+"%/%";
232   Atom a (afm_find (substitute_args (symbolname, arr)));
233   if (!a.empty ()) 
234     return a;
235   // Try if the full name was given
236   a = afm_find ("timesig-"+s);
237   if (!a.empty ()) 
238     return a;
239   // Resort to default layout with numbers
240   return time_signature (arr);
241 }
242
243 static void
244 substitute_arg (String& r, String arg)
245 {
246   int p = r.index_i ('%');
247   if (p < 0)
248         return ;
249
250   r = r.left_str (p) + arg + r.right_str (r.length_i () - p - 1);
251 }
252
253 String
254 Lookup::substitute_args (String source, Array<String> args) const
255 {
256   String str (source);
257   for (int i = 0 ; i < args.size (); i++)
258     substitute_arg (str, args[i]);
259   return str;
260 }
261
262 String
263 Lookup::substitute_args (String source, Array<Scalar> args) const
264 {
265   Array<String> sv;
266   for (int i = 0 ; i < args.size (); i++)
267     sv.push (args[i]);
268   return substitute_args (source, sv);
269 }
270
271 Atom
272 Lookup::stem (Real y1, Real y2, String str) const
273 {
274   if (y1 > y2)
275     {
276       Real t = y1;
277       y1 = y2;
278       y2 = t;
279     }
280   Atom s;
281
282   s.dim_.x () = Interval (0,0);
283   s.dim_.y () = Interval (y1,y2);
284
285   Array<String> a;
286
287   Real stem_width = paper_l_->get_var ("stemthickness");
288   a.push (print_dimen (-stem_width /2));
289   a.push (print_dimen (stem_width));
290   a.push (print_dimen (y2));
291   a.push (print_dimen (-y1));
292
293   s.str_ = substitute_args (str, a);
294   s.font_ = font_;
295   return s;
296 }
297
298 Atom
299 Lookup::streepje (int type) const
300 {
301   if (type > 2)
302     type = 2;
303
304   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
305 }
306
307 Atom
308 Lookup::text (String style, String text) const
309 {
310   Array<String> a;
311
312   a.push (text);
313   Atom s =  (*symtables_p_) ("style")->lookup (style);
314   s.str_ = substitute_args (s.str_,a);
315   s.font_ = font_;
316
317   return s;
318 }
319
320 Atom
321 Lookup::time_signature (Array<Scalar> a) const
322 {
323   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
324   s.str_ = substitute_args (s.str_, a);
325
326   return s;
327 }
328
329 /*
330   should be handled via Tex_ code and Lookup::bar ()
331  */
332 Atom
333 Lookup::vbrace (Real &y) const
334 {
335   Atom brace = (*symtables_p_) ("param")->lookup ( "brace");
336   Interval ydims = brace.dim_[Y_AXIS];
337   Real min_y = ydims[LEFT];
338   Real max_y = ydims[RIGHT];
339   Real step = 1.0 PT;
340  
341   if (y < min_y)
342     {
343       warning (_ ("piano brace") 
344         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
345       y = min_y;
346     }
347   if (y > max_y)
348     {
349       warning (_ ("piano brace")
350        + " " + _ ("too big") + " (" + print_dimen (y) + ")");
351       y = max_y;
352     }
353
354   
355   int idx = int (rint ( (y- min_y)/step)) + 1;
356   
357   {
358     Array<String> a;
359     a.push (character_str (idx));
360     brace.str_ = substitute_args (brace.str_,a);
361     brace.dim_[Y_AXIS] = Interval (-y/2,y/2);
362   }
363
364   brace.font_ = font_;
365
366   return brace;
367 }
368
369