diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 106143d38..5c522f1f1 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -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 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 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 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 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 baseGlyphs = (this+baseGlyphsZ).as_array (numBaseGlyphs); hb_array_t 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 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); diff --git a/src/OT/Color/COLR/colrv1-closure.hh b/src/OT/Color/COLR/colrv1-closure.hh index f94ce070c..24ef3bda5 100644 --- a/src/OT/Color/COLR/colrv1-closure.hh +++ b/src/OT/Color/COLR/colrv1-closure.hh @@ -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); diff --git a/src/OT/Color/COLR/colrv1-depend.hh b/src/OT/Color/COLR/colrv1-depend.hh index 22184c1b4..4193a3f49 100644 --- a/src/OT/Color/COLR/colrv1-depend.hh +++ b/src/OT/Color/COLR/colrv1-depend.hh @@ -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); diff --git a/src/hb-ft-colr.hh b/src/hb-ft-colr.hh index ef7862a2b..a73efd9ca 100644 --- a/src/hb-ft-colr.hh +++ b/src/hb-ft-colr.hh @@ -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; diff --git a/src/hb-paint.cc b/src/hb-paint.cc index 0224a1c0f..3899274ca 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -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 ((double) g + 0.5), hb_clamp_to ((double) r + 0.5), hb_clamp_to ((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); } } } diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 9d62f4a2c..7dd6908ca 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -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 diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index 9b20b3385..3d122731a 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -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; diff --git a/test/api/fonts/colr-shared-layers.ttf b/test/api/fonts/colr-shared-layers.ttf new file mode 100644 index 000000000..484f8a075 Binary files /dev/null and b/test/api/fonts/colr-shared-layers.ttf differ diff --git a/test/api/fonts/generate-colr-shared-layers-font.py b/test/api/fonts/generate-colr-shared-layers-font.py new file mode 100644 index 000000000..3fa303129 --- /dev/null +++ b/test/api/fonts/generate-colr-shared-layers-font.py @@ -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")) diff --git a/test/api/test-ot-depend.c b/test/api/test-ot-depend.c index 9340e0c0a..5ef9ae03d 100644 --- a/test/api/test-ot-depend.c +++ b/test/api/test-ot-depend.c @@ -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); diff --git a/test/api/test-paint.c b/test/api/test-paint.c index d7985959d..04548faf8 100644 --- a/test/api/test-paint.c +++ b/test/api/test-paint.c @@ -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++) { diff --git a/test/api/test-subset-colr.c b/test/api/test-subset-colr.c index feb9767f2..5c8f291db 100644 --- a/test/api/test-subset-colr.c +++ b/test/api/test-subset-colr.c @@ -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(); } diff --git a/test/fuzzing/fonts/colrv1-cliplist-overlapping-records.ttf b/test/fuzzing/fonts/colrv1-cliplist-overlapping-records.ttf new file mode 100644 index 000000000..a26230f5b Binary files /dev/null and b/test/fuzzing/fonts/colrv1-cliplist-overlapping-records.ttf differ