Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/include/efont/cff.hh

raw
1 // -*- related-file-name: "../../libefont/cff.cc" -*-
2 #ifndef EFONT_CFF_HH
3 #define EFONT_CFF_HH
4 #include <lcdf/hashmap.hh>
5 #include <efont/t1cs.hh>
6 class ErrorHandler;
7 namespace Efont {
8 class Type1Encoding;
9
10 class Cff { public:
11
12 class Dict;
13 class IndexIterator;
14 class Charset;
15 class FDSelect;
16 class FontParent;
17 class Font;
18 class CIDFont;
19 class ChildFont;
20
21 explicit Cff(const String& str, unsigned units_per_em,
22 ErrorHandler* errh = 0);
23 ~Cff();
24
25 bool ok() const { return _error >= 0; }
26 int error() const { return _error; }
27
28 const String& data_string() const { return _data_string; }
29 const uint8_t* data() const { return _data; }
30 int length() const { return _len; }
31
32 int nfonts() const { return _name_index.size(); }
33 PermString font_name(int i) const { return _name_index[i]; }
34 FontParent* font(PermString = PermString(), ErrorHandler* = 0);
35
36 enum { NSTANDARD_STRINGS = 391, MAX_SID = 64999 };
37 int max_sid() const { return NSTANDARD_STRINGS - 1 + _strings.size(); }
38 int sid(PermString);
39 String sid_string(int sid) const;
40 PermString sid_permstring(int sid) const;
41
42 int ngsubrs() const { return _gsubrs_index.nitems(); }
43 Charstring* gsubr(int i);
44
45 unsigned units_per_em() const { return _units_per_em; }
46
47 enum DictOperator {
48 oVersion = 0, oNotice = 1, oFullName = 2, oFamilyName = 3,
49 oWeight = 4, oFontBBox = 5, oBlueValues = 6, oOtherBlues = 7,
50 oFamilyBlues = 8, oFamilyOtherBlues = 9, oStdHW = 10, oStdVW = 11,
51 oUniqueID = 13, oXUID = 14, oCharset = 15, oEncoding = 16,
52 oCharStrings = 17, oPrivate = 18, oSubrs = 19, oDefaultWidthX = 20,
53 oNominalWidthX = 21,
54 oCopyright = 32 + 0, oIsFixedPitch = 32 + 1, oItalicAngle = 32 + 2,
55 oUnderlinePosition = 32 + 3, oUnderlineThickness = 32 + 4,
56 oPaintType = 32 + 5, oCharstringType = 32 + 6, oFontMatrix = 32 + 7,
57 oStrokeWidth = 32 + 8, oBlueScale = 32 + 9, oBlueShift = 32 + 10,
58 oBlueFuzz = 32 + 11, oStemSnapH = 32 + 12, oStemSnapV = 32 + 13,
59 oForceBold = 32 + 14, oLanguageGroup = 32 + 17,
60 oExpansionFactor = 32 + 18, oInitialRandomSeed = 32 + 19,
61 oSyntheticBase = 32 + 20, oPostScript = 32 + 21,
62 oBaseFontName = 32 + 22, oBaseFontBlend = 32 + 23,
63 oROS = 32 + 30, oCIDFontVersion = 32 + 31, oCIDFontRevision = 32 + 32,
64 oCIDFontType = 32 + 33, oCIDCount = 32 + 34, oUIDBase = 32 + 35,
65 oFDArray = 32 + 36, oFDSelect = 32 + 37, oFontName = 32 + 38,
66 oLastOperator = oFontName
67 };
68
69 enum DictType {
70 tNone = 0, tSID, tFontNumber, tBoolean, tNumber, tOffset, tLocalOffset,
71 tArray, tArray2, tArray3, tArray4, tArray5, tArray6, tPrivateType,
72 tTypeMask = 0x7F, tPrivate = 0x80, tP = tPrivate
73 };
74
75 static const char* const operator_names[];
76 static const int operator_types[];
77
78
79 class IndexIterator { public:
80
81 IndexIterator() : _offset(0), _last_offset(0), _offsize(-1) { }
82 IndexIterator(const uint8_t*, int, int, ErrorHandler* = 0, const char* index_name = "INDEX");
83
84 int error() const { return (_offsize < 0 ? _offsize : 0); }
85
86 typedef bool (IndexIterator::*unspecified_bool_type)() const;
87 bool live() const { return _offset < _last_offset; }
88 operator unspecified_bool_type() const {
89 return live() ? &IndexIterator::live : 0;
90 }
91 int nitems() const;
92
93 inline const uint8_t* operator*() const;
94 inline const uint8_t* operator[](int) const;
95 const uint8_t* index_end() const;
96
97 void operator++() { _offset += _offsize; }
98 void operator++(int) { ++(*this); }
99
100 private:
101
102 const uint8_t* _contents;
103 const uint8_t* _offset;
104 const uint8_t* _last_offset;
105 int _offsize;
106
107 inline uint32_t offset_at(const uint8_t*) const;
108
109 };
110
111 private:
112
113 String _data_string;
114 const uint8_t* _data;
115 int _len;
116
117 int _error;
118
119 Vector<PermString> _name_index;
120
121 IndexIterator _top_dict_index;
122
123 IndexIterator _strings_index;
124 mutable Vector<PermString> _strings;
125 mutable HashMap<PermString, int> _strings_map;
126
127 IndexIterator _gsubrs_index;
128 Vector<Charstring*> _gsubrs_cs;
129 Vector<FontParent*> _fonts;
130
131 unsigned _units_per_em;
132
133 int parse_header(ErrorHandler*);
134
135 enum { HEADER_SIZE = 4 };
136
137 };
138
139
140 class Cff::Dict { public:
141
142 Dict();
143 Dict(Cff*, int pos, int dict_len, ErrorHandler* = 0, const char* dict_name = "DICT");
144 int assign(Cff*, int pos, int dict_len, ErrorHandler* = 0, const char* dict_name = "DICT");
145
146 bool ok() const { return _error >= 0; }
147 int error() const { return _error; }
148
149 int check(bool is_private, ErrorHandler* = 0, const char* dict_name = "DICT") const;
150
151 bool has(DictOperator) const;
152 inline bool has_first(DictOperator) const;
153 bool xvalue(DictOperator, Vector<double>&) const;
154 bool xvalue(DictOperator, int*) const;
155 bool xvalue(DictOperator, double*) const;
156 bool value(DictOperator, Vector<double>&) const;
157 bool value(DictOperator, int*) const;
158 bool value(DictOperator, double*) const;
159
160 void unparse(ErrorHandler*, const char*) const;
161
162 private:
163
164 Cff* _cff;
165 int _pos;
166 Vector<int> _operators;
167 Vector<int> _pointers;
168 Vector<double> _operands;
169 int _error;
170
171 };
172
173 class Cff::Charset { public:
174
175 Charset() : _error(-1) { }
176 Charset(const Cff*, int pos, int nglyphs, int max_sid, ErrorHandler* = 0);
177 void assign(const Cff*, int pos, int nglyphs, int max_sid, ErrorHandler* = 0);
178
179 int error() const { return _error; }
180
181 int nglyphs() const { return _sids.size(); }
182 int nsids() const { return _gids.size(); }
183
184 inline int gid_to_sid(int gid) const;
185 inline int sid_to_gid(int sid) const;
186
187 private:
188
189 Vector<int> _sids;
190 Vector<int> _gids;
191 int _error;
192
193 void assign(const int*, int, int);
194 int parse(const Cff*, int pos, int nglyphs, int max_sid, ErrorHandler*);
195
196 };
197
198 class Cff::FDSelect { public:
199
200 FDSelect() : _fds(0), _my_fds(false), _nglyphs(0), _error(-1) { }
201 ~FDSelect();
202 void assign(const Cff*, int pos, int nglyphs, ErrorHandler* = 0);
203
204 int error() const { return _error; }
205
206 int nglyphs() const { return _nglyphs; }
207 inline int gid_to_fd(int gid) const;
208
209 private:
210
211 const uint8_t* _fds;
212 bool _my_fds;
213 int _nglyphs;
214 int _error;
215
216 FDSelect(const FDSelect&);
217 FDSelect& operator=(const FDSelect&);
218 int parse(const Cff*, int pos, int nglyphs, ErrorHandler*);
219
220 };
221
222
223 class Cff::FontParent : public CharstringProgram { public:
224
225 FontParent(Cff* cff);
226
227 bool ok() const { return _error >= 0; }
228 int error() const { return _error; }
229
230 int ngsubrs_x() const { return _cff->ngsubrs(); }
231 int ngsubrs() const { return ngsubrs_x(); }
232 Charstring* gsubr(int) const;
233 int gsubr_bias() const;
234
235 private:
236
237 Cff* _cff;
238 int _charstring_type;
239 int _error;
240 int _font_index;
241
242 FontParent(const FontParent&);
243 FontParent& operator=(const FontParent&);
244
245 Charstring* charstring(const IndexIterator&, int) const;
246
247 friend class Cff;
248 friend class Cff::Font;
249 friend class Cff::CIDFont;
250 friend class Cff::ChildFont;
251
252 };
253
254 class Cff::CIDFont : public Cff::FontParent { public:
255
256 CIDFont(Cff* cff, PermString, const Dict&, ErrorHandler*);
257 ~CIDFont();
258
259 PermString font_name() const { return _font_name; }
260 void font_matrix(double[6]) const;
261
262 int nglyphs() const { return _charstrings_index.nitems(); }
263 PermString glyph_name(int) const;
264 void glyph_names(Vector<PermString>&) const;
265 Charstring* glyph(int) const;
266 Charstring* glyph(PermString) const;
267 int glyphid(PermString) const;
268 const CharstringProgram* child_program(int) const;
269
270 bool dict_has(DictOperator) const;
271 String dict_string(DictOperator) const;
272 bool dict_value(DictOperator, double*) const;
273 bool dict_xvalue(DictOperator, double, double*) const;
274 bool dict_value(DictOperator, Vector<double>&) const;
275
276 private:
277
278 PermString _font_name;
279
280 Dict _top_dict;
281 Dict _private_dict;
282
283 Cff::Charset _charset;
284
285 IndexIterator _charstrings_index;
286 mutable Vector<Charstring*> _charstrings_cs;
287
288 Vector<ChildFont*> _child_fonts;
289 Cff::FDSelect _fdselect;
290
291 const Dict& dict_of(DictOperator) const;
292
293 };
294
295 class Cff::ChildFont : public Cff::FontParent { public:
296
297 ChildFont(Cff* cff, Cff::CIDFont*, int charstring_type, const Dict&, ErrorHandler* = 0);
298 ~ChildFont();
299
300 bool ok() const { return _error >= 0; }
301 int error() const { return _error; }
302
303 PermString font_name() const { return _parent->font_name(); }
304 void font_matrix(double m[6]) const { _parent->font_matrix(m); }
305
306 inline bool cid() const;
307
308 int nsubrs_x() const { return _subrs_index.nitems(); }
309 int nsubrs() const { return nsubrs_x(); }
310 Charstring* subr(int) const;
311 int subr_bias() const;
312
313 int nglyphs() const { return _parent->nglyphs(); }
314 PermString glyph_name(int gi) const { return _parent->glyph_name(gi); }
315 void glyph_names(Vector<PermString>& v) const { _parent->glyph_names(v); }
316 Charstring* glyph(int gi) const { return _parent->glyph(gi); }
317 Charstring* glyph(PermString n) const { return _parent->glyph(n); }
318
319 double global_width_x(bool) const;
320
321 private:
322
323 Cff::CIDFont* _parent;
324
325 Dict _top_dict;
326 Dict _private_dict;
327
328 IndexIterator _subrs_index;
329 mutable Vector<Charstring*> _subrs_cs;
330
331 double _default_width_x;
332 double _nominal_width_x;
333
334 ChildFont(const ChildFont&); // does not exist
335 ChildFont& operator=(const ChildFont&); // does not exist
336
337 Charstring* charstring(const IndexIterator&, int) const;
338
339 friend class Cff::Font;
340
341 };
342
343 class Cff::Font : public Cff::ChildFont { public:
344
345 Font(Cff* cff, PermString, const Dict&, ErrorHandler*);
346 ~Font();
347
348 PermString font_name() const { return _font_name; }
349 void font_matrix(double[6]) const;
350
351 int nglyphs() const { return _charstrings_index.nitems(); }
352 PermString glyph_name(int) const;
353 void glyph_names(Vector<PermString>&) const;
354 Charstring* glyph(int) const;
355 Charstring* glyph(PermString) const;
356 int glyphid(PermString) const;
357
358 Type1Encoding* type1_encoding() const;
359 Type1Encoding* type1_encoding_copy() const;
360
361 bool dict_has(DictOperator) const;
362 String dict_string(DictOperator) const;
363 inline bool dict_value(DictOperator, double*) const;
364 inline bool dict_value(DictOperator, Vector<double>&) const;
365 const Dict& top_dict() const { return _top_dict; }
366
367 private:
368
369 PermString _font_name;
370
371 Cff::Charset _charset;
372
373 IndexIterator _charstrings_index;
374 mutable Vector<Charstring*> _charstrings_cs;
375
376 int _encoding_pos;
377 int _encoding[256];
378 mutable Type1Encoding* _t1encoding;
379
380 int parse_encoding(int pos, ErrorHandler*);
381 int assign_standard_encoding(const int* standard_encoding);
382
383 inline const Dict& dict_of(DictOperator) const;
384
385 };
386
387
388 inline uint32_t Cff::IndexIterator::offset_at(const uint8_t* x) const
389 {
390 switch (_offsize) {
391 case 0:
392 return 0;
393 case 1:
394 return x[0];
395 case 2:
396 return (x[0] << 8) | x[1];
397 case 3:
398 return (x[0] << 16) | (x[1] << 8) | x[2];
399 default:
400 return (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3];
401 }
402 }
403
404 inline const uint8_t* Cff::IndexIterator::operator*() const
405 {
406 assert(live());
407 return _contents + offset_at(_offset);
408 }
409
410 inline const uint8_t* Cff::IndexIterator::operator[](int which) const
411 {
412 assert(live() && _offset + which * _offsize <= _last_offset);
413 return _contents + offset_at(_offset + which * _offsize);
414 }
415
416 inline int Cff::Charset::gid_to_sid(int gid) const
417 {
418 if (gid >= 0 && gid < _sids.size())
419 return _sids[gid];
420 else
421 return -1;
422 }
423
424 inline int Cff::Charset::sid_to_gid(int sid) const
425 {
426 if (sid >= 0 && sid < _gids.size())
427 return _gids[sid];
428 else
429 return -1;
430 }
431
432 inline int Cff::FDSelect::gid_to_fd(int gid) const
433 {
434 if (gid >= 0 && gid < _nglyphs)
435 return _fds[gid];
436 else
437 return -1;
438 }
439
440 inline bool Cff::Dict::has_first(DictOperator op) const
441 {
442 return _operators.size() && _operators[0] == op;
443 }
444
445 inline const Cff::Dict& Cff::Font::dict_of(DictOperator op) const
446 {
447 return (op >= 0 && op <= oLastOperator && (operator_types[op] & tP) ? _private_dict : _top_dict);
448 }
449
450 inline bool Cff::Font::dict_value(DictOperator op, double* val) const
451 {
452 return dict_of(op).value(op, val);
453 }
454
455 inline bool Cff::Font::dict_value(DictOperator op, Vector<double>& val) const
456 {
457 return dict_of(op).value(op, val);
458 }
459
460 }
461 #endif