|
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-private.hh" |
|
31 #include "hb-dsalgs.hh" |
|
32 |
|
33 #define HB_STRING_ARRAY_NAME format1_names |
|
34 #define HB_STRING_ARRAY_LIST "hb-ot-post-macroman.hh" |
|
35 #include "hb-string-array.hh" |
|
36 #undef HB_STRING_ARRAY_LIST |
|
37 #undef HB_STRING_ARRAY_NAME |
|
38 |
|
39 #define NUM_FORMAT1_NAMES 258 |
|
40 |
|
41 namespace OT { |
|
42 |
|
43 |
|
44 /* |
|
45 * post -- PostScript |
|
46 */ |
|
47 |
|
48 #define HB_OT_TAG_post HB_TAG('p','o','s','t') |
|
49 |
|
50 |
|
51 struct postV2Tail |
|
52 { |
|
53 inline bool sanitize (hb_sanitize_context_t *c) const |
|
54 { |
|
55 TRACE_SANITIZE (this); |
|
56 return_trace (glyphNameIndex.sanitize (c)); |
|
57 } |
|
58 |
|
59 ArrayOf<USHORT>glyphNameIndex; /* This is not an offset, but is the |
|
60 * ordinal number of the glyph in 'post' |
|
61 * string tables. */ |
|
62 BYTE namesX[VAR]; /* Glyph names with length bytes [variable] |
|
63 * (a Pascal string). */ |
|
64 |
|
65 DEFINE_SIZE_ARRAY2 (2, glyphNameIndex, namesX); |
|
66 }; |
|
67 |
|
68 struct post |
|
69 { |
|
70 static const hb_tag_t tableTag = HB_OT_TAG_post; |
|
71 |
|
72 inline bool sanitize (hb_sanitize_context_t *c) const |
|
73 { |
|
74 TRACE_SANITIZE (this); |
|
75 if (unlikely (!c->check_struct (this))) |
|
76 return_trace (false); |
|
77 if (version.to_int () == 0x00020000) |
|
78 { |
|
79 const postV2Tail &v2 = StructAfter<postV2Tail> (*this); |
|
80 return_trace (v2.sanitize (c)); |
|
81 } |
|
82 return_trace (true); |
|
83 } |
|
84 |
|
85 struct accelerator_t |
|
86 { |
|
87 inline void init (const post *table, unsigned int post_len) |
|
88 { |
|
89 version = table->version.to_int (); |
|
90 index_to_offset.init (); |
|
91 if (version != 0x00020000) |
|
92 return; |
|
93 |
|
94 const postV2Tail &v2 = StructAfter<postV2Tail> (*table); |
|
95 |
|
96 glyphNameIndex = &v2.glyphNameIndex; |
|
97 pool = &StructAfter<uint8_t> (v2.glyphNameIndex); |
|
98 |
|
99 const uint8_t *end = (uint8_t *) table + post_len; |
|
100 for (const uint8_t *data = pool; data < end && data + *data <= end; data += 1 + *data) |
|
101 { |
|
102 uint32_t *offset = index_to_offset.push (); |
|
103 if (unlikely (!offset)) |
|
104 break; |
|
105 *offset = data - pool; |
|
106 } |
|
107 } |
|
108 inline void fini (void) |
|
109 { |
|
110 index_to_offset.finish (); |
|
111 free (gids_sorted_by_name); |
|
112 } |
|
113 |
|
114 inline bool get_glyph_name (hb_codepoint_t glyph, |
|
115 char *buf, unsigned int buf_len) const |
|
116 { |
|
117 hb_string_t s = find_glyph_name (glyph); |
|
118 if (!s.len) |
|
119 return false; |
|
120 if (!buf_len) |
|
121 return true; |
|
122 if (buf_len <= s.len) /* What to do with truncation? Returning false for now. */ |
|
123 return false; |
|
124 strncpy (buf, s.bytes, s.len); |
|
125 buf[s.len] = '\0'; |
|
126 return true; |
|
127 } |
|
128 |
|
129 inline bool get_glyph_from_name (const char *name, int len, |
|
130 hb_codepoint_t *glyph) const |
|
131 { |
|
132 unsigned int count = get_glyph_count (); |
|
133 if (unlikely (!count)) |
|
134 return false; |
|
135 |
|
136 if (len < 0) |
|
137 len = strlen (name); |
|
138 |
|
139 if (unlikely (!len)) |
|
140 return false; |
|
141 |
|
142 retry: |
|
143 uint16_t *gids = (uint16_t *) hb_atomic_ptr_get (&gids_sorted_by_name); |
|
144 |
|
145 if (unlikely (!gids)) |
|
146 { |
|
147 gids = (uint16_t *) malloc (count * sizeof (gids[0])); |
|
148 if (unlikely (!gids)) |
|
149 return false; /* Anything better?! */ |
|
150 |
|
151 for (unsigned int i = 0; i < count; i++) |
|
152 gids[i] = i; |
|
153 hb_sort_r (gids, count, sizeof (gids[0]), cmp_gids, (void *) this); |
|
154 |
|
155 if (!hb_atomic_ptr_cmpexch (&gids_sorted_by_name, nullptr, gids)) { |
|
156 free (gids); |
|
157 goto retry; |
|
158 } |
|
159 } |
|
160 |
|
161 hb_string_t st (name, len); |
|
162 const uint16_t *gid = (const uint16_t *) hb_bsearch_r (&st, gids, count, sizeof (gids[0]), cmp_key, (void *) this); |
|
163 if (gid) |
|
164 { |
|
165 *glyph = *gid; |
|
166 return true; |
|
167 } |
|
168 |
|
169 return false; |
|
170 } |
|
171 |
|
172 protected: |
|
173 |
|
174 inline unsigned int get_glyph_count (void) const |
|
175 { |
|
176 if (version == 0x00010000) |
|
177 return NUM_FORMAT1_NAMES; |
|
178 |
|
179 if (version == 0x00020000) |
|
180 return glyphNameIndex->len; |
|
181 |
|
182 return 0; |
|
183 } |
|
184 |
|
185 static inline int cmp_gids (const void *pa, const void *pb, void *arg) |
|
186 { |
|
187 const accelerator_t *thiz = (const accelerator_t *) arg; |
|
188 uint16_t a = * (const uint16_t *) pa; |
|
189 uint16_t b = * (const uint16_t *) pb; |
|
190 return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a)); |
|
191 } |
|
192 |
|
193 static inline int cmp_key (const void *pk, const void *po, void *arg) |
|
194 { |
|
195 const accelerator_t *thiz = (const accelerator_t *) arg; |
|
196 const hb_string_t *key = (const hb_string_t *) pk; |
|
197 uint16_t o = * (const uint16_t *) po; |
|
198 return thiz->find_glyph_name (o).cmp (*key); |
|
199 } |
|
200 |
|
201 inline hb_string_t find_glyph_name (hb_codepoint_t glyph) const |
|
202 { |
|
203 if (version == 0x00010000) |
|
204 { |
|
205 if (glyph >= NUM_FORMAT1_NAMES) |
|
206 return hb_string_t (); |
|
207 |
|
208 return format1_names (glyph); |
|
209 } |
|
210 |
|
211 if (version != 0x00020000 || glyph >= glyphNameIndex->len) |
|
212 return hb_string_t (); |
|
213 |
|
214 unsigned int index = glyphNameIndex->array[glyph]; |
|
215 if (index < NUM_FORMAT1_NAMES) |
|
216 return format1_names (index); |
|
217 index -= NUM_FORMAT1_NAMES; |
|
218 |
|
219 if (index >= index_to_offset.len) |
|
220 return hb_string_t (); |
|
221 unsigned int offset = index_to_offset.array[index]; |
|
222 |
|
223 const uint8_t *data = pool + offset; |
|
224 unsigned int name_length = *data; |
|
225 data++; |
|
226 |
|
227 return hb_string_t ((const char *) data, name_length); |
|
228 } |
|
229 |
|
230 uint32_t version; |
|
231 const ArrayOf<USHORT> *glyphNameIndex; |
|
232 hb_prealloced_array_t<uint32_t, 1> index_to_offset; |
|
233 const uint8_t *pool; |
|
234 mutable uint16_t *gids_sorted_by_name; |
|
235 }; |
|
236 |
|
237 public: |
|
238 FixedVersion<>version; /* 0x00010000 for version 1.0 |
|
239 * 0x00020000 for version 2.0 |
|
240 * 0x00025000 for version 2.5 (deprecated) |
|
241 * 0x00030000 for version 3.0 */ |
|
242 Fixed italicAngle; /* Italic angle in counter-clockwise degrees |
|
243 * from the vertical. Zero for upright text, |
|
244 * negative for text that leans to the right |
|
245 * (forward). */ |
|
246 FWORD underlinePosition; /* This is the suggested distance of the top |
|
247 * of the underline from the baseline |
|
248 * (negative values indicate below baseline). |
|
249 * The PostScript definition of this FontInfo |
|
250 * dictionary key (the y coordinate of the |
|
251 * center of the stroke) is not used for |
|
252 * historical reasons. The value of the |
|
253 * PostScript key may be calculated by |
|
254 * subtracting half the underlineThickness |
|
255 * from the value of this field. */ |
|
256 FWORD underlineThickness; /* Suggested values for the underline |
|
257 thickness. */ |
|
258 ULONG isFixedPitch; /* Set to 0 if the font is proportionally |
|
259 * spaced, non-zero if the font is not |
|
260 * proportionally spaced (i.e. monospaced). */ |
|
261 ULONG minMemType42; /* Minimum memory usage when an OpenType font |
|
262 * is downloaded. */ |
|
263 ULONG maxMemType42; /* Maximum memory usage when an OpenType font |
|
264 * is downloaded. */ |
|
265 ULONG minMemType1; /* Minimum memory usage when an OpenType font |
|
266 * is downloaded as a Type 1 font. */ |
|
267 ULONG maxMemType1; /* Maximum memory usage when an OpenType font |
|
268 * is downloaded as a Type 1 font. */ |
|
269 /*postV2Tail v2[VAR];*/ |
|
270 DEFINE_SIZE_STATIC (32); |
|
271 }; |
|
272 |
|
273 } /* namespace OT */ |
|
274 |
|
275 |
|
276 #endif /* HB_OT_POST_TABLE_HH */ |