1 | /* |
2 | * Copyright © 2016 Google, Inc. |
3 | * |
4 | * This is part of HarfBuzz, a text shaping library. |
5 | * |
6 | * Permission is hereby granted, without written agreement and without |
7 | * license or royalty fees, to use, copy, modify, and distribute this |
8 | * software and its documentation for any purpose, provided that the |
9 | * above copyright notice and the following two paragraphs appear in |
10 | * all copies of this software. |
11 | * |
12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
16 | * DAMAGE. |
17 | * |
18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
23 | * |
24 | * Google Author(s): Behdad Esfahbod |
25 | */ |
26 | |
27 | #ifndef HB_OT_POST_TABLE_HH |
28 | #define HB_OT_POST_TABLE_HH |
29 | |
30 | #include "hb-open-type.hh" |
31 | |
32 | #define HB_STRING_ARRAY_NAME format1_names |
33 | #define HB_STRING_ARRAY_LIST "hb-ot-post-macroman.hh" |
34 | #include "hb-string-array.hh" |
35 | #undef HB_STRING_ARRAY_LIST |
36 | #undef HB_STRING_ARRAY_NAME |
37 | |
38 | /* |
39 | * post -- PostScript |
40 | * https://docs.microsoft.com/en-us/typography/opentype/spec/post |
41 | */ |
42 | #define HB_OT_TAG_post HB_TAG('p','o','s','t') |
43 | |
44 | |
45 | namespace OT { |
46 | |
47 | |
48 | struct postV2Tail |
49 | { |
50 | friend struct post; |
51 | |
52 | bool sanitize (hb_sanitize_context_t *c) const |
53 | { |
54 | TRACE_SANITIZE (this); |
55 | return_trace (glyphNameIndex.sanitize (c)); |
56 | } |
57 | |
58 | protected: |
59 | ArrayOf<HBUINT16> glyphNameIndex; /* This is not an offset, but is the |
60 | * ordinal number of the glyph in 'post' |
61 | * string tables. */ |
62 | /*UnsizedArrayOf<HBUINT8> |
63 | namesX;*/ /* Glyph names with length bytes [variable] |
64 | * (a Pascal string). */ |
65 | |
66 | public: |
67 | DEFINE_SIZE_ARRAY (2, glyphNameIndex); |
68 | }; |
69 | |
70 | struct post |
71 | { |
72 | static constexpr hb_tag_t tableTag = HB_OT_TAG_post; |
73 | |
74 | void serialize (hb_serialize_context_t *c) const |
75 | { |
76 | post *post_prime = c->allocate_min<post> (); |
77 | if (unlikely (!post_prime)) return; |
78 | |
79 | memcpy (post_prime, this, post::min_size); |
80 | post_prime->version.major = 3; // Version 3 does not have any glyph names. |
81 | } |
82 | |
83 | bool subset (hb_subset_context_t *c) const |
84 | { |
85 | TRACE_SUBSET (this); |
86 | post *post_prime = c->serializer->start_embed<post> (); |
87 | if (unlikely (!post_prime)) return_trace (false); |
88 | |
89 | serialize (c->serializer); |
90 | if (c->serializer->in_error () || c->serializer->ran_out_of_room) return_trace (false); |
91 | |
92 | return_trace (true); |
93 | } |
94 | |
95 | struct accelerator_t |
96 | { |
97 | void init (hb_face_t *face) |
98 | { |
99 | index_to_offset.init (); |
100 | |
101 | table = hb_sanitize_context_t ().reference_table<post> (face); |
102 | unsigned int table_length = table.get_length (); |
103 | |
104 | version = table->version.to_int (); |
105 | if (version != 0x00020000) return; |
106 | |
107 | const postV2Tail &v2 = table->v2X; |
108 | |
109 | glyphNameIndex = &v2.glyphNameIndex; |
110 | pool = &StructAfter<uint8_t> (v2.glyphNameIndex); |
111 | |
112 | const uint8_t *end = (const uint8_t *) (const void *) table + table_length; |
113 | for (const uint8_t *data = pool; |
114 | index_to_offset.length < 65535 && data < end && data + *data < end; |
115 | data += 1 + *data) |
116 | index_to_offset.push (data - pool); |
117 | } |
118 | void fini () |
119 | { |
120 | index_to_offset.fini (); |
121 | free (gids_sorted_by_name.get ()); |
122 | table.destroy (); |
123 | } |
124 | |
125 | bool get_glyph_name (hb_codepoint_t glyph, |
126 | char *buf, unsigned int buf_len) const |
127 | { |
128 | hb_bytes_t s = find_glyph_name (glyph); |
129 | if (!s.length) return false; |
130 | if (!buf_len) return true; |
131 | unsigned int len = hb_min (buf_len - 1, s.length); |
132 | strncpy (buf, s.arrayZ, len); |
133 | buf[len] = '\0'; |
134 | return true; |
135 | } |
136 | |
137 | bool get_glyph_from_name (const char *name, int len, |
138 | hb_codepoint_t *glyph) const |
139 | { |
140 | unsigned int count = get_glyph_count (); |
141 | if (unlikely (!count)) return false; |
142 | |
143 | if (len < 0) len = strlen (name); |
144 | |
145 | if (unlikely (!len)) return false; |
146 | |
147 | retry: |
148 | uint16_t *gids = gids_sorted_by_name.get (); |
149 | |
150 | if (unlikely (!gids)) |
151 | { |
152 | gids = (uint16_t *) malloc (count * sizeof (gids[0])); |
153 | if (unlikely (!gids)) |
154 | return false; /* Anything better?! */ |
155 | |
156 | for (unsigned int i = 0; i < count; i++) |
157 | gids[i] = i; |
158 | hb_qsort (gids, count, sizeof (gids[0]), cmp_gids, (void *) this); |
159 | |
160 | if (unlikely (!gids_sorted_by_name.cmpexch (nullptr, gids))) |
161 | { |
162 | free (gids); |
163 | goto retry; |
164 | } |
165 | } |
166 | |
167 | hb_bytes_t st (name, len); |
168 | auto* gid = hb_bsearch (st, gids, count, sizeof (gids[0]), cmp_key, (void *) this); |
169 | if (gid) |
170 | { |
171 | *glyph = *gid; |
172 | return true; |
173 | } |
174 | |
175 | return false; |
176 | } |
177 | |
178 | hb_blob_ptr_t<post> table; |
179 | |
180 | protected: |
181 | |
182 | unsigned int get_glyph_count () const |
183 | { |
184 | if (version == 0x00010000) |
185 | return format1_names_length; |
186 | |
187 | if (version == 0x00020000) |
188 | return glyphNameIndex->len; |
189 | |
190 | return 0; |
191 | } |
192 | |
193 | static int cmp_gids (const void *pa, const void *pb, void *arg) |
194 | { |
195 | const accelerator_t *thiz = (const accelerator_t *) arg; |
196 | uint16_t a = * (const uint16_t *) pa; |
197 | uint16_t b = * (const uint16_t *) pb; |
198 | return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a)); |
199 | } |
200 | |
201 | static int cmp_key (const void *pk, const void *po, void *arg) |
202 | { |
203 | const accelerator_t *thiz = (const accelerator_t *) arg; |
204 | const hb_bytes_t *key = (const hb_bytes_t *) pk; |
205 | uint16_t o = * (const uint16_t *) po; |
206 | return thiz->find_glyph_name (o).cmp (*key); |
207 | } |
208 | |
209 | hb_bytes_t find_glyph_name (hb_codepoint_t glyph) const |
210 | { |
211 | if (version == 0x00010000) |
212 | { |
213 | if (glyph >= format1_names_length) |
214 | return hb_bytes_t (); |
215 | |
216 | return format1_names (glyph); |
217 | } |
218 | |
219 | if (version != 0x00020000 || glyph >= glyphNameIndex->len) |
220 | return hb_bytes_t (); |
221 | |
222 | unsigned int index = glyphNameIndex->arrayZ[glyph]; |
223 | if (index < format1_names_length) |
224 | return format1_names (index); |
225 | index -= format1_names_length; |
226 | |
227 | if (index >= index_to_offset.length) |
228 | return hb_bytes_t (); |
229 | unsigned int offset = index_to_offset[index]; |
230 | |
231 | const uint8_t *data = pool + offset; |
232 | unsigned int name_length = *data; |
233 | data++; |
234 | |
235 | return hb_bytes_t ((const char *) data, name_length); |
236 | } |
237 | |
238 | private: |
239 | uint32_t version; |
240 | const ArrayOf<HBUINT16> *glyphNameIndex; |
241 | hb_vector_t<uint32_t> index_to_offset; |
242 | const uint8_t *pool; |
243 | hb_atomic_ptr_t<uint16_t *> gids_sorted_by_name; |
244 | }; |
245 | |
246 | bool has_data () const { return version.to_int (); } |
247 | |
248 | bool sanitize (hb_sanitize_context_t *c) const |
249 | { |
250 | TRACE_SANITIZE (this); |
251 | return_trace (likely (c->check_struct (this) && |
252 | (version.to_int () == 0x00010000 || |
253 | (version.to_int () == 0x00020000 && v2X.sanitize (c)) || |
254 | version.to_int () == 0x00030000))); |
255 | } |
256 | |
257 | public: |
258 | FixedVersion<>version; /* 0x00010000 for version 1.0 |
259 | * 0x00020000 for version 2.0 |
260 | * 0x00025000 for version 2.5 (deprecated) |
261 | * 0x00030000 for version 3.0 */ |
262 | HBFixed italicAngle; /* Italic angle in counter-clockwise degrees |
263 | * from the vertical. Zero for upright text, |
264 | * negative for text that leans to the right |
265 | * (forward). */ |
266 | FWORD underlinePosition; /* This is the suggested distance of the top |
267 | * of the underline from the baseline |
268 | * (negative values indicate below baseline). |
269 | * The PostScript definition of this FontInfo |
270 | * dictionary key (the y coordinate of the |
271 | * center of the stroke) is not used for |
272 | * historical reasons. The value of the |
273 | * PostScript key may be calculated by |
274 | * subtracting half the underlineThickness |
275 | * from the value of this field. */ |
276 | FWORD underlineThickness; /* Suggested values for the underline |
277 | thickness. */ |
278 | HBUINT32 isFixedPitch; /* Set to 0 if the font is proportionally |
279 | * spaced, non-zero if the font is not |
280 | * proportionally spaced (i.e. monospaced). */ |
281 | HBUINT32 minMemType42; /* Minimum memory usage when an OpenType font |
282 | * is downloaded. */ |
283 | HBUINT32 maxMemType42; /* Maximum memory usage when an OpenType font |
284 | * is downloaded. */ |
285 | HBUINT32 minMemType1; /* Minimum memory usage when an OpenType font |
286 | * is downloaded as a Type 1 font. */ |
287 | HBUINT32 maxMemType1; /* Maximum memory usage when an OpenType font |
288 | * is downloaded as a Type 1 font. */ |
289 | postV2Tail v2X; |
290 | DEFINE_SIZE_MIN (32); |
291 | }; |
292 | |
293 | struct post_accelerator_t : post::accelerator_t {}; |
294 | |
295 | } /* namespace OT */ |
296 | |
297 | |
298 | #endif /* HB_OT_POST_TABLE_HH */ |
299 | |