lilypond-1.1.55
[lilypond.git] / lily / tfm-reader.cc
bloba4ddd108e9deb4ba25a7b3afa965fcea5adb424d
1 /*
2 tfm-reader.cc -- implement Tex_font_metric_reader
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
9 some code shamelessly copied from GNU fontutils-0.6/tfm/tfm_input.c
12 #include "tfm-reader.hh"
13 #include "string-convert.hh"
14 #include "debug.hh"
15 #include "warn.hh"
17 #define format_str String_convert::form_str
18 #define FIX_UNITY (1 << 20)
19 static const Real fix_to_real (Fix f);
22 Tex_font_metric_reader::Tex_font_metric_reader (String name)
23 : input_ (name)
25 tfm_.clear (TFM_SIZE);
28 Tex_font_metric
29 Tex_font_metric_reader::read_tfm ()
31 read_header ();
32 read_params ();
33 read_char_metrics ();
34 return tfm_;
38 static const Real
39 fix_to_real (Fix f)
41 Real r = f / FIX_UNITY + ((Real) (f % FIX_UNITY) / (Real) FIX_UNITY);
42 return r;
45 /* Most quantities are fixed-point fractions. */
47 Real
48 Tex_font_metric_reader::get_U32_fix_f ()
50 return fix_to_real (input_.get_U32 ());
53 /* Dimensions are a `Fix' scaled by the design size. */
55 Real
56 Tex_font_metric_reader::get_U32_fix_scaled_f ()
58 return get_U32_fix_f () * tfm_.info_.design_size;
61 String
62 Tex_font_metric_reader::get_bcpl_str ()
64 U8 length_u8 = input_.get_U8 ();
65 String str = input_.get_str (length_u8);
66 return str;
69 /* Here we read the information at the beginning of the file. We store
70 the result into the static variables `global_info' and
71 `tfm_header'. */
72 void
73 Tex_font_metric_reader::read_header ()
75 U16 file_length = input_.get_U16 ();
76 (void) file_length;
77 U16 header_length = input_.get_U16 ();
79 tfm_.info_.first_charcode = input_.get_U16 ();
80 tfm_.info_.last_charcode = input_.get_U16 ();
81 U16 width_word_count = input_.get_U16 ();
82 U16 height_word_count = input_.get_U16 ();
83 U16 depth_word_count = input_.get_U16 ();
84 U16 italic_correction_word_count = input_.get_U16 ();
85 U16 lig_kern_word_count = input_.get_U16 ();
86 U16 kern_word_count = input_.get_U16 ();
87 (void)kern_word_count;
88 U16 extensible_word_count = input_.get_U16 ();
89 (void)extensible_word_count;
91 tfm_.header_.param_word_count = input_.get_U16 ();
92 tfm_.info_.parameter_count = tfm_.header_.param_word_count;
94 tfm_.header_.char_info_pos = (6 + header_length) * 4;
95 tfm_.header_.width_pos = tfm_.header_.char_info_pos
96 + (tfm_.info_.last_charcode
97 - tfm_.info_.first_charcode + 1) * 4;
98 tfm_.header_.height_pos = tfm_.header_.width_pos + width_word_count * 4;
99 tfm_.header_.depth_pos = tfm_.header_.height_pos + height_word_count * 4;
100 tfm_.header_.italic_correction_pos = tfm_.header_.depth_pos
101 + depth_word_count * 4;
102 tfm_.header_.lig_kern_pos = tfm_.header_.italic_correction_pos
103 + italic_correction_word_count * 4;
104 tfm_.header_.kern_pos = tfm_.header_.lig_kern_pos + lig_kern_word_count * 4;
105 /* We don't care about the extensible table. */
107 if (header_length < 2)
108 error (_f ("TFM header of `%s' has only %u word(s)",
109 input_.name_str ().ch_C (), header_length));
111 tfm_.info_.checksum = input_.get_U32 ();
112 tfm_.info_.design_size = get_U32_fix_f ();
114 /* Although the coding scheme might be interesting to the caller, the
115 font family and face byte probably aren't. So we don't read them. */
116 tfm_.info_.coding_scheme = header_length > 2
117 ? get_bcpl_str () : "unspecified";
119 DOUT << format_str ("TFM checksum = %u, design_size = %fpt, coding scheme = `%s'.\n",
120 tfm_.info_.checksum,
121 tfm_.info_.design_size,
122 tfm_.info_.coding_scheme.ch_C ());
125 /* Although TFM files are only usable by TeX if they have at least seven
126 parameters, that is not a requirement of the file format itself, so
127 we don't impose it. And they can have many more than seven, of
128 course. We do impose a limit of TFM_MAX_FONT_PARAMETERS. We assume
129 that `tfm_header' has already been filled in. */
131 void
132 Tex_font_metric_reader::read_params ()
134 /* If we have no font parameters at all, we're done. */
135 if (tfm_.header_.param_word_count == 0)
136 return;
138 //brrr
139 /* Move to the beginning of the parameter table in the file. */
140 input_.seek_ch_C (-4 * tfm_.header_.param_word_count);
142 /* It's unlikely but possible that this TFM file has more fontdimens
143 than we can deal with. */
144 if (tfm_.header_.param_word_count > TFM_MAX_FONTDIMENS)
146 warning (_f ("%s: TFM file has %u parameters, which is more than the
147 %u I can handle",
148 input_.name_str ().ch_C (),
149 tfm_.header_.param_word_count,
150 TFM_MAX_FONTDIMENS));
151 tfm_.header_.param_word_count = TFM_MAX_FONTDIMENS;
154 /* The first parameter is different than all the rest, because it
155 isn't scaled by the design size. */
156 tfm_.info_.parameters[(TFM_SLANT_PARAMETER) - 1] = get_U32_fix_f ();
158 for (Char_code i = 2; i <= tfm_.header_.param_word_count; i++)
159 tfm_.info_.parameters[i - 1] = get_U32_fix_scaled_f ();
161 #ifdef PRINT
162 for (Char_code i = 1; i <= tfm_.header_.param_word_count; i++)
163 DOUT << format_str ("TFM parameter %d: %.3f", i, tfm_.info_.parameters[i - 1]);
164 #endif
167 /* Read every character in the TFM file, storing the result in the
168 static `tfm_char_table'. We return a copy of that variable. */
170 void
171 Tex_font_metric_reader::read_char_metrics ()
173 for (int i = tfm_.info_.first_charcode; i <= tfm_.info_.last_charcode; i++)
175 Tex_font_char_metric tfm_char = read_char_metric (i);
176 if (tfm_char.exists_b_)
177 tfm_.ascii_to_metric_idx_[tfm_char.code_] = tfm_.char_metrics_.size ();
178 tfm_.char_metrics_.push (tfm_char);
182 /* Read the character CODE. If the character doesn't exist, return
183 NULL. If it does, save the information in `tfm_char_table', as well
184 as returning it. */
186 Tex_font_char_metric
187 Tex_font_metric_reader::read_char_metric (Char_code code)
189 Tex_font_char_metric tfm_char;
191 /* If the character is outside the declared bounds in the file, don't
192 try to read it. */
193 if (code < tfm_.info_.first_charcode || code > tfm_.info_.last_charcode)
194 return tfm_char;
196 //brr
197 /* Move to the appropriate place in the `char_info' array. */
198 input_.seek_ch_C (tfm_.header_.char_info_pos + (code - tfm_.info_.first_charcode) * 4);
200 /* Read the character. */
201 tfm_char = read_char ();
203 if (tfm_char.exists_b_)
204 tfm_char.code_ = code;
206 return tfm_char;
210 /* We assume we are positioned at the beginning of a `char_info' word.
211 We read that word to get the indexes into the dimension tables; then
212 we go read the tables to get the values (if the character exists). */
214 Tex_font_char_metric
215 Tex_font_metric_reader::read_char ()
217 /* Read the char_info word. */
218 U8 width_index = input_.get_U8 ();
220 U8 packed;
221 packed = input_.get_U8 ();
222 U8 height_index = (packed & 0xf0) >> 4;
223 U8 depth_index = packed & 0x0f;
225 packed = input_.get_U8 ();
226 U8 italic_correction_index = (packed & 0xfc) >> 6;
227 U8 tag = packed & 0x3;
229 U8 remainder = input_.get_U8 ();
231 Tex_font_char_metric tfm_char;
233 #define GET_CHAR_DIMEN(d) \
234 if (d##_index != 0) \
236 input_.seek_ch_C (tfm_.header_.##d##_pos + d##_index*4); \
237 tfm_char.d##_fix_ = input_.get_U32 (); \
238 tfm_char.d##_ = fix_to_real (tfm_char.d##_fix_) \
239 * tfm_.info_.design_size; \
242 GET_CHAR_DIMEN (width);
243 GET_CHAR_DIMEN (height);
244 GET_CHAR_DIMEN (depth);
245 GET_CHAR_DIMEN (italic_correction);
247 /* The other condition for a character existing is that it be between
248 the first and last character codes given in the header. We've
249 already assumed that's true (or we couldn't be positioned at a
250 `char_info_word'). */
251 tfm_char.exists_b_ = width_index != 0;
253 #ifdef PRINT
254 DOUT << format_str (" width = %f, height = %f, ",
255 tfm_char.width_, tfm_char.height_);
256 DOUT << format_str ("depth = %f, ic = %f.\n",
257 tfm_char.depth, tfm_char.italic_correction);
258 #endif
260 if (tag == 1)
262 input_.seek_ch_C (tfm_.header_.lig_kern_pos + remainder * 4);
263 read_lig_kern_program (&tfm_char.ligature_arr_, &tfm_char.kern_arr_);
266 /* We don't handle the other tags. */
267 return tfm_char;
270 /* Read a ligature/kern program at the current position, storing the
271 result into *LIGATURE and *KERN. We don't distinguish all the kinds
272 of ligatures that Metafont can output. */
274 #define STOP_FLAG 128
275 #define KERN_FLAG 128
277 void
278 Tex_font_metric_reader::read_lig_kern_program (Array <Tfm_ligature>* ligature_arr_p, Array <Tfm_kern>* kern_arr_p)
280 bool end_b;
284 end_b = input_.get_U8 () >= STOP_FLAG;
286 U8 next_char = input_.get_U8 ();
287 bool kern_step_b = input_.get_U8 () >= KERN_FLAG;
288 U8 remainder = input_.get_U8 ();
290 #ifdef PRINT
291 DOUT << format_str (" if next = %u (%c), ", next_char, next_char);
292 #endif
294 if (kern_step_b)
296 Tfm_kern kern_element;
297 kern_element.character = next_char;
299 char const* old_pos = input_.pos_ch_C ();
300 input_.seek_ch_C (tfm_.header_.kern_pos + remainder * 4);
301 kern_element.kern = get_U32_fix_scaled_f ();
302 input_.set_pos (old_pos);
304 kern_arr_p->push (kern_element);
306 #ifdef PRINT
307 DOUT << format_str ("kern %f.\n", kern_element.kern);
308 #endif
310 else
312 Tfm_ligature ligature_element;
313 ligature_element.character = next_char;
314 ligature_element.ligature = remainder;
315 ligature_arr_p->push (ligature_element);
317 #ifdef PRINT
318 DOUT format_str ("ligature %d (hex %x).\n",
319 ligature_element.ligature,
320 ligature_element.ligature);
321 #endif
323 } while (!end_b);