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 hb_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 (c->plan->user_axes_location->has (HB_TAG ('s','l','n','t')) && 106 !c->plan->pinned_at_default) 107 { 108 float italic_angle = c->plan->user_axes_location->get (HB_TAG ('s','l','n','t')); 109 italic_angle = hb_max (-90.f, hb_min (italic_angle, 90.f)); 110 post_prime->italicAngle.set_float (italic_angle); 111 } 112 113 if (glyph_names && version.major == 2) 114 return_trace (v2X.subset (c)); 115 116 return_trace (true); 117 } 118 119 struct accelerator_t 120 { 121 friend struct postV2Tail; 122 accelerator_tOT::post::accelerator_t123 accelerator_t (hb_face_t *face) 124 { 125 table = hb_sanitize_context_t ().reference_table<post> (face); 126 unsigned int table_length = table.get_length (); 127 128 version = table->version.to_int (); 129 if (version != 0x00020000) return; 130 131 const postV2Tail &v2 = table->v2X; 132 133 glyphNameIndex = &v2.glyphNameIndex; 134 pool = &StructAfter<uint8_t> (v2.glyphNameIndex); 135 136 const uint8_t *end = (const uint8_t *) (const void *) table + table_length; 137 for (const uint8_t *data = pool; 138 index_to_offset.length < 65535 && data < end && data + *data < end; 139 data += 1 + *data) 140 index_to_offset.push (data - pool); 141 } ~accelerator_tOT::post::accelerator_t142 ~accelerator_t () 143 { 144 hb_free (gids_sorted_by_name.get_acquire ()); 145 table.destroy (); 146 } 147 get_glyph_nameOT::post::accelerator_t148 bool get_glyph_name (hb_codepoint_t glyph, 149 char *buf, unsigned int buf_len) const 150 { 151 hb_bytes_t s = find_glyph_name (glyph); 152 if (!s.length) return false; 153 if (!buf_len) return true; 154 unsigned int len = hb_min (buf_len - 1, s.length); 155 strncpy (buf, s.arrayZ, len); 156 buf[len] = '\0'; 157 return true; 158 } 159 get_glyph_from_nameOT::post::accelerator_t160 bool get_glyph_from_name (const char *name, int len, 161 hb_codepoint_t *glyph) const 162 { 163 unsigned int count = get_glyph_count (); 164 if (unlikely (!count)) return false; 165 166 if (len < 0) len = strlen (name); 167 168 if (unlikely (!len)) return false; 169 170 retry: 171 uint16_t *gids = gids_sorted_by_name.get_acquire (); 172 173 if (unlikely (!gids)) 174 { 175 gids = (uint16_t *) hb_malloc (count * sizeof (gids[0])); 176 if (unlikely (!gids)) 177 return false; /* Anything better?! */ 178 179 for (unsigned int i = 0; i < count; i++) 180 gids[i] = i; 181 hb_qsort (gids, count, sizeof (gids[0]), cmp_gids, (void *) this); 182 183 if (unlikely (!gids_sorted_by_name.cmpexch (nullptr, gids))) 184 { 185 hb_free (gids); 186 goto retry; 187 } 188 } 189 190 hb_bytes_t st (name, len); 191 auto* gid = hb_bsearch (st, gids, count, sizeof (gids[0]), cmp_key, (void *) this); 192 if (gid) 193 { 194 *glyph = *gid; 195 return true; 196 } 197 198 return false; 199 } 200 201 hb_blob_ptr_t<post> table; 202 203 protected: 204 get_glyph_countOT::post::accelerator_t205 unsigned int get_glyph_count () const 206 { 207 if (version == 0x00010000) 208 return format1_names_length; 209 210 if (version == 0x00020000) 211 return glyphNameIndex->len; 212 213 return 0; 214 } 215 cmp_gidsOT::post::accelerator_t216 static int cmp_gids (const void *pa, const void *pb, void *arg) 217 { 218 const accelerator_t *thiz = (const accelerator_t *) arg; 219 uint16_t a = * (const uint16_t *) pa; 220 uint16_t b = * (const uint16_t *) pb; 221 return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a)); 222 } 223 cmp_keyOT::post::accelerator_t224 static int cmp_key (const void *pk, const void *po, void *arg) 225 { 226 const accelerator_t *thiz = (const accelerator_t *) arg; 227 const hb_bytes_t *key = (const hb_bytes_t *) pk; 228 uint16_t o = * (const uint16_t *) po; 229 return thiz->find_glyph_name (o).cmp (*key); 230 } 231 find_glyph_nameOT::post::accelerator_t232 hb_bytes_t find_glyph_name (hb_codepoint_t glyph) const 233 { 234 if (version == 0x00010000) 235 { 236 if (glyph >= format1_names_length) 237 return hb_bytes_t (); 238 239 return format1_names (glyph); 240 } 241 242 if (version != 0x00020000 || glyph >= glyphNameIndex->len) 243 return hb_bytes_t (); 244 245 unsigned int index = glyphNameIndex->arrayZ[glyph]; 246 if (index < format1_names_length) 247 return format1_names (index); 248 index -= format1_names_length; 249 250 if (index >= index_to_offset.length) 251 return hb_bytes_t (); 252 unsigned int offset = index_to_offset[index]; 253 254 const uint8_t *data = pool + offset; 255 unsigned int name_length = *data; 256 data++; 257 258 return hb_bytes_t ((const char *) data, name_length); 259 } 260 261 private: 262 uint32_t version; 263 const Array16Of<HBUINT16> *glyphNameIndex = nullptr; 264 hb_vector_t<uint32_t> index_to_offset; 265 const uint8_t *pool = nullptr; 266 hb_atomic_ptr_t<uint16_t *> gids_sorted_by_name; 267 }; 268 has_dataOT::post269 bool has_data () const { return version.to_int (); } 270 sanitizeOT::post271 bool sanitize (hb_sanitize_context_t *c) const 272 { 273 TRACE_SANITIZE (this); 274 return_trace (c->check_struct (this) && 275 (version.to_int () == 0x00010000 || 276 (version.to_int () == 0x00020000 && v2X.sanitize (c)) || 277 version.to_int () == 0x00030000)); 278 } 279 280 public: 281 FixedVersion<>version; /* 0x00010000 for version 1.0 282 * 0x00020000 for version 2.0 283 * 0x00025000 for version 2.5 (deprecated) 284 * 0x00030000 for version 3.0 */ 285 F16DOT16 italicAngle; /* Italic angle in counter-clockwise degrees 286 * from the vertical. Zero for upright text, 287 * negative for text that leans to the right 288 * (forward). */ 289 FWORD underlinePosition; /* This is the suggested distance of the top 290 * of the underline from the baseline 291 * (negative values indicate below baseline). 292 * The PostScript definition of this FontInfo 293 * dictionary key (the y coordinate of the 294 * center of the stroke) is not used for 295 * historical reasons. The value of the 296 * PostScript key may be calculated by 297 * subtracting half the underlineThickness 298 * from the value of this field. */ 299 FWORD underlineThickness; /* Suggested values for the underline 300 thickness. */ 301 HBUINT32 isFixedPitch; /* Set to 0 if the font is proportionally 302 * spaced, non-zero if the font is not 303 * proportionally spaced (i.e. monospaced). */ 304 HBUINT32 minMemType42; /* Minimum memory usage when an OpenType font 305 * is downloaded. */ 306 HBUINT32 maxMemType42; /* Maximum memory usage when an OpenType font 307 * is downloaded. */ 308 HBUINT32 minMemType1; /* Minimum memory usage when an OpenType font 309 * is downloaded as a Type 1 font. */ 310 HBUINT32 maxMemType1; /* Maximum memory usage when an OpenType font 311 * is downloaded as a Type 1 font. */ 312 postV2Tail v2X; 313 DEFINE_SIZE_MIN (32); 314 }; 315 316 struct post_accelerator_t : post::accelerator_t { post_accelerator_tOT::post_accelerator_t317 post_accelerator_t (hb_face_t *face) : post::accelerator_t (face) {} 318 }; 319 320 321 } /* namespace OT */ 322 323 324 #endif /* HB_OT_POST_TABLE_HH */ 325