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