mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-12-01 10:34:23 +01:00
[COLR] Harden COLR subsetting, instancing and paint against DoS findings (#6508)
* [subset] Clamp PaintColrLayers layer range to LayerList in COLRv1 closure PaintColrLayers::closurev1() recorded firstLayerIndex .. firstLayerIndex + numLayers - 1 as retained layers without checking the range against LayerList.len (PaintColrLayers::sanitize is a plain check_struct), and _colr_closure() then creates one colrv1_layers map entry per retained index. A crafted font whose leaf paints point past the end of the LayerList could therefore add up to 255 entries each for layers that do not exist, driving subset-plan memory to roughly 1200x the font size (585 MiB for a 512 KB font). Clamp the range to the LayerList at the one place the retained-layer set is built, before add_layer_indices() and for the dispatch loop alike; this also removes the firstLayerIndex + numLayers wrap-around in the loop bound. Readers (paint_glyph, dependv1) never record indices and already tolerate out-of-range ones via LayerList::get_paint() returning Null, so they are left alone. A PaintColrLayers whose range lies past the source LayerList now has no mapping and subset writes 0xFFFFFFFF; both that and a renumbered index past the new LayerList are out of range and draw nothing. Adds a regression test that builds such a font inline and pins the output; it fails without the fix. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ROQFK0RA. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [subset] Bound shared COLRv0 layer expansion and make V0 closures linear COLR::subset() gives every retained COLRv0 base glyph a private copy of its layer range, so N base glyphs sharing L layers cost N x L layer copies with two hash lookups each, and nothing checked the total against the 16-bit numLayers header field. serialize_V0() walks the lazy iterator twice and _hb_subset_table_try() retries while doubling the buffer, so a ~200 KB font with all records naming the same range kept hb-subset busy for about two minutes before failing. Cap the summed layer count with a cheap walk over base_it before layer_it is built, and drop COLR (return_trace (false)) when the total exceeds 0xFFFF, the same way this function already handles V0 records it cannot subset; this also removes the 16-bit wrap in serialize_V0(). Any total above 65535 could only have produced a failed subset or a wrapped, invalid numLayers, so no valid output is lost. The two V0 closure helpers had the same N x L shape during planning; collect the layer indices of all retained base glyphs into one set first and visit each layer once. closure_glyphs() now takes the glyph set in one call (the accelerator wrapper changes signature with it; no public API change), and _colr_closure() passes the whole set instead of looping. Adds test_subset_colr_shared_layers (256 base glyphs all claiming the same 256 layers): retaining 255 gives a 65280-layer table, retaining all 256 gives an empty COLR. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-4T7NHCTK. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [subset] Look up each retained glyph's ClipRecord by bsearch in ClipList::subset ClipList::subset() walked startGlyphID..endGlyphID of every ClipRecord, which is only O(num_glyphs) overall when the records are sorted and disjoint. ClipList::sanitize() checks neither, so R overlapping records each spanning all glyphs cost R x 65535 iterations: a 332 KB font with 10000 whole-range records took about 20 s of CPU in hb-subset whatever glyphs or text the request asked for. Invert the loop: iterate the retained glyph set once and find each glyph's record with clips.as_array ().bsearch (gid), exactly how ClipList::get_extents() resolves a glyph's clip box when rendering, and the same shape as the COLRv0 path in COLR::subset(). Cost is now O(|glyphset| log R) regardless of record overlap. For well-formed (sorted, disjoint) lists the output is unchanged; for malformed overlapping/unsorted lists the chosen record can differ (bsearch instead of last-record-wins), which makes the subset agree with the clip box HarfBuzz itself renders from the source font. Adds an extended-format hb-subset-fuzzer input (5000 records covering all 65535 glyphs) that the subset-fuzzer tests pick up. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-MRSCGMJ7. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [subset] Compute COLR constant_after_pinning once per plan PaintRadialGradient::constant_after_pinning() answers a question about the source ItemVariationStore and the subset plan only, yet it rescanned every variation region (building a hashmap per region, up to 32767) on every paint visit. Shared paints are visited once per reference by OffsetTo::serialize_subset(), so during partial instancing the cost grew as visits x regionCount: 40 s to 6 minutes for a 192 KB font with a PaintComposite chain and many regions. Cache the answer in a tri-state plan field (-1 until computed, like the lazily-set has_gdef_varstore flag) so the scan runs at most once per plan; the plan is valid for one source face and one set of axis locations, so this is sound. Move the cheap varIdxBase != NO_VARIATION test ahead of the scan so non-variable format 6 gradients never trigger it. The allocation-failure path of get_var_region() still sets the serializer error and returns uncached, as before. Output is byte-identical; the attack font drops from ~41 s to ~0.03 s. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-910I4C0Y. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [COLR] Charge gradient color stops to the paint session work budget The COLRv1 paint walk bounds paint-table visits (HB_MAX_NESTING_LEVEL, HB_MAX_GRAPH_EDGE_COUNT) but handed out ColorLine stops uncharged, so a single 65535-stop ColorLine shared across up to 16384 gradient edges could deliver ~10^9 stops from one hb_font_paint_glyph() call, keeping the rendering thread busy for minutes per glyph in every backend. Give hb_paint_context_t the session's live budget via funcs->get_budget_remaining_ptr(), with the same local HB_BUDGET_GLYPH fallback hb_draw_session_t uses, charge HB_BUDGET_16 per stop delivered in ColorLine::get_color_stops() (the one place every consumer fetches stops, so vector, raster, cairo and client paint functions are all covered), and make recurse() skip further paints once the budget is negative, as it already does for an oversized graph. The backend's one budget is only spent, never re-armed, so this does not reintroduce the cross-subsystem budget multiplication removed in #6147. hb-ft-colr.hh has the same shape and gets the identical change. With the default 2^24 budget about a million stops per session are delivered before the rest of the walk is skipped best-effort. A separate quadratic cost inside cairo's own gradient pattern building is not addressed here. Adds a regression test that fails without the fix. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-F2NWHRVP. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [paint] Clip sweep-gradient tiles to 0..2π in hb_paint_sweep_gradient_tiles hb_paint_sweep_gradient_tiles() documents that it iterates the 0..2π sweep, and all three consumers (hb-cairo mesh patches, the SVG and PDF writers) size their per-tile loops by tile width on that assumption. Several emit sites (the start==end PAD case, the first PAD stop past 0, and repeat/reflect segments straddling 0 or 2π) passed the angles straight through, and COLRv1 variation deltas can place them hundreds of thousands of radians outside the circle, so one tile could expand to millions of patches: a 504-byte variable font drawn off its default allocated ~1.6 GiB through hb-cairo. #6157 bounded the number of sweep tiles; this bounds the width of each one. Route every emit through a small clipping lambda inside the tiler that clamps the tile to [0, 2π], lerps the end colours at the clip points, and drops tiles entirely outside, so the contract holds at the single place it is established rather than in each backend. HB_PAINT_MAX_SWEEP_TILES and the repeat-loop bounds are untouched; external callers need no change. Adds a regression test calling the public function with far-out angles for PAD/REPEAT/REFLECT; it fails without the fix. Follow-up to #6157. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GCNW3SND. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [depend] Bound COLR dependency extraction with a shared op budget COLR::depend() walked the COLRv1 paint graph from scratch for every BaseGlyphPaintRecord (clearing the visited set each time) with no total work limit, so N base glyphs sharing one paint graph cost N times the graph, quadratic in table size (204 s for a 200 KB font); the COLRv0 loop has the same base-glyphs x layers shape. The API is built only with -Dexperimental_api=true. hb_colrv1_depend_context_t, which already owns the per-walk depth and visited-set guards, now also carries max_ops sized like the sanitizer's (table length x HB_SANITIZE_MAX_OPS_FACTOR, clamped to the MIN/MAX), charged one unit per Paint::dispatch(), per COLRv0 layer record, and per PaintColrLayers layer run. Charging the layer run in dependv1() matters because a layer resolving to the null or an unknown-format paint is dropped by Paint::dispatch() before it reaches the context, so without it a PaintColrLayers could still spin its 255-iteration loop uncharged. Exhausting the budget calls depend_data->fail() (the existing error idiom) so hb_subset_depend_from_face_or_fail() returns NULL rather than a graph with silently missing edges, and the base-glyph loop stops once the builder has failed. The reproducer font is now rejected in a fraction of a second; the control still succeeds, and legitimate fonts keep 64 ops per table byte of headroom. Adds test_depend_colr_budget. Follow-up to #6113. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TO8ND880. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [ft] Drop the dead color-stop iterator resets Since the color line is copied locally ("Keep iteration local"), the iteration advances cl->color_stop_iterator on that local copy, which is discarded when the function returns. The two "reset the iterator for next time" writes (cl->color_stop_iterator = iter) therefore have no effect: there is no persistent iterator state to restore, and positioning across calls is driven entirely by the start argument. Remove both dead writes and the now-unused saved iterator. No behavior change; the return value (num_color_stops) is unaffected by iteration. Full test suite passes (277/277). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
commit
5e84cf30bb
13 files changed
+523
-56
No files matched your search
+102
-33
@@ -85,6 +85,8 @@ public:
|
||||
hb_decycler_t layers_decycler;
|
||||
int depth_left = HB_MAX_NESTING_LEVEL;
|
||||
int edge_count = HB_MAX_GRAPH_EDGE_COUNT;
|
||||
int64_t local_budget = HB_BUDGET_GLYPH;
|
||||
int64_t *budget;
|
||||
|
||||
hb_paint_context_t (const void *base_,
|
||||
hb_paint_funcs_t *funcs_,
|
||||
@@ -104,8 +106,12 @@ public:
|
||||
#endif
|
||||
),
|
||||
foreground (foreground_),
|
||||
instancer (instancer_)
|
||||
instancer (instancer_),
|
||||
budget (funcs->get_budget_remaining_ptr (data))
|
||||
{
|
||||
if (!budget)
|
||||
budget = &local_budget;
|
||||
|
||||
if (font->is_synthetic)
|
||||
{
|
||||
font = hb_font_create_sub_font (font);
|
||||
@@ -236,6 +242,9 @@ struct hb_colrv1_depend_context_t :
|
||||
template <typename T>
|
||||
return_t dispatch (const T &obj)
|
||||
{
|
||||
if (unlikely (!spend_ops ()))
|
||||
return hb_empty_t ();
|
||||
|
||||
if (unlikely (nesting_level_left == 0))
|
||||
return hb_empty_t ();
|
||||
|
||||
@@ -261,6 +270,20 @@ struct hb_colrv1_depend_context_t :
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Layers and paints shared between base glyphs are walked again for each
|
||||
* of them, which sanitize does not charge for. Bound the total work and
|
||||
* fail, rather than return a graph with edges missing. */
|
||||
bool spend_ops (unsigned count = 1)
|
||||
{
|
||||
max_ops -= (int) count;
|
||||
if (unlikely (max_ops < 0))
|
||||
{
|
||||
max_ops = -1;
|
||||
return depend_data->fail ();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const COLR* get_colr_table () const
|
||||
{ return colr; }
|
||||
|
||||
@@ -268,14 +291,19 @@ struct hb_colrv1_depend_context_t :
|
||||
const COLR *colr;
|
||||
hb_depend_data_builder_t *depend_data;
|
||||
hb_set_t visited_paint;
|
||||
int max_ops;
|
||||
unsigned nesting_level_left;
|
||||
hb_codepoint_t source_gid;
|
||||
|
||||
hb_colrv1_depend_context_t (const COLR *colr_,
|
||||
unsigned colr_length,
|
||||
hb_depend_data_builder_t *depend_data_,
|
||||
unsigned nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
|
||||
colr (colr_),
|
||||
depend_data (depend_data_),
|
||||
max_ops ((int) hb_clamp ((uint64_t) colr_length * HB_SANITIZE_MAX_OPS_FACTOR,
|
||||
(uint64_t) HB_SANITIZE_MAX_OPS_MIN,
|
||||
(uint64_t) HB_SANITIZE_MAX_OPS_MAX)),
|
||||
nesting_level_left (nesting_level_left_),
|
||||
source_gid (0)
|
||||
{}
|
||||
@@ -606,6 +634,10 @@ struct ColorLine
|
||||
for (i = 0; i < *count && start + i < len; i++)
|
||||
stops[start + i].get_color_stop (c, &color_stops[i], instancer);
|
||||
*count = i;
|
||||
|
||||
/* A gradient is one graph edge however long its color line is, and
|
||||
* color lines can be shared. recurse() stops once this runs out. */
|
||||
hb_budget_spend (*c->budget, HB_BUDGET_16, i);
|
||||
}
|
||||
|
||||
return len;
|
||||
@@ -890,6 +922,10 @@ struct PaintRadialGradient
|
||||
const ItemVarStoreInstancer &instancer) const
|
||||
{
|
||||
if (c->plan->has_avar2) return false;
|
||||
/* The answer depends on the table and the plan only, but shared paints
|
||||
* are visited once per reference, so compute it once per plan. */
|
||||
int &constant = c->plan->colrv1_constant_after_pinning;
|
||||
if (constant >= 0) return constant;
|
||||
const auto ®ions = instancer.varStore->get_region_list ();
|
||||
for (unsigned i = 0; i < regions.regionCount; i++)
|
||||
{
|
||||
@@ -904,10 +940,10 @@ struct PaintRadialGradient
|
||||
Triple *limit;
|
||||
if (!c->plan->user_axes_location.has (axis.first, &limit) ||
|
||||
limit->minimum != limit->maximum)
|
||||
return false;
|
||||
return (constant = false);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return (constant = true);
|
||||
}
|
||||
|
||||
void dependv1 (hb_colrv1_depend_context_t* c) const {}
|
||||
@@ -1052,9 +1088,8 @@ struct PaintRadialGradient
|
||||
uint32_t varIdxBase) const
|
||||
{
|
||||
TRACE_SUBSET (this);
|
||||
if (instancer && !c->plan->pinned_at_default &&
|
||||
if (instancer && !c->plan->pinned_at_default && varIdxBase != VarIdx::NO_VARIATION &&
|
||||
(c->plan->all_axes_pinned || constant_after_pinning (c, instancer)) &&
|
||||
varIdxBase != VarIdx::NO_VARIATION &&
|
||||
(radius0 + (double) roundf (instancer (varIdxBase, 2)) < 0 ||
|
||||
radius1 + (double) roundf (instancer (varIdxBase, 5)) < 0))
|
||||
return_trace (subset_nonnegative (c, instancer, varIdxBase));
|
||||
@@ -2179,17 +2214,17 @@ struct ClipList
|
||||
|
||||
hb_map_t new_gid_offset_map;
|
||||
hb_set_t new_gids;
|
||||
for (const ClipRecord& record : clips.iter ())
|
||||
/* Look up each retained glyph the same way get_extents () does, instead of
|
||||
* walking every gid of every record: records are not required to be
|
||||
* disjoint, so that can cost num_records * num_glyphs. */
|
||||
for (hb_codepoint_t gid : glyphset)
|
||||
{
|
||||
unsigned start_gid = record.startGlyphID;
|
||||
unsigned end_gid = record.endGlyphID;
|
||||
for (unsigned gid = start_gid; gid <= end_gid; gid++)
|
||||
{
|
||||
if (!glyphset.has (gid) || !glyph_map.has (gid)) continue;
|
||||
unsigned new_gid = glyph_map.get (gid);
|
||||
new_gid_offset_map.set (new_gid, record.clipBox);
|
||||
new_gids.add (new_gid);
|
||||
}
|
||||
const hb_codepoint_t *new_gid;
|
||||
if (!glyph_map.has (gid, &new_gid)) continue;
|
||||
const ClipRecord *record = clips.as_array ().bsearch (gid);
|
||||
if (!record) continue;
|
||||
new_gid_offset_map.set (*new_gid, record->clipBox);
|
||||
new_gids.add (*new_gid);
|
||||
}
|
||||
|
||||
unsigned count = serialize_clip_records (c, instancer, new_gids, new_gid_offset_map);
|
||||
@@ -2588,12 +2623,12 @@ struct COLR
|
||||
void depend (hb_depend_data_builder_t *depend_data) const
|
||||
{
|
||||
if (!has_data ()) return;
|
||||
colr->depend (depend_data);
|
||||
colr->depend (depend_data, colr.get_length ());
|
||||
}
|
||||
|
||||
void closure_glyphs (hb_codepoint_t glyph,
|
||||
void closure_glyphs (const hb_set_t *glyphs,
|
||||
hb_set_t *related_ids /* OUT */) const
|
||||
{ colr->closure_glyphs (glyph, related_ids); }
|
||||
{ colr->closure_glyphs (glyphs, related_ids); }
|
||||
|
||||
void closure_V0palette_indices (const hb_set_t *glyphs,
|
||||
hb_set_t *palettes /* OUT */) const
|
||||
@@ -2652,14 +2687,18 @@ struct COLR
|
||||
mutable hb_atomic_t<hb_colr_scratch_t *> cached_scratch;
|
||||
};
|
||||
|
||||
void depend (hb_depend_data_builder_t *depend_data) const
|
||||
void depend (hb_depend_data_builder_t *depend_data, unsigned table_length) const
|
||||
{
|
||||
hb_colrv1_depend_context_t c (this, table_length, depend_data);
|
||||
|
||||
// v0
|
||||
hb_array_t<const BaseGlyphRecord> baseGlyphs = (this+baseGlyphsZ).as_array (numBaseGlyphs);
|
||||
for (const BaseGlyphRecord &record : baseGlyphs)
|
||||
{
|
||||
auto glyph_layers = (this+layersZ).as_array (numLayers).sub_array (record.firstLayerIdx,
|
||||
record.numLayers);
|
||||
if (unlikely (!c.spend_ops (glyph_layers.length)))
|
||||
return;
|
||||
for (const LayerRecord layer : glyph_layers)
|
||||
depend_data->add_depend(record.glyphId, HB_OT_TAG_COLR, layer.glyphId);
|
||||
}
|
||||
@@ -2668,11 +2707,13 @@ struct COLR
|
||||
return;
|
||||
|
||||
// v1
|
||||
hb_colrv1_depend_context_t c (this, depend_data);
|
||||
const BaseGlyphList &baseglyph_paintrecords = this+baseGlyphList;
|
||||
|
||||
for (const BaseGlyphPaintRecord &baseglyph_paintrecord: baseglyph_paintrecords.iter ())
|
||||
{
|
||||
if (unlikely (!depend_data->successful))
|
||||
return;
|
||||
|
||||
c.source_gid = baseglyph_paintrecord.glyphId;
|
||||
c.visited_paint.clear ();
|
||||
|
||||
@@ -2681,16 +2722,33 @@ struct COLR
|
||||
}
|
||||
}
|
||||
|
||||
void closure_glyphs (hb_codepoint_t glyph,
|
||||
/* Base glyphs may share layers. The V0 closures collect the layer
|
||||
* indices of all base glyphs first, so each layer is visited once. */
|
||||
void collect_V0layer_indices (const BaseGlyphRecord &record,
|
||||
hb_set_t *layer_indices /* OUT */) const
|
||||
{
|
||||
unsigned count = (this+layersZ).as_array (numLayers).sub_array (record.firstLayerIdx,
|
||||
record.numLayers).length;
|
||||
if (!count) return;
|
||||
layer_indices->add_range (record.firstLayerIdx, record.firstLayerIdx + count - 1);
|
||||
}
|
||||
|
||||
void closure_glyphs (const hb_set_t *glyphs,
|
||||
hb_set_t *related_ids /* OUT */) const
|
||||
{
|
||||
const BaseGlyphRecord *record = get_base_glyph_record (glyph);
|
||||
if (!record) return;
|
||||
if (!numBaseGlyphs || !numLayers) return;
|
||||
hb_array_t<const LayerRecord> all_layers = (this+layersZ).as_array (numLayers);
|
||||
|
||||
auto glyph_layers = (this+layersZ).as_array (numLayers).sub_array (record->firstLayerIdx,
|
||||
record->numLayers);
|
||||
if (!glyph_layers.length) return;
|
||||
related_ids->add_array (&glyph_layers[0].glyphId, glyph_layers.length, LayerRecord::min_size);
|
||||
hb_set_t layer_indices;
|
||||
for (hb_codepoint_t glyph : *glyphs)
|
||||
{
|
||||
const BaseGlyphRecord *record = get_base_glyph_record (glyph);
|
||||
if (!record) continue;
|
||||
collect_V0layer_indices (*record, &layer_indices);
|
||||
}
|
||||
|
||||
for (unsigned idx : layer_indices)
|
||||
related_ids->add (all_layers[idx].glyphId);
|
||||
}
|
||||
|
||||
void closure_V0palette_indices (const hb_set_t *glyphs,
|
||||
@@ -2700,14 +2758,15 @@ struct COLR
|
||||
hb_array_t<const BaseGlyphRecord> baseGlyphs = (this+baseGlyphsZ).as_array (numBaseGlyphs);
|
||||
hb_array_t<const LayerRecord> all_layers = (this+layersZ).as_array (numLayers);
|
||||
|
||||
for (const BaseGlyphRecord record : baseGlyphs)
|
||||
hb_set_t layer_indices;
|
||||
for (const BaseGlyphRecord &record : baseGlyphs)
|
||||
{
|
||||
if (!glyphs->has (record.glyphId)) continue;
|
||||
hb_array_t<const LayerRecord> glyph_layers = all_layers.sub_array (record.firstLayerIdx,
|
||||
record.numLayers);
|
||||
for (const LayerRecord layer : glyph_layers)
|
||||
palettes->add (layer.colorIdx);
|
||||
collect_V0layer_indices (record, &layer_indices);
|
||||
}
|
||||
|
||||
for (unsigned idx : layer_indices)
|
||||
palettes->add (all_layers[idx].colorIdx);
|
||||
}
|
||||
|
||||
void closure_forV1 (hb_set_t *glyphset,
|
||||
@@ -2980,6 +3039,16 @@ struct COLR
|
||||
| hb_map_retains_sorting (hb_second)
|
||||
;
|
||||
|
||||
/* Base glyphs sharing layers each get their own copy in the output.
|
||||
* Drop the table instead of expanding more layers than numLayers holds. */
|
||||
unsigned num_layers = 0;
|
||||
for (const BaseGlyphRecord record : + base_it)
|
||||
{
|
||||
num_layers += record.numLayers;
|
||||
if (unlikely (num_layers > 0xFFFFu))
|
||||
return_trace (false);
|
||||
}
|
||||
|
||||
auto layer_it =
|
||||
+ hb_range (c->plan->num_output_glyphs ())
|
||||
| hb_map (reverse_glyph_map)
|
||||
@@ -3235,7 +3304,7 @@ struct COLR_accelerator_t : COLR::accelerator_t {
|
||||
void
|
||||
hb_paint_context_t::recurse (const Paint &paint)
|
||||
{
|
||||
if (unlikely (depth_left <= 0 || edge_count <= 0)) return;
|
||||
if (unlikely (depth_left <= 0 || edge_count <= 0 || *budget < 0)) return;
|
||||
depth_left--;
|
||||
edge_count--;
|
||||
paint.dispatch (this);
|
||||
|
||||
@@ -38,9 +38,13 @@ namespace OT {
|
||||
|
||||
HB_INTERNAL void PaintColrLayers::closurev1 (hb_colrv1_closure_context_t* c) const
|
||||
{
|
||||
c->add_layer_indices (firstLayerIndex, numLayers);
|
||||
const LayerList &paint_offset_lists = c->get_colr_table ()->get_layerList ();
|
||||
for (unsigned i = firstLayerIndex; i < firstLayerIndex + numLayers; i++)
|
||||
/* Only retain layers that exist in the LayerList; firstLayerIndex is not
|
||||
* checked against it at sanitize time. */
|
||||
unsigned first = hb_min ((unsigned) firstLayerIndex, (unsigned) paint_offset_lists.len);
|
||||
unsigned count = hb_min ((unsigned) numLayers, paint_offset_lists.len - first);
|
||||
c->add_layer_indices (first, count);
|
||||
for (unsigned i = first; i < first + count; i++)
|
||||
{
|
||||
const Paint &paint = paint_offset_lists.get_paint (i);
|
||||
paint.dispatch (c);
|
||||
|
||||
@@ -39,6 +39,10 @@ namespace OT {
|
||||
HB_INTERNAL void PaintColrLayers::dependv1 (hb_colrv1_depend_context_t* c) const
|
||||
{
|
||||
const LayerList &paint_offset_lists = c->get_colr_table ()->get_layerList ();
|
||||
/* Charge the whole layer run: a layer resolving to the null or an
|
||||
* unknown-format paint is dropped by Paint::dispatch() before it reaches
|
||||
* the context, so without this the loop can still spin uncharged. */
|
||||
if (unlikely (!c->spend_ops (numLayers))) return;
|
||||
for (unsigned i = firstLayerIndex; i < firstLayerIndex + numLayers; i++)
|
||||
{
|
||||
const Paint &paint = paint_offset_lists.get_paint (i);
|
||||
|
||||
+11
-8
@@ -113,8 +113,12 @@ struct hb_ft_paint_context_t
|
||||
hb_color_t foreground) :
|
||||
ft_font (ft_font_), font (font_),
|
||||
funcs (paint_funcs), data (paint_data),
|
||||
palette (palette), palette_index (palette_index), foreground (foreground)
|
||||
palette (palette), palette_index (palette_index), foreground (foreground),
|
||||
budget (funcs->get_budget_remaining_ptr (data))
|
||||
{
|
||||
if (!budget)
|
||||
budget = &local_budget;
|
||||
|
||||
if (font->is_synthetic)
|
||||
{
|
||||
font = hb_font_create_sub_font (font);
|
||||
@@ -132,7 +136,7 @@ struct hb_ft_paint_context_t
|
||||
|
||||
void recurse (FT_OpaquePaint paint)
|
||||
{
|
||||
if (unlikely (depth_left <= 0 || edge_count <= 0)) return;
|
||||
if (unlikely (depth_left <= 0 || edge_count <= 0 || *budget < 0)) return;
|
||||
depth_left--;
|
||||
edge_count--;
|
||||
_hb_ft_paint (this, paint);
|
||||
@@ -150,6 +154,8 @@ struct hb_ft_paint_context_t
|
||||
hb_decycler_t layers_decycler;
|
||||
int depth_left = HB_MAX_NESTING_LEVEL;
|
||||
int edge_count = HB_MAX_GRAPH_EDGE_COUNT;
|
||||
int64_t local_budget = HB_BUDGET_GLYPH;
|
||||
int64_t *budget;
|
||||
};
|
||||
|
||||
static void
|
||||
@@ -210,7 +216,6 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line,
|
||||
hb_lock_t lock (c->ft_font->lock);
|
||||
FT_ColorStop stop;
|
||||
unsigned wrote = 0;
|
||||
FT_ColorStopIterator iter = cl->color_stop_iterator;
|
||||
|
||||
if (start >= cl->color_stop_iterator.num_color_stops)
|
||||
{
|
||||
@@ -227,9 +232,6 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line,
|
||||
// FT_Get_Colorline_Stops can in some cases return 0, and
|
||||
// does not advance the iterator. So stop iteration to prevent
|
||||
// infinite loop here.
|
||||
//
|
||||
// reset the iterator for next time
|
||||
cl->color_stop_iterator = iter;
|
||||
return cl->color_stop_iterator.num_color_stops;
|
||||
}
|
||||
}
|
||||
@@ -253,8 +255,9 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line,
|
||||
}
|
||||
|
||||
*count = wrote;
|
||||
// reset the iterator for next time
|
||||
cl->color_stop_iterator = iter;
|
||||
|
||||
// recurse() stops once this runs out
|
||||
hb_budget_spend (*c->budget, HB_BUDGET_16, wrote);
|
||||
}
|
||||
|
||||
return cl->color_stop_iterator.num_color_stops;
|
||||
|
||||
+29
-11
@@ -1080,6 +1080,7 @@ hb_paint_normalize_color_line (hb_color_stop_t *stops,
|
||||
*
|
||||
* Iterates the full 0..2π sweep produced by a color-stop list,
|
||||
* invoking @emit_patch once per (start, end) angular segment.
|
||||
* Segments are clipped to 0..2π, wherever the angles lie.
|
||||
* Handles #HB_PAINT_EXTEND_PAD, #HB_PAINT_EXTEND_REPEAT, and
|
||||
* #HB_PAINT_EXTEND_REFLECT. Stops must be pre-sorted by
|
||||
* offset; use hb_paint_normalize_color_line() first if they
|
||||
@@ -1098,14 +1099,31 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops,
|
||||
{
|
||||
if (!n_stops) return;
|
||||
|
||||
/* Variation deltas can put the angles any distance outside the
|
||||
* circle. Emit only the part of each tile that is within 0..2π;
|
||||
* callers do work proportional to the width of the tile. */
|
||||
auto emit = [&] (float a0, hb_color_t c0, float a1, hb_color_t c1)
|
||||
{
|
||||
float t0 = hb_max (a0, 0.f);
|
||||
float t1 = hb_min (a1, HB_2_PI);
|
||||
if (t0 > t1) return;
|
||||
hb_color_t d0 = c0, d1 = c1;
|
||||
if (c0 != c1)
|
||||
{
|
||||
if (t0 != a0) d0 = hb_color_lerp (c0, c1, (t0 - a0) / (a1 - a0));
|
||||
if (t1 != a1) d1 = hb_color_lerp (c0, c1, (t1 - a0) / (a1 - a0));
|
||||
}
|
||||
emit_patch (t0, d0, t1, d1, user_data);
|
||||
};
|
||||
|
||||
if (start_angle == end_angle)
|
||||
{
|
||||
if (extend == HB_PAINT_EXTEND_PAD)
|
||||
{
|
||||
if (start_angle > 0.f)
|
||||
emit_patch (0.f, stops[0].color, start_angle, stops[0].color, user_data);
|
||||
emit (0.f, stops[0].color, start_angle, stops[0].color);
|
||||
if (end_angle < HB_2_PI)
|
||||
emit_patch (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color, user_data);
|
||||
emit (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -1168,26 +1186,26 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops,
|
||||
if (pos == n_stops)
|
||||
{
|
||||
color0 = colors[n_stops - 1];
|
||||
emit_patch (0.f, color0, HB_2_PI, color0, user_data);
|
||||
emit (0.f, color0, HB_2_PI, color0);
|
||||
goto done;
|
||||
}
|
||||
emit_patch (0.f, color0, angles[pos], colors[pos], user_data);
|
||||
emit (0.f, color0, angles[pos], colors[pos]);
|
||||
for (pos++; pos < n_stops; pos++)
|
||||
{
|
||||
if (angles[pos] <= HB_2_PI)
|
||||
emit_patch (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos], user_data);
|
||||
emit (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]);
|
||||
else
|
||||
{
|
||||
float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]);
|
||||
hb_color_t color1 = hb_color_lerp (colors[pos - 1], colors[pos], f);
|
||||
emit_patch (angles[pos - 1], colors[pos - 1], HB_2_PI, color1, user_data);
|
||||
emit (angles[pos - 1], colors[pos - 1], HB_2_PI, color1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos == n_stops)
|
||||
{
|
||||
color0 = colors[n_stops - 1];
|
||||
emit_patch (angles[n_stops - 1], color0, HB_2_PI, color0, user_data);
|
||||
emit (angles[n_stops - 1], color0, HB_2_PI, color0);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
@@ -1217,7 +1235,7 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops,
|
||||
hb_clamp_to<uint8_t> ((double) g + 0.5),
|
||||
hb_clamp_to<uint8_t> ((double) r + 0.5),
|
||||
hb_clamp_to<uint8_t> ((double) a + 0.5));
|
||||
emit_patch (0.f, avg, HB_2_PI, avg, user_data);
|
||||
emit (0.f, avg, HB_2_PI, avg);
|
||||
goto done;
|
||||
}
|
||||
|
||||
@@ -1265,17 +1283,17 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops,
|
||||
{
|
||||
float f = (0.f - a0_l) / (a1_l - a0_l);
|
||||
hb_color_t c = hb_color_lerp (col0, col1, f);
|
||||
emit_patch (0.f, c, a1_l, col1, user_data);
|
||||
emit (0.f, c, a1_l, col1);
|
||||
}
|
||||
else if (a1_l >= HB_2_PI)
|
||||
{
|
||||
float f = (HB_2_PI - a0_l) / (a1_l - a0_l);
|
||||
hb_color_t c = hb_color_lerp (col0, col1, f);
|
||||
emit_patch (a0_l, col0, HB_2_PI, c, user_data);
|
||||
emit (a0_l, col0, HB_2_PI, c);
|
||||
goto done;
|
||||
}
|
||||
else
|
||||
emit_patch (a0_l, col0, a1_l, col1, user_data);
|
||||
emit (a0_l, col0, a1_l, col1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,8 +118,7 @@ static void _colr_closure (hb_subset_plan_t* plan,
|
||||
hb_set_t palette_indices, layer_indices;
|
||||
// Collect all glyphs referenced by COLRv0
|
||||
hb_set_t glyphset_colrv0;
|
||||
for (hb_codepoint_t gid : *glyphs_colred)
|
||||
colr.closure_glyphs (gid, &glyphset_colrv0);
|
||||
colr.closure_glyphs (glyphs_colred, &glyphset_colrv0);
|
||||
|
||||
glyphs_colred->union_ (glyphset_colrv0);
|
||||
|
||||
@@ -696,6 +695,7 @@ hb_subset_plan_t::hb_subset_plan_t (hb_face_t *face,
|
||||
all_axes_pinned = false;
|
||||
pinned_at_default = true;
|
||||
has_gdef_varstore = false;
|
||||
colrv1_constant_after_pinning = -1;
|
||||
has_avar2 = false;
|
||||
|
||||
#ifdef HB_EXPERIMENTAL_API
|
||||
|
||||
@@ -169,6 +169,9 @@ struct hb_subset_plan_t
|
||||
// whether GDEF ItemVariationStore is retained
|
||||
mutable bool has_gdef_varstore;
|
||||
|
||||
// whether every COLR variation region uses pinned axes only; -1 until computed
|
||||
int colrv1_constant_after_pinning;
|
||||
|
||||
// whether we're doing avar2 partial instancing
|
||||
bool has_avar2;
|
||||
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,34 @@
|
||||
"""Generate a COLRv0 font whose base glyphs all share the same layer range."""
|
||||
|
||||
from pathlib import Path
|
||||
from fontTools.fontBuilder import FontBuilder
|
||||
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
||||
from fontTools.ttLib.tables.DefaultTable import DefaultTable
|
||||
import struct
|
||||
|
||||
# Every base glyph claims all the layers, so retaining 256 of them expands
|
||||
# to 65536 layer records, one more than the COLR header can count.
|
||||
count = 256
|
||||
bases = [f"base{i}" for i in range(count)]
|
||||
layers = [f"layer{i}" for i in range(count)]
|
||||
names = [".notdef"] + bases + layers
|
||||
fb = FontBuilder(1000, isTTF=True)
|
||||
fb.setupGlyphOrder(names)
|
||||
fb.setupCharacterMap({0xE000 + i: name for i, name in enumerate(bases)})
|
||||
fb.setupGlyf({name: TTGlyphPen(None).glyph() for name in names})
|
||||
fb.setupHorizontalMetrics({name: (1000, 0) for name in names})
|
||||
fb.setupHorizontalHeader(ascent=800, descent=-200)
|
||||
fb.setupNameTable({"familyName": "COLR Shared Layers", "styleName": "Regular"})
|
||||
fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200)
|
||||
fb.setupPost()
|
||||
fb.setupCPAL([[(0, 0, 0, 1), (1, 1, 1, 1)]])
|
||||
|
||||
# fontTools writes each base glyph its own layers; build the table by hand.
|
||||
header = struct.pack(">HHIIH", 0, count, 14, 14 + count * 6, count)
|
||||
base_records = b"".join(struct.pack(">HHH", 1 + i, 0, count) for i in range(count))
|
||||
layer_records = b"".join(struct.pack(">HH", 1 + count + i, i % 2) for i in range(count))
|
||||
colr = DefaultTable("COLR")
|
||||
colr.data = header + base_records + layer_records
|
||||
fb.font["COLR"] = colr
|
||||
|
||||
fb.save(Path(__file__).with_name("colr-shared-layers.ttf"))
|
||||
@@ -353,6 +353,86 @@ test_depend_colr (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static hb_bool_t
|
||||
colr_depend_succeeds (const unsigned char *colr, unsigned int length, unsigned int num_glyphs)
|
||||
{
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
hb_blob_t *blob = hb_blob_create_or_fail ((const char *) colr, length, HB_MEMORY_MODE_DUPLICATE, NULL, NULL);
|
||||
hb_face_builder_add_table (face, HB_TAG ('C','O','L','R'), blob);
|
||||
hb_blob_destroy (blob);
|
||||
hb_face_set_glyph_count (face, num_glyphs);
|
||||
|
||||
hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face);
|
||||
hb_bool_t ret = depend != NULL;
|
||||
|
||||
hb_subset_depend_destroy (depend);
|
||||
hb_face_destroy (face);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Test that COLR work repeated for every base glyph is bounded */
|
||||
static void
|
||||
test_depend_colr_budget (void)
|
||||
{
|
||||
{
|
||||
/* COLRv0: every base glyph uses all of the layers. */
|
||||
enum { NUM_GLYPHS = 2048, LAYERS = 14 + NUM_GLYPHS * 6 };
|
||||
static unsigned char colr[LAYERS + NUM_GLYPHS * 4];
|
||||
colr[7] = 14;
|
||||
colr[10] = LAYERS >> 8;
|
||||
colr[11] = LAYERS & 0xFF;
|
||||
colr[12] = NUM_GLYPHS >> 8;
|
||||
for (unsigned i = 0; i < NUM_GLYPHS; i++)
|
||||
{
|
||||
unsigned char *record = colr + 14 + i * 6;
|
||||
record[0] = i >> 8;
|
||||
record[1] = i & 0xFF;
|
||||
record[4] = NUM_GLYPHS >> 8;
|
||||
}
|
||||
|
||||
colr[3] = 1;
|
||||
g_assert_true (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS));
|
||||
colr[2] = NUM_GLYPHS >> 8;
|
||||
colr[3] = 0;
|
||||
g_assert_false (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS));
|
||||
}
|
||||
|
||||
{
|
||||
/* COLRv1: 255 PaintColrLayers that each list all 255 of them as layers,
|
||||
* and every base glyph uses the first one. */
|
||||
enum { NUM_GLYPHS = 64, NUM_PAINTS = 255,
|
||||
BASE_GLYPHS = 34,
|
||||
LAYERS = BASE_GLYPHS + 4 + NUM_GLYPHS * 6,
|
||||
PAINTS = LAYERS + 4 + NUM_PAINTS * 4 };
|
||||
static unsigned char colr[PAINTS + NUM_PAINTS * 6];
|
||||
colr[1] = 1;
|
||||
colr[17] = BASE_GLYPHS;
|
||||
colr[20] = LAYERS >> 8;
|
||||
colr[21] = LAYERS & 0xFF;
|
||||
for (unsigned i = 0; i < NUM_GLYPHS; i++)
|
||||
{
|
||||
unsigned char *record = colr + BASE_GLYPHS + 4 + i * 6;
|
||||
record[1] = i;
|
||||
record[4] = (PAINTS - BASE_GLYPHS) >> 8;
|
||||
record[5] = (PAINTS - BASE_GLYPHS) & 0xFF;
|
||||
}
|
||||
colr[LAYERS + 3] = NUM_PAINTS;
|
||||
for (unsigned i = 0; i < NUM_PAINTS; i++)
|
||||
{
|
||||
unsigned offset = PAINTS - LAYERS + i * 6;
|
||||
colr[LAYERS + 4 + i * 4 + 2] = offset >> 8;
|
||||
colr[LAYERS + 4 + i * 4 + 3] = offset & 0xFF;
|
||||
colr[PAINTS + i * 6] = 1;
|
||||
colr[PAINTS + i * 6 + 1] = NUM_PAINTS;
|
||||
}
|
||||
|
||||
colr[BASE_GLYPHS + 3] = 1;
|
||||
g_assert_true (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS));
|
||||
colr[BASE_GLYPHS + 3] = NUM_GLYPHS;
|
||||
g_assert_false (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS));
|
||||
}
|
||||
}
|
||||
|
||||
/* Test MATH (mathematical variants) dependency extraction */
|
||||
static void
|
||||
test_depend_math (void)
|
||||
@@ -631,6 +711,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_depend_glyf);
|
||||
hb_test_add (test_depend_cff);
|
||||
hb_test_add (test_depend_colr);
|
||||
hb_test_add (test_depend_colr_budget);
|
||||
hb_test_add (test_depend_math);
|
||||
hb_test_add (test_depend_varc);
|
||||
hb_test_add (test_depend_gsub_formats);
|
||||
|
||||
@@ -1149,6 +1149,149 @@ test_hb_paint_budget (void)
|
||||
hb_paint_funcs_destroy (funcs);
|
||||
}
|
||||
|
||||
static unsigned color_stops_fetched;
|
||||
|
||||
static void
|
||||
fetch_color_stops (hb_paint_funcs_t *funcs HB_UNUSED,
|
||||
void *paint_data HB_UNUSED,
|
||||
hb_color_line_t *color_line,
|
||||
float x0 HB_UNUSED, float y0 HB_UNUSED,
|
||||
float x1 HB_UNUSED, float y1 HB_UNUSED,
|
||||
float x2 HB_UNUSED, float y2 HB_UNUSED,
|
||||
void *user_data HB_UNUSED)
|
||||
{
|
||||
hb_color_stop_t stops[100];
|
||||
unsigned count = G_N_ELEMENTS (stops);
|
||||
g_assert_cmpuint (hb_color_line_get_color_stops (color_line, 0, &count, stops), ==, 100);
|
||||
g_assert_cmpuint (count, ==, 100);
|
||||
color_stops_fetched += count;
|
||||
}
|
||||
|
||||
static void
|
||||
test_hb_paint_color_stops_budget (void)
|
||||
{
|
||||
/* A COLRv1 glyph whose four layers all are the same linear gradient
|
||||
* with 100 color stops. Fetching the stops must consume budget. */
|
||||
char colr_data[34 + 16 + 20 + 16 + 3 + 100 * 6 + 21] = {0};
|
||||
colr_data[1] = 1; /* version */
|
||||
colr_data[17] = 34; /* baseGlyphList */
|
||||
colr_data[21] = 50; /* layerList */
|
||||
colr_data[24] = 0x02; colr_data[25] = (char) 0xB1; /* clipList */
|
||||
colr_data[34 + 3] = 1; /* BaseGlyphList: glyph 0 */
|
||||
colr_data[34 + 9] = 10;
|
||||
colr_data[34 + 10] = 1; /* PaintColrLayers */
|
||||
colr_data[34 + 11] = 4;
|
||||
colr_data[50 + 3] = 4; /* LayerList */
|
||||
for (unsigned i = 0; i < 4; i++)
|
||||
colr_data[50 + 4 + i * 4 + 3] = 20;
|
||||
colr_data[50 + 20] = 4; /* PaintLinearGradient */
|
||||
colr_data[50 + 23] = 16;
|
||||
colr_data[50 + 36 + 2] = 100; /* ColorLine */
|
||||
colr_data[689] = 1; /* ClipList: glyph 0 */
|
||||
colr_data[689 + 4] = 1;
|
||||
colr_data[689 + 11] = 12;
|
||||
colr_data[689 + 12] = 1; /* ClipBox */
|
||||
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
HB_FACE_ADD_TABLE (face, "COLR", colr_data);
|
||||
hb_face_set_glyph_count (face, 1);
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
|
||||
paint_budget_data_t data = {HB_BUDGET_DEFAULT, 0};
|
||||
hb_paint_funcs_t *funcs = hb_paint_funcs_create ();
|
||||
hb_paint_funcs_set_linear_gradient_func (funcs, fetch_color_stops, NULL, NULL);
|
||||
hb_paint_funcs_set_set_budget_func (funcs, paint_set_budget, NULL, NULL);
|
||||
hb_paint_funcs_set_get_budget_remaining_func (funcs, paint_get_budget_remaining, NULL, NULL);
|
||||
|
||||
color_stops_fetched = 0;
|
||||
g_assert_true (hb_paint_set_budget (funcs, &data, 100000));
|
||||
hb_font_paint_glyph (font, 0, funcs, &data, 0, HB_COLOR (0, 0, 0, 255));
|
||||
g_assert_cmpuint (color_stops_fetched, ==, 400);
|
||||
g_assert_cmpint (data.live, <=, 100000 - 400);
|
||||
g_assert_cmpint (data.live, >=, 0);
|
||||
|
||||
color_stops_fetched = 0;
|
||||
g_assert_true (hb_paint_set_budget (funcs, &data, 100));
|
||||
hb_font_paint_glyph (font, 0, funcs, &data, 0, HB_COLOR (0, 0, 0, 255));
|
||||
g_assert_cmpuint (color_stops_fetched, ==, 100);
|
||||
g_assert_cmpint (data.live, <, 0);
|
||||
|
||||
hb_paint_funcs_destroy (funcs);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
unsigned tiles;
|
||||
float covered;
|
||||
hb_color_t c0, c1;
|
||||
} sweep_tiles_data_t;
|
||||
|
||||
static void
|
||||
check_sweep_tile (float a0, hb_color_t c0,
|
||||
float a1, hb_color_t c1,
|
||||
void *user_data)
|
||||
{
|
||||
sweep_tiles_data_t *data = (sweep_tiles_data_t *) user_data;
|
||||
|
||||
g_assert_cmpfloat (a0, >=, 0.f);
|
||||
g_assert_cmpfloat (a0, <=, a1);
|
||||
g_assert_cmpfloat (a1, <=, (float) (2 * G_PI));
|
||||
data->tiles++;
|
||||
data->covered += a1 - a0;
|
||||
data->c0 = c0;
|
||||
data->c1 = c1;
|
||||
}
|
||||
|
||||
static void
|
||||
test_hb_paint_sweep_gradient_tiles_clipped (void)
|
||||
{
|
||||
/* Variation deltas can put the angles far outside the circle. The
|
||||
* tiles must still cover 0..2π, and nothing else. */
|
||||
static const struct {
|
||||
hb_paint_extend_t extend;
|
||||
unsigned n_stops;
|
||||
float start_angle, end_angle;
|
||||
} tests[] = {
|
||||
{ HB_PAINT_EXTEND_PAD, 1, 1e6f, 1e6f },
|
||||
{ HB_PAINT_EXTEND_PAD, 1, -1e6f, -1e6f },
|
||||
{ HB_PAINT_EXTEND_PAD, 1, 1e6f, 2e6f },
|
||||
{ HB_PAINT_EXTEND_PAD, 2, 1e6f, 2e6f },
|
||||
{ HB_PAINT_EXTEND_PAD, 2, -2e6f, -1e6f },
|
||||
{ HB_PAINT_EXTEND_PAD, 2, -1e6f, 1e6f },
|
||||
{ HB_PAINT_EXTEND_PAD, 2, 1e6f, -1e6f },
|
||||
{ HB_PAINT_EXTEND_PAD, 2, 1.f, 1e6f },
|
||||
{ HB_PAINT_EXTEND_REPEAT, 2, -1e6f, 1e6f },
|
||||
{ HB_PAINT_EXTEND_REPEAT, 2, 1e6f, 3e6f },
|
||||
{ HB_PAINT_EXTEND_REPEAT, 2, 1e20f, 3e20f },
|
||||
{ HB_PAINT_EXTEND_REFLECT, 2, -1e6f, 1e6f },
|
||||
{ HB_PAINT_EXTEND_REFLECT, 2, -3e6f, -1e6f },
|
||||
};
|
||||
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (tests); i++)
|
||||
{
|
||||
hb_color_stop_t stops[2] = {
|
||||
{ 0.f, FALSE, HB_COLOR (0, 0, 0, 255) },
|
||||
{ 1.f, FALSE, HB_COLOR (255, 255, 255, 255) },
|
||||
};
|
||||
sweep_tiles_data_t data = {0, 0.f, 0, 0};
|
||||
|
||||
hb_paint_sweep_gradient_tiles (stops, tests[i].n_stops, tests[i].extend,
|
||||
tests[i].start_angle, tests[i].end_angle,
|
||||
check_sweep_tile, &data);
|
||||
|
||||
g_assert_cmpuint (data.tiles, >=, 1);
|
||||
g_assert_cmpuint (data.tiles, <=, 3);
|
||||
g_assert_cmpfloat (data.covered, >, (float) (2 * G_PI) - 1e-3f);
|
||||
g_assert_cmpfloat (data.covered, <, (float) (2 * G_PI) + 1e-3f);
|
||||
if (tests[i].n_stops == 1)
|
||||
{
|
||||
g_assert_cmphex (data.c0, ==, stops[0].color);
|
||||
g_assert_cmphex (data.c1, ==, stops[0].color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
@@ -1164,6 +1307,8 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_fill_glyph_nil_decomposes);
|
||||
hb_test_add (test_color_glyph_user_data);
|
||||
hb_test_add (test_hb_paint_budget);
|
||||
hb_test_add (test_hb_paint_color_stops_budget);
|
||||
hb_test_add (test_hb_paint_sweep_gradient_tiles_clipped);
|
||||
|
||||
for (unsigned int i = 0; i < G_N_ELEMENTS (paint_tests); i++)
|
||||
{
|
||||
|
||||
@@ -587,6 +587,110 @@ test_subset_colr_var_overflow (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
/* A PaintColrLayers whose layer range lies past the end of the LayerList
|
||||
* must not make the subsetter retain (and allocate mappings for) layers
|
||||
* that do not exist. */
|
||||
static void
|
||||
test_subset_colr_layers_out_of_range (void)
|
||||
{
|
||||
const uint8_t maxp_data[6] = {
|
||||
0x00, 0x00, 0x50, 0x00,
|
||||
0x00, 0x02 /* numGlyphs */
|
||||
};
|
||||
const uint8_t colr_data[73] = {
|
||||
/* header: version 1, no COLRv0 records, BaseGlyphList @34, LayerList @50 */
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x32,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
/* BaseGlyphList: glyph 1 -> PaintColrLayers (numLayers 2, firstLayerIndex 0) */
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0A,
|
||||
0x01, 0x02, 0x00, 0x00, 0x00, 0x00,
|
||||
/* LayerList: PaintSolid, then PaintColrLayers (numLayers 255, firstLayerIndex 0xFFFFFF00) */
|
||||
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x11,
|
||||
0x02, 0x00, 0x00, 0x40, 0x00,
|
||||
0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
|
||||
};
|
||||
|
||||
hb_blob_t *colr = hb_blob_create ((const char *) colr_data, sizeof (colr_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
|
||||
hb_blob_t *maxp = hb_blob_create ((const char *) maxp_data, sizeof (maxp_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
|
||||
hb_face_t *builder = hb_face_builder_create ();
|
||||
hb_face_builder_add_table (builder, HB_TAG ('C','O','L','R'), colr);
|
||||
hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), maxp);
|
||||
hb_blob_t *face_blob = hb_face_reference_blob (builder);
|
||||
hb_face_t *face = hb_face_create (face_blob, 0);
|
||||
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add (hb_subset_input_glyph_set (input), 1);
|
||||
hb_face_t *subset = hb_subset_test_create_subset (face, input);
|
||||
|
||||
hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('C','O','L','R'));
|
||||
unsigned length;
|
||||
const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length);
|
||||
g_assert_cmpuint (length, >=, 34);
|
||||
unsigned list = read_uint_be (data + 18, 4);
|
||||
g_assert_cmpuint (list + 12, <=, length);
|
||||
/* Only the two layers that exist are kept. */
|
||||
g_assert_cmpuint (read_uint_be (data + list, 4), ==, 2);
|
||||
unsigned paint = list + read_uint_be (data + list + 8, 4);
|
||||
g_assert_cmpuint (paint + 6, <=, length);
|
||||
g_assert_cmpuint (data[paint], ==, 1);
|
||||
g_assert_cmpuint (data[paint + 1], ==, 255);
|
||||
/* The out-of-range range was not retained, so it has no mapping. */
|
||||
g_assert_cmpuint (read_uint_be (data + paint + 2, 4), ==, 0xFFFFFFFFu);
|
||||
hb_blob_destroy (blob);
|
||||
|
||||
hb_face_destroy (subset);
|
||||
hb_face_destroy (face);
|
||||
hb_blob_destroy (face_blob);
|
||||
hb_face_destroy (builder);
|
||||
hb_blob_destroy (maxp);
|
||||
hb_blob_destroy (colr);
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_colr_shared_layers (void)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/colr-shared-layers.ttf");
|
||||
/* Every base glyph claims all 256 layers. Retaining 255 of them expands
|
||||
* to 65280 layer records; one more does not fit numLayers. */
|
||||
for (unsigned count = 255; count <= 256; count++)
|
||||
{
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_unicode_set (input), 0xE000, 0xE000 + count - 1);
|
||||
hb_face_t *subset = hb_subset_test_create_subset (face, input);
|
||||
|
||||
hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('C','O','L','R'));
|
||||
unsigned length;
|
||||
const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length);
|
||||
if (count == 256)
|
||||
{
|
||||
g_assert_cmpuint (length, ==, 0);
|
||||
hb_blob_destroy (blob);
|
||||
hb_face_destroy (subset);
|
||||
continue;
|
||||
}
|
||||
g_assert_cmpuint (length, ==, 14 + count * 6 + count * 256 * 4);
|
||||
g_assert_cmpuint (read_uint_be (data + 2, 2), ==, count);
|
||||
g_assert_cmpuint (read_uint_be (data + 12, 2), ==, count * 256);
|
||||
unsigned bases = read_uint_be (data + 4, 4), layers = read_uint_be (data + 8, 4);
|
||||
for (unsigned i = 0; i < count; i++)
|
||||
{
|
||||
g_assert_cmpuint (read_uint_be (data + bases + i * 6, 2), ==, i + 1);
|
||||
g_assert_cmpuint (read_uint_be (data + bases + i * 6 + 2, 2), ==, i * 256);
|
||||
g_assert_cmpuint (read_uint_be (data + bases + i * 6 + 4, 2), ==, 256);
|
||||
for (unsigned j = 0; j < 256; j++)
|
||||
{
|
||||
unsigned layer = layers + (i * 256 + j) * 4;
|
||||
g_assert_cmpuint (read_uint_be (data + layer, 2), ==, count + 1 + j);
|
||||
g_assert_cmpuint (read_uint_be (data + layer + 2, 2), ==, j % 2);
|
||||
}
|
||||
}
|
||||
hb_blob_destroy (blob);
|
||||
hb_face_destroy (subset);
|
||||
}
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -603,6 +707,8 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_subset_colr_affine_values);
|
||||
hb_test_add (test_subset_colr_sentinel_map_without_variations);
|
||||
hb_test_add (test_subset_colr_var_overflow);
|
||||
hb_test_add (test_subset_colr_layers_out_of_range);
|
||||
hb_test_add (test_subset_colr_shared_layers);
|
||||
|
||||
return hb_test_run();
|
||||
}
|
||||
Binary file not shown.
Reference in new issue
Block a user