Debian Codegraph @

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

raw
1 // -*- related-file-name: "../../libefont/otf.cc" -*-
2 #ifndef EFONT_OTF_HH
3 #define EFONT_OTF_HH
4 #include <efont/otfdata.hh>
5 #include <lcdf/hashcode.hh>
6 class ErrorHandler;
7 namespace Efont { namespace OpenType {
8 class Post;
9 class Name;
10
11 typedef int Glyph; // 16-bit integer
12
13 class Tag {
14 public:
15 Tag() : _tag(0U) { }
16 explicit Tag(uint32_t tag) : _tag(tag) { }
17 Tag(const char* name);
18 Tag(const String& name);
19 // default destructor
20
21 static Tag head_tag() { return Tag(0x68656164U); }
22
23 typedef bool (Tag::*unspecified_bool_type)() const;
24 bool null() const { return _tag == 0; }
25 operator unspecified_bool_type() const {
26 return _tag != 0 ? &Tag::null : 0;
27 }
28 bool valid() const;
29
30 uint32_t value() const { return _tag; }
31
32 String text() const;
33 static String langsys_text(Tag script, Tag langsys = Tag());
34
35 const uint8_t* table_entry(const uint8_t* table, int n, int entry_size) const;
36
37 const char* script_description() const;
38 const char* language_description() const;
39 const char* feature_description() const;
40
41 private:
42 uint32_t _tag;
43 };
44
45 inline hashcode_t hashcode(const Tag& t) {
46 return t.value();
47 }
48
49 class Font {
50 public:
51 Font(const String& str, ErrorHandler* errh = 0);
52 // default destructor
53
54 bool ok() const { return _error >= 0; }
55 bool check_checksums(ErrorHandler* errh = 0) const;
56 int error() const { return _error; }
57
58 const String& data_string() const { return _str; }
59 const uint8_t* data() const { return _str.udata(); }
60 int length() const { return _str.length(); }
61
62 unsigned units_per_em() const { return _units_per_em; }
63
64 int ntables() const;
65 bool has_table(Tag tag) const;
66 String table(Tag tag) const;
67 uint32_t table_checksum(Tag tag) const;
68 Tag table_tag(int i) const;
69
70 static uint32_t checksum(const uint8_t *, const uint8_t *);
71 static uint32_t checksum(const String &);
72 static Font make(bool truetype, const Vector<Tag>& tags, const Vector<String>& data);
73
74 enum { HEADER_SIZE = 12, TABLE_DIR_ENTRY_SIZE = 16 };
75
76 private:
77 String _str;
78 int _error;
79 unsigned _units_per_em;
80
81 int parse_header(ErrorHandler*);
82 };
83
84 class ScriptList {
85 public:
86 ScriptList() { }
87 inline ScriptList(const String&, ErrorHandler* = 0);
88 int assign(const String&, ErrorHandler* = 0);
89 // default destructor
90
91 bool ok() const { return _str.length() > 0; }
92
93 int language_systems(Vector<Tag>& scripts, Vector<Tag>& langsys, ErrorHandler* = 0) const;
94 int features(Tag script, Tag langsys, int& required_fid, Vector<int>& fids, ErrorHandler* = 0, bool clear_fids = true) const;
95
96 private:
97 enum { SCRIPTLIST_HEADERSIZE = 2, SCRIPT_RECSIZE = 6,
98 SCRIPT_HEADERSIZE = 4, LANGSYS_RECSIZE = 6,
99 LANGSYS_HEADERSIZE = 6, FEATURE_RECSIZE = 2 };
100
101 String _str;
102
103 int check_header(ErrorHandler*);
104 int script_offset(Tag) const;
105 int check_script(Tag, int, ErrorHandler*) const;
106 int langsys_offset(Tag, Tag, ErrorHandler* = 0) const;
107 };
108
109 class FeatureList {
110 public:
111 FeatureList() { }
112 inline FeatureList(const String&, ErrorHandler* = 0);
113 int assign(const String&, ErrorHandler* = 0);
114 // default destructor
115
116 bool ok() const { return _str.length() > 0; }
117
118 Tag tag(int fid) const;
119 String params(int fid, int length, ErrorHandler* = 0, bool old_style_offset = false) const;
120 String size_params(int fid, const Name& name, ErrorHandler* = 0) const;
121 int lookups(int fid, Vector<int>& results, ErrorHandler* = 0, bool clear_results = true) const;
122
123 int find(Tag, const Vector<int>& fids) const;
124 void filter(Vector<int>& fids, const Vector<Tag>& sorted_ftags) const;
125 inline void filter(Vector<int>& fids, Tag ftag) const;
126
127 int lookups(const Vector<int>& fids, Vector<int>& results, ErrorHandler* = 0) const;
128 int lookups(const Vector<int>& required_fids, const Vector<int>& fids, const Vector<Tag>& sorted_ftags, Vector<int>& results, ErrorHandler* = 0) const;
129 int lookups(int required_fid, const Vector<int>& fids, const Vector<Tag>& sorted_ftags, Vector<int>& results, ErrorHandler* = 0) const;
130 int lookups(const ScriptList&, Tag script, Tag langsys, const Vector<Tag>& sorted_ftags, Vector<int>& results, ErrorHandler* = 0) const;
131
132 private:
133 enum { FEATURELIST_HEADERSIZE = 2, FEATURE_RECSIZE = 6,
134 FEATURE_HEADERSIZE = 4, LOOKUPLIST_RECSIZE = 2 };
135
136 String _str;
137
138 int check_header(ErrorHandler*);
139 int script_offset(Tag) const;
140 int langsys_offset(Tag, Tag, ErrorHandler* = 0) const;
141 };
142
143 class Coverage {
144 public:
145 Coverage() noexcept; // empty coverage
146 Coverage(Glyph first, Glyph last) noexcept; // range coverage
147 Coverage(const Vector<bool> &gmap) noexcept; // used-bytemap coverage
148 Coverage(const String &str, ErrorHandler *errh = 0, bool check = true) noexcept;
149 // default destructor
150
151 bool ok() const noexcept { return _str.length() > 0; }
152 int size() const noexcept;
153 bool has_fast_covers() const noexcept {
154 return _str.length() > 0 && _str.data()[1] == T_X_BYTEMAP;
155 }
156
157 int coverage_index(Glyph) const noexcept;
158 bool covers(Glyph g) const noexcept { return coverage_index(g) >= 0; }
159
160 void unparse(StringAccum&) const noexcept;
161 String unparse() const noexcept;
162
163 class iterator { public:
164 iterator() : _pos(0), _value(0) { }
165 // private constructor
166 // default destructor
167
168 bool ok() const { return _pos < _str.length(); }
169 operator bool() const { return ok(); }
170
171 Glyph operator*() const { return _value; }
172 Glyph value() const { return _value; }
173 int coverage_index() const;
174
175 void operator++(int);
176 void operator++() { (*this)++; }
177 bool forward_to(Glyph);
178
179 // XXX should check iterators are of same type
180 bool operator<(const iterator& o) { return _value < o._value; }
181 bool operator<=(const iterator& o) { return _value <= o._value; }
182 bool operator>=(const iterator& o) { return _value >= o._value; }
183 bool operator>(const iterator& o) { return _value > o._value; }
184 bool operator==(const iterator& o) { return _value == o._value; }
185 bool operator!=(const iterator& o) { return _value != o._value; }
186
187 private:
188 String _str;
189 int _pos;
190 Glyph _value;
191 friend class Coverage;
192 iterator(const String &str, bool is_end);
193 };
194
195 iterator begin() const { return iterator(_str, false); }
196 iterator end() const { return iterator(_str, true); }
197 Glyph operator[](int) const noexcept;
198
199 enum { T_LIST = 1, T_RANGES = 2, T_X_BYTEMAP = 3,
200 HEADERSIZE = 4, LIST_RECSIZE = 2, RANGES_RECSIZE = 6 };
201
202 private:
203 String _str;
204
205 int check(ErrorHandler*);
206 };
207
208 Coverage operator&(const Coverage&, const Coverage&);
209 bool operator<=(const Coverage&, const Coverage&);
210
211 inline bool operator>=(const Coverage& a, const Coverage& b) {
212 return b <= a;
213 }
214
215 class GlyphSet {
216 public:
217 GlyphSet();
218 GlyphSet(const GlyphSet&);
219 ~GlyphSet();
220
221 inline bool covers(Glyph g) const;
222 inline bool operator[](Glyph g) const;
223 int change(Glyph, bool);
224 void insert(Glyph g) { change(g, true); }
225 void remove(Glyph g) { change(g, false); }
226
227 GlyphSet& operator=(const GlyphSet&);
228
229 private:
230 enum { GLYPHBITS = 16, SHIFT = 8,
231 MAXGLYPH = (1 << GLYPHBITS) - 1, UNSHIFT = GLYPHBITS - SHIFT,
232 MASK = (1 << UNSHIFT) - 1, VLEN = (1 << SHIFT),
233 VULEN = (1 << UNSHIFT) >> 5
234 };
235
236 uint32_t* _v[VLEN];
237 };
238
239 class ClassDef {
240 public:
241 ClassDef(const String&, ErrorHandler* = 0) noexcept;
242 // default destructor
243
244 bool ok() const { return _str.length() > 0; }
245 int nclass() const noexcept;
246
247 int lookup(Glyph) const noexcept;
248 int operator[](Glyph g) const noexcept { return lookup(g); }
249
250 void unparse(StringAccum&) const noexcept;
251 String unparse() const noexcept;
252
253 class class_iterator {
254 public:
255 // private constructor
256 // default destructor
257
258 bool ok() const { return _pos < _str.length(); }
259 operator bool() const { return ok(); }
260
261 Glyph operator*() const { return *_coviter; }
262 Glyph value() const { return *_coviter; }
263 int class_value() const { return _class; }
264
265 void operator++(int);
266 void operator++() { (*this)++; }
267
268 // XXX should check iterators are of same type
269 bool operator<(const class_iterator& o) { return _coviter < o._coviter; }
270 bool operator<=(const class_iterator& o) { return _coviter <= o._coviter; }
271 bool operator>=(const class_iterator& o) { return _coviter >= o._coviter; }
272 bool operator>(const class_iterator& o) { return _coviter > o._coviter; }
273 bool operator==(const class_iterator& o) { return _coviter == o._coviter; }
274 bool operator!=(const class_iterator& o) { return _coviter != o._coviter; }
275
276 private:
277 String _str;
278 int _pos;
279 int _class;
280 Coverage::iterator _coviter;
281 friend class ClassDef;
282 class_iterator(const String&, int, int, const Coverage::iterator&);
283 void increment_class0();
284 enum { FIRST_POS = -1, LAST_POS = -2 };
285 };
286
287 // XXX does not work correctly for class 0
288 class_iterator begin(int c) const { return class_iterator(_str, 0, c, Coverage::iterator()); }
289 class_iterator begin(int c, const Coverage& coverage) const { return class_iterator(_str, 0, c, coverage.begin()); }
290 class_iterator end(int c) const { return class_iterator(_str, _str.length(), c, Coverage::iterator()); }
291
292 enum { T_LIST = 1, T_RANGES = 2,
293 LIST_HEADERSIZE = 6, LIST_RECSIZE = 2,
294 RANGES_HEADERSIZE = 4, RANGES_RECSIZE = 6 };
295
296 private:
297 String _str;
298
299 int check(ErrorHandler*);
300 };
301
302 extern Vector<PermString> debug_glyph_names;
303
304
305 inline bool operator==(Tag t1, uint32_t t2) {
306 return t1.value() == t2;
307 }
308
309 inline bool operator!=(Tag t1, uint32_t t2) {
310 return t1.value() != t2;
311 }
312
313 inline bool operator<(Tag t1, uint32_t t2) {
314 return t1.value() < t2;
315 }
316
317 inline bool operator>(Tag t1, uint32_t t2) {
318 return t1.value() > t2;
319 }
320
321 inline bool operator<=(Tag t1, uint32_t t2) {
322 return t1.value() <= t2;
323 }
324
325 inline bool operator>=(Tag t1, uint32_t t2) {
326 return t1.value() >= t2;
327 }
328
329 inline bool operator==(Tag t1, Tag t2) {
330 return t1.value() == t2.value();
331 }
332
333 inline bool operator!=(Tag t1, Tag t2) {
334 return t1.value() != t2.value();
335 }
336
337 inline bool operator<(Tag t1, Tag t2) {
338 return t1.value() < t2.value();
339 }
340
341 inline ScriptList::ScriptList(const String& str, ErrorHandler* errh) {
342 assign(str, errh);
343 }
344
345 inline FeatureList::FeatureList(const String& str, ErrorHandler* errh) {
346 assign(str, errh);
347 }
348
349 inline void FeatureList::filter(Vector<int>& fids, Tag ftag) const {
350 Vector<Tag> tags;
351 tags.push_back(ftag);
352 filter(fids, tags);
353 }
354
355 inline bool GlyphSet::covers(Glyph g) const {
356 if ((unsigned)g > MAXGLYPH)
357 return false;
358 else if (const uint32_t* u = _v[g >> SHIFT])
359 return (u[(g & MASK) >> 5] & (1 << (g & 0x1F))) != 0;
360 else
361 return false;
362 }
363
364 inline bool GlyphSet::operator[](Glyph g) const {
365 return covers(g);
366 }
367
368 } // namespace Efont::OpenType
369 } // namespace Efont
370
371 #endif