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