summaryrefslogtreecommitdiff
path: root/thirdparty/harfbuzz/src/hb-subset.cc
diff options
context:
space:
mode:
Diffstat (limited to 'thirdparty/harfbuzz/src/hb-subset.cc')
-rw-r--r--thirdparty/harfbuzz/src/hb-subset.cc229
1 files changed, 169 insertions, 60 deletions
diff --git a/thirdparty/harfbuzz/src/hb-subset.cc b/thirdparty/harfbuzz/src/hb-subset.cc
index 8b77ecd45a..048bdf1888 100644
--- a/thirdparty/harfbuzz/src/hb-subset.cc
+++ b/thirdparty/harfbuzz/src/hb-subset.cc
@@ -39,8 +39,10 @@
#include "hb-ot-maxp-table.hh"
#include "hb-ot-color-sbix-table.hh"
#include "hb-ot-color-colr-table.hh"
+#include "hb-ot-color-cpal-table.hh"
#include "hb-ot-os2-table.hh"
#include "hb-ot-post-table.hh"
+#include "hb-ot-post-table-v2subset.hh"
#include "hb-ot-cff1-table.hh"
#include "hb-ot-cff2-table.hh"
#include "hb-ot-vorg-table.hh"
@@ -50,7 +52,29 @@
#include "hb-ot-layout-gpos-table.hh"
#include "hb-ot-var-gvar-table.hh"
#include "hb-ot-var-hvar-table.hh"
+#include "hb-ot-math-table.hh"
+#include "hb-repacker.hh"
+/**
+ * SECTION:hb-subset
+ * @title: hb-subset
+ * @short_description: Subsets font files.
+ * @include: hb-subset.h
+ *
+ * Subsetting reduces the codepoint coverage of font files and removes all data
+ * that is no longer needed. A subset input describes the desired subset. The input is
+ * provided along with a font to the subsetting operation. Output is a new font file
+ * containing only the data specified in the input.
+ *
+ * Currently most outline and bitmap tables are supported: glyf, CFF, CFF2, sbix,
+ * COLR, and CBDT/CBLC. This also includes fonts with variable outlines via OpenType
+ * variations. Notably EBDT/EBLC and SVG are not supported. Layout subsetting is supported
+ * only for OpenType Layout tables (GSUB, GPOS, GDEF). Notably subsetting of graphite or AAT tables
+ * is not yet supported.
+ *
+ * Fonts with graphite or AAT tables may still be subsetted but will likely need to use the
+ * retain glyph ids option and configure the subset to pass through the layout tables untouched.
+ */
static unsigned
_plan_estimate_subset_table_size (hb_subset_plan_t *plan, unsigned table_len)
@@ -64,69 +88,133 @@ _plan_estimate_subset_table_size (hb_subset_plan_t *plan, unsigned table_len)
return 512 + (unsigned) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
}
+/*
+ * Repack the serialization buffer if any offset overflows exist.
+ */
+static hb_blob_t*
+_repack (hb_tag_t tag, const hb_serialize_context_t& c)
+{
+ if (tag != HB_OT_TAG_GPOS
+ && tag != HB_OT_TAG_GSUB)
+ {
+ // Check for overflow in a non-handled table.
+ return c.successful () ? c.copy_blob () : nullptr;
+ }
+
+ if (!c.offset_overflow ())
+ return c.copy_blob ();
+
+ hb_vector_t<char> buf;
+ int buf_size = c.end - c.start;
+ if (unlikely (!buf.alloc (buf_size)))
+ return nullptr;
+
+ hb_serialize_context_t repacked ((void *) buf, buf_size);
+ hb_resolve_overflows (c.object_graph (), tag, &repacked);
+
+ if (unlikely (repacked.in_error ()))
+ // TODO(garretrieger): refactor so we can share the resize/retry logic with the subset
+ // portion.
+ return nullptr;
+
+ return repacked.copy_blob ();
+}
+
+template<typename TableType>
+static
+bool
+_try_subset (const TableType *table,
+ hb_vector_t<char>* buf,
+ unsigned buf_size,
+ hb_subset_context_t* c /* OUT */)
+{
+ c->serializer->start_serialize<TableType> ();
+ if (c->serializer->in_error ()) return false;
+
+ bool needed = table->subset (c);
+ if (!c->serializer->ran_out_of_room ())
+ {
+ c->serializer->end_serialize ();
+ return needed;
+ }
+
+ buf_size += (buf_size >> 1) + 32;
+ DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.",
+ HB_UNTAG (c->table_tag), buf_size);
+
+ if (unlikely (!buf->alloc (buf_size)))
+ {
+ DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.",
+ HB_UNTAG (c->table_tag), buf_size);
+ return needed;
+ }
+
+ c->serializer->reset (buf->arrayZ, buf_size);
+ return _try_subset (table, buf, buf_size, c);
+}
+
template<typename TableType>
static bool
_subset (hb_subset_plan_t *plan)
{
- bool result = false;
hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
const TableType *table = source_blob->as<TableType> ();
hb_tag_t tag = TableType::tableTag;
- if (source_blob->data)
+ if (!source_blob->data)
+ {
+ DEBUG_MSG (SUBSET, nullptr,
+ "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
+ hb_blob_destroy (source_blob);
+ return false;
+ }
+
+ hb_vector_t<char> buf;
+ /* TODO Not all tables are glyph-related. 'name' table size for example should not be
+ * affected by number of glyphs. Accommodate that. */
+ unsigned buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
+ DEBUG_MSG (SUBSET, nullptr,
+ "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
+ if (unlikely (!buf.alloc (buf_size)))
+ {
+ DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
+ hb_blob_destroy (source_blob);
+ return false;
+ }
+
+ bool needed = false;
+ hb_serialize_context_t serializer (buf.arrayZ, buf_size);
{
- hb_vector_t<char> buf;
- /* TODO Not all tables are glyph-related. 'name' table size for example should not be
- * affected by number of glyphs. Accommodate that. */
- unsigned buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
- DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
- if (unlikely (!buf.alloc (buf_size)))
- {
- DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
- hb_blob_destroy (source_blob);
- return false;
- }
- retry:
- hb_serialize_context_t serializer ((void *) buf, buf_size);
- serializer.start_serialize<TableType> ();
hb_subset_context_t c (source_blob, plan, &serializer, tag);
- bool needed = table->subset (&c);
- if (serializer.ran_out_of_room)
- {
- buf_size += (buf_size >> 1) + 32;
- DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size);
- if (unlikely (!buf.alloc (buf_size)))
- {
- DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.", HB_UNTAG (tag), buf_size);
- hb_blob_destroy (source_blob);
- return false;
- }
- goto retry;
- }
- serializer.end_serialize ();
+ needed = _try_subset (table, &buf, buf_size, &c);
+ }
+ hb_blob_destroy (source_blob);
- result = !serializer.in_error ();
+ if (serializer.in_error () && !serializer.only_offset_overflow ())
+ {
+ DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset FAILED!", HB_UNTAG (tag));
+ return false;
+ }
- if (result)
- {
- if (needed)
- {
- hb_blob_t *dest_blob = serializer.copy_blob ();
- DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c final subset table size: %u bytes.", HB_UNTAG (tag), dest_blob->length);
- result = c.plan->add_table (tag, dest_blob);
- hb_blob_destroy (dest_blob);
- }
- else
- {
- DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
- }
- }
+ if (!needed)
+ {
+ DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
+ return true;
}
- else
- DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
- hb_blob_destroy (source_blob);
- DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
+ bool result = false;
+ hb_blob_t *dest_blob = _repack (tag, serializer);
+ if (dest_blob)
+ {
+ DEBUG_MSG (SUBSET, nullptr,
+ "OT::%c%c%c%c final subset table size: %u bytes.",
+ HB_UNTAG (tag), dest_blob->length);
+ result = plan->add_table (tag, dest_blob);
+ hb_blob_destroy (dest_blob);
+ }
+
+ DEBUG_MSG (SUBSET, nullptr, "OT::%c%c%c%c::subset %s",
+ HB_UNTAG (tag), result ? "success" : "FAILED!");
return result;
}
@@ -159,7 +247,7 @@ _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
case HB_TAG ('p','r','e','p'): /* hint table, fallthrough */
case HB_TAG ('h','d','m','x'): /* hint table, fallthrough */
case HB_TAG ('V','D','M','X'): /* hint table, fallthrough */
- return plan->drop_hints;
+ return plan->flags & HB_SUBSET_FLAGS_NO_HINTING;
#ifdef HB_NO_SUBSET_LAYOUT
// Drop Layout Tables if requested.
@@ -179,8 +267,21 @@ _should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
}
static bool
+_passthrough (hb_subset_plan_t *plan, hb_tag_t tag)
+{
+ hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
+ bool result = plan->add_table (tag, source_table);
+ hb_blob_destroy (source_table);
+ return result;
+}
+
+static bool
_subset_table (hb_subset_plan_t *plan, hb_tag_t tag)
{
+ if (plan->no_subset_tables->has (tag)) {
+ return _passthrough (plan, tag);
+ }
+
DEBUG_MSG (SUBSET, nullptr, "subset %c%c%c%c", HB_UNTAG (tag));
switch (tag)
{
@@ -202,8 +303,10 @@ _subset_table (hb_subset_plan_t *plan, hb_tag_t tag)
case HB_OT_TAG_OS2 : return _subset<const OT::OS2 > (plan);
case HB_OT_TAG_post: return _subset<const OT::post> (plan);
case HB_OT_TAG_COLR: return _subset<const OT::COLR> (plan);
+ case HB_OT_TAG_CPAL: return _subset<const OT::CPAL> (plan);
case HB_OT_TAG_CBLC: return _subset<const OT::CBLC> (plan);
case HB_OT_TAG_CBDT: return true; /* skip CBDT, handled by CBLC */
+ case HB_OT_TAG_MATH: return _subset<const OT::MATH> (plan);
#ifndef HB_NO_SUBSET_CFF
case HB_OT_TAG_cff1: return _subset<const OT::cff1> (plan);
@@ -221,28 +324,34 @@ _subset_table (hb_subset_plan_t *plan, hb_tag_t tag)
#endif
default:
- hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
- bool result = plan->add_table (tag, source_table);
- hb_blob_destroy (source_table);
- return result;
+ if (plan->flags & HB_SUBSET_FLAGS_PASSTHROUGH_UNRECOGNIZED)
+ return _passthrough (plan, tag);
+
+ // Drop table
+ return true;
}
}
/**
- * hb_subset:
+ * hb_subset_or_fail:
* @source: font face data to be subset.
* @input: input to use for the subsetting.
*
- * Subsets a font according to provided input.
+ * Subsets a font according to provided input. Returns nullptr
+ * if the subset operation fails.
+ *
+ * Since: 2.9.0
**/
hb_face_t *
-hb_subset (hb_face_t *source, hb_subset_input_t *input)
+hb_subset_or_fail (hb_face_t *source, const hb_subset_input_t *input)
{
if (unlikely (!input || !source)) return hb_face_get_empty ();
hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
- if (unlikely (plan->in_error ()))
- return hb_face_get_empty ();
+ if (unlikely (plan->in_error ())) {
+ hb_subset_plan_destroy (plan);
+ return nullptr;
+ }
hb_set_t tags_set;
bool success = true;
@@ -262,7 +371,7 @@ hb_subset (hb_face_t *source, hb_subset_input_t *input)
}
end:
- hb_face_t *result = success ? hb_face_reference (plan->dest) : hb_face_get_empty ();
+ hb_face_t *result = success ? hb_face_reference (plan->dest) : nullptr;
hb_subset_plan_destroy (plan);
return result;