]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
7f09106f3bdf5496f8f1ea4d1be3c28e7cf62d88
[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
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 String
127 Lookup::character_str (int i) const
128 {
129   return to_str (i);
130 }
131
132 Atom
133 Lookup::clef (String st) const
134 {
135   return afm_find (String ("clefs") + String ("-") + st);
136 }
137
138 Atom
139 Lookup::dots () const
140 {
141   return afm_find (String ("dots") + String ("-") + String ("dot"));
142 }
143
144 Atom
145 Lookup::dynamic (String st) const
146 {
147   return (*symtables_p_) ("dynamics")->lookup (st);
148 }
149
150 Atom
151 Lookup::fill (Box b) const
152 {
153   Atom a;
154   a.dim_ = b;
155   return a;
156 }
157
158 Atom
159 Lookup::flag (int j, Direction d) const
160 {
161   char c = (d == UP) ? 'u' : 'd';
162   return afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
163 }
164
165 void
166 Lookup::print () const
167 {
168 #ifndef NPRINT
169   DOUT << "Lookup {\n";
170   symtables_p_->print ();
171   DOUT << "}\n";
172 #endif
173 }
174
175 String
176 Lookup::print_dimen (Real r) const
177 {
178   return paper_l_->dimension_str (r);
179 }
180
181 Atom
182 Lookup::rest (int j, bool o) const
183 {
184    return afm_find (String ("rests")
185                     + String ("-") + to_str (j) + (o ? "o" : ""));
186 }
187
188 Atom
189 Lookup::rule_symbol (Real height, Real width) const
190 {
191   Atom bs= (*symtables_p_) ("param")->lookup ("rule");
192   Array<String> args;
193   args.push (print_dimen (height));
194   args.push (print_dimen (width));
195   bs.str_ = substitute_args (bs.str_, args);
196   bs.dim_.x () = Interval (0, width);
197   bs.dim_.y () = Interval (0, height);
198   return bs;
199 }
200
201 Atom
202 Lookup::script (String str) const
203 {
204   return afm_find (String ("scripts") + String ("-") + str);
205 }
206
207 Atom
208 Lookup::special_time_signature (String s, Array<Scalar> arr) const
209 {
210   String symbolname = "timesig-"+s+"%/%";
211   Atom a (afm_find (substitute_args (symbolname, arr)));
212   if (!a.empty ()) 
213     return a;
214   // Try if the full name was given
215   a = afm_find ("timesig-"+s);
216   if (!a.empty ()) 
217     return a;
218   // Resort to default layout with numbers
219   return time_signature (arr);
220 }
221
222 static void
223 substitute_arg (String& r, String arg)
224 {
225   int p = r.index_i ('%');
226   if (p < 0)
227         return ;
228
229   r = r.left_str (p) + arg + r.right_str (r.length_i () - p - 1);
230 }
231
232 String
233 Lookup::substitute_args (String source, Array<String> args) const
234 {
235   String str (source);
236   for (int i = 0 ; i < args.size (); i++)
237     substitute_arg (str, args[i]);
238   return str;
239 }
240
241 String
242 Lookup::substitute_args (String source, Array<Scalar> args) const
243 {
244   Array<String> sv;
245   for (int i = 0 ; i < args.size (); i++)
246     sv.push (args[i]);
247   return substitute_args (source, sv);
248 }
249
250 Atom
251 Lookup::stem (Real y1, Real y2, String str) const
252 {
253   if (y1 > y2)
254     {
255       Real t = y1;
256       y1 = y2;
257       y2 = t;
258     }
259   Atom s;
260
261   s.dim_.x () = Interval (0,0);
262   s.dim_.y () = Interval (y1,y2);
263
264   Array<String> a;
265
266   Real stem_width = paper_l_->get_var ("stemthickness");
267   a.push (print_dimen (-stem_width /2));
268   a.push (print_dimen (stem_width));
269   a.push (print_dimen (y2));
270   a.push (print_dimen (-y1));
271
272   s.str_ = substitute_args (str, a);
273   s.font_ = font_;
274   return s;
275 }
276
277 Atom
278 Lookup::streepje (int type) const
279 {
280   if (type > 2)
281     type = 2;
282
283   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
284 }
285
286 Atom
287 Lookup::text (String style, String text) const
288 {
289   Array<String> a;
290
291   a.push (text);
292   Atom s =  (*symtables_p_) ("style")->lookup (style);
293   s.str_ = substitute_args (s.str_,a);
294   s.font_ = font_;
295
296   return s;
297 }
298
299 Atom
300 Lookup::time_signature (Array<Scalar> a) const
301 {
302   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
303   s.str_ = substitute_args (s.str_, a);
304
305   return s;
306 }
307
308 /*
309   should be handled via Tex_ code and Lookup::bar ()
310  */
311 Atom
312 Lookup::vbrace (Real &y) const
313 {
314   Atom brace = (*symtables_p_) ("param")->lookup ( "brace");
315   Interval ydims = brace.dim_[Y_AXIS];
316   Real min_y = ydims[LEFT];
317   Real max_y = ydims[RIGHT];
318   Real step = 1.0 PT;
319  
320   if (y < min_y)
321     {
322       warning (_ ("piano brace") 
323         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
324       y = min_y;
325     }
326   if (y > max_y)
327     {
328       warning (_ ("piano brace")
329        + " " + _ ("too big") + " (" + print_dimen (y) + ")");
330       y = max_y;
331     }
332
333   
334   int idx = int (rint ( (y- min_y)/step)) + 1;
335   
336   {
337     Array<String> a;
338     a.push (character_str (idx));
339     brace.str_ = substitute_args (brace.str_,a);
340     brace.dim_[Y_AXIS] = Interval (-y/2,y/2);
341   }
342
343   brace.font_ = font_;
344
345   return brace;
346 }
347
348