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#define NUM_FORMAT1_NAMES 258
39
40/*
41 * post -- PostScript
42 * https://docs.microsoft.com/en-us/typography/opentype/spec/post
43 */
44#define HB_OT_TAG_post HB_TAG('p','o','s','t')
45
46
47namespace OT {
48
49
50struct postV2Tail
51{
52 friend struct post;
53
54 bool sanitize (hb_sanitize_context_t *c) const
55 {
56 TRACE_SANITIZE (this);
57 return_trace (glyphNameIndex.sanitize (c));
58 }
59
60 protected:
61 ArrayOf<HBUINT16> glyphNameIndex; /* This is not an offset, but is the
62 * ordinal number of the glyph in 'post'
63 * string tables. */
64/*UnsizedArrayOf<HBUINT8>
65 namesX;*/ /* Glyph names with length bytes [variable]
66 * (a Pascal string). */
67
68 public:
69 DEFINE_SIZE_ARRAY (2, glyphNameIndex);
70};
71
72struct post
73{
74 static constexpr hb_tag_t tableTag = HB_OT_TAG_post;
75
76 void serialize (hb_serialize_context_t *c) const
77 {
78 post *post_prime = c->allocate_min<post> ();
79 if (unlikely (!post_prime)) return;
80
81 memcpy (post_prime, this, post::min_size);
82 post_prime->version.major = 3; // Version 3 does not have any glyph names.
83 }
84
85 bool subset (hb_subset_context_t *c) const
86 {
87 TRACE_SUBSET (this);
88 post *post_prime = c->serializer->start_embed<post> ();
89 if (unlikely (!post_prime)) return_trace (false);
90
91 serialize (c->serializer);
92 if (c->serializer->in_error () || c->serializer->ran_out_of_room) return_trace (false);
93
94 return_trace (true);
95 }
96
97 struct accelerator_t
98 {
99 void init (hb_face_t *face)
100 {
101 index_to_offset.init ();
102
103 table = hb_sanitize_context_t ().reference_table<post> (face);
104 unsigned int table_length = table.get_length ();
105
106 version = table->version.to_int ();
107 if (version != 0x00020000) return;
108
109 const postV2Tail &v2 = table->v2X;
110
111 glyphNameIndex = &v2.glyphNameIndex;
112 pool = &StructAfter<uint8_t> (v2.glyphNameIndex);
113
114 const uint8_t *end = (const uint8_t *) (const void *) table + table_length;
115 for (const uint8_t *data = pool;
116 index_to_offset.length < 65535 && data < end && data + *data < end;
117 data += 1 + *data)
118 index_to_offset.push (data - pool);
119 }
120 void fini ()
121 {
122 index_to_offset.fini ();
123 free (gids_sorted_by_name.get ());
124 table.destroy ();
125 }
126
127 bool get_glyph_name (hb_codepoint_t glyph,
128 char *buf, unsigned int buf_len) const
129 {
130 hb_bytes_t s = find_glyph_name (glyph);
131 if (!s.length) return false;
132 if (!buf_len) return true;
133 unsigned int len = hb_min (buf_len - 1, s.length);
134 strncpy (buf, s.arrayZ, len);
135 buf[len] = '\0';
136 return true;
137 }
138
139 bool get_glyph_from_name (const char *name, int len,
140 hb_codepoint_t *glyph) const
141 {
142 unsigned int count = get_glyph_count ();
143 if (unlikely (!count)) return false;
144
145 if (len < 0) len = strlen (name);
146
147 if (unlikely (!len)) return false;
148
149 retry:
150 uint16_t *gids = gids_sorted_by_name.get ();
151
152 if (unlikely (!gids))
153 {
154 gids = (uint16_t *) malloc (count * sizeof (gids[0]));
155 if (unlikely (!gids))
156 return false; /* Anything better?! */
157
158 for (unsigned int i = 0; i < count; i++)
159 gids[i] = i;
160 hb_qsort (gids, count, sizeof (gids[0]), cmp_gids, (void *) this);
161
162 if (unlikely (!gids_sorted_by_name.cmpexch (nullptr, gids)))
163 {
164 free (gids);
165 goto retry;
166 }
167 }
168
169 hb_bytes_t st (name, len);
170 const uint16_t *gid = (const uint16_t *) hb_bsearch (hb_addressof (st), gids, count,
171 sizeof (gids[0]), cmp_key, (void *) this);
172 if (gid)
173 {
174 *glyph = *gid;
175 return true;
176 }
177
178 return false;
179 }
180
181 hb_blob_ptr_t<post> table;
182
183 protected:
184
185 unsigned int get_glyph_count () const
186 {
187 if (version == 0x00010000)
188 return NUM_FORMAT1_NAMES;
189
190 if (version == 0x00020000)
191 return glyphNameIndex->len;
192
193 return 0;
194 }
195
196 static int cmp_gids (const void *pa, const void *pb, void *arg)
197 {
198 const accelerator_t *thiz = (const accelerator_t *) arg;
199 uint16_t a = * (const uint16_t *) pa;
200 uint16_t b = * (const uint16_t *) pb;
201 return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a));
202 }
203
204 static int cmp_key (const void *pk, const void *po, void *arg)
205 {
206 const accelerator_t *thiz = (const accelerator_t *) arg;
207 const hb_bytes_t *key = (const hb_bytes_t *) pk;
208 uint16_t o = * (const uint16_t *) po;
209 return thiz->find_glyph_name (o).cmp (*key);
210 }
211
212 hb_bytes_t find_glyph_name (hb_codepoint_t glyph) const
213 {
214 if (version == 0x00010000)
215 {
216 if (glyph >= NUM_FORMAT1_NAMES)
217 return hb_bytes_t ();
218
219 return format1_names (glyph);
220 }
221
222 if (version != 0x00020000 || glyph >= glyphNameIndex->len)
223 return hb_bytes_t ();
224
225 unsigned int index = glyphNameIndex->arrayZ[glyph];
226 if (index < NUM_FORMAT1_NAMES)
227 return format1_names (index);
228 index -= NUM_FORMAT1_NAMES;
229
230 if (index >= index_to_offset.length)
231 return hb_bytes_t ();
232 unsigned int offset = index_to_offset[index];
233
234 const uint8_t *data = pool + offset;
235 unsigned int name_length = *data;
236 data++;
237
238 return hb_bytes_t ((const char *) data, name_length);
239 }
240
241 private:
242 uint32_t version;
243 const ArrayOf<HBUINT16> *glyphNameIndex;
244 hb_vector_t<uint32_t> index_to_offset;
245 const uint8_t *pool;
246 hb_atomic_ptr_t<uint16_t *> gids_sorted_by_name;
247 };
248
249 bool has_data () const { return version.to_int (); }
250
251 bool sanitize (hb_sanitize_context_t *c) const
252 {
253 TRACE_SANITIZE (this);
254 return_trace (likely (c->check_struct (this) &&
255 (version.to_int () == 0x00010000 ||
256 (version.to_int () == 0x00020000 && v2X.sanitize (c)) ||
257 version.to_int () == 0x00030000)));
258 }
259
260 public:
261 FixedVersion<>version; /* 0x00010000 for version 1.0
262 * 0x00020000 for version 2.0
263 * 0x00025000 for version 2.5 (deprecated)
264 * 0x00030000 for version 3.0 */
265 HBFixed italicAngle; /* Italic angle in counter-clockwise degrees
266 * from the vertical. Zero for upright text,
267 * negative for text that leans to the right
268 * (forward). */
269 FWORD underlinePosition; /* This is the suggested distance of the top
270 * of the underline from the baseline
271 * (negative values indicate below baseline).
272 * The PostScript definition of this FontInfo
273 * dictionary key (the y coordinate of the
274 * center of the stroke) is not used for
275 * historical reasons. The value of the
276 * PostScript key may be calculated by
277 * subtracting half the underlineThickness
278 * from the value of this field. */
279 FWORD underlineThickness; /* Suggested values for the underline
280 thickness. */
281 HBUINT32 isFixedPitch; /* Set to 0 if the font is proportionally
282 * spaced, non-zero if the font is not
283 * proportionally spaced (i.e. monospaced). */
284 HBUINT32 minMemType42; /* Minimum memory usage when an OpenType font
285 * is downloaded. */
286 HBUINT32 maxMemType42; /* Maximum memory usage when an OpenType font
287 * is downloaded. */
288 HBUINT32 minMemType1; /* Minimum memory usage when an OpenType font
289 * is downloaded as a Type 1 font. */
290 HBUINT32 maxMemType1; /* Maximum memory usage when an OpenType font
291 * is downloaded as a Type 1 font. */
292 postV2Tail v2X;
293 DEFINE_SIZE_MIN (32);
294};
295
296struct post_accelerator_t : post::accelerator_t {};
297
298} /* namespace OT */
299
300
301#endif /* HB_OT_POST_TABLE_HH */
302