< prev index next >

src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-post-table.hh

Print this page




  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 #include "hb-subset-plan.hh"
  33 
  34 #define HB_STRING_ARRAY_NAME format1_names
  35 #define HB_STRING_ARRAY_LIST "hb-ot-post-macroman.hh"
  36 #include "hb-string-array.hh"
  37 #undef HB_STRING_ARRAY_LIST
  38 #undef HB_STRING_ARRAY_NAME
  39 
  40 #define NUM_FORMAT1_NAMES 258
  41 
  42 /*
  43  * post -- PostScript
  44  * https://docs.microsoft.com/en-us/typography/opentype/spec/post
  45  */
  46 #define HB_OT_TAG_post HB_TAG('p','o','s','t')
  47 
  48 
  49 namespace OT {
  50 
  51 
  52 struct postV2Tail
  53 {
  54   inline bool sanitize (hb_sanitize_context_t *c) const


  55   {
  56     TRACE_SANITIZE (this);
  57     return_trace (glyphNameIndex.sanitize (c));
  58   }
  59 
  60   ArrayOf<HBUINT16>glyphNameIndex;      /* This is not an offset, but is the

  61                                          * ordinal number of the glyph in 'post'
  62                                          * string tables. */
  63   HBUINT8               namesX[VAR];            /* Glyph names with length bytes [variable]

  64                                          * (a Pascal string). */
  65 
  66   DEFINE_SIZE_ARRAY2 (2, glyphNameIndex, namesX);

  67 };
  68 
  69 struct post
  70 {
  71   static const hb_tag_t tableTag = HB_OT_TAG_post;
  72 
  73   inline bool sanitize (hb_sanitize_context_t *c) const
  74   {
  75     TRACE_SANITIZE (this);
  76     if (unlikely (!c->check_struct (this)))
  77       return_trace (false);
  78     if (version.to_int () == 0x00020000)
  79     {
  80       const postV2Tail &v2 = StructAfter<postV2Tail> (*this);
  81       return_trace (v2.sanitize (c));
  82     }
  83     return_trace (true);
  84   }
  85 
  86   inline bool subset (hb_subset_plan_t *plan) const
  87   {
  88     unsigned int post_prime_length;
  89     hb_blob_t *post_blob = OT::Sanitizer<post>().sanitize (hb_face_reference_table (plan->source, HB_OT_TAG_post));
  90     hb_blob_t *post_prime_blob = hb_blob_create_sub_blob (post_blob, 0, post::static_size);
  91     post *post_prime = (post *) hb_blob_get_data_writable (post_prime_blob, &post_prime_length);
  92     hb_blob_destroy (post_blob);
  93 
  94     if (unlikely (!post_prime || post_prime_length != post::static_size))
  95     {
  96       hb_blob_destroy (post_prime_blob);
  97       DEBUG_MSG(SUBSET, nullptr, "Invalid source post table with length %d.", post_prime_length);
  98       return false;
  99     }
 100 
 101     post_prime->version.major.set (3); // Version 3 does not have any glyph names.
 102     bool result = plan->add_table (HB_OT_TAG_post, post_prime_blob);
 103     hb_blob_destroy (post_prime_blob);
 104 
 105     return result;
 106   }
 107 
 108   struct accelerator_t
 109   {
 110     inline void init (hb_face_t *face)
 111     {
 112       index_to_offset.init ();
 113 
 114       blob = Sanitizer<post>().sanitize (face->reference_table (HB_OT_TAG_post));
 115       const post *table = blob->as<post> ();
 116       unsigned int table_length = blob->length;
 117 
 118       version = table->version.to_int ();
 119       if (version != 0x00020000)
 120         return;
 121 
 122       const postV2Tail &v2 = StructAfter<postV2Tail> (*table);
 123 
 124       glyphNameIndex = &v2.glyphNameIndex;
 125       pool = &StructAfter<uint8_t> (v2.glyphNameIndex);
 126 
 127       const uint8_t *end = (uint8_t *) table + table_length;
 128       for (const uint8_t *data = pool; data < end && data + *data <= end; data += 1 + *data)


 129         index_to_offset.push (data - pool);
 130     }
 131     inline void fini (void)
 132     {
 133       index_to_offset.fini ();
 134       free (gids_sorted_by_name);

 135     }
 136 
 137     inline bool get_glyph_name (hb_codepoint_t glyph,
 138                                 char *buf, unsigned int buf_len) const
 139     {
 140       hb_bytes_t s = find_glyph_name (glyph);
 141       if (!s.len)
 142         return false;
 143       if (!buf_len)
 144         return true;
 145       if (buf_len <= s.len) /* What to do with truncation? Returning false for now. */
 146         return false;
 147       strncpy (buf, s.bytes, s.len);
 148       buf[s.len] = '\0';
 149       return true;
 150     }
 151 
 152     inline bool get_glyph_from_name (const char *name, int len,
 153                                      hb_codepoint_t *glyph) const
 154     {
 155       unsigned int count = get_glyph_count ();
 156       if (unlikely (!count))
 157         return false;
 158 
 159       if (len < 0)
 160         len = strlen (name);
 161 
 162       if (unlikely (!len))
 163         return false;
 164 
 165     retry:
 166       uint16_t *gids = (uint16_t *) hb_atomic_ptr_get (&gids_sorted_by_name);
 167 
 168       if (unlikely (!gids))
 169       {
 170         gids = (uint16_t *) malloc (count * sizeof (gids[0]));
 171         if (unlikely (!gids))
 172           return false; /* Anything better?! */
 173 
 174         for (unsigned int i = 0; i < count; i++)
 175           gids[i] = i;
 176         hb_sort_r (gids, count, sizeof (gids[0]), cmp_gids, (void *) this);
 177 
 178         if (!hb_atomic_ptr_cmpexch (&gids_sorted_by_name, nullptr, gids)) {

 179           free (gids);
 180           goto retry;
 181         }
 182       }
 183 
 184       hb_bytes_t st (name, len);
 185       const uint16_t *gid = (const uint16_t *) hb_bsearch_r (&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     protected:
 196 
 197     inline unsigned int get_glyph_count (void) const
 198     {
 199       if (version == 0x00010000)
 200         return NUM_FORMAT1_NAMES;
 201 
 202       if (version == 0x00020000)
 203         return glyphNameIndex->len;
 204 
 205       return 0;
 206     }
 207 
 208     static inline int cmp_gids (const void *pa, const void *pb, void *arg)
 209     {
 210       const accelerator_t *thiz = (const accelerator_t *) arg;
 211       uint16_t a = * (const uint16_t *) pa;
 212       uint16_t b = * (const uint16_t *) pb;
 213       return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a));
 214     }
 215 
 216     static inline int cmp_key (const void *pk, const void *po, void *arg)
 217     {
 218       const accelerator_t *thiz = (const accelerator_t *) arg;
 219       const hb_bytes_t *key = (const hb_bytes_t *) pk;
 220       uint16_t o = * (const uint16_t *) po;
 221       return thiz->find_glyph_name (o).cmp (*key);
 222     }
 223 
 224     inline hb_bytes_t find_glyph_name (hb_codepoint_t glyph) const
 225     {
 226       if (version == 0x00010000)
 227       {
 228         if (glyph >= NUM_FORMAT1_NAMES)
 229           return hb_bytes_t ();
 230 
 231         return format1_names (glyph);
 232       }
 233 
 234       if (version != 0x00020000 || glyph >= glyphNameIndex->len)
 235         return hb_bytes_t ();
 236 
 237       unsigned int index = glyphNameIndex->arrayZ[glyph];
 238       if (index < NUM_FORMAT1_NAMES)
 239         return format1_names (index);
 240       index -= NUM_FORMAT1_NAMES;
 241 
 242       if (index >= index_to_offset.len)
 243         return hb_bytes_t ();
 244       unsigned int offset = index_to_offset.arrayZ[index];
 245 
 246       const uint8_t *data = pool + offset;
 247       unsigned int name_length = *data;
 248       data++;
 249 
 250       return hb_bytes_t ((const char *) data, name_length);
 251     }
 252 
 253     private:
 254     hb_blob_t *blob;
 255     uint32_t version;
 256     const ArrayOf<HBUINT16> *glyphNameIndex;
 257     hb_vector_t<uint32_t, 1> index_to_offset;
 258     const uint8_t *pool;
 259     mutable uint16_t *gids_sorted_by_name;
 260   };
 261 









 262   public:
 263   FixedVersion<>version;                /* 0x00010000 for version 1.0
 264                                          * 0x00020000 for version 2.0
 265                                          * 0x00025000 for version 2.5 (deprecated)
 266                                          * 0x00030000 for version 3.0 */
 267   Fixed         italicAngle;            /* Italic angle in counter-clockwise degrees
 268                                          * from the vertical. Zero for upright text,
 269                                          * negative for text that leans to the right
 270                                          * (forward). */
 271   FWORD         underlinePosition;      /* This is the suggested distance of the top
 272                                          * of the underline from the baseline
 273                                          * (negative values indicate below baseline).
 274                                          * The PostScript definition of this FontInfo
 275                                          * dictionary key (the y coordinate of the
 276                                          * center of the stroke) is not used for
 277                                          * historical reasons. The value of the
 278                                          * PostScript key may be calculated by
 279                                          * subtracting half the underlineThickness
 280                                          * from the value of this field. */
 281   FWORD         underlineThickness;     /* Suggested values for the underline
 282                                            thickness. */
 283   HBUINT32      isFixedPitch;           /* Set to 0 if the font is proportionally
 284                                          * spaced, non-zero if the font is not
 285                                          * proportionally spaced (i.e. monospaced). */
 286   HBUINT32      minMemType42;           /* Minimum memory usage when an OpenType font
 287                                          * is downloaded. */
 288   HBUINT32      maxMemType42;           /* Maximum memory usage when an OpenType font
 289                                          * is downloaded. */
 290   HBUINT32      minMemType1;            /* Minimum memory usage when an OpenType font
 291                                          * is downloaded as a Type 1 font. */
 292   HBUINT32      maxMemType1;            /* Maximum memory usage when an OpenType font
 293                                          * is downloaded as a Type 1 font. */
 294 /*postV2Tail    v2[VAR];*/
 295   DEFINE_SIZE_STATIC (32);
 296 };


 297 
 298 } /* namespace OT */
 299 
 300 
 301 #endif /* HB_OT_POST_TABLE_HH */


  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   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 
  72 struct post
  73 {
  74   static constexpr hb_tag_t tableTag = HB_OT_TAG_post;













  75 
  76   bool subset (hb_subset_plan_t *plan) const
  77   {
  78     unsigned int post_prime_length;
  79     hb_blob_t *post_blob = hb_sanitize_context_t ().reference_table<post>(plan->source);
  80     hb_blob_t *post_prime_blob = hb_blob_create_sub_blob (post_blob, 0, post::min_size);
  81     post *post_prime = (post *) hb_blob_get_data_writable (post_prime_blob, &post_prime_length);
  82     hb_blob_destroy (post_blob);
  83 
  84     if (unlikely (!post_prime || post_prime_length != post::min_size))
  85     {
  86       hb_blob_destroy (post_prime_blob);
  87       DEBUG_MSG(SUBSET, nullptr, "Invalid source post table with length %d.", post_prime_length);
  88       return false;
  89     }
  90 
  91     post_prime->version.major.set (3); // Version 3 does not have any glyph names.
  92     bool result = plan->add_table (HB_OT_TAG_post, post_prime_blob);
  93     hb_blob_destroy (post_prime_blob);
  94 
  95     return result;
  96   }
  97 
  98   struct accelerator_t
  99   {
 100     void init (hb_face_t *face)
 101     {
 102       index_to_offset.init ();
 103 
 104       table = hb_sanitize_context_t ().reference_table<post> (face);
 105       unsigned int table_length = table.get_length ();

 106 
 107       version = table->version.to_int ();
 108       if (version != 0x00020000) return;

 109 
 110       const postV2Tail &v2 = table->v2X;
 111 
 112       glyphNameIndex = &v2.glyphNameIndex;
 113       pool = &StructAfter<uint8_t> (v2.glyphNameIndex);
 114 
 115       const uint8_t *end = (const uint8_t *) (const void *) table + table_length;
 116       for (const uint8_t *data = pool;
 117            index_to_offset.length < 65535 && data < end && data + *data < end;
 118            data += 1 + *data)
 119         index_to_offset.push (data - pool);
 120     }
 121     void fini ()
 122     {
 123       index_to_offset.fini ();
 124       free (gids_sorted_by_name.get ());
 125       table.destroy ();
 126     }
 127 
 128     bool get_glyph_name (hb_codepoint_t glyph,
 129                          char *buf, unsigned int buf_len) const
 130     {
 131       hb_bytes_t s = find_glyph_name (glyph);
 132       if (!s.length) return false;
 133       if (!buf_len) return true;
 134       unsigned int len = MIN (buf_len - 1, s.length);
 135       strncpy (buf, s.arrayZ, len);
 136       buf[len] = '\0';



 137       return true;
 138     }
 139 
 140     bool get_glyph_from_name (const char *name, int len,
 141                               hb_codepoint_t *glyph) const
 142     {
 143       unsigned int count = get_glyph_count ();
 144       if (unlikely (!count)) return false;

 145 
 146       if (len < 0) len = strlen (name);

 147 
 148       if (unlikely (!len)) return false;

 149 
 150     retry:
 151       uint16_t *gids = gids_sorted_by_name.get ();
 152 
 153       if (unlikely (!gids))
 154       {
 155         gids = (uint16_t *) malloc (count * sizeof (gids[0]));
 156         if (unlikely (!gids))
 157           return false; /* Anything better?! */
 158 
 159         for (unsigned int i = 0; i < count; i++)
 160           gids[i] = i;
 161         hb_sort_r (gids, count, sizeof (gids[0]), cmp_gids, (void *) this);
 162 
 163         if (unlikely (!gids_sorted_by_name.cmpexch (nullptr, gids)))
 164         {
 165           free (gids);
 166           goto retry;
 167         }
 168       }
 169 
 170       hb_bytes_t st (name, len);
 171       const uint16_t *gid = (const uint16_t *) hb_bsearch_r (hb_addressof (st), gids, count,
 172                                                              sizeof (gids[0]), cmp_key, (void *) this);
 173       if (gid)
 174       {
 175         *glyph = *gid;
 176         return true;
 177       }
 178 
 179       return false;
 180     }
 181 
 182     protected:
 183 
 184     unsigned int get_glyph_count () const
 185     {
 186       if (version == 0x00010000)
 187         return NUM_FORMAT1_NAMES;
 188 
 189       if (version == 0x00020000)
 190         return glyphNameIndex->len;
 191 
 192       return 0;
 193     }
 194 
 195     static int cmp_gids (const void *pa, const void *pb, void *arg)
 196     {
 197       const accelerator_t *thiz = (const accelerator_t *) arg;
 198       uint16_t a = * (const uint16_t *) pa;
 199       uint16_t b = * (const uint16_t *) pb;
 200       return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a));
 201     }
 202 
 203     static int cmp_key (const void *pk, const void *po, void *arg)
 204     {
 205       const accelerator_t *thiz = (const accelerator_t *) arg;
 206       const hb_bytes_t *key = (const hb_bytes_t *) pk;
 207       uint16_t o = * (const uint16_t *) po;
 208       return thiz->find_glyph_name (o).cmp (*key);
 209     }
 210 
 211     hb_bytes_t find_glyph_name (hb_codepoint_t glyph) const
 212     {
 213       if (version == 0x00010000)
 214       {
 215         if (glyph >= NUM_FORMAT1_NAMES)
 216           return hb_bytes_t ();
 217 
 218         return format1_names (glyph);
 219       }
 220 
 221       if (version != 0x00020000 || glyph >= glyphNameIndex->len)
 222         return hb_bytes_t ();
 223 
 224       unsigned int index = glyphNameIndex->arrayZ[glyph];
 225       if (index < NUM_FORMAT1_NAMES)
 226         return format1_names (index);
 227       index -= NUM_FORMAT1_NAMES;
 228 
 229       if (index >= index_to_offset.length)
 230         return hb_bytes_t ();
 231       unsigned int offset = index_to_offset[index];
 232 
 233       const uint8_t *data = pool + offset;
 234       unsigned int name_length = *data;
 235       data++;
 236 
 237       return hb_bytes_t ((const char *) data, name_length);
 238     }
 239 
 240     private:
 241     hb_blob_ptr_t<post> table;
 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 sanitize (hb_sanitize_context_t *c) const
 250   {
 251     TRACE_SANITIZE (this);
 252     return_trace (likely (c->check_struct (this) &&
 253                           (version.to_int () == 0x00010000 ||
 254                            (version.to_int () == 0x00020000 && v2X.sanitize (c)) ||
 255                            version.to_int () == 0x00030000)));
 256   }
 257 
 258   public:
 259   FixedVersion<>version;                /* 0x00010000 for version 1.0
 260                                          * 0x00020000 for version 2.0
 261                                          * 0x00025000 for version 2.5 (deprecated)
 262                                          * 0x00030000 for version 3.0 */
 263   Fixed         italicAngle;            /* Italic angle in counter-clockwise degrees
 264                                          * from the vertical. Zero for upright text,
 265                                          * negative for text that leans to the right
 266                                          * (forward). */
 267   FWORD         underlinePosition;      /* This is the suggested distance of the top
 268                                          * of the underline from the baseline
 269                                          * (negative values indicate below baseline).
 270                                          * The PostScript definition of this FontInfo
 271                                          * dictionary key (the y coordinate of the
 272                                          * center of the stroke) is not used for
 273                                          * historical reasons. The value of the
 274                                          * PostScript key may be calculated by
 275                                          * subtracting half the underlineThickness
 276                                          * from the value of this field. */
 277   FWORD         underlineThickness;     /* Suggested values for the underline
 278                                            thickness. */
 279   HBUINT32      isFixedPitch;           /* Set to 0 if the font is proportionally
 280                                          * spaced, non-zero if the font is not
 281                                          * proportionally spaced (i.e. monospaced). */
 282   HBUINT32      minMemType42;           /* Minimum memory usage when an OpenType font
 283                                          * is downloaded. */
 284   HBUINT32      maxMemType42;           /* Maximum memory usage when an OpenType font
 285                                          * is downloaded. */
 286   HBUINT32      minMemType1;            /* Minimum memory usage when an OpenType font
 287                                          * is downloaded as a Type 1 font. */
 288   HBUINT32      maxMemType1;            /* Maximum memory usage when an OpenType font
 289                                          * is downloaded as a Type 1 font. */
 290   postV2Tail    v2X;
 291   DEFINE_SIZE_MIN (32);
 292 };
 293 
 294 struct post_accelerator_t : post::accelerator_t {};
 295 
 296 } /* namespace OT */
 297 
 298 
 299 #endif /* HB_OT_POST_TABLE_HH */
< prev index next >