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