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