]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.1.2
[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 Molecule
57 Lookup::accidental (int j, bool cautionary) const
58 {
59   Molecule m(afm_find (String ("accidentals") + String ("-") + to_str (j)));
60   if (cautionary) 
61     {
62       m.add_at_edge(X_AXIS, LEFT, 
63                     Molecule(afm_find (String ("accidentals") + String ("-("))))
64 ;
65       m.add_at_edge(X_AXIS, RIGHT, 
66                     Molecule(afm_find (String ("accidentals") + String ("-)"))))
67 ;
68     }
69   return m;
70 }
71
72 void
73 Lookup::add (String s, Symtable*p)
74 {
75   symtables_p_->add (s, p);
76 }
77
78 Atom
79 Lookup::afm_find (String s, bool warn) const
80 {
81   if (!afm_p_)
82     {
83       *mlog << "[" << font_path_;
84       ( (Lookup*)this)->afm_p_ = new Adobe_font_metric (read_afm (font_path_));
85       *mlog << "]" << flush ;
86       DOUT << this->afm_p_->str ();
87     }
88   Adobe_font_char_metric m = afm_p_->find_char (s, warn);
89
90   Atom a;
91   if (m.code () < 0)
92     return a;
93   
94   a.dim_ = m.B_;
95   a.dim_[X_AXIS] *= 1 / 1000.0;
96   a.dim_[Y_AXIS] *= 1 / 1000.0;
97   Array<Real> arr;
98   arr.push (m.code ());
99   a.lambda_ =  (lambda_scm ("char", arr));
100   a.str_ = "afm_find: " + s;
101   a.font_ = font_;
102   return a;
103 }
104
105 Atom
106 Lookup::ball (int j) const
107 {
108   if (j > 2)
109     j = 2;
110
111   return afm_find (String ("balls") + String ("-") + to_str (j));
112 }
113
114 Atom
115 Lookup::bar (String str, Real h) const
116 {
117   Array<Real> arr;
118   arr.push (h);
119   Atom a = (*symtables_p_) ("bars")->lookup (str);
120   a.lambda_ =  (lambda_scm (a.str_, arr));
121   a.str_ = "bar";
122   a.dim_.y () = Interval (-h/2, h/2);
123   a.font_ = font_;
124   return a;
125 }
126
127 Atom 
128 Lookup::beam (Real slope, Real width, Real thick) const
129 {
130   Real height = slope * width; 
131   Real min_y = (0 <? height) - thick/2;
132   Real max_y = (0 >? height) + thick/2;
133
134   Array<Real> arr;
135   arr.push (width);
136   arr.push (slope);
137   arr.push (thick);
138
139   Atom a;
140   a.lambda_ =  (lambda_scm ("beam", arr));
141   a.str_ = "beam";
142   a.dim_[X_AXIS] = Interval (0, width);
143   a.dim_[Y_AXIS] = Interval (min_y, max_y);
144   return a;
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   // (lambda (o) (dashed-slur o '((0.1 0.2) (1.1 1.2) (2.1 2.2) (3.1 3.2))))
167   a.lambda_ =  (
168     ly_append (ly_lambda_o (), 
169     ly_list1 (ly_append (ly_func_o ("dashed-slur"),
170     gh_cons (gh_double2scm (thick), gh_cons (gh_double2scm (dash),
171     ly_list1 (ly_list2 (ly_quote (),
172     gh_cons (ly_list2 (gh_double2scm (controls[1].x ()), gh_double2scm (controls[1].y ())),
173     gh_cons (ly_list2 (gh_double2scm (controls[2].x ()), gh_double2scm (controls[2].y ())),
174     gh_cons (ly_list2 (gh_double2scm (controls[3].x ()), gh_double2scm (controls[3].y ())),
175     gh_cons (ly_list2 (gh_double2scm (controls[0].x ()), gh_double2scm (controls[0].y ())),
176     SCM_EOL))))))))))));
177
178   a.str_ = "dashed_slur";
179   return a;
180 }
181
182 Atom
183 Lookup::dots () const
184 {
185   return afm_find (String ("dots") + String ("-") + String ("dot"));
186 }
187
188 Atom
189 Lookup::dynamic (String st) const
190 {
191   return (*symtables_p_) ("dynamics")->lookup (st);
192 }
193
194 Atom
195 Lookup::fill (Box b) const
196 {
197   Atom a;
198   a.dim_ = b;
199   return a;
200 }
201
202 Atom
203 Lookup::flag (int j, Direction d) const
204 {
205   char c = (d == UP) ? 'u' : 'd';
206   Atom a = afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
207   return a;
208 }
209
210 void
211 Lookup::print () const
212 {
213 #ifndef NPRINT
214   DOUT << "Lookup {\n";
215   symtables_p_->print ();
216   DOUT << "}\n";
217 #endif
218 }
219
220 Atom
221 Lookup::rest (int j, bool o) const
222 {
223    return afm_find (String ("rests")
224                     + String ("-") + to_str (j) + (o ? "o" : ""));
225 }
226
227 Atom
228 Lookup::rule_symbol (Real height, Real width) const
229 {
230   Atom a = (*symtables_p_) ("param")->lookup ("rule");
231   Array<Real> arr;
232   arr.push (height);
233   arr.push (width);
234   a.lambda_ = (lambda_scm (a.str_, arr));
235   a.str_ = "rule_symbol";
236   a.dim_.x () = Interval (0, width);
237   a.dim_.y () = Interval (0, height);
238   return a;
239 }
240
241 Atom
242 Lookup::script (String str) const
243 {
244   return afm_find (String ("scripts") + String ("-") + str);
245 }
246
247 Atom
248 Lookup::special_time_signature (String s, Array<int> arr) const
249 {
250   // First guess: s contains only the signature style
251   assert (arr.size () >1);
252   String symbolname = "timesig-" + s + to_str (arr[0]) + "/" + to_str (arr[1]);
253   
254   Atom a = afm_find (symbolname, false);
255   if (!a.empty ()) 
256     return a;
257
258   // Second guess: s contains the full signature name
259   a = afm_find ("timesig-"+s, false);
260   if (!a.empty ()) 
261     return a;
262
263   // Resort to default layout with numbers
264   return time_signature (arr);
265 }
266
267 Atom
268 Lookup::stem (Real y1, Real y2) const
269 {
270   if (y1 > y2)
271     {
272       Real t = y1;
273       y1 = y2;
274       y2 = t;
275     }
276   Atom a;
277
278   a.dim_.x () = Interval (0,0);
279   a.dim_.y () = Interval (y1,y2);
280
281   Array<Real> arr;
282
283   Real stem_width = paper_l_->get_var ("stemthickness");
284   arr.push (-stem_width /2);
285   arr.push (stem_width);
286   arr.push (y2);
287   arr.push (-y1);
288
289   a.lambda_ = (lambda_scm ("stem", arr));
290   a.str_ = "stem";
291   a.font_ = font_;
292   return a;
293 }
294
295 Atom
296 Lookup::streepje (int type) const
297 {
298   if (type > 2)
299     type = 2;
300
301   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
302 }
303
304 Atom
305 Lookup::text (String style, String text) const
306 {
307   Array<Scalar> arr;
308
309   arr.push (text);
310   Atom a =  (*symtables_p_) ("style")->lookup (style);
311   a.lambda_ = lambda_scm (a.str_, arr);
312   a.str_ = "text";
313   a.font_ = font_;
314   return a;
315    
316 }
317   
318
319 Atom
320 Lookup::time_signature (Array<int> a) const
321 {
322   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
323   s.lambda_ =  (lambda_scm (s.str_, a));
324
325   return s;
326 }
327
328 /*
329   should be handled via Tex_ code and Lookup::bar ()
330  */
331 Atom
332 Lookup::vbrace (Real &y) const
333 {
334   Atom a = (*symtables_p_) ("param")->lookup ( "brace");
335   Interval ydims = a.dim_[Y_AXIS];
336   Real min_y = ydims[LEFT];
337   Real max_y = ydims[RIGHT];
338   Real step = 1.0 PT;
339  
340   if (y < min_y)
341     {
342       warning (_ ("piano brace") 
343                + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
344       y = min_y;
345     }
346   if (y > max_y)
347     {
348       warning (_ ("piano brace")
349                + " " + _ ("too big") + " (" + print_dimen (y) + ")");
350       y = max_y;
351     }
352
353   
354   int idx = int (rint ( (y- min_y)/step)) + 1;
355   
356   Array<Real> arr;
357   arr.push (idx);
358   a.lambda_ = (lambda_scm (a.str_, arr));
359   a.str_ = "brace";
360   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
361   a.font_ = font_;
362   return a;
363 }
364
365 Atom
366 Lookup::hairpin (Real width, bool decresc, bool continued) const
367 {
368   Atom a;  
369   Real height = paper_l_->staffheight_f () / 6;
370   String ps;
371   ps += to_str (width) + " " 
372     + to_str (height) + " " 
373     + to_str (continued ? height/2 : 0) + 
374     + " draw_"  + String (decresc ? "de" : "") + "cresc\n";
375   a.str_ = ps;
376
377
378   a.dim_.x () = Interval (0, width);
379   a.dim_.y () = Interval (-2*height, 2*height);
380   a.font_ = font_;
381   return a;
382 }
383
384 Atom
385 Lookup::plet (Real dy , Real dx, Direction dir) const
386 {
387   String ps;
388   
389   
390   ps += String_convert::double_str (dx) + " " 
391     + String_convert::double_str (dy) + " "
392     + String_convert::int_str ( (int)dir) +
393     " draw_plet ";
394
395   Atom a;
396   a.str_ = ps;
397   return a;
398 }
399
400 Atom
401 Lookup::slur (Array<Offset> controls) const
402 {
403   assert (controls.size () == 8);
404
405   String ps;
406   
407   Real dx = controls[3].x () - controls[0].x ();
408   Real dy = controls[3].y () - controls[0].y ();
409   Atom a;
410  
411   // (lambda (o) (slur o '((0.1 0.2) (1.1 1.2) (2.1 2.2) (3.1 3.2) .. )))
412   a.lambda_ =  (
413                 ly_append (ly_lambda_o (), 
414                            ly_list1 (ly_append (ly_func_o ("slur"),
415                                                 ly_list1 (ly_list2 (ly_quote (),
416                                                                     gh_cons (ly_list2 (gh_double2scm (controls[5].x ()), gh_double2scm (controls[5].y ())),
417                                                                              gh_cons (ly_list2 (gh_double2scm (controls[6].x ()), gh_double2scm (controls[6].y ())),
418                                                                                       gh_cons (ly_list2 (gh_double2scm (controls[7].x ()), gh_double2scm (controls[7].y ())),
419                                                                                                gh_cons (ly_list2 (gh_double2scm (controls[4].x ()), gh_double2scm (controls[4].y ())),
420                                                                                                         gh_cons (ly_list2 (gh_double2scm (controls[1].x ()), gh_double2scm (controls[1].y ())),
421                                                                                                                  gh_cons (ly_list2 (gh_double2scm (controls[2].x ()), gh_double2scm (controls[2].y ())),
422                                                                                                                           gh_cons (ly_list2 (gh_double2scm (controls[3].x ()), gh_double2scm (controls[3].y ())),
423                                                                                                                                    gh_cons (ly_list2 (gh_double2scm (controls[0].x ()), gh_double2scm (controls[0].y ())),
424                                                                                                                                             SCM_EOL))))))))))))));
425   a.str_ = "slur";
426
427   a.dim_[X_AXIS] = Interval (0, dx);
428   a.dim_[Y_AXIS] = Interval (0 <? dy,  0 >? dy);
429   a.font_ = font_;
430   return a;
431 }
432
433 Atom
434 Lookup::vbracket (Real &y) const
435 {
436   Atom a;
437   Real min_y = paper_l_->staffheight_f ();
438   if (y < min_y)
439     {
440       warning (_ ("bracket")
441                + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
442       //      y = min_y;
443     }
444   Array<Real> arr;
445   arr.push (y);
446   a.lambda_ =  (lambda_scm ("bracket", arr));
447   a.str_ = "vbracket";
448   a.dim_[Y_AXIS] = Interval (-y/2,y/2);
449   a.dim_[X_AXIS] = Interval (0,4 PT);
450   return a;
451 }
452
453