2016-04-30 19:20:56 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2016 Google, Inc.
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Google Author(s): Behdad Esfahbod
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HB_OT_POST_TABLE_HH
|
|
|
|
#define HB_OT_POST_TABLE_HH
|
|
|
|
|
|
|
|
#include "hb-open-type-private.hh"
|
2017-10-31 22:34:01 +01:00
|
|
|
#include "hb-dsalgs.hh"
|
2018-04-17 06:43:35 +02:00
|
|
|
#include "hb-subset-plan.hh"
|
2016-04-30 19:20:56 +02:00
|
|
|
|
2017-10-27 04:34:04 +02:00
|
|
|
#define HB_STRING_ARRAY_NAME format1_names
|
|
|
|
#define HB_STRING_ARRAY_LIST "hb-ot-post-macroman.hh"
|
|
|
|
#include "hb-string-array.hh"
|
|
|
|
#undef HB_STRING_ARRAY_LIST
|
|
|
|
#undef HB_STRING_ARRAY_NAME
|
2017-10-16 10:05:42 +02:00
|
|
|
|
2017-10-27 04:34:04 +02:00
|
|
|
#define NUM_FORMAT1_NAMES 258
|
2016-04-30 19:20:56 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* post -- PostScript
|
2018-04-12 11:08:19 +02:00
|
|
|
* https://docs.microsoft.com/en-us/typography/opentype/spec/post
|
2016-04-30 19:20:56 +02:00
|
|
|
*/
|
|
|
|
#define HB_OT_TAG_post HB_TAG('p','o','s','t')
|
|
|
|
|
|
|
|
|
2018-04-12 11:08:19 +02:00
|
|
|
namespace OT {
|
|
|
|
|
|
|
|
|
2016-04-30 19:20:56 +02:00
|
|
|
struct postV2Tail
|
|
|
|
{
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2017-10-29 00:58:56 +02:00
|
|
|
return_trace (glyphNameIndex.sanitize (c));
|
2016-04-30 19:20:56 +02:00
|
|
|
}
|
|
|
|
|
2018-01-10 03:07:30 +01:00
|
|
|
ArrayOf<HBUINT16>glyphNameIndex; /* This is not an offset, but is the
|
2016-04-30 19:20:56 +02:00
|
|
|
* ordinal number of the glyph in 'post'
|
|
|
|
* string tables. */
|
2018-01-10 03:07:30 +01:00
|
|
|
HBUINT8 namesX[VAR]; /* Glyph names with length bytes [variable]
|
2016-04-30 19:20:56 +02:00
|
|
|
* (a Pascal string). */
|
|
|
|
|
|
|
|
DEFINE_SIZE_ARRAY2 (2, glyphNameIndex, namesX);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct post
|
|
|
|
{
|
|
|
|
static const hb_tag_t tableTag = HB_OT_TAG_post;
|
|
|
|
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
if (unlikely (!c->check_struct (this)))
|
|
|
|
return_trace (false);
|
|
|
|
if (version.to_int () == 0x00020000)
|
|
|
|
{
|
2017-10-26 01:40:50 +02:00
|
|
|
const postV2Tail &v2 = StructAfter<postV2Tail> (*this);
|
2016-04-30 19:20:56 +02:00
|
|
|
return_trace (v2.sanitize (c));
|
|
|
|
}
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-03-07 03:33:39 +01:00
|
|
|
inline bool subset (hb_subset_plan_t *plan) const
|
|
|
|
{
|
2018-03-08 20:06:49 +01:00
|
|
|
unsigned int post_prime_length;
|
2018-03-07 03:33:39 +01:00
|
|
|
hb_blob_t *post_blob = OT::Sanitizer<post>().sanitize (hb_face_reference_table (plan->source, HB_OT_TAG_post));
|
2018-03-08 20:06:49 +01:00
|
|
|
hb_blob_t *post_prime_blob = hb_blob_create_sub_blob (post_blob, 0, post::static_size);
|
|
|
|
post *post_prime = (post *) hb_blob_get_data_writable (post_prime_blob, &post_prime_length);
|
|
|
|
hb_blob_destroy (post_blob);
|
2018-03-07 03:33:39 +01:00
|
|
|
|
2018-03-08 20:06:49 +01:00
|
|
|
if (unlikely (!post_prime || post_prime_length != post::static_size))
|
2018-03-07 03:33:39 +01:00
|
|
|
{
|
2018-03-08 20:06:49 +01:00
|
|
|
hb_blob_destroy (post_prime_blob);
|
|
|
|
DEBUG_MSG(SUBSET, nullptr, "Invalid source post table with length %d.", post_prime_length);
|
2018-03-07 03:33:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
post_prime->version.major.set (3); // Version 3 does not have any glyph names.
|
|
|
|
bool result = hb_subset_plan_add_table (plan, HB_OT_TAG_post, post_prime_blob);
|
|
|
|
hb_blob_destroy (post_prime_blob);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-10-29 02:54:04 +01:00
|
|
|
struct accelerator_t
|
2017-10-16 10:05:42 +02:00
|
|
|
{
|
2017-11-15 04:31:50 +01:00
|
|
|
inline void init (hb_face_t *face)
|
2017-10-16 10:05:42 +02:00
|
|
|
{
|
2018-01-09 15:48:51 +01:00
|
|
|
blob = Sanitizer<post>().sanitize (face->reference_table (HB_OT_TAG_post));
|
2017-11-15 04:31:50 +01:00
|
|
|
const post *table = Sanitizer<post>::lock_instance (blob);
|
|
|
|
unsigned int table_length = hb_blob_get_length (blob);
|
|
|
|
|
2017-10-29 02:54:04 +01:00
|
|
|
version = table->version.to_int ();
|
|
|
|
index_to_offset.init ();
|
|
|
|
if (version != 0x00020000)
|
|
|
|
return;
|
2017-10-16 10:05:42 +02:00
|
|
|
|
2017-10-29 02:54:04 +01:00
|
|
|
const postV2Tail &v2 = StructAfter<postV2Tail> (*table);
|
|
|
|
|
|
|
|
glyphNameIndex = &v2.glyphNameIndex;
|
|
|
|
pool = &StructAfter<uint8_t> (v2.glyphNameIndex);
|
|
|
|
|
2017-11-15 04:31:50 +01:00
|
|
|
const uint8_t *end = (uint8_t *) table + table_length;
|
2017-10-29 22:00:54 +01:00
|
|
|
for (const uint8_t *data = pool; data < end && data + *data <= end; data += 1 + *data)
|
2017-10-29 02:54:04 +01:00
|
|
|
{
|
|
|
|
uint32_t *offset = index_to_offset.push ();
|
|
|
|
if (unlikely (!offset))
|
|
|
|
break;
|
|
|
|
*offset = data - pool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
2018-05-02 01:01:25 +02:00
|
|
|
index_to_offset.fini ();
|
2017-10-30 20:15:05 +01:00
|
|
|
free (gids_sorted_by_name);
|
2017-10-16 10:05:42 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 20:15:05 +01:00
|
|
|
inline bool get_glyph_name (hb_codepoint_t glyph,
|
|
|
|
char *buf, unsigned int buf_len) const
|
|
|
|
{
|
2018-05-08 10:57:27 +02:00
|
|
|
hb_bytes_t s = find_glyph_name (glyph);
|
2017-10-30 20:15:05 +01:00
|
|
|
if (!s.len)
|
|
|
|
return false;
|
|
|
|
if (!buf_len)
|
|
|
|
return true;
|
|
|
|
if (buf_len <= s.len) /* What to do with truncation? Returning false for now. */
|
|
|
|
return false;
|
|
|
|
strncpy (buf, s.bytes, s.len);
|
|
|
|
buf[s.len] = '\0';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool get_glyph_from_name (const char *name, int len,
|
|
|
|
hb_codepoint_t *glyph) const
|
|
|
|
{
|
|
|
|
unsigned int count = get_glyph_count ();
|
|
|
|
if (unlikely (!count))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen (name);
|
|
|
|
|
|
|
|
if (unlikely (!len))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
uint16_t *gids = (uint16_t *) hb_atomic_ptr_get (&gids_sorted_by_name);
|
|
|
|
|
|
|
|
if (unlikely (!gids))
|
|
|
|
{
|
|
|
|
gids = (uint16_t *) malloc (count * sizeof (gids[0]));
|
|
|
|
if (unlikely (!gids))
|
|
|
|
return false; /* Anything better?! */
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
gids[i] = i;
|
|
|
|
hb_sort_r (gids, count, sizeof (gids[0]), cmp_gids, (void *) this);
|
|
|
|
|
|
|
|
if (!hb_atomic_ptr_cmpexch (&gids_sorted_by_name, nullptr, gids)) {
|
|
|
|
free (gids);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 10:57:27 +02:00
|
|
|
hb_bytes_t st (name, len);
|
2017-10-30 20:15:05 +01:00
|
|
|
const uint16_t *gid = (const uint16_t *) hb_bsearch_r (&st, gids, count, sizeof (gids[0]), cmp_key, (void *) this);
|
|
|
|
if (gid)
|
|
|
|
{
|
|
|
|
*glyph = *gid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
inline unsigned int get_glyph_count (void) const
|
|
|
|
{
|
|
|
|
if (version == 0x00010000)
|
|
|
|
return NUM_FORMAT1_NAMES;
|
|
|
|
|
|
|
|
if (version == 0x00020000)
|
|
|
|
return glyphNameIndex->len;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int cmp_gids (const void *pa, const void *pb, void *arg)
|
|
|
|
{
|
|
|
|
const accelerator_t *thiz = (const accelerator_t *) arg;
|
|
|
|
uint16_t a = * (const uint16_t *) pa;
|
|
|
|
uint16_t b = * (const uint16_t *) pb;
|
|
|
|
return thiz->find_glyph_name (b).cmp (thiz->find_glyph_name (a));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int cmp_key (const void *pk, const void *po, void *arg)
|
|
|
|
{
|
|
|
|
const accelerator_t *thiz = (const accelerator_t *) arg;
|
2018-05-08 10:57:27 +02:00
|
|
|
const hb_bytes_t *key = (const hb_bytes_t *) pk;
|
2017-10-30 20:15:05 +01:00
|
|
|
uint16_t o = * (const uint16_t *) po;
|
|
|
|
return thiz->find_glyph_name (o).cmp (*key);
|
|
|
|
}
|
|
|
|
|
2018-05-08 10:57:27 +02:00
|
|
|
inline hb_bytes_t find_glyph_name (hb_codepoint_t glyph) const
|
2017-10-16 10:05:42 +02:00
|
|
|
{
|
2017-10-29 02:54:04 +01:00
|
|
|
if (version == 0x00010000)
|
|
|
|
{
|
|
|
|
if (glyph >= NUM_FORMAT1_NAMES)
|
2018-05-08 10:57:27 +02:00
|
|
|
return hb_bytes_t ();
|
2017-10-16 10:05:42 +02:00
|
|
|
|
2017-10-30 19:21:44 +01:00
|
|
|
return format1_names (glyph);
|
2017-10-29 02:54:04 +01:00
|
|
|
}
|
|
|
|
|
2017-10-30 16:46:36 +01:00
|
|
|
if (version != 0x00020000 || glyph >= glyphNameIndex->len)
|
2018-05-08 10:57:27 +02:00
|
|
|
return hb_bytes_t ();
|
2017-10-26 01:40:50 +02:00
|
|
|
|
2017-10-29 02:54:04 +01:00
|
|
|
unsigned int index = glyphNameIndex->array[glyph];
|
2017-10-26 01:40:50 +02:00
|
|
|
if (index < NUM_FORMAT1_NAMES)
|
2017-10-30 19:21:44 +01:00
|
|
|
return format1_names (index);
|
2017-10-26 01:40:50 +02:00
|
|
|
index -= NUM_FORMAT1_NAMES;
|
|
|
|
|
2017-10-29 02:54:04 +01:00
|
|
|
if (index >= index_to_offset.len)
|
2018-05-08 10:57:27 +02:00
|
|
|
return hb_bytes_t ();
|
2017-10-29 21:52:52 +01:00
|
|
|
unsigned int offset = index_to_offset.array[index];
|
2017-10-16 10:05:42 +02:00
|
|
|
|
2017-10-29 02:54:04 +01:00
|
|
|
const uint8_t *data = pool + offset;
|
|
|
|
unsigned int name_length = *data;
|
|
|
|
data++;
|
2017-10-18 20:49:16 +02:00
|
|
|
|
2018-05-08 10:57:27 +02:00
|
|
|
return hb_bytes_t ((const char *) data, name_length);
|
2017-10-30 16:46:36 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 04:47:31 +01:00
|
|
|
private:
|
2017-11-15 04:31:50 +01:00
|
|
|
hb_blob_t *blob;
|
2017-10-29 02:54:04 +01:00
|
|
|
uint32_t version;
|
2018-01-10 03:07:30 +01:00
|
|
|
const ArrayOf<HBUINT16> *glyphNameIndex;
|
2018-05-02 01:07:04 +02:00
|
|
|
hb_vector_t<uint32_t, 1> index_to_offset;
|
2017-10-29 02:54:04 +01:00
|
|
|
const uint8_t *pool;
|
2017-10-30 20:15:05 +01:00
|
|
|
mutable uint16_t *gids_sorted_by_name;
|
2017-10-29 02:54:04 +01:00
|
|
|
};
|
2017-10-18 20:49:16 +02:00
|
|
|
|
2016-04-30 19:20:56 +02:00
|
|
|
public:
|
|
|
|
FixedVersion<>version; /* 0x00010000 for version 1.0
|
|
|
|
* 0x00020000 for version 2.0
|
|
|
|
* 0x00025000 for version 2.5 (deprecated)
|
|
|
|
* 0x00030000 for version 3.0 */
|
|
|
|
Fixed italicAngle; /* Italic angle in counter-clockwise degrees
|
|
|
|
* from the vertical. Zero for upright text,
|
|
|
|
* negative for text that leans to the right
|
|
|
|
* (forward). */
|
|
|
|
FWORD underlinePosition; /* This is the suggested distance of the top
|
|
|
|
* of the underline from the baseline
|
|
|
|
* (negative values indicate below baseline).
|
|
|
|
* The PostScript definition of this FontInfo
|
|
|
|
* dictionary key (the y coordinate of the
|
|
|
|
* center of the stroke) is not used for
|
|
|
|
* historical reasons. The value of the
|
|
|
|
* PostScript key may be calculated by
|
|
|
|
* subtracting half the underlineThickness
|
|
|
|
* from the value of this field. */
|
|
|
|
FWORD underlineThickness; /* Suggested values for the underline
|
|
|
|
thickness. */
|
2018-01-17 21:46:08 +01:00
|
|
|
HBUINT32 isFixedPitch; /* Set to 0 if the font is proportionally
|
2016-04-30 19:20:56 +02:00
|
|
|
* spaced, non-zero if the font is not
|
|
|
|
* proportionally spaced (i.e. monospaced). */
|
2018-01-17 21:46:08 +01:00
|
|
|
HBUINT32 minMemType42; /* Minimum memory usage when an OpenType font
|
2016-04-30 19:20:56 +02:00
|
|
|
* is downloaded. */
|
2018-01-17 21:46:08 +01:00
|
|
|
HBUINT32 maxMemType42; /* Maximum memory usage when an OpenType font
|
2016-04-30 19:20:56 +02:00
|
|
|
* is downloaded. */
|
2018-01-17 21:46:08 +01:00
|
|
|
HBUINT32 minMemType1; /* Minimum memory usage when an OpenType font
|
2016-04-30 19:20:56 +02:00
|
|
|
* is downloaded as a Type 1 font. */
|
2018-01-17 21:46:08 +01:00
|
|
|
HBUINT32 maxMemType1; /* Maximum memory usage when an OpenType font
|
2016-04-30 19:20:56 +02:00
|
|
|
* is downloaded as a Type 1 font. */
|
|
|
|
/*postV2Tail v2[VAR];*/
|
|
|
|
DEFINE_SIZE_STATIC (32);
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace OT */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_OT_POST_TABLE_HH */
|