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