]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
07e7e63ca4ddeb5578e2cb1e30f83d78514a5b9e
[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 #include "lily-guile.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, bool warn) 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, warn);
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 String
114 Lookup::base_output_str () const
115 {
116   assert (paper_l_);
117   String str = paper_l_->get_default_output ();
118
119   if (str.empty_b ())
120     {
121       str = default_outname_base_global;
122       int def = paper_l_->get_next_default_count ();
123       if (def)
124         str += "-" + to_str (def);
125     }
126   return str;
127 }
128
129 Atom 
130 Lookup::beam (Real slope, Real width, Real thick) const
131 {
132   Atom a (ps_beam (slope, width, thick));
133   Real height = slope * width; 
134   Real min_y = (0 <? height) - thick/2;
135   Real max_y = (0 >? height) + thick/2;
136   
137   a.dim_[X_AXIS] = Interval (0, width);
138   a.dim_[Y_AXIS] = Interval (min_y, max_y);
139   return a;
140 }
141
142 String
143 Lookup::character_str (int i) const
144 {
145   return to_str (i);
146 }
147
148 Atom
149 Lookup::clef (String st) const
150 {
151   return afm_find (String ("clefs") + String ("-") + st);
152 }
153
154 Atom
155 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
156 {
157   assert (controls.size () == 8);
158
159   Real dx = controls[3].x () - controls[0].x ();
160   Real dy = controls[3].y () - controls[0].y ();
161
162   Atom a;
163   a.font_ = font_;
164   a.dim_[X_AXIS] = Interval (0, dx);
165   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
166
167 #ifndef HAVE_LIBGUILE
168
169   String ps;
170   for (int i = 1; i < 4; i++)
171     ps += String_convert::double_str (controls[i].x ()) + " "
172       + String_convert::double_str (controls[i].y ()) + " ";
173
174   ps += String_convert::double_str (controls[0].x ()) + " "
175     + String_convert::double_str (controls[0].y ()) + " ";
176
177   ps += String_convert::double_str (thick) + " ";
178   Real on = dash > 1? thick * dash - thick : 0;
179   Real off = 2 * thick;
180   ps += "[" + String_convert::double_str (on) + " ";
181   ps += String_convert::double_str (off) + "] ";
182   ps += String_convert::int_str (0) + " ";
183   ps += "draw_dashed_slur ";
184
185   a.str_ = ps;
186
187 #else // HAVE_LIBGUILE
188
189   // (lambda (o) (dashed-slur o '((0.1 0.2) (1.1 1.2) (2.1 2.2) (3.1 3.2))))
190   a.lambda_ = 
191     gh_append (gh_lambda_o (), 
192     gh_list1 (gh_append (gh_func_o ("dashed-slur"),
193     gh_cons (gh_double2scm (thick), gh_cons (gh_double2scm (dash),
194     gh_list1 (gh_list2 (gh_quote (),
195     gh_cons (gh_list2 (gh_double2scm (controls[0].x ()), gh_double2scm (controls[0].y ())),
196     gh_cons (gh_list2 (gh_double2scm (controls[1].x ()), gh_double2scm (controls[1].y ())),
197     gh_cons (gh_list2 (gh_double2scm (controls[2].x ()), gh_double2scm (controls[2].y ())),
198     gh_cons (gh_list2 (gh_double2scm (controls[3].x ()), gh_double2scm (controls[3].y ())),
199     SCM_EOL)))))))))));
200
201 #endif // HAVE_LIBGUILE
202
203   return a;
204 }
205
206 Atom
207 Lookup::dots () const
208 {
209   return afm_find (String ("dots") + String ("-") + String ("dot"));
210 }
211
212 Atom
213 Lookup::dynamic (String st) const
214 {
215   return (*symtables_p_) ("dynamics")->lookup (st);
216 }
217
218 Atom
219 Lookup::fill (Box b) const
220 {
221   Atom a;
222   a.dim_ = b;
223   return a;
224 }
225
226 Atom
227 Lookup::flag (int j, Direction d) const
228 {
229   char c = (d == UP) ? 'u' : 'd';
230   return afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
231 }
232
233 void
234 Lookup::print () const
235 {
236 #ifndef NPRINT
237   DOUT << "Lookup {\n";
238   symtables_p_->print ();
239   DOUT << "}\n";
240 #endif
241 }
242
243 String
244 Lookup::print_dimen (Real r) const
245 {
246   String s = to_str (r, "%.3f");
247   if (s.index_i ("NaN") != -1)
248     {
249       warning (_ ("NaN"));
250       s = "0.0";
251     }
252   return s;
253 }
254
255 Atom
256 Lookup::rest (int j, bool o) const
257 {
258    return afm_find (String ("rests")
259                     + String ("-") + to_str (j) + (o ? "o" : ""));
260 }
261
262 Atom
263 Lookup::rule_symbol (Real height, Real width) const
264 {
265   Atom bs= (*symtables_p_) ("param")->lookup ("rule");
266   Array<String> args;
267   args.push (print_dimen (height));
268   args.push (print_dimen (width));
269   bs.str_ = substitute_args (bs.str_, args);
270   bs.dim_.x () = Interval (0, width);
271   bs.dim_.y () = Interval (0, height);
272   return bs;
273 }
274
275 Atom
276 Lookup::script (String str) const
277 {
278   return afm_find (String ("scripts") + String ("-") + str);
279 }
280
281 Atom
282 Lookup::special_time_signature (String s, Array<Scalar> arr) const
283 {
284   String symbolname = "timesig-"+s+"%/%";
285   Atom a (afm_find (substitute_args (symbolname, arr), false));
286   if (!a.empty ()) 
287     return a;
288   // Try if the full name was given
289   a = afm_find ("timesig-"+s, false);
290   if (!a.empty ()) 
291     return a;
292   // Resort to default layout with numbers
293   return time_signature (arr);
294 }
295
296 static void
297 substitute_arg (String& r, String arg)
298 {
299   int p = r.index_i ('%');
300   if (p < 0)
301         return ;
302
303   r = r.left_str (p) + arg + r.right_str (r.length_i () - p - 1);
304 }
305
306 String
307 Lookup::substitute_args (String source, Array<String> args) const
308 {
309   String str (source);
310   for (int i = 0 ; i < args.size (); i++)
311     substitute_arg (str, args[i]);
312   return str;
313 }
314
315 String
316 Lookup::substitute_args (String source, Array<Scalar> args) const
317 {
318   Array<String> sv;
319   for (int i = 0 ; i < args.size (); i++)
320     sv.push (args[i]);
321   return substitute_args (source, sv);
322 }
323
324 Atom
325 Lookup::stem (Real y1, Real y2, String str) const
326 {
327   if (y1 > y2)
328     {
329       Real t = y1;
330       y1 = y2;
331       y2 = t;
332     }
333   Atom s;
334
335   s.dim_.x () = Interval (0,0);
336   s.dim_.y () = Interval (y1,y2);
337
338   Array<String> a;
339
340   Real stem_width = paper_l_->get_var ("stemthickness");
341   a.push (print_dimen (-stem_width /2));
342   a.push (print_dimen (stem_width));
343   a.push (print_dimen (y2));
344   a.push (print_dimen (-y1));
345
346   s.str_ = substitute_args (str, a);
347   s.font_ = font_;
348   return s;
349 }
350
351 Atom
352 Lookup::streepje (int type) const
353 {
354   if (type > 2)
355     type = 2;
356
357   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
358 }
359
360 Atom
361 Lookup::text (String style, String text) const
362 {
363   Array<String> a;
364
365   a.push (text);
366   Atom s =  (*symtables_p_) ("style")->lookup (style);
367   s.str_ = substitute_args (s.str_,a);
368   s.font_ = font_;
369
370   return s;
371 }
372
373 Atom
374 Lookup::time_signature (Array<Scalar> a) const
375 {
376   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
377   s.str_ = substitute_args (s.str_, a);
378
379   return s;
380 }
381
382 /*
383   should be handled via Tex_ code and Lookup::bar ()
384  */
385 Atom
386 Lookup::vbrace (Real &y) const
387 {
388   Atom brace = (*symtables_p_) ("param")->lookup ( "brace");
389   Interval ydims = brace.dim_[Y_AXIS];
390   Real min_y = ydims[LEFT];
391   Real max_y = ydims[RIGHT];
392   Real step = 1.0 PT;
393  
394   if (y < min_y)
395     {
396       warning (_ ("piano brace") 
397         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
398       y = min_y;
399     }
400   if (y > max_y)
401     {
402       warning (_ ("piano brace")
403        + " " + _ ("too big") + " (" + print_dimen (y) + ")");
404       y = max_y;
405     }
406
407   
408   int idx = int (rint ( (y- min_y)/step)) + 1;
409   
410   {
411     Array<String> a;
412     a.push (character_str (idx));
413     brace.str_ = substitute_args (brace.str_,a);
414     brace.dim_[Y_AXIS] = Interval (-y/2,y/2);
415   }
416
417   brace.font_ = font_;
418
419   return brace;
420 }
421
422