]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
patch::: 1.0.12.jcn2: opniewvisite
[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 "symtable.hh"
18 #include "dimension.hh"
19 #include "tex.hh"
20 #include "scalar.hh"
21 #include "paper-def.hh"
22 #include "string-convert.hh"
23 #include "main.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) 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   a.dim_ = m.B_;
82   a.dim_[X_AXIS] *= 1 / 1000.0;
83   a.dim_[Y_AXIS] *= 1 / 1000.0;
84   a.str_ = String_convert::form_str (str.ch_C (), m.code ());
85   a.font_ = font_;
86   return a;
87 }
88
89 Atom
90 Lookup::ball (int j) const
91 {
92   if (j > 2)
93     j = 2;
94
95   return afm_find (String ("balls") + String ("-") + to_str (j));
96 }
97
98 Atom
99 Lookup::bar (String str, Real h) const
100 {
101   Array<String> a;
102   a.push (print_dimen (h));
103   Atom s = (*symtables_p_) ("bars")->lookup (str);
104   s.str_ = substitute_args (s.str_, a);
105   s.dim_.y () = Interval (-h/2, h/2);
106   s.font_ = font_;
107   return s;
108 }
109
110 Atom 
111 Lookup::beam (Real slope, Real width, Real thick) const
112 {
113   Atom a (ps_beam (slope, width, thick));
114   Real height = slope * width; 
115   Real min_y = (0 <? height) - thick/2;
116   Real max_y = (0 >? height) + thick/2;
117   
118   a.dim_[X_AXIS] = Interval (0, width);
119   a.dim_[Y_AXIS] = Interval (min_y, max_y);
120   return a;
121 }
122
123 Atom
124 Lookup::clef (String st) const
125 {
126   return afm_find (String ("clefs") + String ("-") + st);
127 }
128
129 Atom
130 Lookup::dots () const
131 {
132   return afm_find (String ("dots") + String ("-") + String ("dot"));
133 }
134
135 Atom
136 Lookup::dynamic (String st) const
137 {
138   return (*symtables_p_) ("dynamics")->lookup (st);
139 }
140
141 Atom
142 Lookup::fill (Box b) const
143 {
144   Atom a;
145   a.dim_ = b;
146   return a;
147 }
148
149 Atom
150 Lookup::flag (int j, Direction d) const
151 {
152   char c = (d == UP) ? 'u' : 'd';
153   return afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
154 }
155
156 void
157 Lookup::print () const
158 {
159 #ifndef NPRINT
160   DOUT << "Lookup {\n";
161   symtables_p_->print ();
162   DOUT << "}\n";
163 #endif
164 }
165
166 Atom
167 Lookup::rest (int j, bool o) const
168 {
169    return afm_find (String ("rests")
170                     + String ("-") + to_str (j) + (o ? "o" : ""));
171 }
172
173 Atom
174 Lookup::rule_symbol (Real height, Real width) const
175 {
176   Atom bs= (*symtables_p_) ("param")->lookup ("rule");
177   Array<String> args;
178   args.push (print_dimen (height));
179   args.push (print_dimen (width));
180   bs.str_ = substitute_args (bs.str_,args);
181   bs.dim_.x () = Interval (0,width);
182   bs.dim_.y () = Interval (0,height);
183   return bs;
184 }
185
186 Atom
187 Lookup::script (String str) const
188 {
189   return afm_find (String ("scripts") + String ("-") + str);
190 }
191
192 Atom
193 Lookup::special_time_signature (String s) const
194 {
195   Atom a (afm_find ("timesig-"+ s));
196   return a;
197 }
198
199 Atom
200 Lookup::stem (Real y1, Real y2, String str) const
201 {
202   if (y1 > y2)
203     {
204       Real t = y1;
205       y1 = y2;
206       y2 = t;
207     }
208   Atom s;
209
210   s.dim_.x () = Interval (0,0);
211   s.dim_.y () = Interval (y1,y2);
212
213   Array<String> a;
214
215   Real stem_width = paper_l_->get_var ("stemthickness");
216   a.push (print_dimen (-stem_width /2));
217   a.push (print_dimen (stem_width));
218   a.push (print_dimen (y2));
219   a.push (print_dimen (-y1));
220
221   s.str_ = substitute_args (str, a);
222   s.font_ = font_;
223   return s;
224 }
225
226 Atom
227 Lookup::streepje (int type) const
228 {
229   if (type > 2)
230     type = 2;
231
232   return  afm_find ("balls" + String ("-") +to_str (type) + "l");
233 }
234
235 Atom
236 Lookup::text (String style, String text) const
237 {
238   Array<String> a;
239
240   a.push (text);
241   Atom s =  (*symtables_p_) ("style")->lookup (style);
242   s.str_ = substitute_args (s.str_,a);
243   s.font_ = font_;
244
245   return s;
246 }
247
248 Atom
249 Lookup::time_signature (Array<Scalar> a) const
250 {
251   Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
252   s.str_ = substitute_args (s.str_, a);
253
254   return s;
255 }
256
257 /*
258   should be handled via Tex_ code and Lookup::bar ()
259  */
260 Atom
261 Lookup::vbrace (Real &y) const
262 {
263   Atom brace = (*symtables_p_) ("param")->lookup ( "brace");
264   Interval ydims = brace.dim_[Y_AXIS];
265   Real min_y = ydims[LEFT];
266   Real max_y = ydims[RIGHT];
267   Real step = 1.0 PT;
268  
269   if (y < min_y)
270     {
271       warning (_ ("piano brace") 
272         + " " + _ ("too small") +  " (" + print_dimen (y) + ")");
273       y = min_y;
274     }
275   if (y > max_y)
276     {
277       warning (_ ("piano brace")
278        + " " + _ ("too big") + " (" + print_dimen (y) + ")");
279       y = max_y;
280     }
281
282   
283   int idx = int (rint ( (y- min_y)/step)) + 1;
284   
285   {
286     Array<String> a;
287     a.push (to_str (idx));
288     brace.str_ = substitute_args (brace.str_,a);
289     brace.dim_[Y_AXIS] = Interval (-y/2,y/2);
290   }
291
292   brace.font_ = font_;
293
294   return brace;
295 }
296
297