From 5130b0f5174e5c5c7093be2842b9916e9ef1297f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 22:56:49 -0300 Subject: [PATCH 001/105] raster/svg: support style props and harden gradient/url parsing --- src/hb-raster-svg.cc | 231 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 195 insertions(+), 36 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index afcd005cb..cfac1fcc1 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -113,6 +113,99 @@ struct hb_svg_str_t } }; +static inline char +svg_ascii_lower (char c) +{ + if (c >= 'A' && c <= 'Z') + return c + ('a' - 'A'); + return c; +} + +static bool +svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit) +{ + unsigned n = (unsigned) strlen (lit); + if (s.len != n) return false; + for (unsigned i = 0; i < n; i++) + if (svg_ascii_lower (s.data[i]) != svg_ascii_lower (lit[i])) + return false; + return true; +} + +static bool +svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit) +{ + unsigned n = (unsigned) strlen (lit); + if (s.len < n) return false; + for (unsigned i = 0; i < n; i++) + if (svg_ascii_lower (s.data[i]) != svg_ascii_lower (lit[i])) + return false; + return true; +} + +static hb_svg_str_t +svg_style_find_property (hb_svg_str_t style, const char *name) +{ + if (style.is_null ()) return {}; + const char *p = style.data; + const char *end = style.data + style.len; + while (p < end) + { + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ';')) + p++; + if (p >= end) break; + const char *name_start = p; + while (p < end && *p != ':' && *p != ';') + p++; + const char *name_end = p; + while (name_end > name_start && + (name_end[-1] == ' ' || name_end[-1] == '\t' || name_end[-1] == '\n' || name_end[-1] == '\r')) + name_end--; + if (p >= end || *p != ':') + { + while (p < end && *p != ';') p++; + continue; + } + p++; /* skip ':' */ + const char *value_start = p; + while (p < end && *p != ';') + p++; + const char *value_end = p; + while (value_start < value_end && + (*value_start == ' ' || *value_start == '\t' || *value_start == '\n' || *value_start == '\r')) + value_start++; + while (value_end > value_start && + (value_end[-1] == ' ' || value_end[-1] == '\t' || value_end[-1] == '\n' || value_end[-1] == '\r')) + value_end--; + hb_svg_str_t prop_name = {name_start, (unsigned) (name_end - name_start)}; + if (svg_str_eq_ascii_ci (prop_name, name)) + return {value_start, (unsigned) (value_end - value_start)}; + if (p < end && *p == ';') p++; + } + return {}; +} + +static float +svg_parse_float_clamped01 (hb_svg_str_t s) +{ + return hb_clamp (s.to_float (), 0.f, 1.f); +} + +static float +svg_parse_number_or_percent (hb_svg_str_t s, bool *is_percent) +{ + if (is_percent) *is_percent = false; + s = s.trim (); + if (!s.len) return 0.f; + if (s.data[s.len - 1] == '%') + { + if (is_percent) *is_percent = true; + hb_svg_str_t n = {s.data, s.len - 1}; + return n.to_float () / 100.f; + } + return s.to_float (); +} + /* * 2. Minimal XML tokenizer @@ -284,6 +377,17 @@ struct hb_svg_xml_parser_t } }; +static hb_svg_str_t +svg_attr_or_style (const hb_svg_xml_parser_t &parser, + hb_svg_str_t style, + const char *name) +{ + hb_svg_str_t styled = svg_style_find_property (style, name); + if (!styled.is_null ()) + return styled; + return parser.find_attr (name); +} + /* * 3. SVG Color parser @@ -470,17 +574,17 @@ svg_parse_color (hb_svg_str_t s, s = s.trim (); if (!s.len) { *is_none = true; return HB_COLOR (0, 0, 0, 0); } - if (s.eq ("none") || s.eq ("transparent")) + if (svg_str_eq_ascii_ci (s, "none") || svg_str_eq_ascii_ci (s, "transparent")) { *is_none = true; return HB_COLOR (0, 0, 0, 0); } - if (s.eq ("currentColor")) + if (svg_str_eq_ascii_ci (s, "currentColor")) return foreground; /* var(--colorN) → CPAL palette color */ - if (s.starts_with ("var(")) + if (svg_str_starts_with_ascii_ci (s, "var(")) { const char *p = s.data + 4; const char *e = s.data + s.len; @@ -614,25 +718,47 @@ struct hb_svg_float_parser_t { skip_ws_comma (); if (p >= end) return 0.f; + const char *start = p; char buf[64]; unsigned n = 0; + bool has_digit = false; if (p < end && (*p == '-' || *p == '+')) buf[n++] = *p++; while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') + { buf[n++] = *p++; + has_digit = true; + } if (p < end && n < sizeof (buf) - 1 && *p == '.') { buf[n++] = *p++; while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') + { buf[n++] = *p++; + has_digit = true; + } } if (p < end && n < sizeof (buf) - 1 && (*p == 'e' || *p == 'E')) { buf[n++] = *p++; if (p < end && n < sizeof (buf) - 1 && (*p == '+' || *p == '-')) buf[n++] = *p++; + bool has_exp_digit = false; while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') + { buf[n++] = *p++; + has_exp_digit = true; + } + if (!has_exp_digit) + { + p = start; + has_digit = false; + } + } + if (!has_digit) + { + if (p < end) p++; + return 0.f; } buf[n] = '\0'; return strtof (buf, nullptr); @@ -1387,17 +1513,28 @@ struct hb_svg_defs_t static bool parse_paint_server_id (hb_svg_str_t s, char out[64]) { - if (!s.starts_with ("url(")) + s = s.trim (); + if (!svg_str_starts_with_ascii_ci (s, "url(")) return false; const char *p = s.data + 4; const char *e = s.data + s.len; - while (p < e && *p == ' ') p++; + while (p < e && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + while (e > p && *(e - 1) != ')') e--; + if (e <= p) return false; + e--; /* drop ')' */ + while (e > p && (*(e - 1) == ' ' || *(e - 1) == '\t' || *(e - 1) == '\n' || *(e - 1) == '\r')) e--; + if (e > p && ((*p == '\'' && *(e - 1) == '\'') || (*p == '"' && *(e - 1) == '"'))) + { + p++; + e--; + } + while (p < e && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + while (e > p && (*(e - 1) == ' ' || *(e - 1) == '\t' || *(e - 1) == '\n' || *(e - 1) == '\r')) e--; if (p < e && *p == '#') p++; - const char *start = p; - while (p < e && *p != ')') p++; - unsigned n = hb_min ((unsigned) (p - start), (unsigned) 63); - memcpy (out, start, n); + if (p >= e) return false; + unsigned n = hb_min ((unsigned) (e - p), (unsigned) 63); + memcpy (out, p, n); out[n] = '\0'; return true; } @@ -1806,18 +1943,14 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, hb_face_t *face, unsigned palette) { - hb_svg_str_t offset_str = parser.find_attr ("offset"); - hb_svg_str_t color_str = parser.find_attr ("stop-color"); - hb_svg_str_t opacity_str = parser.find_attr ("stop-opacity"); + hb_svg_str_t style = parser.find_attr ("style"); + hb_svg_str_t offset_str = svg_attr_or_style (parser, style, "offset"); + hb_svg_str_t color_str = svg_attr_or_style (parser, style, "stop-color"); + hb_svg_str_t opacity_str = svg_attr_or_style (parser, style, "stop-opacity"); float offset = 0; if (offset_str.len) - { - offset = svg_parse_float (offset_str); - /* Check for percentage */ - if (offset_str.len && offset_str.data[offset_str.len - 1] == '%') - offset /= 100.f; - } + offset = hb_clamp (svg_parse_number_or_percent (offset_str, nullptr), 0.f, 1.f); bool is_none = false; hb_color_t color = HB_COLOR (0, 0, 0, 255); @@ -1826,7 +1959,7 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, if (opacity_str.len) { - float opacity = svg_parse_float (opacity_str); + float opacity = svg_parse_float_clamped01 (opacity_str); color = HB_COLOR (hb_color_get_blue (color), hb_color_get_green (color), hb_color_get_red (color), @@ -1899,7 +2032,7 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, static void svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, - hb_svg_gradient_t &grad) + hb_svg_gradient_t &grad) { if (grad.type == SVG_GRADIENT_LINEAR) { @@ -1907,10 +2040,10 @@ svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, hb_svg_str_t y1_str = parser.find_attr ("y1"); hb_svg_str_t x2_str = parser.find_attr ("x2"); hb_svg_str_t y2_str = parser.find_attr ("y2"); - if (x1_str.len) { grad.x1 = svg_parse_float (x1_str); grad.has_x1 = true; } - if (y1_str.len) { grad.y1 = svg_parse_float (y1_str); grad.has_y1 = true; } - if (x2_str.len) { grad.x2 = svg_parse_float (x2_str); grad.has_x2 = true; } - if (y2_str.len) { grad.y2 = svg_parse_float (y2_str); grad.has_y2 = true; } + if (x1_str.len) { grad.x1 = svg_parse_number_or_percent (x1_str, nullptr); grad.has_x1 = true; } + if (y1_str.len) { grad.y1 = svg_parse_number_or_percent (y1_str, nullptr); grad.has_y1 = true; } + if (x2_str.len) { grad.x2 = svg_parse_number_or_percent (x2_str, nullptr); grad.has_x2 = true; } + if (y2_str.len) { grad.y2 = svg_parse_number_or_percent (y2_str, nullptr); grad.has_y2 = true; } /* Default x2 to 1 if not explicitly set (objectBoundingBox default). */ if (!grad.has_x2) @@ -1924,11 +2057,11 @@ svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, hb_svg_str_t fx_str = parser.find_attr ("fx"); hb_svg_str_t fy_str = parser.find_attr ("fy"); - if (cx_str.len) { grad.cx = svg_parse_float (cx_str); grad.has_cx = true; } - if (cy_str.len) { grad.cy = svg_parse_float (cy_str); grad.has_cy = true; } - if (r_str.len) { grad.r = svg_parse_float (r_str); grad.has_r = true; } - if (fx_str.len) { grad.fx = svg_parse_float (fx_str); grad.has_fx = true; } - if (fy_str.len) { grad.fy = svg_parse_float (fy_str); grad.has_fy = true; } + if (cx_str.len) { grad.cx = svg_parse_number_or_percent (cx_str, nullptr); grad.has_cx = true; } + if (cy_str.len) { grad.cy = svg_parse_number_or_percent (cy_str, nullptr); grad.has_cy = true; } + if (r_str.len) { grad.r = svg_parse_number_or_percent (r_str, nullptr); grad.has_r = true; } + if (fx_str.len) { grad.fx = svg_parse_number_or_percent (fx_str, nullptr); grad.has_fx = true; } + if (fy_str.len) { grad.fy = svg_parse_number_or_percent (fy_str, nullptr); grad.has_fy = true; } } } @@ -2609,6 +2742,19 @@ svg_render_use_element (hb_svg_render_context_t *ctx, ctx->pop_transform (); } +static void +svg_skip_subtree (hb_svg_xml_parser_t &parser) +{ + int depth = 1; + while (depth > 0) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok == SVG_TOKEN_CLOSE_TAG) depth--; + else if (tok == SVG_TOKEN_OPEN_TAG) depth++; + } +} + /* Render one element (may be a container or shape) */ static void svg_render_element (hb_svg_render_context_t *ctx, @@ -2629,17 +2775,30 @@ svg_render_element (hb_svg_render_context_t *ctx, bool self_closing = parser.self_closing; /* Extract common attributes */ - hb_svg_str_t fill_attr = parser.find_attr ("fill"); - hb_svg_str_t fill_opacity_str = parser.find_attr ("fill-opacity"); - hb_svg_str_t opacity_str = parser.find_attr ("opacity"); - hb_svg_str_t transform_str = parser.find_attr ("transform"); - hb_svg_str_t clip_path_attr = parser.find_attr ("clip-path"); + hb_svg_str_t style = parser.find_attr ("style"); + hb_svg_str_t fill_attr = svg_attr_or_style (parser, style, "fill"); + hb_svg_str_t fill_opacity_str = svg_attr_or_style (parser, style, "fill-opacity"); + hb_svg_str_t opacity_str = svg_attr_or_style (parser, style, "opacity"); + hb_svg_str_t transform_str = svg_attr_or_style (parser, style, "transform"); + hb_svg_str_t clip_path_attr = svg_attr_or_style (parser, style, "clip-path"); + hb_svg_str_t display_str = svg_attr_or_style (parser, style, "display"); hb_svg_str_t fill_str = fill_attr.is_null () ? inherited_fill : fill_attr; - float fill_opacity = fill_opacity_str.len ? svg_parse_float (fill_opacity_str) : inherited_fill_opacity; - float opacity = opacity_str.len ? svg_parse_float (opacity_str) : 1.f; + float fill_opacity = fill_opacity_str.len ? svg_parse_float_clamped01 (fill_opacity_str) : inherited_fill_opacity; + float opacity = opacity_str.len ? svg_parse_float_clamped01 (opacity_str) : 1.f; hb_svg_str_t clip_path_str = clip_path_attr.is_null () ? inherited_clip_path : clip_path_attr; + if (svg_str_eq_ascii_ci (display_str.trim (), "none")) + { + if (!self_closing) + svg_skip_subtree (parser); + ctx->depth--; + assert (ctx->paint->transform_stack.length == transform_depth); + assert (ctx->paint->clip_stack.length == clip_depth); + assert (ctx->paint->surface_stack.length == surface_depth); + return; + } + if (tag.eq ("g") || tag.eq ("svg")) svg_render_container_element (ctx, parser, tag, self_closing, fill_str, fill_opacity, opacity, From e46cc48295aa3acd2a2c2cd2752a0f432fb155c8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:07:03 -0300 Subject: [PATCH 002/105] raster/svg: add visibility/color cascade and global defs scan --- src/hb-raster-svg.cc | 147 ++++++++++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 49 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index cfac1fcc1..4d5f2d0b7 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -1666,7 +1666,8 @@ static void svg_emit_fill (hb_svg_render_context_t *ctx, hb_svg_str_t fill_str, float fill_opacity, - const hb_extents_t<> *object_bbox) + const hb_extents_t<> *object_bbox, + hb_color_t current_color) { bool is_none = false; @@ -1784,12 +1785,12 @@ svg_emit_fill (hb_svg_render_context_t *ctx, /* Solid color */ hb_color_t color = svg_parse_color (fill_str, - ctx->pfuncs, - ctx->paint, - ctx->foreground, - hb_font_get_face (ctx->font), - ctx->palette, - &is_none); + ctx->pfuncs, + ctx->paint, + current_color, + hb_font_get_face (ctx->font), + ctx->palette, + &is_none); if (is_none) return; if (fill_opacity < 1.f) @@ -1931,7 +1932,9 @@ static void svg_render_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, hb_svg_str_t inherited_fill, float inherited_fill_opacity, - hb_svg_str_t inherited_clip_path); + hb_svg_str_t inherited_clip_path, + hb_color_t inherited_color, + bool inherited_visibility); /* Parse a gradient element */ static bool @@ -2423,7 +2426,8 @@ svg_render_shape (hb_svg_render_context_t *ctx, float fill_opacity, float opacity, hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str) + hb_svg_str_t clip_path_str, + hb_color_t current_color) { bool has_transform = transform_str.len > 0; bool has_opacity = opacity < 1.f; @@ -2455,7 +2459,7 @@ svg_render_shape (hb_svg_render_context_t *ctx, ctx->paint_color (black); } else - svg_emit_fill (ctx, fill_str, fill_opacity, has_bbox ? &bbox : nullptr); + svg_emit_fill (ctx, fill_str, fill_opacity, has_bbox ? &bbox : nullptr, current_color); ctx->pop_clip (); if (has_clip_path) @@ -2470,14 +2474,16 @@ svg_render_shape (hb_svg_render_context_t *ctx, static void svg_render_container_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_str_t tag, - bool self_closing, - hb_svg_str_t fill_str, - float fill_opacity, - float opacity, - hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str) + hb_svg_xml_parser_t &parser, + hb_svg_str_t tag, + bool self_closing, + hb_svg_str_t fill_str, + float fill_opacity, + float opacity, + hb_svg_str_t transform_str, + hb_svg_str_t clip_path_str, + hb_color_t current_color, + bool current_visibility) { bool has_transform = transform_str.len > 0; bool has_opacity = opacity < 1.f; @@ -2537,7 +2543,8 @@ svg_render_container_element (hb_svg_render_context_t *ctx, svg_process_defs (ctx, parser); continue; } - svg_render_element (ctx, parser, fill_str, fill_opacity, clip_path_str); + svg_render_element (ctx, parser, fill_str, fill_opacity, clip_path_str, + current_color, current_visibility); if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && !child_tag.eq ("svg") && !child_tag.eq ("use")) { @@ -2570,13 +2577,14 @@ svg_render_container_element (hb_svg_render_context_t *ctx, static bool svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_str_t tag, - hb_svg_str_t fill_str, - float fill_opacity, - float opacity, - hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str) + hb_svg_xml_parser_t &parser, + hb_svg_str_t tag, + hb_svg_str_t fill_str, + float fill_opacity, + float opacity, + hb_svg_str_t transform_str, + hb_svg_str_t clip_path_str, + hb_color_t current_color) { if (tag.eq ("path")) { @@ -2586,7 +2594,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, hb_svg_shape_emit_data_t shape; shape.type = hb_svg_shape_emit_data_t::SHAPE_PATH; shape.str_data = d; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str); + svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); } return true; } @@ -2609,7 +2617,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, shape.params[3] = h; shape.params[4] = rx; shape.params[5] = ry; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str); + svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); } return true; } @@ -2626,7 +2634,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, shape.params[0] = cx; shape.params[1] = cy; shape.params[2] = r; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str); + svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); } return true; } @@ -2645,7 +2653,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, shape.params[1] = cy; shape.params[2] = rx; shape.params[3] = ry; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str); + svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); } return true; } @@ -2662,7 +2670,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, shape.params[1] = y1; shape.params[2] = x2; shape.params[3] = y2; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str); + svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); return true; } if (tag.eq ("polyline")) @@ -2673,7 +2681,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, hb_svg_shape_emit_data_t shape; shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; shape.str_data = points; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str); + svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); } return true; } @@ -2685,7 +2693,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, hb_svg_shape_emit_data_t shape; shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; shape.str_data = points; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str); + svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); } return true; } @@ -2695,11 +2703,13 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, static void svg_render_use_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_str_t fill_str, - float fill_opacity, - hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str) + hb_svg_xml_parser_t &parser, + hb_svg_str_t fill_str, + float fill_opacity, + hb_svg_str_t transform_str, + hb_svg_str_t clip_path_str, + hb_color_t current_color, + bool current_visibility) { hb_svg_str_t href = svg_find_href_attr (parser); @@ -2732,7 +2742,8 @@ svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t ref_parser (found, remaining); hb_svg_token_type_t tok = ref_parser.next (); if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - svg_render_element (ctx, ref_parser, fill_str, fill_opacity, clip_path_str); + svg_render_element (ctx, ref_parser, fill_str, fill_opacity, clip_path_str, + current_color, current_visibility); } if (has_translate) @@ -2761,7 +2772,9 @@ svg_render_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, hb_svg_str_t inherited_fill, float inherited_fill_opacity, - hb_svg_str_t inherited_clip_path) + hb_svg_str_t inherited_clip_path, + hb_color_t inherited_color, + bool inherited_visibility) { if (ctx->depth >= SVG_MAX_DEPTH) return; @@ -2782,11 +2795,24 @@ svg_render_element (hb_svg_render_context_t *ctx, hb_svg_str_t transform_str = svg_attr_or_style (parser, style, "transform"); hb_svg_str_t clip_path_attr = svg_attr_or_style (parser, style, "clip-path"); hb_svg_str_t display_str = svg_attr_or_style (parser, style, "display"); + hb_svg_str_t color_str = svg_attr_or_style (parser, style, "color"); + hb_svg_str_t visibility_str = svg_attr_or_style (parser, style, "visibility"); hb_svg_str_t fill_str = fill_attr.is_null () ? inherited_fill : fill_attr; float fill_opacity = fill_opacity_str.len ? svg_parse_float_clamped01 (fill_opacity_str) : inherited_fill_opacity; float opacity = opacity_str.len ? svg_parse_float_clamped01 (opacity_str) : 1.f; hb_svg_str_t clip_path_str = clip_path_attr.is_null () ? inherited_clip_path : clip_path_attr; + hb_color_t current_color = inherited_color; + bool is_none = false; + if (color_str.len && !svg_str_eq_ascii_ci (color_str.trim (), "inherit")) + current_color = svg_parse_color (color_str, ctx->pfuncs, ctx->paint, + inherited_color, hb_font_get_face (ctx->font), + ctx->palette, &is_none); + bool current_visibility = inherited_visibility; + hb_svg_str_t visibility_trim = visibility_str.trim (); + if (visibility_trim.len && !svg_str_eq_ascii_ci (visibility_trim, "inherit")) + current_visibility = !(svg_str_eq_ascii_ci (visibility_trim, "hidden") || + svg_str_eq_ascii_ci (visibility_trim, "collapse")); if (svg_str_eq_ascii_ci (display_str.trim (), "none")) { @@ -2798,18 +2824,31 @@ svg_render_element (hb_svg_render_context_t *ctx, assert (ctx->paint->surface_stack.length == surface_depth); return; } + if (!current_visibility) + { + if (!self_closing) + svg_skip_subtree (parser); + ctx->depth--; + assert (ctx->paint->transform_stack.length == transform_depth); + assert (ctx->paint->clip_stack.length == clip_depth); + assert (ctx->paint->surface_stack.length == surface_depth); + return; + } if (tag.eq ("g") || tag.eq ("svg")) svg_render_container_element (ctx, parser, tag, self_closing, fill_str, fill_opacity, opacity, - transform_str, clip_path_str); + transform_str, clip_path_str, + current_color, current_visibility); else if (svg_render_primitive_shape_element (ctx, parser, tag, - fill_str, fill_opacity, opacity, - transform_str, clip_path_str)) + fill_str, fill_opacity, opacity, + transform_str, clip_path_str, + current_color)) ; else if (tag.eq ("use")) svg_render_use_element (ctx, parser, fill_str, fill_opacity, - transform_str, clip_path_str); + transform_str, clip_path_str, + current_color, current_visibility); ctx->depth--; @@ -2863,15 +2902,23 @@ hb_raster_svg_render (hb_raster_paint_t *paint, ctx.svg_accel = face ? face->table.SVG.get () : nullptr; ctx.doc_cache = doc_cache; - /* First pass: collect defs (gradients, clip-paths) from the entire document */ + /* First pass: collect defs (gradients, clip-paths) from the entire document, + * including entries that are authored outside . */ { hb_svg_xml_parser_t defs_parser (data, data_len); while (true) { hb_svg_token_type_t tok = defs_parser.next (); if (tok == SVG_TOKEN_EOF) break; - if ((tok == SVG_TOKEN_OPEN_TAG) && defs_parser.tag_name.eq ("defs")) - svg_process_defs (&ctx, defs_parser); + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + if (defs_parser.tag_name.eq ("linearGradient")) + svg_process_gradient_def (&ctx, defs_parser, tok, SVG_GRADIENT_LINEAR); + else if (defs_parser.tag_name.eq ("radialGradient")) + svg_process_gradient_def (&ctx, defs_parser, tok, SVG_GRADIENT_RADIAL); + else if (defs_parser.tag_name.eq ("clipPath")) + svg_process_clip_path_def (&ctx, defs_parser, tok); + } } } @@ -2885,7 +2932,8 @@ hb_raster_svg_render (hb_raster_paint_t *paint, { hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); ctx.push_transform (1, 0, 0, -1, 0, 0); - svg_render_element (&ctx, parser, hb_svg_str_t (), 1.f, hb_svg_str_t ()); + svg_render_element (&ctx, parser, hb_svg_str_t (), 1.f, hb_svg_str_t (), + foreground, true); ctx.pop_transform (); hb_paint_pop_transform (ctx.pfuncs, ctx.paint); found_glyph = true; @@ -2916,7 +2964,8 @@ hb_raster_svg_render (hb_raster_paint_t *paint, { hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); ctx.push_transform (1, 0, 0, -1, 0, 0); - svg_render_element (&ctx, parser, hb_svg_str_t (), 1.f, hb_svg_str_t ()); + svg_render_element (&ctx, parser, hb_svg_str_t (), 1.f, hb_svg_str_t (), + foreground, true); ctx.pop_transform (); hb_paint_pop_transform (ctx.pfuncs, ctx.paint); found_glyph = true; From 2f3ca23a07cd90d4df0b954c52787f38800e4585 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:10:12 -0300 Subject: [PATCH 003/105] raster/svg: handle inherit and currentColor in gradients --- src/hb-raster-svg.cc | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 4d5f2d0b7..cd457f603 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -206,6 +206,12 @@ svg_parse_number_or_percent (hb_svg_str_t s, bool *is_percent) return s.to_float (); } +static bool +svg_str_is_inherit (hb_svg_str_t s) +{ + return svg_str_eq_ascii_ci (s.trim (), "inherit"); +} + /* * 2. Minimal XML tokenizer @@ -1373,6 +1379,7 @@ struct hb_svg_gradient_stop_t { float offset; hb_color_t color; + bool is_current_color = false; }; struct hb_svg_gradient_t @@ -1562,6 +1569,7 @@ struct hb_svg_color_line_data_t const hb_svg_gradient_t *grad; hb_paint_extend_t extend; float alpha_scale; + hb_color_t current_color; }; static unsigned @@ -1581,9 +1589,18 @@ svg_color_line_get_stops (hb_color_line_t *color_line HB_UNUSED, unsigned n = hb_min (*count, total > start ? total - start : 0u); for (unsigned i = 0; i < n; i++) { - color_stops[i].offset = grad->stops[start + i].offset; + const hb_svg_gradient_stop_t &stop = grad->stops[start + i]; + color_stops[i].offset = stop.offset; color_stops[i].is_foreground = false; - hb_color_t c = grad->stops[start + i].color; + hb_color_t c = stop.is_current_color ? cl->current_color : stop.color; + if (stop.is_current_color) + { + uint8_t a = (uint8_t) hb_raster_div255 (hb_color_get_alpha (c) * hb_color_get_alpha (stop.color)); + c = HB_COLOR (hb_color_get_blue (c), + hb_color_get_green (c), + hb_color_get_red (c), + a); + } if (cl->alpha_scale < 1.f) { uint8_t a = (uint8_t) hb_clamp ((int) (hb_color_get_alpha (c) * cl->alpha_scale + 0.5f), 0, 255); @@ -1731,6 +1748,7 @@ svg_emit_fill (hb_svg_render_context_t *ctx, cl_data.grad = &effective; cl_data.extend = effective.spread; cl_data.alpha_scale = hb_clamp (fill_opacity, 0.f, 1.f); + cl_data.current_color = current_color; hb_color_line_t cl = { &cl_data, @@ -1957,8 +1975,12 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, bool is_none = false; hb_color_t color = HB_COLOR (0, 0, 0, 255); + bool is_current_color = false; if (color_str.len) + { + is_current_color = svg_str_eq_ascii_ci (color_str.trim (), "currentColor"); color = svg_parse_color (color_str, pfuncs, paint_data, foreground, face, palette, &is_none); + } if (opacity_str.len) { @@ -1969,7 +1991,10 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, (uint8_t) (hb_color_get_alpha (color) * opacity + 0.5f)); } - hb_svg_gradient_stop_t stop = {offset, color}; + hb_svg_gradient_stop_t stop; + stop.offset = offset; + stop.color = color; + stop.is_current_color = is_current_color; grad.stops.push (stop); return !grad.stops.in_error (); } @@ -2798,10 +2823,14 @@ svg_render_element (hb_svg_render_context_t *ctx, hb_svg_str_t color_str = svg_attr_or_style (parser, style, "color"); hb_svg_str_t visibility_str = svg_attr_or_style (parser, style, "visibility"); - hb_svg_str_t fill_str = fill_attr.is_null () ? inherited_fill : fill_attr; - float fill_opacity = fill_opacity_str.len ? svg_parse_float_clamped01 (fill_opacity_str) : inherited_fill_opacity; + hb_svg_str_t fill_str = (fill_attr.is_null () || svg_str_is_inherit (fill_attr)) ? inherited_fill : fill_attr; + float fill_opacity = (fill_opacity_str.len && !svg_str_is_inherit (fill_opacity_str)) + ? svg_parse_float_clamped01 (fill_opacity_str) + : inherited_fill_opacity; float opacity = opacity_str.len ? svg_parse_float_clamped01 (opacity_str) : 1.f; - hb_svg_str_t clip_path_str = clip_path_attr.is_null () ? inherited_clip_path : clip_path_attr; + hb_svg_str_t clip_path_str = (clip_path_attr.is_null () || svg_str_is_inherit (clip_path_attr)) + ? inherited_clip_path + : clip_path_attr; hb_color_t current_color = inherited_color; bool is_none = false; if (color_str.len && !svg_str_eq_ascii_ci (color_str.trim (), "inherit")) From 0d1ef4c14cf25b67d76640aeb5c9c8c6178d5577 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:12:11 -0300 Subject: [PATCH 004/105] raster/svg: add url fallback paint and normalize inherit/none --- src/hb-raster-svg.cc | 86 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index cd457f603..c9e500d22 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -212,6 +212,12 @@ svg_str_is_inherit (hb_svg_str_t s) return svg_str_eq_ascii_ci (s.trim (), "inherit"); } +static bool +svg_str_is_none (hb_svg_str_t s) +{ + return svg_str_eq_ascii_ci (s.trim (), "none"); +} + /* * 2. Minimal XML tokenizer @@ -1674,6 +1680,67 @@ struct hb_svg_render_context_t } }; +static bool +svg_parse_paint_url_with_fallback (hb_svg_str_t s, + char out_id[64], + hb_svg_str_t *fallback) +{ + if (fallback) *fallback = {}; + s = s.trim (); + if (!svg_str_starts_with_ascii_ci (s, "url(")) + return false; + + const char *p = s.data + 4; + const char *end = s.data + s.len; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + + const char *q = p; + char quote = 0; + while (q < end) + { + char c = *q; + if (quote) + { + if (c == quote) quote = 0; + } + else + { + if (c == '"' || c == '\'') quote = c; + else if (c == ')') break; + } + q++; + } + if (q >= end || *q != ')') + return false; + + const char *id_b = p; + const char *id_e = q; + while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; + while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; + if (id_e > id_b && ((*id_b == '\'' && *(id_e - 1) == '\'') || (*id_b == '"' && *(id_e - 1) == '"'))) + { + id_b++; + id_e--; + } + while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; + while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; + if (id_b < id_e && *id_b == '#') id_b++; + if (id_b >= id_e) + return false; + + unsigned n = hb_min ((unsigned) (id_e - id_b), (unsigned) 63); + memcpy (out_id, id_b, n); + out_id[n] = '\0'; + + if (fallback) + { + const char *f = q + 1; + while (f < end && (*f == ' ' || *f == '\t' || *f == '\n' || *f == '\r')) f++; + if (f < end) *fallback = {f, (unsigned) (end - f)}; + } + return true; +} + /* * Emit fill (solid or gradient) @@ -1688,8 +1755,19 @@ svg_emit_fill (hb_svg_render_context_t *ctx, { bool is_none = false; + char url_id[64]; + hb_svg_str_t fallback_paint; + bool has_url_paint = svg_parse_paint_url_with_fallback (fill_str, url_id, &fallback_paint); + /* Check for gradient reference */ - const hb_svg_gradient_t *grad = ctx->defs.find_gradient_str (fill_str); + const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs.find_gradient (url_id) + : ctx->defs.find_gradient_str (fill_str); + if (has_url_paint && !grad) + { + if (fallback_paint.len) + svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); + return; + } if (grad) { /* Resolve href chain and inherit unspecified attributes from references. */ @@ -2827,10 +2905,14 @@ svg_render_element (hb_svg_render_context_t *ctx, float fill_opacity = (fill_opacity_str.len && !svg_str_is_inherit (fill_opacity_str)) ? svg_parse_float_clamped01 (fill_opacity_str) : inherited_fill_opacity; - float opacity = opacity_str.len ? svg_parse_float_clamped01 (opacity_str) : 1.f; + float opacity = (opacity_str.len && !svg_str_is_inherit (opacity_str) && !svg_str_is_none (opacity_str)) + ? svg_parse_float_clamped01 (opacity_str) + : 1.f; hb_svg_str_t clip_path_str = (clip_path_attr.is_null () || svg_str_is_inherit (clip_path_attr)) ? inherited_clip_path : clip_path_attr; + if (svg_str_is_inherit (transform_str) || svg_str_is_none (transform_str)) + transform_str = {}; hb_color_t current_color = inherited_color; bool is_none = false; if (color_str.len && !svg_str_eq_ascii_ci (color_str.trim (), "inherit")) From dc8ac302d4873feb0f5cd6b5bd7dbac7107ab4ad Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:15:49 -0300 Subject: [PATCH 005/105] raster/svg: fix opacity inherit and gradient stop inherit --- src/hb-raster-svg.cc | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index c9e500d22..0ab1e5f13 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -2030,7 +2030,8 @@ static void svg_render_element (hb_svg_render_context_t *ctx, float inherited_fill_opacity, hb_svg_str_t inherited_clip_path, hb_color_t inherited_color, - bool inherited_visibility); + bool inherited_visibility, + float inherited_opacity); /* Parse a gradient element */ static bool @@ -2054,13 +2055,13 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, bool is_none = false; hb_color_t color = HB_COLOR (0, 0, 0, 255); bool is_current_color = false; - if (color_str.len) + if (color_str.len && !svg_str_is_inherit (color_str)) { is_current_color = svg_str_eq_ascii_ci (color_str.trim (), "currentColor"); color = svg_parse_color (color_str, pfuncs, paint_data, foreground, face, palette, &is_none); } - if (opacity_str.len) + if (opacity_str.len && !svg_str_is_inherit (opacity_str)) { float opacity = svg_parse_float_clamped01 (opacity_str); color = HB_COLOR (hb_color_get_blue (color), @@ -2586,7 +2587,8 @@ svg_render_container_element (hb_svg_render_context_t *ctx, hb_svg_str_t transform_str, hb_svg_str_t clip_path_str, hb_color_t current_color, - bool current_visibility) + bool current_visibility, + float inherited_opacity) { bool has_transform = transform_str.len > 0; bool has_opacity = opacity < 1.f; @@ -2647,7 +2649,7 @@ svg_render_container_element (hb_svg_render_context_t *ctx, continue; } svg_render_element (ctx, parser, fill_str, fill_opacity, clip_path_str, - current_color, current_visibility); + current_color, current_visibility, opacity); if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && !child_tag.eq ("svg") && !child_tag.eq ("use")) { @@ -2812,7 +2814,8 @@ svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_str_t transform_str, hb_svg_str_t clip_path_str, hb_color_t current_color, - bool current_visibility) + bool current_visibility, + float inherited_opacity) { hb_svg_str_t href = svg_find_href_attr (parser); @@ -2846,7 +2849,7 @@ svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_token_type_t tok = ref_parser.next (); if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) svg_render_element (ctx, ref_parser, fill_str, fill_opacity, clip_path_str, - current_color, current_visibility); + current_color, current_visibility, inherited_opacity); } if (has_translate) @@ -2877,7 +2880,8 @@ svg_render_element (hb_svg_render_context_t *ctx, float inherited_fill_opacity, hb_svg_str_t inherited_clip_path, hb_color_t inherited_color, - bool inherited_visibility) + bool inherited_visibility, + float inherited_opacity) { if (ctx->depth >= SVG_MAX_DEPTH) return; @@ -2907,7 +2911,7 @@ svg_render_element (hb_svg_render_context_t *ctx, : inherited_fill_opacity; float opacity = (opacity_str.len && !svg_str_is_inherit (opacity_str) && !svg_str_is_none (opacity_str)) ? svg_parse_float_clamped01 (opacity_str) - : 1.f; + : (svg_str_is_inherit (opacity_str) ? inherited_opacity : 1.f); hb_svg_str_t clip_path_str = (clip_path_attr.is_null () || svg_str_is_inherit (clip_path_attr)) ? inherited_clip_path : clip_path_attr; @@ -2950,7 +2954,8 @@ svg_render_element (hb_svg_render_context_t *ctx, svg_render_container_element (ctx, parser, tag, self_closing, fill_str, fill_opacity, opacity, transform_str, clip_path_str, - current_color, current_visibility); + current_color, current_visibility, + inherited_opacity); else if (svg_render_primitive_shape_element (ctx, parser, tag, fill_str, fill_opacity, opacity, transform_str, clip_path_str, @@ -2959,7 +2964,8 @@ svg_render_element (hb_svg_render_context_t *ctx, else if (tag.eq ("use")) svg_render_use_element (ctx, parser, fill_str, fill_opacity, transform_str, clip_path_str, - current_color, current_visibility); + current_color, current_visibility, + inherited_opacity); ctx->depth--; @@ -3044,7 +3050,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); ctx.push_transform (1, 0, 0, -1, 0, 0); svg_render_element (&ctx, parser, hb_svg_str_t (), 1.f, hb_svg_str_t (), - foreground, true); + foreground, true, 1.f); ctx.pop_transform (); hb_paint_pop_transform (ctx.pfuncs, ctx.paint); found_glyph = true; @@ -3076,7 +3082,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); ctx.push_transform (1, 0, 0, -1, 0, 0); svg_render_element (&ctx, parser, hb_svg_str_t (), 1.f, hb_svg_str_t (), - foreground, true); + foreground, true, 1.f); ctx.pop_transform (); hb_paint_pop_transform (ctx.pfuncs, ctx.paint); found_glyph = true; From ada78d50416f5e212f674f50bb7fc60483006f29 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:19:01 -0300 Subject: [PATCH 006/105] raster/svg: pass cascade state struct through renderer --- src/hb-raster-svg.cc | 149 ++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 88 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 0ab1e5f13..e7e2903f7 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -1680,6 +1680,16 @@ struct hb_svg_render_context_t } }; +struct hb_svg_cascade_t +{ + hb_svg_str_t fill; + float fill_opacity = 1.f; + hb_svg_str_t clip_path; + hb_color_t color = HB_COLOR (0, 0, 0, 255); + bool visibility = true; + float opacity = 1.f; +}; + static bool svg_parse_paint_url_with_fallback (hb_svg_str_t s, char out_id[64], @@ -2026,12 +2036,7 @@ svg_parse_shape_tag (hb_svg_xml_parser_t &parser, static void svg_render_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, - hb_svg_str_t inherited_fill, - float inherited_fill_opacity, - hb_svg_str_t inherited_clip_path, - hb_color_t inherited_color, - bool inherited_visibility, - float inherited_opacity); + const struct hb_svg_cascade_t &inherited); /* Parse a gradient element */ static bool @@ -2526,15 +2531,11 @@ svg_compute_shape_bbox (const hb_svg_shape_emit_data_t &shape, static void svg_render_shape (hb_svg_render_context_t *ctx, hb_svg_shape_emit_data_t &shape, - hb_svg_str_t fill_str, - float fill_opacity, - float opacity, - hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str, - hb_color_t current_color) + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str) { bool has_transform = transform_str.len > 0; - bool has_opacity = opacity < 1.f; + bool has_opacity = state.opacity < 1.f; bool has_clip_path = false; if (has_opacity) @@ -2549,21 +2550,21 @@ svg_render_shape (hb_svg_render_context_t *ctx, hb_extents_t<> bbox; bool has_bbox = svg_compute_shape_bbox (shape, &bbox); - has_clip_path = svg_push_clip_path_ref (ctx, clip_path_str, has_bbox ? &bbox : nullptr); + has_clip_path = svg_push_clip_path_ref (ctx, state.clip_path, has_bbox ? &bbox : nullptr); /* Clip with shape path, then fill */ hb_raster_paint_push_clip_path (ctx->paint, svg_shape_path_emit, &shape); /* Default fill is black */ - if (fill_str.is_null ()) + if (state.fill.is_null ()) { hb_color_t black = HB_COLOR (0, 0, 0, 255); - if (fill_opacity < 1.f) - black = HB_COLOR (0, 0, 0, (uint8_t) (255 * fill_opacity + 0.5f)); + if (state.fill_opacity < 1.f) + black = HB_COLOR (0, 0, 0, (uint8_t) (255 * state.fill_opacity + 0.5f)); ctx->paint_color (black); } else - svg_emit_fill (ctx, fill_str, fill_opacity, has_bbox ? &bbox : nullptr, current_color); + svg_emit_fill (ctx, state.fill, state.fill_opacity, has_bbox ? &bbox : nullptr, state.color); ctx->pop_clip (); if (has_clip_path) @@ -2581,17 +2582,12 @@ svg_render_container_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, hb_svg_str_t tag, bool self_closing, - hb_svg_str_t fill_str, - float fill_opacity, - float opacity, + const hb_svg_cascade_t &state, hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str, - hb_color_t current_color, - bool current_visibility, - float inherited_opacity) + hb_svg_str_t clip_path_str) { bool has_transform = transform_str.len > 0; - bool has_opacity = opacity < 1.f; + bool has_opacity = state.opacity < 1.f; bool has_clip = false; bool has_viewbox = false; float vb_x = 0, vb_y = 0, vb_w = 0, vb_h = 0; @@ -2648,8 +2644,7 @@ svg_render_container_element (hb_svg_render_context_t *ctx, svg_process_defs (ctx, parser); continue; } - svg_render_element (ctx, parser, fill_str, fill_opacity, clip_path_str, - current_color, current_visibility, opacity); + svg_render_element (ctx, parser, state); if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && !child_tag.eq ("svg") && !child_tag.eq ("use")) { @@ -2684,12 +2679,8 @@ static bool svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, hb_svg_str_t tag, - hb_svg_str_t fill_str, - float fill_opacity, - float opacity, - hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str, - hb_color_t current_color) + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str) { if (tag.eq ("path")) { @@ -2699,7 +2690,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, hb_svg_shape_emit_data_t shape; shape.type = hb_svg_shape_emit_data_t::SHAPE_PATH; shape.str_data = d; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); + svg_render_shape (ctx, shape, state, transform_str); } return true; } @@ -2722,7 +2713,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, shape.params[3] = h; shape.params[4] = rx; shape.params[5] = ry; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); + svg_render_shape (ctx, shape, state, transform_str); } return true; } @@ -2739,7 +2730,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, shape.params[0] = cx; shape.params[1] = cy; shape.params[2] = r; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); + svg_render_shape (ctx, shape, state, transform_str); } return true; } @@ -2758,7 +2749,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, shape.params[1] = cy; shape.params[2] = rx; shape.params[3] = ry; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); + svg_render_shape (ctx, shape, state, transform_str); } return true; } @@ -2775,7 +2766,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, shape.params[1] = y1; shape.params[2] = x2; shape.params[3] = y2; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); + svg_render_shape (ctx, shape, state, transform_str); return true; } if (tag.eq ("polyline")) @@ -2786,7 +2777,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, hb_svg_shape_emit_data_t shape; shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; shape.str_data = points; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); + svg_render_shape (ctx, shape, state, transform_str); } return true; } @@ -2798,7 +2789,7 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, hb_svg_shape_emit_data_t shape; shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; shape.str_data = points; - svg_render_shape (ctx, shape, fill_str, fill_opacity, opacity, transform_str, clip_path_str, current_color); + svg_render_shape (ctx, shape, state, transform_str); } return true; } @@ -2809,13 +2800,8 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, static void svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, - hb_svg_str_t fill_str, - float fill_opacity, - hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str, - hb_color_t current_color, - bool current_visibility, - float inherited_opacity) + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str) { hb_svg_str_t href = svg_find_href_attr (parser); @@ -2848,8 +2834,7 @@ svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t ref_parser (found, remaining); hb_svg_token_type_t tok = ref_parser.next (); if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - svg_render_element (ctx, ref_parser, fill_str, fill_opacity, clip_path_str, - current_color, current_visibility, inherited_opacity); + svg_render_element (ctx, ref_parser, state); } if (has_translate) @@ -2876,12 +2861,7 @@ svg_skip_subtree (hb_svg_xml_parser_t &parser) static void svg_render_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, - hb_svg_str_t inherited_fill, - float inherited_fill_opacity, - hb_svg_str_t inherited_clip_path, - hb_color_t inherited_color, - bool inherited_visibility, - float inherited_opacity) + const hb_svg_cascade_t &inherited) { if (ctx->depth >= SVG_MAX_DEPTH) return; @@ -2905,29 +2885,30 @@ svg_render_element (hb_svg_render_context_t *ctx, hb_svg_str_t color_str = svg_attr_or_style (parser, style, "color"); hb_svg_str_t visibility_str = svg_attr_or_style (parser, style, "visibility"); - hb_svg_str_t fill_str = (fill_attr.is_null () || svg_str_is_inherit (fill_attr)) ? inherited_fill : fill_attr; - float fill_opacity = (fill_opacity_str.len && !svg_str_is_inherit (fill_opacity_str)) + hb_svg_cascade_t state = inherited; + state.fill = (fill_attr.is_null () || svg_str_is_inherit (fill_attr)) ? inherited.fill : fill_attr; + state.fill_opacity = (fill_opacity_str.len && !svg_str_is_inherit (fill_opacity_str)) ? svg_parse_float_clamped01 (fill_opacity_str) - : inherited_fill_opacity; - float opacity = (opacity_str.len && !svg_str_is_inherit (opacity_str) && !svg_str_is_none (opacity_str)) + : inherited.fill_opacity; + state.opacity = (opacity_str.len && !svg_str_is_inherit (opacity_str) && !svg_str_is_none (opacity_str)) ? svg_parse_float_clamped01 (opacity_str) - : (svg_str_is_inherit (opacity_str) ? inherited_opacity : 1.f); - hb_svg_str_t clip_path_str = (clip_path_attr.is_null () || svg_str_is_inherit (clip_path_attr)) - ? inherited_clip_path - : clip_path_attr; + : (svg_str_is_inherit (opacity_str) ? inherited.opacity : 1.f); + state.clip_path = (clip_path_attr.is_null () || svg_str_is_inherit (clip_path_attr)) + ? inherited.clip_path + : clip_path_attr; if (svg_str_is_inherit (transform_str) || svg_str_is_none (transform_str)) transform_str = {}; - hb_color_t current_color = inherited_color; + state.color = inherited.color; bool is_none = false; if (color_str.len && !svg_str_eq_ascii_ci (color_str.trim (), "inherit")) - current_color = svg_parse_color (color_str, ctx->pfuncs, ctx->paint, - inherited_color, hb_font_get_face (ctx->font), - ctx->palette, &is_none); - bool current_visibility = inherited_visibility; + state.color = svg_parse_color (color_str, ctx->pfuncs, ctx->paint, + inherited.color, hb_font_get_face (ctx->font), + ctx->palette, &is_none); + state.visibility = inherited.visibility; hb_svg_str_t visibility_trim = visibility_str.trim (); if (visibility_trim.len && !svg_str_eq_ascii_ci (visibility_trim, "inherit")) - current_visibility = !(svg_str_eq_ascii_ci (visibility_trim, "hidden") || - svg_str_eq_ascii_ci (visibility_trim, "collapse")); + state.visibility = !(svg_str_eq_ascii_ci (visibility_trim, "hidden") || + svg_str_eq_ascii_ci (visibility_trim, "collapse")); if (svg_str_eq_ascii_ci (display_str.trim (), "none")) { @@ -2939,7 +2920,7 @@ svg_render_element (hb_svg_render_context_t *ctx, assert (ctx->paint->surface_stack.length == surface_depth); return; } - if (!current_visibility) + if (!state.visibility) { if (!self_closing) svg_skip_subtree (parser); @@ -2952,20 +2933,11 @@ svg_render_element (hb_svg_render_context_t *ctx, if (tag.eq ("g") || tag.eq ("svg")) svg_render_container_element (ctx, parser, tag, self_closing, - fill_str, fill_opacity, opacity, - transform_str, clip_path_str, - current_color, current_visibility, - inherited_opacity); - else if (svg_render_primitive_shape_element (ctx, parser, tag, - fill_str, fill_opacity, opacity, - transform_str, clip_path_str, - current_color)) + state, transform_str, state.clip_path); + else if (svg_render_primitive_shape_element (ctx, parser, tag, state, transform_str)) ; else if (tag.eq ("use")) - svg_render_use_element (ctx, parser, fill_str, fill_opacity, - transform_str, clip_path_str, - current_color, current_visibility, - inherited_opacity); + svg_render_use_element (ctx, parser, state, transform_str); ctx->depth--; @@ -3019,6 +2991,9 @@ hb_raster_svg_render (hb_raster_paint_t *paint, ctx.svg_accel = face ? face->table.SVG.get () : nullptr; ctx.doc_cache = doc_cache; + hb_svg_cascade_t initial_state; + initial_state.color = foreground; + /* First pass: collect defs (gradients, clip-paths) from the entire document, * including entries that are authored outside . */ { @@ -3049,8 +3024,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, { hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); ctx.push_transform (1, 0, 0, -1, 0, 0); - svg_render_element (&ctx, parser, hb_svg_str_t (), 1.f, hb_svg_str_t (), - foreground, true, 1.f); + svg_render_element (&ctx, parser, initial_state); ctx.pop_transform (); hb_paint_pop_transform (ctx.pfuncs, ctx.paint); found_glyph = true; @@ -3081,8 +3055,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, { hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); ctx.push_transform (1, 0, 0, -1, 0, 0); - svg_render_element (&ctx, parser, hb_svg_str_t (), 1.f, hb_svg_str_t (), - foreground, true, 1.f); + svg_render_element (&ctx, parser, initial_state); ctx.pop_transform (); hb_paint_pop_transform (ctx.pfuncs, ctx.paint); found_glyph = true; From d20baa570c16f4f3f8acba08195b679c7c989a2f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:22:15 -0300 Subject: [PATCH 007/105] raster/svg: reuse shared url parser and tidy renderer formatting --- src/hb-raster-svg.cc | 58 +++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index e7e2903f7..38785af1a 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -1370,6 +1370,11 @@ struct hb_svg_shape_emit_data_t float params[6]; /* for rect/circle/ellipse/line */ }; +static bool +svg_parse_paint_url_with_fallback (hb_svg_str_t s, + char out_id[64], + hb_svg_str_t *fallback); + /* * 9. Defs store @@ -1526,30 +1531,7 @@ struct hb_svg_defs_t static bool parse_paint_server_id (hb_svg_str_t s, char out[64]) { - s = s.trim (); - if (!svg_str_starts_with_ascii_ci (s, "url(")) - return false; - - const char *p = s.data + 4; - const char *e = s.data + s.len; - while (p < e && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - while (e > p && *(e - 1) != ')') e--; - if (e <= p) return false; - e--; /* drop ')' */ - while (e > p && (*(e - 1) == ' ' || *(e - 1) == '\t' || *(e - 1) == '\n' || *(e - 1) == '\r')) e--; - if (e > p && ((*p == '\'' && *(e - 1) == '\'') || (*p == '"' && *(e - 1) == '"'))) - { - p++; - e--; - } - while (p < e && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - while (e > p && (*(e - 1) == ' ' || *(e - 1) == '\t' || *(e - 1) == '\n' || *(e - 1) == '\r')) e--; - if (p < e && *p == '#') p++; - if (p >= e) return false; - unsigned n = hb_min ((unsigned) (e - p), (unsigned) 63); - memcpy (out, p, n); - out[n] = '\0'; - return true; + return svg_parse_paint_url_with_fallback (s, out, nullptr); } static bool parse_fragment_id (hb_svg_str_t s, char out[64]) @@ -2579,12 +2561,12 @@ svg_render_shape (hb_svg_render_context_t *ctx, static void svg_render_container_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_str_t tag, - bool self_closing, - const hb_svg_cascade_t &state, - hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str) + hb_svg_xml_parser_t &parser, + hb_svg_str_t tag, + bool self_closing, + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str, + hb_svg_str_t clip_path_str) { bool has_transform = transform_str.len > 0; bool has_opacity = state.opacity < 1.f; @@ -2644,7 +2626,7 @@ svg_render_container_element (hb_svg_render_context_t *ctx, svg_process_defs (ctx, parser); continue; } - svg_render_element (ctx, parser, state); + svg_render_element (ctx, parser, state); if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && !child_tag.eq ("svg") && !child_tag.eq ("use")) { @@ -2677,10 +2659,10 @@ svg_render_container_element (hb_svg_render_context_t *ctx, static bool svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_str_t tag, - const hb_svg_cascade_t &state, - hb_svg_str_t transform_str) + hb_svg_xml_parser_t &parser, + hb_svg_str_t tag, + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str) { if (tag.eq ("path")) { @@ -2799,9 +2781,9 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, static void svg_render_use_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - const hb_svg_cascade_t &state, - hb_svg_str_t transform_str) + hb_svg_xml_parser_t &parser, + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str) { hb_svg_str_t href = svg_find_href_attr (parser); From 153418b15e6cdc911e10e3d4cee101e71b68f3da Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:27:25 -0300 Subject: [PATCH 008/105] raster/svg: parse style properties once per element --- src/hb-raster-svg.cc | 83 +++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 38785af1a..52e618908 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -143,10 +143,25 @@ svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit) return true; } -static hb_svg_str_t -svg_style_find_property (hb_svg_str_t style, const char *name) +struct hb_svg_style_props_t { - if (style.is_null ()) return {}; + hb_svg_str_t fill; + hb_svg_str_t fill_opacity; + hb_svg_str_t opacity; + hb_svg_str_t transform; + hb_svg_str_t clip_path; + hb_svg_str_t display; + hb_svg_str_t color; + hb_svg_str_t visibility; + hb_svg_str_t offset; + hb_svg_str_t stop_color; + hb_svg_str_t stop_opacity; +}; + +static void +svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) +{ + if (style.is_null ()) return; const char *p = style.data; const char *end = style.data + style.len; while (p < end) @@ -159,7 +174,7 @@ svg_style_find_property (hb_svg_str_t style, const char *name) p++; const char *name_end = p; while (name_end > name_start && - (name_end[-1] == ' ' || name_end[-1] == '\t' || name_end[-1] == '\n' || name_end[-1] == '\r')) + (name_end[-1] == ' ' || name_end[-1] == '\t' || name_end[-1] == '\n' || name_end[-1] == '\r')) name_end--; if (p >= end || *p != ':') { @@ -172,17 +187,28 @@ svg_style_find_property (hb_svg_str_t style, const char *name) p++; const char *value_end = p; while (value_start < value_end && - (*value_start == ' ' || *value_start == '\t' || *value_start == '\n' || *value_start == '\r')) + (*value_start == ' ' || *value_start == '\t' || *value_start == '\n' || *value_start == '\r')) value_start++; while (value_end > value_start && - (value_end[-1] == ' ' || value_end[-1] == '\t' || value_end[-1] == '\n' || value_end[-1] == '\r')) + (value_end[-1] == ' ' || value_end[-1] == '\t' || value_end[-1] == '\n' || value_end[-1] == '\r')) value_end--; + hb_svg_str_t prop_name = {name_start, (unsigned) (name_end - name_start)}; - if (svg_str_eq_ascii_ci (prop_name, name)) - return {value_start, (unsigned) (value_end - value_start)}; + hb_svg_str_t prop_value = {value_start, (unsigned) (value_end - value_start)}; + if (svg_str_eq_ascii_ci (prop_name, "fill")) out->fill = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "fill-opacity")) out->fill_opacity = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "opacity")) out->opacity = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "transform")) out->transform = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "clip-path")) out->clip_path = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "display")) out->display = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "color")) out->color = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "visibility")) out->visibility = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "offset")) out->offset = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "stop-color")) out->stop_color = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "stop-opacity")) out->stop_opacity = prop_value; + if (p < end && *p == ';') p++; } - return {}; } static float @@ -389,15 +415,12 @@ struct hb_svg_xml_parser_t } }; -static hb_svg_str_t -svg_attr_or_style (const hb_svg_xml_parser_t &parser, - hb_svg_str_t style, - const char *name) +static inline hb_svg_str_t +svg_pick_attr_or_style (const hb_svg_xml_parser_t &parser, + hb_svg_str_t style_value, + const char *attr_name) { - hb_svg_str_t styled = svg_style_find_property (style, name); - if (!styled.is_null ()) - return styled; - return parser.find_attr (name); + return style_value.is_null () ? parser.find_attr (attr_name) : style_value; } @@ -2031,9 +2054,11 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, unsigned palette) { hb_svg_str_t style = parser.find_attr ("style"); - hb_svg_str_t offset_str = svg_attr_or_style (parser, style, "offset"); - hb_svg_str_t color_str = svg_attr_or_style (parser, style, "stop-color"); - hb_svg_str_t opacity_str = svg_attr_or_style (parser, style, "stop-opacity"); + hb_svg_style_props_t style_props; + svg_parse_style_props (style, &style_props); + hb_svg_str_t offset_str = svg_pick_attr_or_style (parser, style_props.offset, "offset"); + hb_svg_str_t color_str = svg_pick_attr_or_style (parser, style_props.stop_color, "stop-color"); + hb_svg_str_t opacity_str = svg_pick_attr_or_style (parser, style_props.stop_opacity, "stop-opacity"); float offset = 0; if (offset_str.len) @@ -2858,14 +2883,16 @@ svg_render_element (hb_svg_render_context_t *ctx, /* Extract common attributes */ hb_svg_str_t style = parser.find_attr ("style"); - hb_svg_str_t fill_attr = svg_attr_or_style (parser, style, "fill"); - hb_svg_str_t fill_opacity_str = svg_attr_or_style (parser, style, "fill-opacity"); - hb_svg_str_t opacity_str = svg_attr_or_style (parser, style, "opacity"); - hb_svg_str_t transform_str = svg_attr_or_style (parser, style, "transform"); - hb_svg_str_t clip_path_attr = svg_attr_or_style (parser, style, "clip-path"); - hb_svg_str_t display_str = svg_attr_or_style (parser, style, "display"); - hb_svg_str_t color_str = svg_attr_or_style (parser, style, "color"); - hb_svg_str_t visibility_str = svg_attr_or_style (parser, style, "visibility"); + hb_svg_style_props_t style_props; + svg_parse_style_props (style, &style_props); + hb_svg_str_t fill_attr = svg_pick_attr_or_style (parser, style_props.fill, "fill"); + hb_svg_str_t fill_opacity_str = svg_pick_attr_or_style (parser, style_props.fill_opacity, "fill-opacity"); + hb_svg_str_t opacity_str = svg_pick_attr_or_style (parser, style_props.opacity, "opacity"); + hb_svg_str_t transform_str = svg_pick_attr_or_style (parser, style_props.transform, "transform"); + hb_svg_str_t clip_path_attr = svg_pick_attr_or_style (parser, style_props.clip_path, "clip-path"); + hb_svg_str_t display_str = svg_pick_attr_or_style (parser, style_props.display, "display"); + hb_svg_str_t color_str = svg_pick_attr_or_style (parser, style_props.color, "color"); + hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, style_props.visibility, "visibility"); hb_svg_cascade_t state = inherited; state.fill = (fill_attr.is_null () || svg_str_is_inherit (fill_attr)) ? inherited.fill : fill_attr; From bce954ddecb90216f1dde0f6c0a587458c895854 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:32:24 -0300 Subject: [PATCH 009/105] raster/svg: speed up defs lookups with hb_bytes_t hash maps --- src/hb-raster-svg.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 52e618908..d38440780 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -1473,6 +1473,40 @@ struct hb_svg_defs_t hb_vector_t clip_shapes; hb_vector_t clip_paths; hb_vector_t defs; + hb_hashmap_t gradient_by_id; + hb_hashmap_t clip_path_by_id; + hb_vector_t owned_id_strings; + + ~hb_svg_defs_t () + { + for (unsigned i = 0; i < owned_id_strings.length; i++) + hb_free (owned_id_strings.arrayZ[i]); + } + + bool add_id_mapping (hb_hashmap_t *map, + const char *id, + unsigned idx) + { + hb_bytes_t key = hb_bytes_t (id, (unsigned) strlen (id)); + if (map->has (key)) + return true; + + unsigned n = (unsigned) strlen (id); + char *owned = (char *) hb_malloc (n + 1); + if (unlikely (!owned)) + return false; + hb_memcpy (owned, id, n + 1); + if (unlikely (!owned_id_strings.push (owned))) + { + hb_free (owned); + return false; + } + + hb_bytes_t owned_key = hb_bytes_t (owned, n); + if (unlikely (!map->set (owned_key, idx))) + return false; + return true; + } bool add_gradient (const char *id, const hb_svg_gradient_t &grad) { @@ -1493,11 +1527,20 @@ struct hb_svg_defs_t gradients.pop (); return false; } + if (unlikely (!add_id_mapping (&gradient_by_id, def.id, idx))) + { + defs.pop (); + gradients.pop (); + return false; + } return true; } const hb_svg_gradient_t *find_gradient (const char *id) const { + unsigned *idx = nullptr; + if (id && gradient_by_id.has (hb_bytes_t (id, (unsigned) strlen (id)), &idx)) + return &gradients[*idx]; for (unsigned i = 0; i < defs.length; i++) if (defs[i].type == hb_svg_def_t::DEF_GRADIENT && strcmp (defs[i].id, id) == 0) @@ -1524,11 +1567,20 @@ struct hb_svg_defs_t clip_paths.pop (); return false; } + if (unlikely (!add_id_mapping (&clip_path_by_id, def.id, idx))) + { + defs.pop (); + clip_paths.pop (); + return false; + } return true; } const hb_svg_clip_path_def_t *find_clip_path (const char *id) const { + unsigned *idx = nullptr; + if (id && clip_path_by_id.has (hb_bytes_t (id, (unsigned) strlen (id)), &idx)) + return &clip_paths[*idx]; for (unsigned i = 0; i < defs.length; i++) if (defs[i].type == hb_svg_def_t::DEF_CLIP_PATH && strcmp (defs[i].id, id) == 0) From f6b60d9d644e5f8aeeb5043b4014a208a8affb88 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:37:22 -0300 Subject: [PATCH 010/105] raster/svg: split base helpers into hb-raster-svg-* units --- src/hb-raster-svg-base.cc | 127 ++++++++++++++++++++++++ src/hb-raster-svg-base.hh | 142 ++++++++++++++++++++++++++ src/hb-raster-svg.cc | 203 +------------------------------------- src/meson.build | 2 + 4 files changed, 273 insertions(+), 201 deletions(-) create mode 100644 src/hb-raster-svg-base.cc create mode 100644 src/hb-raster-svg-base.hh diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc new file mode 100644 index 000000000..1d25047f8 --- /dev/null +++ b/src/hb-raster-svg-base.cc @@ -0,0 +1,127 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-base.hh" + +static inline char +svg_ascii_lower (char c) +{ + if (c >= 'A' && c <= 'Z') + return c + ('a' - 'A'); + return c; +} + +bool +svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit) +{ + unsigned n = (unsigned) strlen (lit); + if (s.len != n) return false; + for (unsigned i = 0; i < n; i++) + if (svg_ascii_lower (s.data[i]) != svg_ascii_lower (lit[i])) + return false; + return true; +} + +bool +svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit) +{ + unsigned n = (unsigned) strlen (lit); + if (s.len < n) return false; + for (unsigned i = 0; i < n; i++) + if (svg_ascii_lower (s.data[i]) != svg_ascii_lower (lit[i])) + return false; + return true; +} + +void +svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) +{ + if (style.is_null ()) return; + const char *p = style.data; + const char *end = style.data + style.len; + while (p < end) + { + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ';')) + p++; + if (p >= end) break; + const char *name_start = p; + while (p < end && *p != ':' && *p != ';') + p++; + const char *name_end = p; + while (name_end > name_start && + (name_end[-1] == ' ' || name_end[-1] == '\t' || name_end[-1] == '\n' || name_end[-1] == '\r')) + name_end--; + if (p >= end || *p != ':') + { + while (p < end && *p != ';') p++; + continue; + } + p++; /* skip ':' */ + const char *value_start = p; + while (p < end && *p != ';') + p++; + const char *value_end = p; + while (value_start < value_end && + (*value_start == ' ' || *value_start == '\t' || *value_start == '\n' || *value_start == '\r')) + value_start++; + while (value_end > value_start && + (value_end[-1] == ' ' || value_end[-1] == '\t' || value_end[-1] == '\n' || value_end[-1] == '\r')) + value_end--; + + hb_svg_str_t prop_name = {name_start, (unsigned) (name_end - name_start)}; + hb_svg_str_t prop_value = {value_start, (unsigned) (value_end - value_start)}; + if (svg_str_eq_ascii_ci (prop_name, "fill")) out->fill = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "fill-opacity")) out->fill_opacity = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "opacity")) out->opacity = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "transform")) out->transform = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "clip-path")) out->clip_path = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "display")) out->display = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "color")) out->color = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "visibility")) out->visibility = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "offset")) out->offset = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "stop-color")) out->stop_color = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "stop-opacity")) out->stop_opacity = prop_value; + + if (p < end && *p == ';') p++; + } +} + +float +svg_parse_number_or_percent (hb_svg_str_t s, bool *is_percent) +{ + if (is_percent) *is_percent = false; + s = s.trim (); + if (!s.len) return 0.f; + if (s.data[s.len - 1] == '%') + { + if (is_percent) *is_percent = true; + hb_svg_str_t n = {s.data, s.len - 1}; + return n.to_float () / 100.f; + } + return s.to_float (); +} diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh new file mode 100644 index 000000000..2ec04b468 --- /dev/null +++ b/src/hb-raster-svg-base.hh @@ -0,0 +1,142 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_BASE_HH +#define HB_RASTER_SVG_BASE_HH + +#include "hb.hh" + +#include +#include + +struct hb_svg_str_t +{ + const char *data; + unsigned len; + + hb_svg_str_t () : data (nullptr), len (0) {} + hb_svg_str_t (const char *d, unsigned l) : data (d), len (l) {} + + bool is_null () const { return !data; } + + bool eq (const char *s) const + { + unsigned slen = (unsigned) strlen (s); + return len == slen && memcmp (data, s, len) == 0; + } + + bool starts_with (const char *prefix) const + { + unsigned plen = (unsigned) strlen (prefix); + return len >= plen && memcmp (data, prefix, plen) == 0; + } + + bool eq_cstr (const char *s) const + { + unsigned slen = (unsigned) strlen (s); + return len == slen && memcmp (data, s, len) == 0; + } + + float to_float () const + { + if (!data || !len) return 0.f; + char buf[64]; + unsigned n = hb_min (len, (unsigned) sizeof (buf) - 1); + memcpy (buf, data, n); + buf[n] = '\0'; + return strtof (buf, nullptr); + } + + int to_int () const + { + if (!data || !len) return 0; + char buf[32]; + unsigned n = hb_min (len, (unsigned) sizeof (buf) - 1); + memcpy (buf, data, n); + buf[n] = '\0'; + return atoi (buf); + } + + hb_svg_str_t trim_left () const + { + const char *p = data; + unsigned l = len; + while (l && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) + { + p++; + l--; + } + return {p, l}; + } + + hb_svg_str_t trim () const + { + hb_svg_str_t s = trim_left (); + while (s.len && (s.data[s.len - 1] == ' ' || s.data[s.len - 1] == '\t' || + s.data[s.len - 1] == '\n' || s.data[s.len - 1] == '\r')) + s.len--; + return s; + } +}; + +struct hb_svg_style_props_t +{ + hb_svg_str_t fill; + hb_svg_str_t fill_opacity; + hb_svg_str_t opacity; + hb_svg_str_t transform; + hb_svg_str_t clip_path; + hb_svg_str_t display; + hb_svg_str_t color; + hb_svg_str_t visibility; + hb_svg_str_t offset; + hb_svg_str_t stop_color; + hb_svg_str_t stop_opacity; +}; + +HB_INTERNAL bool svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit); +HB_INTERNAL bool svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit); +HB_INTERNAL void svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out); +HB_INTERNAL float svg_parse_number_or_percent (hb_svg_str_t s, bool *is_percent); +static inline float +svg_parse_float_clamped01 (hb_svg_str_t s) +{ + return hb_clamp (s.to_float (), 0.f, 1.f); +} + +static inline bool +svg_str_is_inherit (hb_svg_str_t s) +{ + return svg_str_eq_ascii_ci (s.trim (), "inherit"); +} + +static inline bool +svg_str_is_none (hb_svg_str_t s) +{ + return svg_str_eq_ascii_ci (s.trim (), "none"); +} + +#endif /* HB_RASTER_SVG_BASE_HH */ diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index d38440780..625980493 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -30,6 +30,7 @@ #include "hb-raster.h" #include "hb-raster-paint.hh" #include "hb-raster-svg.hh" +#include "hb-raster-svg-base.hh" #include "OT/Color/svg/svg.hh" #include "hb-draw.h" #include "hb-ot-color.h" @@ -41,209 +42,9 @@ /* - * 1. String view helper + * 1. String / style helpers live in hb-raster-svg-base.* */ -struct hb_svg_str_t -{ - const char *data; - unsigned len; - - hb_svg_str_t () : data (nullptr), len (0) {} - hb_svg_str_t (const char *d, unsigned l) : data (d), len (l) {} - - bool is_null () const { return !data; } - - bool eq (const char *s) const - { - unsigned slen = strlen (s); - return len == slen && memcmp (data, s, len) == 0; - } - - bool starts_with (const char *prefix) const - { - unsigned plen = strlen (prefix); - return len >= plen && memcmp (data, prefix, plen) == 0; - } - - bool eq_cstr (const char *s) const - { - unsigned slen = strlen (s); - return len == slen && memcmp (data, s, len) == 0; - } - - float to_float () const - { - if (!data || !len) return 0.f; - /* strtof needs null-terminated string; we use a small stack buffer */ - char buf[64]; - unsigned n = hb_min (len, (unsigned) sizeof (buf) - 1); - memcpy (buf, data, n); - buf[n] = '\0'; - return strtof (buf, nullptr); - } - - int to_int () const - { - if (!data || !len) return 0; - char buf[32]; - unsigned n = hb_min (len, (unsigned) sizeof (buf) - 1); - memcpy (buf, data, n); - buf[n] = '\0'; - return atoi (buf); - } - - /* Skip leading whitespace */ - hb_svg_str_t trim_left () const - { - const char *p = data; - unsigned l = len; - while (l && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) - { p++; l--; } - return {p, l}; - } - - hb_svg_str_t trim () const - { - hb_svg_str_t s = trim_left (); - while (s.len && (s.data[s.len - 1] == ' ' || s.data[s.len - 1] == '\t' || - s.data[s.len - 1] == '\n' || s.data[s.len - 1] == '\r')) - s.len--; - return s; - } -}; - -static inline char -svg_ascii_lower (char c) -{ - if (c >= 'A' && c <= 'Z') - return c + ('a' - 'A'); - return c; -} - -static bool -svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit) -{ - unsigned n = (unsigned) strlen (lit); - if (s.len != n) return false; - for (unsigned i = 0; i < n; i++) - if (svg_ascii_lower (s.data[i]) != svg_ascii_lower (lit[i])) - return false; - return true; -} - -static bool -svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit) -{ - unsigned n = (unsigned) strlen (lit); - if (s.len < n) return false; - for (unsigned i = 0; i < n; i++) - if (svg_ascii_lower (s.data[i]) != svg_ascii_lower (lit[i])) - return false; - return true; -} - -struct hb_svg_style_props_t -{ - hb_svg_str_t fill; - hb_svg_str_t fill_opacity; - hb_svg_str_t opacity; - hb_svg_str_t transform; - hb_svg_str_t clip_path; - hb_svg_str_t display; - hb_svg_str_t color; - hb_svg_str_t visibility; - hb_svg_str_t offset; - hb_svg_str_t stop_color; - hb_svg_str_t stop_opacity; -}; - -static void -svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) -{ - if (style.is_null ()) return; - const char *p = style.data; - const char *end = style.data + style.len; - while (p < end) - { - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ';')) - p++; - if (p >= end) break; - const char *name_start = p; - while (p < end && *p != ':' && *p != ';') - p++; - const char *name_end = p; - while (name_end > name_start && - (name_end[-1] == ' ' || name_end[-1] == '\t' || name_end[-1] == '\n' || name_end[-1] == '\r')) - name_end--; - if (p >= end || *p != ':') - { - while (p < end && *p != ';') p++; - continue; - } - p++; /* skip ':' */ - const char *value_start = p; - while (p < end && *p != ';') - p++; - const char *value_end = p; - while (value_start < value_end && - (*value_start == ' ' || *value_start == '\t' || *value_start == '\n' || *value_start == '\r')) - value_start++; - while (value_end > value_start && - (value_end[-1] == ' ' || value_end[-1] == '\t' || value_end[-1] == '\n' || value_end[-1] == '\r')) - value_end--; - - hb_svg_str_t prop_name = {name_start, (unsigned) (name_end - name_start)}; - hb_svg_str_t prop_value = {value_start, (unsigned) (value_end - value_start)}; - if (svg_str_eq_ascii_ci (prop_name, "fill")) out->fill = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "fill-opacity")) out->fill_opacity = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "opacity")) out->opacity = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "transform")) out->transform = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "clip-path")) out->clip_path = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "display")) out->display = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "color")) out->color = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "visibility")) out->visibility = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "offset")) out->offset = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "stop-color")) out->stop_color = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "stop-opacity")) out->stop_opacity = prop_value; - - if (p < end && *p == ';') p++; - } -} - -static float -svg_parse_float_clamped01 (hb_svg_str_t s) -{ - return hb_clamp (s.to_float (), 0.f, 1.f); -} - -static float -svg_parse_number_or_percent (hb_svg_str_t s, bool *is_percent) -{ - if (is_percent) *is_percent = false; - s = s.trim (); - if (!s.len) return 0.f; - if (s.data[s.len - 1] == '%') - { - if (is_percent) *is_percent = true; - hb_svg_str_t n = {s.data, s.len - 1}; - return n.to_float () / 100.f; - } - return s.to_float (); -} - -static bool -svg_str_is_inherit (hb_svg_str_t s) -{ - return svg_str_eq_ascii_ci (s.trim (), "inherit"); -} - -static bool -svg_str_is_none (hb_svg_str_t s) -{ - return svg_str_eq_ascii_ci (s.trim (), "none"); -} - /* * 2. Minimal XML tokenizer diff --git a/src/meson.build b/src/meson.build index c9a2f6f6b..238f1cc95 100644 --- a/src/meson.build +++ b/src/meson.build @@ -705,6 +705,8 @@ hb_raster_sources = [ 'hb-raster-image.hh', 'hb-raster-utils.hh', 'hb-raster-draw.cc', + 'hb-raster-svg-base.cc', + 'hb-raster-svg-base.hh', 'hb-raster-paint.cc', 'hb-raster-paint.hh', 'hb-raster-svg.cc', From a7a7ed6f47408a8db1e606ec54e94744553e69f2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:44:34 -0300 Subject: [PATCH 011/105] [raster] Split SVG parser code into hb-raster-svg-parse --- src/hb-raster-svg-parse.cc | 651 +++++++++++++++++++++++++ src/hb-raster-svg-parse.hh | 314 ++++++++++++ src/hb-raster-svg.cc | 966 +------------------------------------ src/meson.build | 2 + 4 files changed, 970 insertions(+), 963 deletions(-) create mode 100644 src/hb-raster-svg-parse.cc create mode 100644 src/hb-raster-svg-parse.hh diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc new file mode 100644 index 000000000..d75050737 --- /dev/null +++ b/src/hb-raster-svg-parse.cc @@ -0,0 +1,651 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-parse.hh" + +#include +bool +svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) +{ + hb_svg_float_parser_t fp (s); + + while (fp.p < fp.end) + { + fp.skip_ws_comma (); + if (fp.p >= fp.end) break; + + const char *start = fp.p; + while (fp.p < fp.end && *fp.p != '(') fp.p++; + hb_svg_str_t func_name = {start, (unsigned) (fp.p - start)}; + + while (func_name.len && (func_name.data[func_name.len - 1] == ' ' || + func_name.data[func_name.len - 1] == '\t')) + func_name.len--; + + if (fp.p >= fp.end) break; + fp.p++; + + hb_svg_transform_t t; + + if (func_name.eq ("matrix")) + { + t.xx = fp.next_float (); + t.yx = fp.next_float (); + t.xy = fp.next_float (); + t.yy = fp.next_float (); + t.dx = fp.next_float (); + t.dy = fp.next_float (); + } + else if (func_name.eq ("translate")) + { + t.dx = fp.next_float (); + fp.skip_ws_comma (); + if (fp.p < fp.end && *fp.p != ')') + t.dy = fp.next_float (); + } + else if (func_name.eq ("scale")) + { + t.xx = fp.next_float (); + fp.skip_ws_comma (); + if (fp.p < fp.end && *fp.p != ')') + t.yy = fp.next_float (); + else + t.yy = t.xx; + } + else if (func_name.eq ("rotate")) + { + float angle = fp.next_float () * (float) M_PI / 180.f; + float cs = cosf (angle), sn = sinf (angle); + fp.skip_ws_comma (); + if (fp.p < fp.end && *fp.p != ')') + { + float cx = fp.next_float (); + float cy = fp.next_float (); + t.xx = cs; t.yx = sn; + t.xy = -sn; t.yy = cs; + t.dx = cx - cs * cx + sn * cy; + t.dy = cy - sn * cx - cs * cy; + } + else + { + t.xx = cs; t.yx = sn; + t.xy = -sn; t.yy = cs; + } + } + else if (func_name.eq ("skewX")) + { + float angle = fp.next_float () * (float) M_PI / 180.f; + t.xy = tanf (angle); + } + else if (func_name.eq ("skewY")) + { + float angle = fp.next_float () * (float) M_PI / 180.f; + t.yx = tanf (angle); + } + + while (fp.p < fp.end && *fp.p != ')') fp.p++; + if (fp.p < fp.end) fp.p++; + + out->multiply (t); + } + return true; +} + +static void +svg_arc_to_cubics (hb_draw_funcs_t *dfuncs, void *draw_data, hb_draw_state_t *st, + float cx, float cy, + float rx, float ry, + float phi, float theta1, float dtheta) +{ + int n_segs = (int) ceilf (fabsf (dtheta) / ((float) M_PI / 2.f)); + if (n_segs < 1) n_segs = 1; + float seg_angle = dtheta / n_segs; + + float cos_phi = cosf (phi); + float sin_phi = sinf (phi); + + for (int i = 0; i < n_segs; i++) + { + float t1 = theta1 + i * seg_angle; + float t2 = t1 + seg_angle; + float alpha = sinf (seg_angle) * + (sqrtf (4.f + 3.f * tanf (seg_angle / 2.f) * tanf (seg_angle / 2.f)) - 1.f) / 3.f; + + float cos_t1 = cosf (t1), sin_t1 = sinf (t1); + float cos_t2 = cosf (t2), sin_t2 = sinf (t2); + + float e1x = rx * cos_t1, e1y = ry * sin_t1; + float e2x = rx * cos_t2, e2y = ry * sin_t2; + float d1x = -rx * sin_t1, d1y = ry * cos_t1; + float d2x = -rx * sin_t2, d2y = ry * cos_t2; + + float cp1x = e1x + alpha * d1x; + float cp1y = e1y + alpha * d1y; + float cp2x = e2x - alpha * d2x; + float cp2y = e2y - alpha * d2y; + + float r_cp1x = cos_phi * cp1x - sin_phi * cp1y + cx; + float r_cp1y = sin_phi * cp1x + cos_phi * cp1y + cy; + float r_cp2x = cos_phi * cp2x - sin_phi * cp2y + cx; + float r_cp2y = sin_phi * cp2x + cos_phi * cp2y + cy; + float r_e2x = cos_phi * e2x - sin_phi * e2y + cx; + float r_e2y = sin_phi * e2x + cos_phi * e2y + cy; + + hb_draw_cubic_to (dfuncs, draw_data, st, + r_cp1x, r_cp1y, + r_cp2x, r_cp2y, + r_e2x, r_e2y); + } +} + +static void +svg_arc_endpoint_to_center (float x1, float y1, float x2, float y2, + float rx, float ry, float phi_deg, + bool large_arc, bool sweep, + float *cx_out, float *cy_out, + float *theta1_out, float *dtheta_out, + float *rx_out, float *ry_out) +{ + float phi = phi_deg * (float) M_PI / 180.f; + float cos_phi = cosf (phi), sin_phi = sinf (phi); + + float mx = (x1 - x2) / 2.f; + float my = (y1 - y2) / 2.f; + float x1p = cos_phi * mx + sin_phi * my; + float y1p = -sin_phi * mx + cos_phi * my; + + rx = fabsf (rx); ry = fabsf (ry); + if (rx < 1e-10f || ry < 1e-10f) + { + *cx_out = (x1 + x2) / 2.f; + *cy_out = (y1 + y2) / 2.f; + *theta1_out = 0; + *dtheta_out = 0; + *rx_out = rx; *ry_out = ry; + return; + } + + float x1p2 = x1p * x1p, y1p2 = y1p * y1p; + float rx2 = rx * rx, ry2 = ry * ry; + float lambda = x1p2 / rx2 + y1p2 / ry2; + if (lambda > 1.f) + { + float sl = sqrtf (lambda); + rx *= sl; ry *= sl; + rx2 = rx * rx; ry2 = ry * ry; + } + + float num = rx2 * ry2 - rx2 * y1p2 - ry2 * x1p2; + float den = rx2 * y1p2 + ry2 * x1p2; + float sq = (den > 0.f) ? sqrtf (hb_max (num / den, 0.f)) : 0.f; + if (large_arc == sweep) sq = -sq; + + float cxp = sq * rx * y1p / ry; + float cyp = -sq * ry * x1p / rx; + + float cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2.f; + float cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2.f; + + auto angle = [] (float ux, float uy, float vx, float vy) -> float { + float dot = ux * vx + uy * vy; + float len = sqrtf ((ux * ux + uy * uy) * (vx * vx + vy * vy)); + float a = acosf (hb_clamp (dot / len, -1.f, 1.f)); + if (ux * vy - uy * vx < 0.f) a = -a; + return a; + }; + + float theta1 = angle (1.f, 0.f, (x1p - cxp) / rx, (y1p - cyp) / ry); + float dtheta = angle ((x1p - cxp) / rx, (y1p - cyp) / ry, + (-x1p - cxp) / rx, (-y1p - cyp) / ry); + + if (!sweep && dtheta > 0.f) dtheta -= 2.f * (float) M_PI; + if (sweep && dtheta < 0.f) dtheta += 2.f * (float) M_PI; + + *cx_out = cx; *cy_out = cy; + *theta1_out = theta1; *dtheta_out = dtheta; + *rx_out = rx; *ry_out = ry; +} + +void +svg_parse_path_data (hb_svg_str_t d, hb_draw_funcs_t *dfuncs, void *draw_data) +{ + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + hb_svg_float_parser_t fp (d); + + float cur_x = 0, cur_y = 0; + float start_x = 0, start_y = 0; + float last_cx = 0, last_cy = 0; + char last_cmd = 0; + char cmd = 0; + + while (fp.p < fp.end) + { + fp.skip_ws_comma (); + if (fp.p >= fp.end) break; + + char c = *fp.p; + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + { + cmd = c; + fp.p++; + } + + switch (cmd) + { + case 'M': case 'm': + { + float x = fp.next_float (); + float y = fp.next_float (); + if (cmd == 'm') { x += cur_x; y += cur_y; } + hb_draw_move_to (dfuncs, draw_data, &st, x, y); + cur_x = start_x = x; + cur_y = start_y = y; + cmd = (cmd == 'M') ? 'L' : 'l'; + last_cmd = 'M'; + continue; + } + case 'L': case 'l': + { + float x = fp.next_float (); + float y = fp.next_float (); + if (cmd == 'l') { x += cur_x; y += cur_y; } + hb_draw_line_to (dfuncs, draw_data, &st, x, y); + cur_x = x; cur_y = y; + break; + } + case 'H': case 'h': + { + float x = fp.next_float (); + if (cmd == 'h') x += cur_x; + hb_draw_line_to (dfuncs, draw_data, &st, x, cur_y); + cur_x = x; + break; + } + case 'V': case 'v': + { + float y = fp.next_float (); + if (cmd == 'v') y += cur_y; + hb_draw_line_to (dfuncs, draw_data, &st, cur_x, y); + cur_y = y; + break; + } + case 'C': case 'c': + { + float x1 = fp.next_float (); + float y1 = fp.next_float (); + float x2 = fp.next_float (); + float y2 = fp.next_float (); + float x = fp.next_float (); + float y = fp.next_float (); + if (cmd == 'c') + { x1 += cur_x; y1 += cur_y; x2 += cur_x; y2 += cur_y; x += cur_x; y += cur_y; } + hb_draw_cubic_to (dfuncs, draw_data, &st, x1, y1, x2, y2, x, y); + last_cx = x2; last_cy = y2; + cur_x = x; cur_y = y; + break; + } + case 'S': case 's': + { + float cx1, cy1; + if (last_cmd == 'C' || last_cmd == 'c' || last_cmd == 'S' || last_cmd == 's') + { cx1 = 2 * cur_x - last_cx; cy1 = 2 * cur_y - last_cy; } + else + { cx1 = cur_x; cy1 = cur_y; } + float x2 = fp.next_float (); + float y2 = fp.next_float (); + float x = fp.next_float (); + float y = fp.next_float (); + if (cmd == 's') + { x2 += cur_x; y2 += cur_y; x += cur_x; y += cur_y; } + hb_draw_cubic_to (dfuncs, draw_data, &st, cx1, cy1, x2, y2, x, y); + last_cx = x2; last_cy = y2; + cur_x = x; cur_y = y; + break; + } + case 'Q': case 'q': + { + float x1 = fp.next_float (); + float y1 = fp.next_float (); + float x = fp.next_float (); + float y = fp.next_float (); + if (cmd == 'q') + { x1 += cur_x; y1 += cur_y; x += cur_x; y += cur_y; } + hb_draw_quadratic_to (dfuncs, draw_data, &st, x1, y1, x, y); + last_cx = x1; last_cy = y1; + cur_x = x; cur_y = y; + break; + } + case 'T': case 't': + { + float cx1, cy1; + if (last_cmd == 'Q' || last_cmd == 'q' || last_cmd == 'T' || last_cmd == 't') + { cx1 = 2 * cur_x - last_cx; cy1 = 2 * cur_y - last_cy; } + else + { cx1 = cur_x; cy1 = cur_y; } + float x = fp.next_float (); + float y = fp.next_float (); + if (cmd == 't') { x += cur_x; y += cur_y; } + hb_draw_quadratic_to (dfuncs, draw_data, &st, cx1, cy1, x, y); + last_cx = cx1; last_cy = cy1; + cur_x = x; cur_y = y; + break; + } + case 'A': case 'a': + { + float rx = fp.next_float (); + float ry = fp.next_float (); + float x_rot = fp.next_float (); + bool large_arc = fp.next_flag (); + bool sweep = fp.next_flag (); + float x = fp.next_float (); + float y = fp.next_float (); + if (cmd == 'a') { x += cur_x; y += cur_y; } + + if (fabsf (x - cur_x) < 1e-6f && fabsf (y - cur_y) < 1e-6f) + { + cur_x = x; cur_y = y; + break; + } + + float cx, cy, theta1, dtheta, adj_rx, adj_ry; + svg_arc_endpoint_to_center (cur_x, cur_y, x, y, + rx, ry, x_rot, + large_arc, sweep, + &cx, &cy, &theta1, &dtheta, + &adj_rx, &adj_ry); + + float phi = x_rot * (float) M_PI / 180.f; + svg_arc_to_cubics (dfuncs, draw_data, &st, + cx, cy, adj_rx, adj_ry, phi, theta1, dtheta); + cur_x = x; cur_y = y; + break; + } + case 'Z': case 'z': + hb_draw_close_path (dfuncs, draw_data, &st); + cur_x = start_x; + cur_y = start_y; + break; + + default: + fp.p++; + continue; + } + + last_cmd = cmd; + } +} + +static void +svg_rect_to_path (float x, float y, float w, float h, float rx, float ry, + hb_draw_funcs_t *dfuncs, void *draw_data) +{ + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + + if (rx <= 0 && ry <= 0) + { + hb_draw_move_to (dfuncs, draw_data, &st, x, y); + hb_draw_line_to (dfuncs, draw_data, &st, x + w, y); + hb_draw_line_to (dfuncs, draw_data, &st, x + w, y + h); + hb_draw_line_to (dfuncs, draw_data, &st, x, y + h); + hb_draw_close_path (dfuncs, draw_data, &st); + return; + } + + if (rx <= 0) rx = ry; + if (ry <= 0) ry = rx; + rx = hb_min (rx, w / 2); + ry = hb_min (ry, h / 2); + + float kx = rx * 0.5522847498f; + float ky = ry * 0.5522847498f; + + hb_draw_move_to (dfuncs, draw_data, &st, x + rx, y); + hb_draw_line_to (dfuncs, draw_data, &st, x + w - rx, y); + hb_draw_cubic_to (dfuncs, draw_data, &st, + x + w - rx + kx, y, + x + w, y + ry - ky, + x + w, y + ry); + hb_draw_line_to (dfuncs, draw_data, &st, x + w, y + h - ry); + hb_draw_cubic_to (dfuncs, draw_data, &st, + x + w, y + h - ry + ky, + x + w - rx + kx, y + h, + x + w - rx, y + h); + hb_draw_line_to (dfuncs, draw_data, &st, x + rx, y + h); + hb_draw_cubic_to (dfuncs, draw_data, &st, + x + rx - kx, y + h, + x, y + h - ry + ky, + x, y + h - ry); + hb_draw_line_to (dfuncs, draw_data, &st, x, y + ry); + hb_draw_cubic_to (dfuncs, draw_data, &st, + x, y + ry - ky, + x + rx - kx, y, + x + rx, y); + hb_draw_close_path (dfuncs, draw_data, &st); +} + +static void +svg_circle_to_path (float cx, float cy, float r, + hb_draw_funcs_t *dfuncs, void *draw_data) +{ + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + float k = r * 0.5522847498f; + + hb_draw_move_to (dfuncs, draw_data, &st, cx + r, cy); + hb_draw_cubic_to (dfuncs, draw_data, &st, + cx + r, cy + k, + cx + k, cy + r, + cx, cy + r); + hb_draw_cubic_to (dfuncs, draw_data, &st, + cx - k, cy + r, + cx - r, cy + k, + cx - r, cy); + hb_draw_cubic_to (dfuncs, draw_data, &st, + cx - r, cy - k, + cx - k, cy - r, + cx, cy - r); + hb_draw_cubic_to (dfuncs, draw_data, &st, + cx + k, cy - r, + cx + r, cy - k, + cx + r, cy); + hb_draw_close_path (dfuncs, draw_data, &st); +} + +static void +svg_ellipse_to_path (float cx, float cy, float rx, float ry, + hb_draw_funcs_t *dfuncs, void *draw_data) +{ + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + float kx = rx * 0.5522847498f; + float ky = ry * 0.5522847498f; + + hb_draw_move_to (dfuncs, draw_data, &st, cx + rx, cy); + hb_draw_cubic_to (dfuncs, draw_data, &st, + cx + rx, cy + ky, + cx + kx, cy + ry, + cx, cy + ry); + hb_draw_cubic_to (dfuncs, draw_data, &st, + cx - kx, cy + ry, + cx - rx, cy + ky, + cx - rx, cy); + hb_draw_cubic_to (dfuncs, draw_data, &st, + cx - rx, cy - ky, + cx - kx, cy - ry, + cx, cy - ry); + hb_draw_cubic_to (dfuncs, draw_data, &st, + cx + kx, cy - ry, + cx + rx, cy - ky, + cx + rx, cy); + hb_draw_close_path (dfuncs, draw_data, &st); +} + +static void +svg_line_to_path (float x1, float y1, float x2, float y2, + hb_draw_funcs_t *dfuncs, void *draw_data) +{ + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + hb_draw_move_to (dfuncs, draw_data, &st, x1, y1); + hb_draw_line_to (dfuncs, draw_data, &st, x2, y2); +} + +static void +svg_polygon_to_path (hb_svg_str_t points, bool close, + hb_draw_funcs_t *dfuncs, void *draw_data) +{ + hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + hb_svg_float_parser_t fp (points); + bool first = true; + while (fp.has_more ()) + { + float x = fp.next_float (); + float y = fp.next_float (); + if (first) + { + hb_draw_move_to (dfuncs, draw_data, &st, x, y); + first = false; + } + else + hb_draw_line_to (dfuncs, draw_data, &st, x, y); + } + if (close && !first) + hb_draw_close_path (dfuncs, draw_data, &st); +} + +void +svg_shape_path_emit (hb_draw_funcs_t *dfuncs, void *draw_data, void *user_data) +{ + hb_svg_shape_emit_data_t *shape = (hb_svg_shape_emit_data_t *) user_data; + switch (shape->type) + { + case hb_svg_shape_emit_data_t::SHAPE_PATH: + svg_parse_path_data (shape->str_data, dfuncs, draw_data); + break; + case hb_svg_shape_emit_data_t::SHAPE_RECT: + svg_rect_to_path (shape->params[0], shape->params[1], + shape->params[2], shape->params[3], + shape->params[4], shape->params[5], + dfuncs, draw_data); + break; + case hb_svg_shape_emit_data_t::SHAPE_CIRCLE: + svg_circle_to_path (shape->params[0], shape->params[1], shape->params[2], + dfuncs, draw_data); + break; + case hb_svg_shape_emit_data_t::SHAPE_ELLIPSE: + svg_ellipse_to_path (shape->params[0], shape->params[1], + shape->params[2], shape->params[3], + dfuncs, draw_data); + break; + case hb_svg_shape_emit_data_t::SHAPE_LINE: + svg_line_to_path (shape->params[0], shape->params[1], + shape->params[2], shape->params[3], + dfuncs, draw_data); + break; + case hb_svg_shape_emit_data_t::SHAPE_POLYLINE: + svg_polygon_to_path (shape->str_data, false, dfuncs, draw_data); + break; + case hb_svg_shape_emit_data_t::SHAPE_POLYGON: + svg_polygon_to_path (shape->str_data, true, dfuncs, draw_data); + break; + } +} + +bool +svg_parse_shape_tag (hb_svg_xml_parser_t &parser, + hb_svg_shape_emit_data_t *shape) +{ + hb_svg_str_t tag = parser.tag_name; + if (tag.eq ("path")) + { + hb_svg_str_t d = parser.find_attr ("d"); + if (!d.len) return false; + shape->type = hb_svg_shape_emit_data_t::SHAPE_PATH; + shape->str_data = d; + return true; + } + if (tag.eq ("rect")) + { + float w = svg_parse_float (parser.find_attr ("width")); + float h = svg_parse_float (parser.find_attr ("height")); + if (w <= 0 || h <= 0) return false; + shape->type = hb_svg_shape_emit_data_t::SHAPE_RECT; + shape->params[0] = svg_parse_float (parser.find_attr ("x")); + shape->params[1] = svg_parse_float (parser.find_attr ("y")); + shape->params[2] = w; + shape->params[3] = h; + shape->params[4] = svg_parse_float (parser.find_attr ("rx")); + shape->params[5] = svg_parse_float (parser.find_attr ("ry")); + return true; + } + if (tag.eq ("circle")) + { + float r = svg_parse_float (parser.find_attr ("r")); + if (r <= 0) return false; + shape->type = hb_svg_shape_emit_data_t::SHAPE_CIRCLE; + shape->params[0] = svg_parse_float (parser.find_attr ("cx")); + shape->params[1] = svg_parse_float (parser.find_attr ("cy")); + shape->params[2] = r; + return true; + } + if (tag.eq ("ellipse")) + { + float rx = svg_parse_float (parser.find_attr ("rx")); + float ry = svg_parse_float (parser.find_attr ("ry")); + if (rx <= 0 || ry <= 0) return false; + shape->type = hb_svg_shape_emit_data_t::SHAPE_ELLIPSE; + shape->params[0] = svg_parse_float (parser.find_attr ("cx")); + shape->params[1] = svg_parse_float (parser.find_attr ("cy")); + shape->params[2] = rx; + shape->params[3] = ry; + return true; + } + if (tag.eq ("line")) + { + shape->type = hb_svg_shape_emit_data_t::SHAPE_LINE; + shape->params[0] = svg_parse_float (parser.find_attr ("x1")); + shape->params[1] = svg_parse_float (parser.find_attr ("y1")); + shape->params[2] = svg_parse_float (parser.find_attr ("x2")); + shape->params[3] = svg_parse_float (parser.find_attr ("y2")); + return true; + } + if (tag.eq ("polyline")) + { + hb_svg_str_t points = parser.find_attr ("points"); + if (!points.len) return false; + shape->type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; + shape->str_data = points; + return true; + } + if (tag.eq ("polygon")) + { + hb_svg_str_t points = parser.find_attr ("points"); + if (!points.len) return false; + shape->type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; + shape->str_data = points; + return true; + } + return false; +} diff --git a/src/hb-raster-svg-parse.hh b/src/hb-raster-svg-parse.hh new file mode 100644 index 000000000..251a82c54 --- /dev/null +++ b/src/hb-raster-svg-parse.hh @@ -0,0 +1,314 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_PARSE_HH +#define HB_RASTER_SVG_PARSE_HH + +#include "hb.hh" + +#include "hb-raster-svg-base.hh" +#include "hb-draw.h" + +#include + +enum hb_svg_token_type_t +{ + SVG_TOKEN_OPEN_TAG, + SVG_TOKEN_CLOSE_TAG, + SVG_TOKEN_SELF_CLOSE_TAG, + SVG_TOKEN_TEXT, + SVG_TOKEN_EOF +}; + +struct hb_svg_attr_t +{ + hb_svg_str_t name; + hb_svg_str_t value; +}; + +struct hb_svg_xml_parser_t +{ + const char *p; + const char *end; + const char *tag_start = nullptr; + + hb_svg_str_t tag_name; + hb_vector_t attrs; + bool self_closing; + + hb_svg_xml_parser_t (const char *data, unsigned len) + : p (data), end (data + len) {} + + void skip_ws () + { + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) + p++; + } + + hb_svg_str_t read_name () + { + const char *start = p; + while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' && + *p != '>' && *p != '/' && *p != '=') + p++; + return {start, (unsigned) (p - start)}; + } + + hb_svg_str_t read_attr_value () + { + if (p >= end) return {}; + char quote = *p; + if (quote != '"' && quote != '\'') return {}; + p++; + const char *start = p; + while (p < end && *p != quote) + p++; + hb_svg_str_t val = {start, (unsigned) (p - start)}; + if (p < end) p++; /* skip closing quote */ + return val; + } + + void parse_attrs () + { + attrs.clear (); + while (p < end) + { + skip_ws (); + if (p >= end || *p == '>' || *p == '/') break; + hb_svg_str_t name = read_name (); + if (!name.len) { p++; continue; } + skip_ws (); + if (p < end && *p == '=') + { + p++; + skip_ws (); + hb_svg_str_t val = read_attr_value (); + hb_svg_attr_t attr = {name, val}; + attrs.push (attr); + } + else + { + hb_svg_attr_t attr = {name, {}}; + attrs.push (attr); + } + } + } + + hb_svg_token_type_t next () + { + while (p < end) + { + if (*p != '<') + { + while (p < end && *p != '<') p++; + continue; + } + + tag_start = p; + p++; /* skip '<' */ + if (p >= end) return SVG_TOKEN_EOF; + + if (*p == '!') + { + if (p + 2 < end && p[1] == '-' && p[2] == '-') + { + p += 3; + while (p + 2 < end && !(p[0] == '-' && p[1] == '-' && p[2] == '>')) + p++; + if (p + 2 < end) p += 3; + } + else + { + while (p < end && *p != '>') p++; + if (p < end) p++; + } + continue; + } + if (*p == '?') + { + while (p + 1 < end && !(p[0] == '?' && p[1] == '>')) + p++; + if (p + 1 < end) p += 2; + continue; + } + + if (*p == '/') + { + p++; + tag_name = read_name (); + while (p < end && *p != '>') p++; + if (p < end) p++; + attrs.clear (); + self_closing = false; + return SVG_TOKEN_CLOSE_TAG; + } + + tag_name = read_name (); + parse_attrs (); + + self_closing = false; + skip_ws (); + if (p < end && *p == '/') + { + self_closing = true; + p++; + } + if (p < end && *p == '>') + p++; + + return self_closing ? SVG_TOKEN_SELF_CLOSE_TAG : SVG_TOKEN_OPEN_TAG; + } + return SVG_TOKEN_EOF; + } + + hb_svg_str_t find_attr (const char *name) const + { + for (unsigned i = 0; i < attrs.length; i++) + if (attrs[i].name.eq (name)) + return attrs[i].value; + return {}; + } +}; + +static inline hb_svg_str_t +svg_pick_attr_or_style (const hb_svg_xml_parser_t &parser, + hb_svg_str_t style_value, + const char *attr_name) +{ + return style_value.is_null () ? parser.find_attr (attr_name) : style_value; +} + +struct hb_svg_transform_t +{ + float xx = 1, yx = 0, xy = 0, yy = 1, dx = 0, dy = 0; + + void multiply (const hb_svg_transform_t &other) + { + float nxx = xx * other.xx + xy * other.yx; + float nyx = yx * other.xx + yy * other.yx; + float nxy = xx * other.xy + xy * other.yy; + float nyy = yx * other.xy + yy * other.yy; + float ndx = xx * other.dx + xy * other.dy + dx; + float ndy = yx * other.dx + yy * other.dy + dy; + xx = nxx; yx = nyx; xy = nxy; yy = nyy; dx = ndx; dy = ndy; + } +}; + +struct hb_svg_float_parser_t +{ + const char *p; + const char *end; + + hb_svg_float_parser_t (hb_svg_str_t s) : p (s.data), end (s.data + s.len) {} + + void skip_ws_comma () + { + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ',')) + p++; + } + + bool has_more () const { return p < end; } + + float next_float () + { + skip_ws_comma (); + if (p >= end) return 0.f; + const char *start = p; + char buf[64]; + unsigned n = 0; + bool has_digit = false; + if (p < end && (*p == '-' || *p == '+')) + buf[n++] = *p++; + while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') + { + buf[n++] = *p++; + has_digit = true; + } + if (p < end && n < sizeof (buf) - 1 && *p == '.') + { + buf[n++] = *p++; + while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') + { + buf[n++] = *p++; + has_digit = true; + } + } + if (p < end && n < sizeof (buf) - 1 && (*p == 'e' || *p == 'E')) + { + buf[n++] = *p++; + if (p < end && n < sizeof (buf) - 1 && (*p == '+' || *p == '-')) + buf[n++] = *p++; + bool has_exp_digit = false; + while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') + { + buf[n++] = *p++; + has_exp_digit = true; + } + if (!has_exp_digit) + { + p = start; + has_digit = false; + } + } + if (!has_digit) + { + if (p < end) p++; + return 0.f; + } + buf[n] = '\0'; + return strtof (buf, nullptr); + } + + bool next_flag () + { + skip_ws_comma (); + if (p >= end) return false; + bool v = *p != '0'; + p++; + return v; + } +}; + +static inline float +svg_parse_float (hb_svg_str_t s) +{ + return s.to_float (); +} + +struct hb_svg_shape_emit_data_t +{ + enum { SHAPE_PATH, SHAPE_RECT, SHAPE_CIRCLE, SHAPE_ELLIPSE, + SHAPE_LINE, SHAPE_POLYLINE, SHAPE_POLYGON } type; + hb_svg_str_t str_data; + float params[6]; +}; + +HB_INTERNAL bool svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out); +HB_INTERNAL void svg_parse_path_data (hb_svg_str_t d, hb_draw_funcs_t *dfuncs, void *draw_data); +HB_INTERNAL void svg_shape_path_emit (hb_draw_funcs_t *dfuncs, void *draw_data, void *user_data); +HB_INTERNAL bool svg_parse_shape_tag (hb_svg_xml_parser_t &parser, hb_svg_shape_emit_data_t *shape); + +#endif /* HB_RASTER_SVG_PARSE_HH */ diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 625980493..0819954f3 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -31,11 +31,11 @@ #include "hb-raster-paint.hh" #include "hb-raster-svg.hh" #include "hb-raster-svg-base.hh" +#include "hb-raster-svg-parse.hh" #include "OT/Color/svg/svg.hh" #include "hb-draw.h" #include "hb-ot-color.h" -#include #include #include #include @@ -47,183 +47,9 @@ /* - * 2. Minimal XML tokenizer + * 2. XML tokenizer / transform / shape parsing live in hb-raster-svg-parse.* */ -enum hb_svg_token_type_t -{ - SVG_TOKEN_OPEN_TAG, - SVG_TOKEN_CLOSE_TAG, - SVG_TOKEN_SELF_CLOSE_TAG, - SVG_TOKEN_TEXT, - SVG_TOKEN_EOF -}; - -struct hb_svg_attr_t -{ - hb_svg_str_t name; - hb_svg_str_t value; -}; - -struct hb_svg_xml_parser_t -{ - const char *p; - const char *end; - const char *tag_start = nullptr; - - hb_svg_str_t tag_name; - hb_vector_t attrs; - bool self_closing; - - hb_svg_xml_parser_t (const char *data, unsigned len) - : p (data), end (data + len) {} - - void skip_ws () - { - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) - p++; - } - - hb_svg_str_t read_name () - { - const char *start = p; - while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' && - *p != '>' && *p != '/' && *p != '=') - p++; - return {start, (unsigned) (p - start)}; - } - - hb_svg_str_t read_attr_value () - { - if (p >= end) return {}; - char quote = *p; - if (quote != '"' && quote != '\'') return {}; - p++; - const char *start = p; - while (p < end && *p != quote) - p++; - hb_svg_str_t val = {start, (unsigned) (p - start)}; - if (p < end) p++; /* skip closing quote */ - return val; - } - - void parse_attrs () - { - attrs.clear (); - while (p < end) - { - skip_ws (); - if (p >= end || *p == '>' || *p == '/') break; - hb_svg_str_t name = read_name (); - if (!name.len) { p++; continue; } - skip_ws (); - if (p < end && *p == '=') - { - p++; - skip_ws (); - hb_svg_str_t val = read_attr_value (); - hb_svg_attr_t attr = {name, val}; - attrs.push (attr); - } - else - { - hb_svg_attr_t attr = {name, {}}; - attrs.push (attr); - } - } - } - - hb_svg_token_type_t next () - { - while (p < end) - { - if (*p != '<') - { - /* Text content — skip for now */ - while (p < end && *p != '<') p++; - continue; - } - - tag_start = p; - p++; /* skip '<' */ - if (p >= end) return SVG_TOKEN_EOF; - - /* Comment or processing instruction */ - if (*p == '!') - { - /* Skip */ - if (p + 2 < end && p[1] == '-' && p[2] == '-') - { - p += 3; - while (p + 2 < end && !(p[0] == '-' && p[1] == '-' && p[2] == '>')) - p++; - if (p + 2 < end) p += 3; - } - else - { - /* Skip or similar */ - while (p < end && *p != '>') p++; - if (p < end) p++; - } - continue; - } - if (*p == '?') - { - /* */ - while (p + 1 < end && !(p[0] == '?' && p[1] == '>')) - p++; - if (p + 1 < end) p += 2; - continue; - } - - /* Closing tag */ - if (*p == '/') - { - p++; - tag_name = read_name (); - while (p < end && *p != '>') p++; - if (p < end) p++; - attrs.clear (); - self_closing = false; - return SVG_TOKEN_CLOSE_TAG; - } - - /* Opening tag */ - tag_name = read_name (); - parse_attrs (); - - self_closing = false; - skip_ws (); - if (p < end && *p == '/') - { - self_closing = true; - p++; - } - if (p < end && *p == '>') - p++; - - return self_closing ? SVG_TOKEN_SELF_CLOSE_TAG : SVG_TOKEN_OPEN_TAG; - } - return SVG_TOKEN_EOF; - } - - hb_svg_str_t find_attr (const char *name) const - { - for (unsigned i = 0; i < attrs.length; i++) - if (attrs[i].name.eq (name)) - return attrs[i].value; - return {}; - } -}; - -static inline hb_svg_str_t -svg_pick_attr_or_style (const hb_svg_xml_parser_t &parser, - hb_svg_str_t style_value, - const char *attr_name) -{ - return style_value.is_null () ? parser.find_attr (attr_name) : style_value; -} - /* * 3. SVG Color parser @@ -531,679 +357,14 @@ svg_parse_color (hb_svg_str_t s, /* - * 4. SVG Number/Float parser + * 9. Defs store */ -struct hb_svg_float_parser_t -{ - const char *p; - const char *end; - - hb_svg_float_parser_t (hb_svg_str_t s) : p (s.data), end (s.data + s.len) {} - hb_svg_float_parser_t (const char *d, unsigned l) : p (d), end (d + l) {} - - void skip_ws_comma () - { - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ',')) - p++; - } - - bool has_more () const { return p < end; } - - float next_float () - { - skip_ws_comma (); - if (p >= end) return 0.f; - const char *start = p; - char buf[64]; - unsigned n = 0; - bool has_digit = false; - if (p < end && (*p == '-' || *p == '+')) - buf[n++] = *p++; - while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') - { - buf[n++] = *p++; - has_digit = true; - } - if (p < end && n < sizeof (buf) - 1 && *p == '.') - { - buf[n++] = *p++; - while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') - { - buf[n++] = *p++; - has_digit = true; - } - } - if (p < end && n < sizeof (buf) - 1 && (*p == 'e' || *p == 'E')) - { - buf[n++] = *p++; - if (p < end && n < sizeof (buf) - 1 && (*p == '+' || *p == '-')) - buf[n++] = *p++; - bool has_exp_digit = false; - while (p < end && n < sizeof (buf) - 1 && *p >= '0' && *p <= '9') - { - buf[n++] = *p++; - has_exp_digit = true; - } - if (!has_exp_digit) - { - p = start; - has_digit = false; - } - } - if (!has_digit) - { - if (p < end) p++; - return 0.f; - } - buf[n] = '\0'; - return strtof (buf, nullptr); - } - - int next_int () - { - return (int) next_float (); - } - - bool next_flag () - { - skip_ws_comma (); - if (p >= end) return false; - bool v = *p != '0'; - p++; - return v; - } -}; - -static float -svg_parse_float (hb_svg_str_t s) -{ - return s.to_float (); -} - - - -/* - * 5. SVG Transform parser - */ - -struct hb_svg_transform_t -{ - float xx = 1, yx = 0, xy = 0, yy = 1, dx = 0, dy = 0; - - void multiply (const hb_svg_transform_t &other) - { - float nxx = xx * other.xx + xy * other.yx; - float nyx = yx * other.xx + yy * other.yx; - float nxy = xx * other.xy + xy * other.yy; - float nyy = yx * other.xy + yy * other.yy; - float ndx = xx * other.dx + xy * other.dy + dx; - float ndy = yx * other.dx + yy * other.dy + dy; - xx = nxx; yx = nyx; xy = nxy; yy = nyy; dx = ndx; dy = ndy; - } -}; - -static bool -svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) -{ - hb_svg_float_parser_t fp (s); - - while (fp.p < fp.end) - { - fp.skip_ws_comma (); - if (fp.p >= fp.end) break; - - const char *start = fp.p; - /* Read function name */ - while (fp.p < fp.end && *fp.p != '(') fp.p++; - hb_svg_str_t func_name = {start, (unsigned) (fp.p - start)}; - - /* Trim trailing whitespace from function name */ - while (func_name.len && (func_name.data[func_name.len - 1] == ' ' || - func_name.data[func_name.len - 1] == '\t')) - func_name.len--; - - if (fp.p >= fp.end) break; - fp.p++; /* skip '(' */ - - hb_svg_transform_t t; - - if (func_name.eq ("matrix")) - { - t.xx = fp.next_float (); - t.yx = fp.next_float (); - t.xy = fp.next_float (); - t.yy = fp.next_float (); - t.dx = fp.next_float (); - t.dy = fp.next_float (); - } - else if (func_name.eq ("translate")) - { - t.dx = fp.next_float (); - fp.skip_ws_comma (); - if (fp.p < fp.end && *fp.p != ')') - t.dy = fp.next_float (); - } - else if (func_name.eq ("scale")) - { - t.xx = fp.next_float (); - fp.skip_ws_comma (); - if (fp.p < fp.end && *fp.p != ')') - t.yy = fp.next_float (); - else - t.yy = t.xx; - } - else if (func_name.eq ("rotate")) - { - float angle = fp.next_float () * (float) M_PI / 180.f; - float cs = cosf (angle), sn = sinf (angle); - fp.skip_ws_comma (); - if (fp.p < fp.end && *fp.p != ')') - { - float cx = fp.next_float (); - float cy = fp.next_float (); - t.xx = cs; t.yx = sn; - t.xy = -sn; t.yy = cs; - t.dx = cx - cs * cx + sn * cy; - t.dy = cy - sn * cx - cs * cy; - } - else - { - t.xx = cs; t.yx = sn; - t.xy = -sn; t.yy = cs; - } - } - else if (func_name.eq ("skewX")) - { - float angle = fp.next_float () * (float) M_PI / 180.f; - t.xy = tanf (angle); - } - else if (func_name.eq ("skewY")) - { - float angle = fp.next_float () * (float) M_PI / 180.f; - t.yx = tanf (angle); - } - - /* Skip to closing ')' */ - while (fp.p < fp.end && *fp.p != ')') fp.p++; - if (fp.p < fp.end) fp.p++; - - out->multiply (t); - } - return true; -} - - -/* - * 6. SVG Path Data parser - */ - -static void -svg_arc_to_cubics (hb_draw_funcs_t *dfuncs, void *draw_data, hb_draw_state_t *st, - float cx, float cy, - float rx, float ry, - float phi, float theta1, float dtheta) -{ - /* Approximate arc with cubic bezier segments. - * Split into segments of at most 90 degrees. */ - int n_segs = (int) ceilf (fabsf (dtheta) / ((float) M_PI / 2.f)); - if (n_segs < 1) n_segs = 1; - float seg_angle = dtheta / n_segs; - - float cos_phi = cosf (phi); - float sin_phi = sinf (phi); - - for (int i = 0; i < n_segs; i++) - { - float t1 = theta1 + i * seg_angle; - float t2 = t1 + seg_angle; - float alpha = sinf (seg_angle) * (sqrtf (4.f + 3.f * tanf (seg_angle / 2.f) * tanf (seg_angle / 2.f)) - 1.f) / 3.f; - - float cos_t1 = cosf (t1), sin_t1 = sinf (t1); - float cos_t2 = cosf (t2), sin_t2 = sinf (t2); - - /* Endpoint 1 (on unit circle) */ - float e1x = rx * cos_t1, e1y = ry * sin_t1; - /* Endpoint 2 */ - float e2x = rx * cos_t2, e2y = ry * sin_t2; - /* Derivative at t1 */ - float d1x = -rx * sin_t1, d1y = ry * cos_t1; - /* Derivative at t2 */ - float d2x = -rx * sin_t2, d2y = ry * cos_t2; - - /* Control points */ - float cp1x = e1x + alpha * d1x; - float cp1y = e1y + alpha * d1y; - float cp2x = e2x - alpha * d2x; - float cp2y = e2y - alpha * d2y; - - /* Rotate and translate */ - float r_cp1x = cos_phi * cp1x - sin_phi * cp1y + cx; - float r_cp1y = sin_phi * cp1x + cos_phi * cp1y + cy; - float r_cp2x = cos_phi * cp2x - sin_phi * cp2y + cx; - float r_cp2y = sin_phi * cp2x + cos_phi * cp2y + cy; - float r_e2x = cos_phi * e2x - sin_phi * e2y + cx; - float r_e2y = sin_phi * e2x + cos_phi * e2y + cy; - - hb_draw_cubic_to (dfuncs, draw_data, st, - r_cp1x, r_cp1y, - r_cp2x, r_cp2y, - r_e2x, r_e2y); - } -} - -static void -svg_arc_endpoint_to_center (float x1, float y1, float x2, float y2, - float rx, float ry, float phi_deg, - bool large_arc, bool sweep, - float *cx_out, float *cy_out, - float *theta1_out, float *dtheta_out, - float *rx_out, float *ry_out) -{ - float phi = phi_deg * (float) M_PI / 180.f; - float cos_phi = cosf (phi), sin_phi = sinf (phi); - - /* Step 1: compute (x1', y1') */ - float mx = (x1 - x2) / 2.f; - float my = (y1 - y2) / 2.f; - float x1p = cos_phi * mx + sin_phi * my; - float y1p = -sin_phi * mx + cos_phi * my; - - /* Correct radii */ - rx = fabsf (rx); ry = fabsf (ry); - if (rx < 1e-10f || ry < 1e-10f) - { - *cx_out = (x1 + x2) / 2.f; - *cy_out = (y1 + y2) / 2.f; - *theta1_out = 0; - *dtheta_out = 0; - *rx_out = rx; *ry_out = ry; - return; - } - - float x1p2 = x1p * x1p, y1p2 = y1p * y1p; - float rx2 = rx * rx, ry2 = ry * ry; - float lambda = x1p2 / rx2 + y1p2 / ry2; - if (lambda > 1.f) - { - float sl = sqrtf (lambda); - rx *= sl; ry *= sl; - rx2 = rx * rx; ry2 = ry * ry; - } - - /* Step 2: compute (cx', cy') */ - float num = rx2 * ry2 - rx2 * y1p2 - ry2 * x1p2; - float den = rx2 * y1p2 + ry2 * x1p2; - float sq = (den > 0.f) ? sqrtf (hb_max (num / den, 0.f)) : 0.f; - if (large_arc == sweep) sq = -sq; - - float cxp = sq * rx * y1p / ry; - float cyp = -sq * ry * x1p / rx; - - /* Step 3: compute (cx, cy) */ - float cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2.f; - float cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2.f; - - /* Step 4: compute theta1 and dtheta */ - auto angle = [] (float ux, float uy, float vx, float vy) -> float { - float dot = ux * vx + uy * vy; - float len = sqrtf ((ux * ux + uy * uy) * (vx * vx + vy * vy)); - float a = acosf (hb_clamp (dot / len, -1.f, 1.f)); - if (ux * vy - uy * vx < 0.f) a = -a; - return a; - }; - - float theta1 = angle (1.f, 0.f, (x1p - cxp) / rx, (y1p - cyp) / ry); - float dtheta = angle ((x1p - cxp) / rx, (y1p - cyp) / ry, - (-x1p - cxp) / rx, (-y1p - cyp) / ry); - - if (!sweep && dtheta > 0.f) dtheta -= 2.f * (float) M_PI; - if (sweep && dtheta < 0.f) dtheta += 2.f * (float) M_PI; - - *cx_out = cx; *cy_out = cy; - *theta1_out = theta1; *dtheta_out = dtheta; - *rx_out = rx; *ry_out = ry; -} - -static void -svg_parse_path_data (hb_svg_str_t d, hb_draw_funcs_t *dfuncs, void *draw_data) -{ - hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; - hb_svg_float_parser_t fp (d); - - float cur_x = 0, cur_y = 0; - float start_x = 0, start_y = 0; - float last_cx = 0, last_cy = 0; /* for S/T smooth curves */ - char last_cmd = 0; - char cmd = 0; - - while (fp.p < fp.end) - { - fp.skip_ws_comma (); - if (fp.p >= fp.end) break; - - /* Check if next char is a command letter */ - char c = *fp.p; - if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) - { - cmd = c; - fp.p++; - } - /* else repeat previous command with new parameters */ - - switch (cmd) - { - case 'M': case 'm': - { - float x = fp.next_float (); - float y = fp.next_float (); - if (cmd == 'm') { x += cur_x; y += cur_y; } - hb_draw_move_to (dfuncs, draw_data, &st, x, y); - cur_x = start_x = x; - cur_y = start_y = y; - /* Subsequent coordinates after M are treated as L */ - cmd = (cmd == 'M') ? 'L' : 'l'; - last_cmd = 'M'; - continue; - } - case 'L': case 'l': - { - float x = fp.next_float (); - float y = fp.next_float (); - if (cmd == 'l') { x += cur_x; y += cur_y; } - hb_draw_line_to (dfuncs, draw_data, &st, x, y); - cur_x = x; cur_y = y; - break; - } - case 'H': case 'h': - { - float x = fp.next_float (); - if (cmd == 'h') x += cur_x; - hb_draw_line_to (dfuncs, draw_data, &st, x, cur_y); - cur_x = x; - break; - } - case 'V': case 'v': - { - float y = fp.next_float (); - if (cmd == 'v') y += cur_y; - hb_draw_line_to (dfuncs, draw_data, &st, cur_x, y); - cur_y = y; - break; - } - case 'C': case 'c': - { - float x1 = fp.next_float (); - float y1 = fp.next_float (); - float x2 = fp.next_float (); - float y2 = fp.next_float (); - float x = fp.next_float (); - float y = fp.next_float (); - if (cmd == 'c') - { x1 += cur_x; y1 += cur_y; x2 += cur_x; y2 += cur_y; x += cur_x; y += cur_y; } - hb_draw_cubic_to (dfuncs, draw_data, &st, x1, y1, x2, y2, x, y); - last_cx = x2; last_cy = y2; - cur_x = x; cur_y = y; - break; - } - case 'S': case 's': - { - float cx1, cy1; - if (last_cmd == 'C' || last_cmd == 'c' || last_cmd == 'S' || last_cmd == 's') - { cx1 = 2 * cur_x - last_cx; cy1 = 2 * cur_y - last_cy; } - else - { cx1 = cur_x; cy1 = cur_y; } - float x2 = fp.next_float (); - float y2 = fp.next_float (); - float x = fp.next_float (); - float y = fp.next_float (); - if (cmd == 's') - { x2 += cur_x; y2 += cur_y; x += cur_x; y += cur_y; } - hb_draw_cubic_to (dfuncs, draw_data, &st, cx1, cy1, x2, y2, x, y); - last_cx = x2; last_cy = y2; - cur_x = x; cur_y = y; - break; - } - case 'Q': case 'q': - { - float x1 = fp.next_float (); - float y1 = fp.next_float (); - float x = fp.next_float (); - float y = fp.next_float (); - if (cmd == 'q') - { x1 += cur_x; y1 += cur_y; x += cur_x; y += cur_y; } - hb_draw_quadratic_to (dfuncs, draw_data, &st, x1, y1, x, y); - last_cx = x1; last_cy = y1; - cur_x = x; cur_y = y; - break; - } - case 'T': case 't': - { - float cx1, cy1; - if (last_cmd == 'Q' || last_cmd == 'q' || last_cmd == 'T' || last_cmd == 't') - { cx1 = 2 * cur_x - last_cx; cy1 = 2 * cur_y - last_cy; } - else - { cx1 = cur_x; cy1 = cur_y; } - float x = fp.next_float (); - float y = fp.next_float (); - if (cmd == 't') { x += cur_x; y += cur_y; } - hb_draw_quadratic_to (dfuncs, draw_data, &st, cx1, cy1, x, y); - last_cx = cx1; last_cy = cy1; - cur_x = x; cur_y = y; - break; - } - case 'A': case 'a': - { - float rx = fp.next_float (); - float ry = fp.next_float (); - float x_rot = fp.next_float (); - bool large_arc = fp.next_flag (); - bool sweep = fp.next_flag (); - float x = fp.next_float (); - float y = fp.next_float (); - if (cmd == 'a') { x += cur_x; y += cur_y; } - - if (fabsf (x - cur_x) < 1e-6f && fabsf (y - cur_y) < 1e-6f) - { - cur_x = x; cur_y = y; - break; - } - - float cx, cy, theta1, dtheta, adj_rx, adj_ry; - svg_arc_endpoint_to_center (cur_x, cur_y, x, y, - rx, ry, x_rot, - large_arc, sweep, - &cx, &cy, &theta1, &dtheta, - &adj_rx, &adj_ry); - - float phi = x_rot * (float) M_PI / 180.f; - svg_arc_to_cubics (dfuncs, draw_data, &st, - cx, cy, adj_rx, adj_ry, phi, theta1, dtheta); - cur_x = x; cur_y = y; - break; - } - case 'Z': case 'z': - hb_draw_close_path (dfuncs, draw_data, &st); - cur_x = start_x; - cur_y = start_y; - break; - - default: - /* Unknown command, skip a character */ - fp.p++; - continue; - } - - last_cmd = cmd; - } -} - - -/* - * 7. Shape-to-path converters - */ - -static void -svg_rect_to_path (float x, float y, float w, float h, float rx, float ry, - hb_draw_funcs_t *dfuncs, void *draw_data) -{ - hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; - - if (rx <= 0 && ry <= 0) - { - /* Simple rectangle */ - hb_draw_move_to (dfuncs, draw_data, &st, x, y); - hb_draw_line_to (dfuncs, draw_data, &st, x + w, y); - hb_draw_line_to (dfuncs, draw_data, &st, x + w, y + h); - hb_draw_line_to (dfuncs, draw_data, &st, x, y + h); - hb_draw_close_path (dfuncs, draw_data, &st); - return; - } - - /* Rounded rectangle */ - if (rx <= 0) rx = ry; - if (ry <= 0) ry = rx; - rx = hb_min (rx, w / 2); - ry = hb_min (ry, h / 2); - - /* Kappa for circular arcs approximated by cubic bezier */ - float kx = rx * 0.5522847498f; - float ky = ry * 0.5522847498f; - - hb_draw_move_to (dfuncs, draw_data, &st, x + rx, y); - hb_draw_line_to (dfuncs, draw_data, &st, x + w - rx, y); - hb_draw_cubic_to (dfuncs, draw_data, &st, - x + w - rx + kx, y, - x + w, y + ry - ky, - x + w, y + ry); - hb_draw_line_to (dfuncs, draw_data, &st, x + w, y + h - ry); - hb_draw_cubic_to (dfuncs, draw_data, &st, - x + w, y + h - ry + ky, - x + w - rx + kx, y + h, - x + w - rx, y + h); - hb_draw_line_to (dfuncs, draw_data, &st, x + rx, y + h); - hb_draw_cubic_to (dfuncs, draw_data, &st, - x + rx - kx, y + h, - x, y + h - ry + ky, - x, y + h - ry); - hb_draw_line_to (dfuncs, draw_data, &st, x, y + ry); - hb_draw_cubic_to (dfuncs, draw_data, &st, - x, y + ry - ky, - x + rx - kx, y, - x + rx, y); - hb_draw_close_path (dfuncs, draw_data, &st); -} - -static void -svg_circle_to_path (float cx, float cy, float r, - hb_draw_funcs_t *dfuncs, void *draw_data) -{ - hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; - float k = r * 0.5522847498f; - - hb_draw_move_to (dfuncs, draw_data, &st, cx + r, cy); - hb_draw_cubic_to (dfuncs, draw_data, &st, - cx + r, cy + k, - cx + k, cy + r, - cx, cy + r); - hb_draw_cubic_to (dfuncs, draw_data, &st, - cx - k, cy + r, - cx - r, cy + k, - cx - r, cy); - hb_draw_cubic_to (dfuncs, draw_data, &st, - cx - r, cy - k, - cx - k, cy - r, - cx, cy - r); - hb_draw_cubic_to (dfuncs, draw_data, &st, - cx + k, cy - r, - cx + r, cy - k, - cx + r, cy); - hb_draw_close_path (dfuncs, draw_data, &st); -} - -static void -svg_ellipse_to_path (float cx, float cy, float rx, float ry, - hb_draw_funcs_t *dfuncs, void *draw_data) -{ - hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; - float kx = rx * 0.5522847498f; - float ky = ry * 0.5522847498f; - - hb_draw_move_to (dfuncs, draw_data, &st, cx + rx, cy); - hb_draw_cubic_to (dfuncs, draw_data, &st, - cx + rx, cy + ky, - cx + kx, cy + ry, - cx, cy + ry); - hb_draw_cubic_to (dfuncs, draw_data, &st, - cx - kx, cy + ry, - cx - rx, cy + ky, - cx - rx, cy); - hb_draw_cubic_to (dfuncs, draw_data, &st, - cx - rx, cy - ky, - cx - kx, cy - ry, - cx, cy - ry); - hb_draw_cubic_to (dfuncs, draw_data, &st, - cx + kx, cy - ry, - cx + rx, cy - ky, - cx + rx, cy); - hb_draw_close_path (dfuncs, draw_data, &st); -} - -static void -svg_line_to_path (float x1, float y1, float x2, float y2, - hb_draw_funcs_t *dfuncs, void *draw_data) -{ - hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; - hb_draw_move_to (dfuncs, draw_data, &st, x1, y1); - hb_draw_line_to (dfuncs, draw_data, &st, x2, y2); -} - -static void -svg_polygon_to_path (hb_svg_str_t points, bool close, - hb_draw_funcs_t *dfuncs, void *draw_data) -{ - hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; - hb_svg_float_parser_t fp (points); - bool first = true; - while (fp.has_more ()) - { - float x = fp.next_float (); - float y = fp.next_float (); - if (first) - { - hb_draw_move_to (dfuncs, draw_data, &st, x, y); - first = false; - } - else - hb_draw_line_to (dfuncs, draw_data, &st, x, y); - } - if (close && !first) - hb_draw_close_path (dfuncs, draw_data, &st); -} - - -/* - * 8. Shape descriptor - */ - -struct hb_svg_shape_emit_data_t -{ - enum { SHAPE_PATH, SHAPE_RECT, SHAPE_CIRCLE, SHAPE_ELLIPSE, - SHAPE_LINE, SHAPE_POLYLINE, SHAPE_POLYGON } type; - hb_svg_str_t str_data; /* for path 'd' or polyline/polygon 'points' */ - float params[6]; /* for rect/circle/ellipse/line */ -}; - static bool svg_parse_paint_url_with_fallback (hb_svg_str_t s, char out_id[64], hb_svg_str_t *fallback); - -/* - * 9. Defs store - */ - enum hb_svg_gradient_type_t { SVG_GRADIENT_LINEAR, @@ -1767,127 +928,6 @@ svg_emit_fill (hb_svg_render_context_t *ctx, } -/* - * Shape path emitter callback - */ - -static void -svg_shape_path_emit (hb_draw_funcs_t *dfuncs, void *draw_data, void *user_data) -{ - hb_svg_shape_emit_data_t *shape = (hb_svg_shape_emit_data_t *) user_data; - switch (shape->type) - { - case hb_svg_shape_emit_data_t::SHAPE_PATH: - svg_parse_path_data (shape->str_data, dfuncs, draw_data); - break; - case hb_svg_shape_emit_data_t::SHAPE_RECT: - svg_rect_to_path (shape->params[0], shape->params[1], - shape->params[2], shape->params[3], - shape->params[4], shape->params[5], - dfuncs, draw_data); - break; - case hb_svg_shape_emit_data_t::SHAPE_CIRCLE: - svg_circle_to_path (shape->params[0], shape->params[1], shape->params[2], - dfuncs, draw_data); - break; - case hb_svg_shape_emit_data_t::SHAPE_ELLIPSE: - svg_ellipse_to_path (shape->params[0], shape->params[1], - shape->params[2], shape->params[3], - dfuncs, draw_data); - break; - case hb_svg_shape_emit_data_t::SHAPE_LINE: - svg_line_to_path (shape->params[0], shape->params[1], - shape->params[2], shape->params[3], - dfuncs, draw_data); - break; - case hb_svg_shape_emit_data_t::SHAPE_POLYLINE: - svg_polygon_to_path (shape->str_data, false, dfuncs, draw_data); - break; - case hb_svg_shape_emit_data_t::SHAPE_POLYGON: - svg_polygon_to_path (shape->str_data, true, dfuncs, draw_data); - break; - } -} - -static bool -svg_parse_shape_tag (hb_svg_xml_parser_t &parser, - hb_svg_shape_emit_data_t *shape) -{ - hb_svg_str_t tag = parser.tag_name; - if (tag.eq ("path")) - { - hb_svg_str_t d = parser.find_attr ("d"); - if (!d.len) return false; - shape->type = hb_svg_shape_emit_data_t::SHAPE_PATH; - shape->str_data = d; - return true; - } - if (tag.eq ("rect")) - { - float w = svg_parse_float (parser.find_attr ("width")); - float h = svg_parse_float (parser.find_attr ("height")); - if (w <= 0 || h <= 0) return false; - shape->type = hb_svg_shape_emit_data_t::SHAPE_RECT; - shape->params[0] = svg_parse_float (parser.find_attr ("x")); - shape->params[1] = svg_parse_float (parser.find_attr ("y")); - shape->params[2] = w; - shape->params[3] = h; - shape->params[4] = svg_parse_float (parser.find_attr ("rx")); - shape->params[5] = svg_parse_float (parser.find_attr ("ry")); - return true; - } - if (tag.eq ("circle")) - { - float r = svg_parse_float (parser.find_attr ("r")); - if (r <= 0) return false; - shape->type = hb_svg_shape_emit_data_t::SHAPE_CIRCLE; - shape->params[0] = svg_parse_float (parser.find_attr ("cx")); - shape->params[1] = svg_parse_float (parser.find_attr ("cy")); - shape->params[2] = r; - return true; - } - if (tag.eq ("ellipse")) - { - float rx = svg_parse_float (parser.find_attr ("rx")); - float ry = svg_parse_float (parser.find_attr ("ry")); - if (rx <= 0 || ry <= 0) return false; - shape->type = hb_svg_shape_emit_data_t::SHAPE_ELLIPSE; - shape->params[0] = svg_parse_float (parser.find_attr ("cx")); - shape->params[1] = svg_parse_float (parser.find_attr ("cy")); - shape->params[2] = rx; - shape->params[3] = ry; - return true; - } - if (tag.eq ("line")) - { - shape->type = hb_svg_shape_emit_data_t::SHAPE_LINE; - shape->params[0] = svg_parse_float (parser.find_attr ("x1")); - shape->params[1] = svg_parse_float (parser.find_attr ("y1")); - shape->params[2] = svg_parse_float (parser.find_attr ("x2")); - shape->params[3] = svg_parse_float (parser.find_attr ("y2")); - return true; - } - if (tag.eq ("polyline")) - { - hb_svg_str_t points = parser.find_attr ("points"); - if (!points.len) return false; - shape->type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; - shape->str_data = points; - return true; - } - if (tag.eq ("polygon")) - { - hb_svg_str_t points = parser.find_attr ("points"); - if (!points.len) return false; - shape->type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; - shape->str_data = points; - return true; - } - return false; -} - - - /* * 11. Element renderer — recursive SVG rendering */ diff --git a/src/meson.build b/src/meson.build index 238f1cc95..eec7d8a68 100644 --- a/src/meson.build +++ b/src/meson.build @@ -707,6 +707,8 @@ hb_raster_sources = [ 'hb-raster-draw.cc', 'hb-raster-svg-base.cc', 'hb-raster-svg-base.hh', + 'hb-raster-svg-parse.cc', + 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', 'hb-raster-paint.hh', 'hb-raster-svg.cc', From 0549b109f011f613b73b5dc2ca6acc179162f6d6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:49:06 -0300 Subject: [PATCH 012/105] [raster] Split SVG defs store into hb-raster-svg-defs --- src/hb-raster-svg-defs.cc | 147 +++++++++++++++++++++++ src/hb-raster-svg-defs.hh | 116 ++++++++++++++++++ src/hb-raster-svg.cc | 243 +++----------------------------------- src/meson.build | 2 + 4 files changed, 284 insertions(+), 224 deletions(-) create mode 100644 src/hb-raster-svg-defs.cc create mode 100644 src/hb-raster-svg-defs.hh diff --git a/src/hb-raster-svg-defs.cc b/src/hb-raster-svg-defs.cc new file mode 100644 index 000000000..6baa5fdf7 --- /dev/null +++ b/src/hb-raster-svg-defs.cc @@ -0,0 +1,147 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-defs.hh" + +#include + +hb_svg_defs_t::~hb_svg_defs_t () +{ + for (unsigned i = 0; i < owned_id_strings.length; i++) + hb_free (owned_id_strings.arrayZ[i]); +} + +bool +hb_svg_defs_t::add_id_mapping (hb_hashmap_t *map, + const char *id, + unsigned idx) +{ + hb_bytes_t key = hb_bytes_t (id, (unsigned) strlen (id)); + if (map->has (key)) + return true; + + unsigned n = (unsigned) strlen (id); + char *owned = (char *) hb_malloc (n + 1); + if (unlikely (!owned)) + return false; + hb_memcpy (owned, id, n + 1); + if (unlikely (!owned_id_strings.push (owned))) + { + hb_free (owned); + return false; + } + + hb_bytes_t owned_key = hb_bytes_t (owned, n); + if (unlikely (!map->set (owned_key, idx))) + return false; + return true; +} + +bool +hb_svg_defs_t::add_gradient (const char *id, const hb_svg_gradient_t &grad) +{ + unsigned idx = gradients.length; + gradients.push (grad); + if (unlikely (gradients.in_error ())) + return false; + + hb_svg_def_t def; + unsigned n = hb_min (strlen (id), (size_t) sizeof (def.id) - 1); + memcpy (def.id, id, n); + def.id[n] = '\0'; + def.type = hb_svg_def_t::DEF_GRADIENT; + def.index = idx; + defs.push (def); + if (unlikely (defs.in_error ())) + { + gradients.pop (); + return false; + } + if (unlikely (!add_id_mapping (&gradient_by_id, def.id, idx))) + { + defs.pop (); + gradients.pop (); + return false; + } + return true; +} + +const hb_svg_gradient_t * +hb_svg_defs_t::find_gradient (const char *id) const +{ + unsigned *idx = nullptr; + if (id && gradient_by_id.has (hb_bytes_t (id, (unsigned) strlen (id)), &idx)) + return &gradients[*idx]; + for (unsigned i = 0; i < defs.length; i++) + if (defs[i].type == hb_svg_def_t::DEF_GRADIENT && + strcmp (defs[i].id, id) == 0) + return &gradients[defs[i].index]; + return nullptr; +} + +bool +hb_svg_defs_t::add_clip_path (const char *id, const hb_svg_clip_path_def_t &clip) +{ + unsigned idx = clip_paths.length; + clip_paths.push (clip); + if (unlikely (clip_paths.in_error ())) + return false; + + hb_svg_def_t def; + unsigned n = hb_min (strlen (id), (size_t) sizeof (def.id) - 1); + memcpy (def.id, id, n); + def.id[n] = '\0'; + def.type = hb_svg_def_t::DEF_CLIP_PATH; + def.index = idx; + defs.push (def); + if (unlikely (defs.in_error ())) + { + clip_paths.pop (); + return false; + } + if (unlikely (!add_id_mapping (&clip_path_by_id, def.id, idx))) + { + defs.pop (); + clip_paths.pop (); + return false; + } + return true; +} + +const hb_svg_clip_path_def_t * +hb_svg_defs_t::find_clip_path (const char *id) const +{ + unsigned *idx = nullptr; + if (id && clip_path_by_id.has (hb_bytes_t (id, (unsigned) strlen (id)), &idx)) + return &clip_paths[*idx]; + for (unsigned i = 0; i < defs.length; i++) + if (defs[i].type == hb_svg_def_t::DEF_CLIP_PATH && + strcmp (defs[i].id, id) == 0) + return &clip_paths[defs[i].index]; + return nullptr; +} diff --git a/src/hb-raster-svg-defs.hh b/src/hb-raster-svg-defs.hh new file mode 100644 index 000000000..92780e70e --- /dev/null +++ b/src/hb-raster-svg-defs.hh @@ -0,0 +1,116 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_DEFS_HH +#define HB_RASTER_SVG_DEFS_HH + +#include "hb.hh" + +#include "hb-map.hh" +#include "hb-raster-svg-parse.hh" + +enum hb_svg_gradient_type_t +{ + SVG_GRADIENT_LINEAR, + SVG_GRADIENT_RADIAL +}; + +struct hb_svg_gradient_stop_t +{ + float offset; + hb_color_t color; + bool is_current_color = false; +}; + +struct hb_svg_gradient_t +{ + hb_svg_gradient_type_t type; + + hb_paint_extend_t spread = HB_PAINT_EXTEND_PAD; + bool has_spread = false; + hb_svg_transform_t gradient_transform; + bool has_gradient_transform = false; + bool units_user_space = false; + bool has_units_user_space = false; + + float x1 = 0, y1 = 0, x2 = 1, y2 = 0; + bool has_x1 = false, has_y1 = false, has_x2 = false, has_y2 = false; + + float cx = 0.5f, cy = 0.5f, r = 0.5f; + float fx = -1.f, fy = -1.f; + bool has_cx = false, has_cy = false, has_r = false; + bool has_fx = false, has_fy = false; + + hb_vector_t stops; + + char href_id[64] = {}; +}; + +struct hb_svg_clip_path_def_t +{ + unsigned first_shape = 0; + unsigned shape_count = 0; + hb_svg_transform_t clip_transform; + bool has_clip_transform = false; + bool units_user_space = true; +}; + +struct hb_svg_clip_shape_t +{ + hb_svg_shape_emit_data_t shape; + hb_svg_transform_t transform; + bool has_transform = false; +}; + +struct hb_svg_def_t +{ + char id[64]; + enum { DEF_GRADIENT, DEF_CLIP_PATH, DEF_ELEMENT } type; + unsigned index; +}; + +struct hb_svg_defs_t +{ + hb_vector_t gradients; + hb_vector_t clip_shapes; + hb_vector_t clip_paths; + hb_vector_t defs; + hb_hashmap_t gradient_by_id; + hb_hashmap_t clip_path_by_id; + hb_vector_t owned_id_strings; + + ~hb_svg_defs_t (); + + bool add_id_mapping (hb_hashmap_t *map, + const char *id, + unsigned idx); + bool add_gradient (const char *id, const hb_svg_gradient_t &grad); + const hb_svg_gradient_t *find_gradient (const char *id) const; + bool add_clip_path (const char *id, const hb_svg_clip_path_def_t &clip); + const hb_svg_clip_path_def_t *find_clip_path (const char *id) const; +}; + +#endif /* HB_RASTER_SVG_DEFS_HH */ diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 0819954f3..839dc57d6 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -32,6 +32,7 @@ #include "hb-raster-svg.hh" #include "hb-raster-svg-base.hh" #include "hb-raster-svg-parse.hh" +#include "hb-raster-svg-defs.hh" #include "OT/Color/svg/svg.hh" #include "hb-draw.h" #include "hb-ot-color.h" @@ -365,226 +366,6 @@ svg_parse_paint_url_with_fallback (hb_svg_str_t s, char out_id[64], hb_svg_str_t *fallback); -enum hb_svg_gradient_type_t -{ - SVG_GRADIENT_LINEAR, - SVG_GRADIENT_RADIAL -}; - -struct hb_svg_gradient_stop_t -{ - float offset; - hb_color_t color; - bool is_current_color = false; -}; - -struct hb_svg_gradient_t -{ - hb_svg_gradient_type_t type; - - /* Common */ - hb_paint_extend_t spread = HB_PAINT_EXTEND_PAD; - bool has_spread = false; - hb_svg_transform_t gradient_transform; - bool has_gradient_transform = false; - bool units_user_space = false; /* false = objectBoundingBox (default) */ - bool has_units_user_space = false; - - /* Linear gradient: x1, y1, x2, y2 */ - float x1 = 0, y1 = 0, x2 = 1, y2 = 0; - bool has_x1 = false, has_y1 = false, has_x2 = false, has_y2 = false; - - /* Radial gradient: cx, cy, r, fx, fy */ - float cx = 0.5f, cy = 0.5f, r = 0.5f; - float fx = -1.f, fy = -1.f; /* -1 = not set, use cx/cy */ - bool has_cx = false, has_cy = false, has_r = false; - bool has_fx = false, has_fy = false; - - hb_vector_t stops; - - /* href reference to another gradient */ - char href_id[64] = {}; -}; - -struct hb_svg_clip_path_def_t -{ - unsigned first_shape = 0; - unsigned shape_count = 0; - hb_svg_transform_t clip_transform; - bool has_clip_transform = false; - bool units_user_space = true; /* default per SVG */ -}; - -struct hb_svg_clip_shape_t -{ - hb_svg_shape_emit_data_t shape; - hb_svg_transform_t transform; - bool has_transform = false; -}; - -struct hb_svg_def_t -{ - char id[64]; - enum { DEF_GRADIENT, DEF_CLIP_PATH, DEF_ELEMENT } type; - unsigned index; /* index into respective array */ -}; - -struct hb_svg_defs_t -{ - hb_vector_t gradients; - hb_vector_t clip_shapes; - hb_vector_t clip_paths; - hb_vector_t defs; - hb_hashmap_t gradient_by_id; - hb_hashmap_t clip_path_by_id; - hb_vector_t owned_id_strings; - - ~hb_svg_defs_t () - { - for (unsigned i = 0; i < owned_id_strings.length; i++) - hb_free (owned_id_strings.arrayZ[i]); - } - - bool add_id_mapping (hb_hashmap_t *map, - const char *id, - unsigned idx) - { - hb_bytes_t key = hb_bytes_t (id, (unsigned) strlen (id)); - if (map->has (key)) - return true; - - unsigned n = (unsigned) strlen (id); - char *owned = (char *) hb_malloc (n + 1); - if (unlikely (!owned)) - return false; - hb_memcpy (owned, id, n + 1); - if (unlikely (!owned_id_strings.push (owned))) - { - hb_free (owned); - return false; - } - - hb_bytes_t owned_key = hb_bytes_t (owned, n); - if (unlikely (!map->set (owned_key, idx))) - return false; - return true; - } - - bool add_gradient (const char *id, const hb_svg_gradient_t &grad) - { - unsigned idx = gradients.length; - gradients.push (grad); - if (unlikely (gradients.in_error ())) - return false; - - hb_svg_def_t def; - unsigned n = hb_min (strlen (id), (size_t) sizeof (def.id) - 1); - memcpy (def.id, id, n); - def.id[n] = '\0'; - def.type = hb_svg_def_t::DEF_GRADIENT; - def.index = idx; - defs.push (def); - if (unlikely (defs.in_error ())) - { - gradients.pop (); - return false; - } - if (unlikely (!add_id_mapping (&gradient_by_id, def.id, idx))) - { - defs.pop (); - gradients.pop (); - return false; - } - return true; - } - - const hb_svg_gradient_t *find_gradient (const char *id) const - { - unsigned *idx = nullptr; - if (id && gradient_by_id.has (hb_bytes_t (id, (unsigned) strlen (id)), &idx)) - return &gradients[*idx]; - for (unsigned i = 0; i < defs.length; i++) - if (defs[i].type == hb_svg_def_t::DEF_GRADIENT && - strcmp (defs[i].id, id) == 0) - return &gradients[defs[i].index]; - return nullptr; - } - - bool add_clip_path (const char *id, const hb_svg_clip_path_def_t &clip) - { - unsigned idx = clip_paths.length; - clip_paths.push (clip); - if (unlikely (clip_paths.in_error ())) - return false; - - hb_svg_def_t def; - unsigned n = hb_min (strlen (id), (size_t) sizeof (def.id) - 1); - memcpy (def.id, id, n); - def.id[n] = '\0'; - def.type = hb_svg_def_t::DEF_CLIP_PATH; - def.index = idx; - defs.push (def); - if (unlikely (defs.in_error ())) - { - clip_paths.pop (); - return false; - } - if (unlikely (!add_id_mapping (&clip_path_by_id, def.id, idx))) - { - defs.pop (); - clip_paths.pop (); - return false; - } - return true; - } - - const hb_svg_clip_path_def_t *find_clip_path (const char *id) const - { - unsigned *idx = nullptr; - if (id && clip_path_by_id.has (hb_bytes_t (id, (unsigned) strlen (id)), &idx)) - return &clip_paths[*idx]; - for (unsigned i = 0; i < defs.length; i++) - if (defs[i].type == hb_svg_def_t::DEF_CLIP_PATH && - strcmp (defs[i].id, id) == 0) - return &clip_paths[defs[i].index]; - return nullptr; - } - - const hb_svg_clip_path_def_t *find_clip_path_str (hb_svg_str_t s) const - { - char id[64]; - if (!parse_paint_server_id (s, id)) - return nullptr; - return find_clip_path (id); - } - - const hb_svg_gradient_t *find_gradient_str (hb_svg_str_t s) const - { - char id[64]; - if (!parse_paint_server_id (s, id)) - return nullptr; - return find_gradient (id); - } - - static bool parse_paint_server_id (hb_svg_str_t s, char out[64]) - { - return svg_parse_paint_url_with_fallback (s, out, nullptr); - } - - static bool parse_fragment_id (hb_svg_str_t s, char out[64]) - { - s = s.trim (); - if (!s.len || s.data[0] != '#') - return false; - - unsigned n = hb_min (s.len - 1, (unsigned) 63); - memcpy (out, s.data + 1, n); - out[n] = '\0'; - return true; - } -}; - - /* * 10. Gradient handler — construct hb_color_line_t */ @@ -770,6 +551,18 @@ svg_parse_paint_url_with_fallback (hb_svg_str_t s, return true; } +static bool +svg_parse_fragment_id (hb_svg_str_t s, char out_id[64]) +{ + s = s.trim (); + if (!s.len || s.data[0] != '#') + return false; + unsigned n = hb_min (s.len - 1, (unsigned) 63); + memcpy (out_id, s.data + 1, n); + out_id[n] = '\0'; + return true; +} + /* * Emit fill (solid or gradient) @@ -789,8 +582,7 @@ svg_emit_fill (hb_svg_render_context_t *ctx, bool has_url_paint = svg_parse_paint_url_with_fallback (fill_str, url_id, &fallback_paint); /* Check for gradient reference */ - const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs.find_gradient (url_id) - : ctx->defs.find_gradient_str (fill_str); + const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs.find_gradient (url_id) : nullptr; if (has_url_paint && !grad) { if (fallback_paint.len) @@ -1326,7 +1118,10 @@ svg_push_clip_path_ref (hb_svg_render_context_t *ctx, hb_svg_str_t trimmed = clip_path_str.trim (); if (!trimmed.len || trimmed.eq ("none")) return false; - const hb_svg_clip_path_def_t *clip = ctx->defs.find_clip_path_str (trimmed); + char clip_id[64]; + if (!svg_parse_paint_url_with_fallback (trimmed, clip_id, nullptr)) + return false; + const hb_svg_clip_path_def_t *clip = ctx->defs.find_clip_path (clip_id); if (!clip || !clip->shape_count) return false; hb_svg_clip_emit_data_t ed; @@ -1706,7 +1501,7 @@ svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_str_t href = svg_find_href_attr (parser); char ref_id[64]; - if (!hb_svg_defs_t::parse_fragment_id (href, ref_id)) + if (!svg_parse_fragment_id (href, ref_id)) return; float use_x = svg_parse_float (parser.find_attr ("x")); diff --git a/src/meson.build b/src/meson.build index eec7d8a68..0ac973065 100644 --- a/src/meson.build +++ b/src/meson.build @@ -707,6 +707,8 @@ hb_raster_sources = [ 'hb-raster-draw.cc', 'hb-raster-svg-base.cc', 'hb-raster-svg-base.hh', + 'hb-raster-svg-defs.cc', + 'hb-raster-svg-defs.hh', 'hb-raster-svg-parse.cc', 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', From fea3052694e1cd04a6d4210cc5e10fb050241248 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:50:05 -0300 Subject: [PATCH 013/105] [raster] Add cached SVG attribute view helper --- src/hb-raster-svg-parse.cc | 41 +++++++++++++++++++------------------- src/hb-raster-svg-parse.hh | 32 +++++++++++++++++++++++++++++ src/hb-raster-svg.cc | 29 +++++++++++++++------------ 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc index d75050737..26b4cbfbb 100644 --- a/src/hb-raster-svg-parse.cc +++ b/src/hb-raster-svg-parse.cc @@ -577,10 +577,11 @@ bool svg_parse_shape_tag (hb_svg_xml_parser_t &parser, hb_svg_shape_emit_data_t *shape) { + hb_svg_attr_view_t attrs (parser); hb_svg_str_t tag = parser.tag_name; if (tag.eq ("path")) { - hb_svg_str_t d = parser.find_attr ("d"); + hb_svg_str_t d = attrs.get ("d"); if (!d.len) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_PATH; shape->str_data = d; @@ -588,36 +589,36 @@ svg_parse_shape_tag (hb_svg_xml_parser_t &parser, } if (tag.eq ("rect")) { - float w = svg_parse_float (parser.find_attr ("width")); - float h = svg_parse_float (parser.find_attr ("height")); + float w = svg_parse_float (attrs.get ("width")); + float h = svg_parse_float (attrs.get ("height")); if (w <= 0 || h <= 0) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_RECT; - shape->params[0] = svg_parse_float (parser.find_attr ("x")); - shape->params[1] = svg_parse_float (parser.find_attr ("y")); + shape->params[0] = svg_parse_float (attrs.get ("x")); + shape->params[1] = svg_parse_float (attrs.get ("y")); shape->params[2] = w; shape->params[3] = h; - shape->params[4] = svg_parse_float (parser.find_attr ("rx")); - shape->params[5] = svg_parse_float (parser.find_attr ("ry")); + shape->params[4] = svg_parse_float (attrs.get ("rx")); + shape->params[5] = svg_parse_float (attrs.get ("ry")); return true; } if (tag.eq ("circle")) { - float r = svg_parse_float (parser.find_attr ("r")); + float r = svg_parse_float (attrs.get ("r")); if (r <= 0) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_CIRCLE; - shape->params[0] = svg_parse_float (parser.find_attr ("cx")); - shape->params[1] = svg_parse_float (parser.find_attr ("cy")); + shape->params[0] = svg_parse_float (attrs.get ("cx")); + shape->params[1] = svg_parse_float (attrs.get ("cy")); shape->params[2] = r; return true; } if (tag.eq ("ellipse")) { - float rx = svg_parse_float (parser.find_attr ("rx")); - float ry = svg_parse_float (parser.find_attr ("ry")); + float rx = svg_parse_float (attrs.get ("rx")); + float ry = svg_parse_float (attrs.get ("ry")); if (rx <= 0 || ry <= 0) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_ELLIPSE; - shape->params[0] = svg_parse_float (parser.find_attr ("cx")); - shape->params[1] = svg_parse_float (parser.find_attr ("cy")); + shape->params[0] = svg_parse_float (attrs.get ("cx")); + shape->params[1] = svg_parse_float (attrs.get ("cy")); shape->params[2] = rx; shape->params[3] = ry; return true; @@ -625,15 +626,15 @@ svg_parse_shape_tag (hb_svg_xml_parser_t &parser, if (tag.eq ("line")) { shape->type = hb_svg_shape_emit_data_t::SHAPE_LINE; - shape->params[0] = svg_parse_float (parser.find_attr ("x1")); - shape->params[1] = svg_parse_float (parser.find_attr ("y1")); - shape->params[2] = svg_parse_float (parser.find_attr ("x2")); - shape->params[3] = svg_parse_float (parser.find_attr ("y2")); + shape->params[0] = svg_parse_float (attrs.get ("x1")); + shape->params[1] = svg_parse_float (attrs.get ("y1")); + shape->params[2] = svg_parse_float (attrs.get ("x2")); + shape->params[3] = svg_parse_float (attrs.get ("y2")); return true; } if (tag.eq ("polyline")) { - hb_svg_str_t points = parser.find_attr ("points"); + hb_svg_str_t points = attrs.get ("points"); if (!points.len) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; shape->str_data = points; @@ -641,7 +642,7 @@ svg_parse_shape_tag (hb_svg_xml_parser_t &parser, } if (tag.eq ("polygon")) { - hb_svg_str_t points = parser.find_attr ("points"); + hb_svg_str_t points = attrs.get ("points"); if (!points.len) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; shape->str_data = points; diff --git a/src/hb-raster-svg-parse.hh b/src/hb-raster-svg-parse.hh index 251a82c54..a18f15bb8 100644 --- a/src/hb-raster-svg-parse.hh +++ b/src/hb-raster-svg-parse.hh @@ -33,6 +33,7 @@ #include "hb-draw.h" #include +#include enum hb_svg_token_type_t { @@ -201,6 +202,37 @@ svg_pick_attr_or_style (const hb_svg_xml_parser_t &parser, return style_value.is_null () ? parser.find_attr (attr_name) : style_value; } +struct hb_svg_attr_view_t +{ + const hb_svg_xml_parser_t &parser; + enum { CACHE_SIZE = 16 }; + struct entry_t + { + const char *name = nullptr; + hb_svg_str_t value; + }; + entry_t cache[CACHE_SIZE]; + unsigned cache_len = 0; + + hb_svg_attr_view_t (const hb_svg_xml_parser_t &p) : parser (p) {} + + hb_svg_str_t get (const char *name) + { + for (unsigned i = 0; i < cache_len; i++) + if (strcmp (cache[i].name, name) == 0) + return cache[i].value; + + hb_svg_str_t value = parser.find_attr (name); + if (cache_len < CACHE_SIZE) + { + cache[cache_len].name = name; + cache[cache_len].value = value; + cache_len++; + } + return value; + } +}; + struct hb_svg_transform_t { float xx = 1, yx = 0, xy = 0, yy = 1, dx = 0, dy = 0; diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 839dc57d6..817c9c1fc 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -738,7 +738,8 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, hb_face_t *face, unsigned palette) { - hb_svg_str_t style = parser.find_attr ("style"); + hb_svg_attr_view_t attrs (parser); + hb_svg_str_t style = attrs.get ("style"); hb_svg_style_props_t style_props; svg_parse_style_props (style, &style_props); hb_svg_str_t offset_str = svg_pick_attr_or_style (parser, style_props.offset, "offset"); @@ -789,7 +790,8 @@ static void svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, hb_svg_gradient_t &grad) { - hb_svg_str_t spread_str = parser.find_attr ("spreadMethod"); + hb_svg_attr_view_t attrs (parser); + hb_svg_str_t spread_str = attrs.get ("spreadMethod"); if (spread_str.eq ("reflect")) { grad.spread = HB_PAINT_EXTEND_REFLECT; @@ -806,7 +808,7 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, grad.has_spread = true; } - hb_svg_str_t units_str = parser.find_attr ("gradientUnits"); + hb_svg_str_t units_str = attrs.get ("gradientUnits"); if (units_str.eq ("userSpaceOnUse")) { grad.units_user_space = true; @@ -818,7 +820,7 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, grad.has_units_user_space = true; } - hb_svg_str_t transform_str = parser.find_attr ("gradientTransform"); + hb_svg_str_t transform_str = attrs.get ("gradientTransform"); if (transform_str.len) { grad.has_gradient_transform = true; @@ -838,12 +840,13 @@ static void svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, hb_svg_gradient_t &grad) { + hb_svg_attr_view_t attrs (parser); if (grad.type == SVG_GRADIENT_LINEAR) { - hb_svg_str_t x1_str = parser.find_attr ("x1"); - hb_svg_str_t y1_str = parser.find_attr ("y1"); - hb_svg_str_t x2_str = parser.find_attr ("x2"); - hb_svg_str_t y2_str = parser.find_attr ("y2"); + hb_svg_str_t x1_str = attrs.get ("x1"); + hb_svg_str_t y1_str = attrs.get ("y1"); + hb_svg_str_t x2_str = attrs.get ("x2"); + hb_svg_str_t y2_str = attrs.get ("y2"); if (x1_str.len) { grad.x1 = svg_parse_number_or_percent (x1_str, nullptr); grad.has_x1 = true; } if (y1_str.len) { grad.y1 = svg_parse_number_or_percent (y1_str, nullptr); grad.has_y1 = true; } if (x2_str.len) { grad.x2 = svg_parse_number_or_percent (x2_str, nullptr); grad.has_x2 = true; } @@ -855,11 +858,11 @@ svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, } else /* SVG_GRADIENT_RADIAL */ { - hb_svg_str_t cx_str = parser.find_attr ("cx"); - hb_svg_str_t cy_str = parser.find_attr ("cy"); - hb_svg_str_t r_str = parser.find_attr ("r"); - hb_svg_str_t fx_str = parser.find_attr ("fx"); - hb_svg_str_t fy_str = parser.find_attr ("fy"); + hb_svg_str_t cx_str = attrs.get ("cx"); + hb_svg_str_t cy_str = attrs.get ("cy"); + hb_svg_str_t r_str = attrs.get ("r"); + hb_svg_str_t fx_str = attrs.get ("fx"); + hb_svg_str_t fy_str = attrs.get ("fy"); if (cx_str.len) { grad.cx = svg_parse_number_or_percent (cx_str, nullptr); grad.has_cx = true; } if (cy_str.len) { grad.cy = svg_parse_number_or_percent (cy_str, nullptr); grad.has_cy = true; } From 209ccf6a7b2a7611c8eab3916a1a22e5fe607569 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:50:54 -0300 Subject: [PATCH 014/105] [raster] Unify SVG id reference parsing --- src/hb-raster-svg.cc | 47 +++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 817c9c1fc..e2d8eaad4 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -362,9 +362,9 @@ svg_parse_color (hb_svg_str_t s, */ static bool -svg_parse_paint_url_with_fallback (hb_svg_str_t s, - char out_id[64], - hb_svg_str_t *fallback); +svg_parse_id_ref_with_fallback (hb_svg_str_t s, + char out_id[64], + hb_svg_str_t *fallback); /* * 10. Gradient handler — construct hb_color_line_t @@ -491,12 +491,21 @@ struct hb_svg_cascade_t }; static bool -svg_parse_paint_url_with_fallback (hb_svg_str_t s, - char out_id[64], - hb_svg_str_t *fallback) +svg_parse_id_ref_with_fallback (hb_svg_str_t s, + char out_id[64], + hb_svg_str_t *fallback) { if (fallback) *fallback = {}; s = s.trim (); + + if (s.len && s.data[0] == '#') + { + unsigned n = hb_min (s.len - 1, (unsigned) 63); + memcpy (out_id, s.data + 1, n); + out_id[n] = '\0'; + return n > 0; + } + if (!svg_str_starts_with_ascii_ci (s, "url(")) return false; @@ -551,18 +560,6 @@ svg_parse_paint_url_with_fallback (hb_svg_str_t s, return true; } -static bool -svg_parse_fragment_id (hb_svg_str_t s, char out_id[64]) -{ - s = s.trim (); - if (!s.len || s.data[0] != '#') - return false; - unsigned n = hb_min (s.len - 1, (unsigned) 63); - memcpy (out_id, s.data + 1, n); - out_id[n] = '\0'; - return true; -} - /* * Emit fill (solid or gradient) @@ -579,7 +576,7 @@ svg_emit_fill (hb_svg_render_context_t *ctx, char url_id[64]; hb_svg_str_t fallback_paint; - bool has_url_paint = svg_parse_paint_url_with_fallback (fill_str, url_id, &fallback_paint); + bool has_url_paint = svg_parse_id_ref_with_fallback (fill_str, url_id, &fallback_paint); /* Check for gradient reference */ const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs.find_gradient (url_id) : nullptr; @@ -828,12 +825,8 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, } hb_svg_str_t href = svg_find_href_attr (parser); - if (href.len && href.data[0] == '#') - { - unsigned n = hb_min (href.len - 1, (unsigned) sizeof (grad.href_id) - 1); - memcpy (grad.href_id, href.data + 1, n); - grad.href_id[n] = '\0'; - } + if (href.len) + (void) svg_parse_id_ref_with_fallback (href, grad.href_id, nullptr); } static void @@ -1122,7 +1115,7 @@ svg_push_clip_path_ref (hb_svg_render_context_t *ctx, if (!trimmed.len || trimmed.eq ("none")) return false; char clip_id[64]; - if (!svg_parse_paint_url_with_fallback (trimmed, clip_id, nullptr)) + if (!svg_parse_id_ref_with_fallback (trimmed, clip_id, nullptr)) return false; const hb_svg_clip_path_def_t *clip = ctx->defs.find_clip_path (clip_id); if (!clip || !clip->shape_count) return false; @@ -1504,7 +1497,7 @@ svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_str_t href = svg_find_href_attr (parser); char ref_id[64]; - if (!svg_parse_fragment_id (href, ref_id)) + if (!svg_parse_id_ref_with_fallback (href, ref_id, nullptr)) return; float use_x = svg_parse_float (parser.find_attr ("x")); From 2ef531de783168510aaa7aed0f8ca51f994610c5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:51:20 -0300 Subject: [PATCH 015/105] [raster] Add SVG parser guardrails for attrs and stops --- src/hb-raster-svg-parse.hh | 16 ++++++++++++---- src/hb-raster-svg.cc | 4 ++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/hb-raster-svg-parse.hh b/src/hb-raster-svg-parse.hh index a18f15bb8..9a8338319 100644 --- a/src/hb-raster-svg-parse.hh +++ b/src/hb-raster-svg-parse.hh @@ -52,6 +52,8 @@ struct hb_svg_attr_t struct hb_svg_xml_parser_t { + enum { SVG_MAX_ATTRS_PER_TAG = 256 }; + const char *p; const char *end; const char *tag_start = nullptr; @@ -107,13 +109,19 @@ struct hb_svg_xml_parser_t p++; skip_ws (); hb_svg_str_t val = read_attr_value (); - hb_svg_attr_t attr = {name, val}; - attrs.push (attr); + if (attrs.length < SVG_MAX_ATTRS_PER_TAG) + { + hb_svg_attr_t attr = {name, val}; + attrs.push (attr); + } } else { - hb_svg_attr_t attr = {name, {}}; - attrs.push (attr); + if (attrs.length < SVG_MAX_ATTRS_PER_TAG) + { + hb_svg_attr_t attr = {name, {}}; + attrs.push (attr); + } } } } diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index e2d8eaad4..9c2a33ae8 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -735,6 +735,10 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, hb_face_t *face, unsigned palette) { + const unsigned SVG_MAX_GRADIENT_STOPS = 1024; + if (grad.stops.length >= SVG_MAX_GRADIENT_STOPS) + return true; + hb_svg_attr_view_t attrs (parser); hb_svg_str_t style = attrs.get ("style"); hb_svg_style_props_t style_props; From 87c18a2c47bfc709c25ec124edd2df3dc397f40c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:51:41 -0300 Subject: [PATCH 016/105] [raster] Drop unused SVG def enum variant --- src/hb-raster-svg-defs.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-raster-svg-defs.hh b/src/hb-raster-svg-defs.hh index 92780e70e..e93bfbb25 100644 --- a/src/hb-raster-svg-defs.hh +++ b/src/hb-raster-svg-defs.hh @@ -88,7 +88,7 @@ struct hb_svg_clip_shape_t struct hb_svg_def_t { char id[64]; - enum { DEF_GRADIENT, DEF_CLIP_PATH, DEF_ELEMENT } type; + enum { DEF_GRADIENT, DEF_CLIP_PATH } type; unsigned index; }; From ba1352a7afe518b0619330086518f96ba0eb85aa Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:53:11 -0300 Subject: [PATCH 017/105] [raster] Fix fill color regression in SVG id parsing --- src/hb-raster-svg.cc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 9c2a33ae8..fce122a0a 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -364,7 +364,8 @@ svg_parse_color (hb_svg_str_t s, static bool svg_parse_id_ref_with_fallback (hb_svg_str_t s, char out_id[64], - hb_svg_str_t *fallback); + hb_svg_str_t *fallback, + bool allow_fragment_direct); /* * 10. Gradient handler — construct hb_color_line_t @@ -493,12 +494,13 @@ struct hb_svg_cascade_t static bool svg_parse_id_ref_with_fallback (hb_svg_str_t s, char out_id[64], - hb_svg_str_t *fallback) + hb_svg_str_t *fallback, + bool allow_fragment_direct) { if (fallback) *fallback = {}; s = s.trim (); - if (s.len && s.data[0] == '#') + if (allow_fragment_direct && s.len && s.data[0] == '#') { unsigned n = hb_min (s.len - 1, (unsigned) 63); memcpy (out_id, s.data + 1, n); @@ -576,7 +578,7 @@ svg_emit_fill (hb_svg_render_context_t *ctx, char url_id[64]; hb_svg_str_t fallback_paint; - bool has_url_paint = svg_parse_id_ref_with_fallback (fill_str, url_id, &fallback_paint); + bool has_url_paint = svg_parse_id_ref_with_fallback (fill_str, url_id, &fallback_paint, false); /* Check for gradient reference */ const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs.find_gradient (url_id) : nullptr; @@ -830,7 +832,7 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, hb_svg_str_t href = svg_find_href_attr (parser); if (href.len) - (void) svg_parse_id_ref_with_fallback (href, grad.href_id, nullptr); + (void) svg_parse_id_ref_with_fallback (href, grad.href_id, nullptr, true); } static void @@ -1119,7 +1121,7 @@ svg_push_clip_path_ref (hb_svg_render_context_t *ctx, if (!trimmed.len || trimmed.eq ("none")) return false; char clip_id[64]; - if (!svg_parse_id_ref_with_fallback (trimmed, clip_id, nullptr)) + if (!svg_parse_id_ref_with_fallback (trimmed, clip_id, nullptr, true)) return false; const hb_svg_clip_path_def_t *clip = ctx->defs.find_clip_path (clip_id); if (!clip || !clip->shape_count) return false; @@ -1501,7 +1503,7 @@ svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_str_t href = svg_find_href_attr (parser); char ref_id[64]; - if (!svg_parse_id_ref_with_fallback (href, ref_id, nullptr)) + if (!svg_parse_id_ref_with_fallback (href, ref_id, nullptr, true)) return; float use_x = svg_parse_float (parser.find_attr ("x")); From b1c188931b6a7e50d7f57c70bb99fdb8dfa82a50 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:56:25 -0300 Subject: [PATCH 018/105] [raster] Split SVG gradient helpers into hb-raster-svg-gradient --- src/hb-raster-svg-gradient.cc | 251 ++++++++++++++++++++++++++++++++++ src/hb-raster-svg-gradient.hh | 55 ++++++++ src/hb-raster-svg.cc | 230 ++++--------------------------- src/meson.build | 2 + 4 files changed, 336 insertions(+), 202 deletions(-) create mode 100644 src/hb-raster-svg-gradient.cc create mode 100644 src/hb-raster-svg-gradient.hh diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc new file mode 100644 index 000000000..0617a0a3b --- /dev/null +++ b/src/hb-raster-svg-gradient.cc @@ -0,0 +1,251 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-gradient.hh" + +#include "hb-raster-svg-base.hh" + +#include + +static bool +svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, + hb_svg_gradient_t &grad, + hb_paint_funcs_t *pfuncs, + void *paint_data, + hb_color_t foreground, + hb_face_t *face, + unsigned palette) +{ + const unsigned SVG_MAX_GRADIENT_STOPS = 1024; + if (grad.stops.length >= SVG_MAX_GRADIENT_STOPS) + return true; + + hb_svg_attr_view_t attrs (parser); + hb_svg_str_t style = attrs.get ("style"); + hb_svg_style_props_t style_props; + svg_parse_style_props (style, &style_props); + hb_svg_str_t offset_str = svg_pick_attr_or_style (parser, style_props.offset, "offset"); + hb_svg_str_t color_str = svg_pick_attr_or_style (parser, style_props.stop_color, "stop-color"); + hb_svg_str_t opacity_str = svg_pick_attr_or_style (parser, style_props.stop_opacity, "stop-opacity"); + + float offset = 0; + if (offset_str.len) + offset = hb_clamp (svg_parse_number_or_percent (offset_str, nullptr), 0.f, 1.f); + + bool is_none = false; + hb_color_t color = HB_COLOR (0, 0, 0, 255); + bool is_current_color = false; + if (color_str.len && !svg_str_is_inherit (color_str)) + { + is_current_color = svg_str_eq_ascii_ci (color_str.trim (), "currentColor"); + color = svg_parse_color (color_str, pfuncs, paint_data, foreground, face, palette, &is_none); + } + + if (opacity_str.len && !svg_str_is_inherit (opacity_str)) + { + float opacity = svg_parse_float_clamped01 (opacity_str); + color = HB_COLOR (hb_color_get_blue (color), + hb_color_get_green (color), + hb_color_get_red (color), + (uint8_t) (hb_color_get_alpha (color) * opacity + 0.5f)); + } + + hb_svg_gradient_stop_t stop; + stop.offset = offset; + stop.color = color; + stop.is_current_color = is_current_color; + grad.stops.push (stop); + return !grad.stops.in_error (); +} + +static hb_svg_str_t +svg_find_href_attr (const hb_svg_xml_parser_t &parser) +{ + hb_svg_str_t href = parser.find_attr ("href"); + if (href.is_null ()) + href = parser.find_attr ("xlink:href"); + return href; +} + +static bool +svg_parse_fragment_id (hb_svg_str_t s, char out_id[64]) +{ + s = s.trim (); + if (!s.len || s.data[0] != '#') + return false; + unsigned n = hb_min (s.len - 1, (unsigned) 63); + memcpy (out_id, s.data + 1, n); + out_id[n] = '\0'; + return n > 0; +} + +static void +svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, + hb_svg_gradient_t &grad) +{ + hb_svg_attr_view_t attrs (parser); + hb_svg_str_t spread_str = attrs.get ("spreadMethod"); + if (spread_str.eq ("reflect")) + { + grad.spread = HB_PAINT_EXTEND_REFLECT; + grad.has_spread = true; + } + else if (spread_str.eq ("repeat")) + { + grad.spread = HB_PAINT_EXTEND_REPEAT; + grad.has_spread = true; + } + else if (spread_str.eq ("pad")) + { + grad.spread = HB_PAINT_EXTEND_PAD; + grad.has_spread = true; + } + + hb_svg_str_t units_str = attrs.get ("gradientUnits"); + if (units_str.eq ("userSpaceOnUse")) + { + grad.units_user_space = true; + grad.has_units_user_space = true; + } + else if (units_str.eq ("objectBoundingBox")) + { + grad.units_user_space = false; + grad.has_units_user_space = true; + } + + hb_svg_str_t transform_str = attrs.get ("gradientTransform"); + if (transform_str.len) + { + grad.has_gradient_transform = true; + svg_parse_transform (transform_str, &grad.gradient_transform); + } + + hb_svg_str_t href = svg_find_href_attr (parser); + if (href.len) + (void) svg_parse_fragment_id (href, grad.href_id); +} + +static void +svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, + hb_svg_gradient_t &grad) +{ + hb_svg_attr_view_t attrs (parser); + if (grad.type == SVG_GRADIENT_LINEAR) + { + hb_svg_str_t x1_str = attrs.get ("x1"); + hb_svg_str_t y1_str = attrs.get ("y1"); + hb_svg_str_t x2_str = attrs.get ("x2"); + hb_svg_str_t y2_str = attrs.get ("y2"); + if (x1_str.len) { grad.x1 = svg_parse_number_or_percent (x1_str, nullptr); grad.has_x1 = true; } + if (y1_str.len) { grad.y1 = svg_parse_number_or_percent (y1_str, nullptr); grad.has_y1 = true; } + if (x2_str.len) { grad.x2 = svg_parse_number_or_percent (x2_str, nullptr); grad.has_x2 = true; } + if (y2_str.len) { grad.y2 = svg_parse_number_or_percent (y2_str, nullptr); grad.has_y2 = true; } + + if (!grad.has_x2) + grad.x2 = 1.f; + } + else + { + hb_svg_str_t cx_str = attrs.get ("cx"); + hb_svg_str_t cy_str = attrs.get ("cy"); + hb_svg_str_t r_str = attrs.get ("r"); + hb_svg_str_t fx_str = attrs.get ("fx"); + hb_svg_str_t fy_str = attrs.get ("fy"); + + if (cx_str.len) { grad.cx = svg_parse_number_or_percent (cx_str, nullptr); grad.has_cx = true; } + if (cy_str.len) { grad.cy = svg_parse_number_or_percent (cy_str, nullptr); grad.has_cy = true; } + if (r_str.len) { grad.r = svg_parse_number_or_percent (r_str, nullptr); grad.has_r = true; } + if (fx_str.len) { grad.fx = svg_parse_number_or_percent (fx_str, nullptr); grad.has_fx = true; } + if (fy_str.len) { grad.fy = svg_parse_number_or_percent (fy_str, nullptr); grad.has_fy = true; } + } +} + +static void +svg_parse_gradient_children (hb_svg_defs_t *defs, + hb_svg_xml_parser_t &parser, + hb_svg_gradient_t &grad, + hb_svg_str_t *id, + hb_paint_funcs_t *pfuncs, + void *paint_data, + hb_color_t foreground, + hb_face_t *face, + unsigned palette) +{ + int gdepth = 1; + bool had_alloc_failure = false; + while (gdepth > 0) + { + hb_svg_token_type_t gt = parser.next (); + if (gt == SVG_TOKEN_EOF) break; + if (gt == SVG_TOKEN_CLOSE_TAG) { gdepth--; continue; } + if ((gt == SVG_TOKEN_OPEN_TAG || gt == SVG_TOKEN_SELF_CLOSE_TAG) && + parser.tag_name.eq ("stop")) + if (unlikely (!svg_parse_gradient_stop (parser, grad, + pfuncs, paint_data, + foreground, face, + palette))) + had_alloc_failure = true; + if (gt == SVG_TOKEN_OPEN_TAG && !parser.tag_name.eq ("stop")) + gdepth++; + } + if (had_alloc_failure || defs->gradients.in_error ()) + *id = {}; +} + +void +svg_process_gradient_def (hb_svg_defs_t *defs, + hb_svg_xml_parser_t &parser, + hb_svg_token_type_t tok, + hb_svg_gradient_type_t type, + hb_paint_funcs_t *pfuncs, + void *paint_data, + hb_color_t foreground, + hb_face_t *face, + unsigned palette) +{ + hb_svg_gradient_t grad; + grad.type = type; + svg_parse_gradient_geometry_attrs (parser, grad); + svg_parse_gradient_attrs (parser, grad); + + hb_svg_str_t id = parser.find_attr ("id"); + if (tok == SVG_TOKEN_OPEN_TAG) + svg_parse_gradient_children (defs, parser, grad, &id, + pfuncs, paint_data, + foreground, face, + palette); + + if (id.len) + { + char id_buf[64]; + unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); + memcpy (id_buf, id.data, n); + id_buf[n] = '\0'; + (void) defs->add_gradient (id_buf, grad); + } +} diff --git a/src/hb-raster-svg-gradient.hh b/src/hb-raster-svg-gradient.hh new file mode 100644 index 000000000..1a2667278 --- /dev/null +++ b/src/hb-raster-svg-gradient.hh @@ -0,0 +1,55 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_GRADIENT_HH +#define HB_RASTER_SVG_GRADIENT_HH + +#include "hb.hh" + +#include "hb-face.hh" +#include "hb-raster-svg-defs.hh" + +HB_INTERNAL hb_color_t +svg_parse_color (hb_svg_str_t s, + hb_paint_funcs_t *pfuncs, + void *paint_data, + hb_color_t foreground, + hb_face_t *face, + unsigned palette, + bool *is_none); + +HB_INTERNAL void +svg_process_gradient_def (hb_svg_defs_t *defs, + hb_svg_xml_parser_t &parser, + hb_svg_token_type_t tok, + hb_svg_gradient_type_t type, + hb_paint_funcs_t *pfuncs, + void *paint_data, + hb_color_t foreground, + hb_face_t *face, + unsigned palette); + +#endif /* HB_RASTER_SVG_GRADIENT_HH */ diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index fce122a0a..d598cebd1 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -33,6 +33,7 @@ #include "hb-raster-svg-base.hh" #include "hb-raster-svg-parse.hh" #include "hb-raster-svg-defs.hh" +#include "hb-raster-svg-gradient.hh" #include "OT/Color/svg/svg.hh" #include "hb-draw.h" #include "hb-ot-color.h" @@ -224,7 +225,7 @@ hexval (char c) /* Parse SVG color value; returns HB_COLOR with alpha = 255. * Sets *is_none if "none". */ -static hb_color_t +hb_color_t svg_parse_color (hb_svg_str_t s, hb_paint_funcs_t *pfuncs, void *paint_data, @@ -727,203 +728,7 @@ static void svg_render_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, const struct hb_svg_cascade_t &inherited); -/* Parse a gradient element */ -static bool -svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, - hb_svg_gradient_t &grad, - hb_paint_funcs_t *pfuncs, - void *paint_data, - hb_color_t foreground, - hb_face_t *face, - unsigned palette) -{ - const unsigned SVG_MAX_GRADIENT_STOPS = 1024; - if (grad.stops.length >= SVG_MAX_GRADIENT_STOPS) - return true; - - hb_svg_attr_view_t attrs (parser); - hb_svg_str_t style = attrs.get ("style"); - hb_svg_style_props_t style_props; - svg_parse_style_props (style, &style_props); - hb_svg_str_t offset_str = svg_pick_attr_or_style (parser, style_props.offset, "offset"); - hb_svg_str_t color_str = svg_pick_attr_or_style (parser, style_props.stop_color, "stop-color"); - hb_svg_str_t opacity_str = svg_pick_attr_or_style (parser, style_props.stop_opacity, "stop-opacity"); - - float offset = 0; - if (offset_str.len) - offset = hb_clamp (svg_parse_number_or_percent (offset_str, nullptr), 0.f, 1.f); - - bool is_none = false; - hb_color_t color = HB_COLOR (0, 0, 0, 255); - bool is_current_color = false; - if (color_str.len && !svg_str_is_inherit (color_str)) - { - is_current_color = svg_str_eq_ascii_ci (color_str.trim (), "currentColor"); - color = svg_parse_color (color_str, pfuncs, paint_data, foreground, face, palette, &is_none); - } - - if (opacity_str.len && !svg_str_is_inherit (opacity_str)) - { - float opacity = svg_parse_float_clamped01 (opacity_str); - color = HB_COLOR (hb_color_get_blue (color), - hb_color_get_green (color), - hb_color_get_red (color), - (uint8_t) (hb_color_get_alpha (color) * opacity + 0.5f)); - } - - hb_svg_gradient_stop_t stop; - stop.offset = offset; - stop.color = color; - stop.is_current_color = is_current_color; - grad.stops.push (stop); - return !grad.stops.in_error (); -} - -static hb_svg_str_t -svg_find_href_attr (const hb_svg_xml_parser_t &parser) -{ - hb_svg_str_t href = parser.find_attr ("href"); - if (href.is_null ()) - href = parser.find_attr ("xlink:href"); - return href; -} - -/* Parse gradient element attributes */ -static void -svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, - hb_svg_gradient_t &grad) -{ - hb_svg_attr_view_t attrs (parser); - hb_svg_str_t spread_str = attrs.get ("spreadMethod"); - if (spread_str.eq ("reflect")) - { - grad.spread = HB_PAINT_EXTEND_REFLECT; - grad.has_spread = true; - } - else if (spread_str.eq ("repeat")) - { - grad.spread = HB_PAINT_EXTEND_REPEAT; - grad.has_spread = true; - } - else if (spread_str.eq ("pad")) - { - grad.spread = HB_PAINT_EXTEND_PAD; - grad.has_spread = true; - } - - hb_svg_str_t units_str = attrs.get ("gradientUnits"); - if (units_str.eq ("userSpaceOnUse")) - { - grad.units_user_space = true; - grad.has_units_user_space = true; - } - else if (units_str.eq ("objectBoundingBox")) - { - grad.units_user_space = false; - grad.has_units_user_space = true; - } - - hb_svg_str_t transform_str = attrs.get ("gradientTransform"); - if (transform_str.len) - { - grad.has_gradient_transform = true; - svg_parse_transform (transform_str, &grad.gradient_transform); - } - - hb_svg_str_t href = svg_find_href_attr (parser); - if (href.len) - (void) svg_parse_id_ref_with_fallback (href, grad.href_id, nullptr, true); -} - -static void -svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, - hb_svg_gradient_t &grad) -{ - hb_svg_attr_view_t attrs (parser); - if (grad.type == SVG_GRADIENT_LINEAR) - { - hb_svg_str_t x1_str = attrs.get ("x1"); - hb_svg_str_t y1_str = attrs.get ("y1"); - hb_svg_str_t x2_str = attrs.get ("x2"); - hb_svg_str_t y2_str = attrs.get ("y2"); - if (x1_str.len) { grad.x1 = svg_parse_number_or_percent (x1_str, nullptr); grad.has_x1 = true; } - if (y1_str.len) { grad.y1 = svg_parse_number_or_percent (y1_str, nullptr); grad.has_y1 = true; } - if (x2_str.len) { grad.x2 = svg_parse_number_or_percent (x2_str, nullptr); grad.has_x2 = true; } - if (y2_str.len) { grad.y2 = svg_parse_number_or_percent (y2_str, nullptr); grad.has_y2 = true; } - - /* Default x2 to 1 if not explicitly set (objectBoundingBox default). */ - if (!grad.has_x2) - grad.x2 = 1.f; - } - else /* SVG_GRADIENT_RADIAL */ - { - hb_svg_str_t cx_str = attrs.get ("cx"); - hb_svg_str_t cy_str = attrs.get ("cy"); - hb_svg_str_t r_str = attrs.get ("r"); - hb_svg_str_t fx_str = attrs.get ("fx"); - hb_svg_str_t fy_str = attrs.get ("fy"); - - if (cx_str.len) { grad.cx = svg_parse_number_or_percent (cx_str, nullptr); grad.has_cx = true; } - if (cy_str.len) { grad.cy = svg_parse_number_or_percent (cy_str, nullptr); grad.has_cy = true; } - if (r_str.len) { grad.r = svg_parse_number_or_percent (r_str, nullptr); grad.has_r = true; } - if (fx_str.len) { grad.fx = svg_parse_number_or_percent (fx_str, nullptr); grad.has_fx = true; } - if (fy_str.len) { grad.fy = svg_parse_number_or_percent (fy_str, nullptr); grad.has_fy = true; } - } -} - -static void -svg_parse_gradient_children (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_gradient_t &grad, - hb_svg_str_t *id) -{ - int gdepth = 1; - bool had_alloc_failure = false; - while (gdepth > 0) - { - hb_svg_token_type_t gt = parser.next (); - if (gt == SVG_TOKEN_EOF) break; - if (gt == SVG_TOKEN_CLOSE_TAG) { gdepth--; continue; } - if ((gt == SVG_TOKEN_OPEN_TAG || gt == SVG_TOKEN_SELF_CLOSE_TAG) && - parser.tag_name.eq ("stop")) - if (unlikely (!svg_parse_gradient_stop (parser, grad, - ctx->pfuncs, ctx->paint, - ctx->foreground, hb_font_get_face (ctx->font), - ctx->palette))) - had_alloc_failure = true; - if (gt == SVG_TOKEN_OPEN_TAG && !parser.tag_name.eq ("stop")) - gdepth++; - } - if (had_alloc_failure) - *id = {}; -} - -static void -svg_process_gradient_def (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_token_type_t tok, - hb_svg_gradient_type_t type) -{ - hb_svg_gradient_t grad; - grad.type = type; - /* Parse geometry and common gradient attrs from the opening tag. */ - svg_parse_gradient_geometry_attrs (parser, grad); - svg_parse_gradient_attrs (parser, grad); - - hb_svg_str_t id = parser.find_attr ("id"); - /* Child elements are only present for non-self-closing gradients. */ - if (tok == SVG_TOKEN_OPEN_TAG) - svg_parse_gradient_children (ctx, parser, grad, &id); - - if (id.len) - { - char id_buf[64]; - unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); - memcpy (id_buf, id.data, n); - id_buf[n] = '\0'; - (void) ctx->defs.add_gradient (id_buf, grad); - } -} +/* Gradient def parsing lives in hb-raster-svg-gradient.* */ static void svg_process_clip_path_def (hb_svg_render_context_t *ctx, @@ -1019,9 +824,15 @@ svg_process_defs (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser) { /* Dispatch supported defs entries to focused helpers. */ if (parser.tag_name.eq ("linearGradient")) - svg_process_gradient_def (ctx, parser, tok, SVG_GRADIENT_LINEAR); + svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, + ctx->pfuncs, ctx->paint, + ctx->foreground, hb_font_get_face (ctx->font), + ctx->palette); else if (parser.tag_name.eq ("radialGradient")) - svg_process_gradient_def (ctx, parser, tok, SVG_GRADIENT_RADIAL); + svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, + ctx->pfuncs, ctx->paint, + ctx->foreground, hb_font_get_face (ctx->font), + ctx->palette); else if (parser.tag_name.eq ("clipPath")) svg_process_clip_path_def (ctx, parser, tok); else @@ -1494,6 +1305,15 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, return false; } +static hb_svg_str_t +svg_find_href_attr (const hb_svg_xml_parser_t &parser) +{ + hb_svg_str_t href = parser.find_attr ("href"); + if (href.is_null ()) + href = parser.find_attr ("xlink:href"); + return href; +} + static void svg_render_use_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, @@ -1704,9 +1524,15 @@ hb_raster_svg_render (hb_raster_paint_t *paint, if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { if (defs_parser.tag_name.eq ("linearGradient")) - svg_process_gradient_def (&ctx, defs_parser, tok, SVG_GRADIENT_LINEAR); + svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_LINEAR, + ctx.pfuncs, ctx.paint, + ctx.foreground, hb_font_get_face (ctx.font), + ctx.palette); else if (defs_parser.tag_name.eq ("radialGradient")) - svg_process_gradient_def (&ctx, defs_parser, tok, SVG_GRADIENT_RADIAL); + svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_RADIAL, + ctx.pfuncs, ctx.paint, + ctx.foreground, hb_font_get_face (ctx.font), + ctx.palette); else if (defs_parser.tag_name.eq ("clipPath")) svg_process_clip_path_def (&ctx, defs_parser, tok); } diff --git a/src/meson.build b/src/meson.build index 0ac973065..64fe646c3 100644 --- a/src/meson.build +++ b/src/meson.build @@ -709,6 +709,8 @@ hb_raster_sources = [ 'hb-raster-svg-base.hh', 'hb-raster-svg-defs.cc', 'hb-raster-svg-defs.hh', + 'hb-raster-svg-gradient.cc', + 'hb-raster-svg-gradient.hh', 'hb-raster-svg-parse.cc', 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', From 8f960c424e61a2a0614f28ef67e52e1ce45b95ba Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Feb 2026 23:58:35 -0300 Subject: [PATCH 019/105] [raster] Split SVG clip helpers into hb-raster-svg-clip --- src/hb-raster-svg-clip.cc | 248 ++++++++++++++++++++++++++++++++++++++ src/hb-raster-svg-clip.hh | 48 ++++++++ src/hb-raster-svg.cc | 163 ++----------------------- src/meson.build | 2 + 4 files changed, 305 insertions(+), 156 deletions(-) create mode 100644 src/hb-raster-svg-clip.cc create mode 100644 src/hb-raster-svg-clip.hh diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc new file mode 100644 index 000000000..4300427be --- /dev/null +++ b/src/hb-raster-svg-clip.cc @@ -0,0 +1,248 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-clip.hh" + +#include "hb-raster.h" +#include "hb-raster-paint.hh" +#include "hb-raster-svg.hh" +#include "hb-raster-svg-base.hh" + +#include + +void +svg_process_clip_path_def (hb_svg_defs_t *defs, + hb_svg_xml_parser_t &parser, + hb_svg_token_type_t tok) +{ + hb_svg_clip_path_def_t clip; + hb_svg_str_t id = parser.find_attr ("id"); + hb_svg_str_t units = parser.find_attr ("clipPathUnits"); + if (units.eq ("objectBoundingBox")) + clip.units_user_space = false; + else + clip.units_user_space = true; + + hb_svg_str_t cp_transform = parser.find_attr ("transform"); + if (cp_transform.len) + { + clip.has_clip_transform = true; + svg_parse_transform (cp_transform, &clip.clip_transform); + } + + clip.first_shape = defs->clip_shapes.length; + clip.shape_count = 0; + + if (tok == SVG_TOKEN_OPEN_TAG) + { + int cdepth = 1; + bool had_alloc_failure = false; + while (cdepth > 0) + { + hb_svg_token_type_t ct = parser.next (); + if (ct == SVG_TOKEN_EOF) break; + if (ct == SVG_TOKEN_CLOSE_TAG) { cdepth--; continue; } + if (ct == SVG_TOKEN_OPEN_TAG || ct == SVG_TOKEN_SELF_CLOSE_TAG) + { + if (cdepth == 1) + { + hb_svg_shape_emit_data_t shape; + if (svg_parse_shape_tag (parser, &shape)) + { + hb_svg_clip_shape_t clip_shape; + clip_shape.shape = shape; + hb_svg_str_t sh_transform = parser.find_attr ("transform"); + if (sh_transform.len) + { + clip_shape.has_transform = true; + svg_parse_transform (sh_transform, &clip_shape.transform); + } + defs->clip_shapes.push (clip_shape); + if (likely (!defs->clip_shapes.in_error ())) + clip.shape_count++; + else + had_alloc_failure = true; + } + } + if (ct == SVG_TOKEN_OPEN_TAG) + cdepth++; + } + } + if (had_alloc_failure) + id = {}; + } + + if (id.len && clip.shape_count) + { + char id_buf[64]; + unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); + memcpy (id_buf, id.data, n); + id_buf[n] = '\0'; + (void) defs->add_clip_path (id_buf, clip); + } +} + +struct hb_svg_clip_emit_data_t +{ + const hb_svg_defs_t *defs; + const hb_svg_clip_path_def_t *clip; + hb_transform_t<> base_transform; + hb_transform_t<> bbox_transform; + bool has_bbox_transform = false; +}; + +static inline hb_transform_t<> +svg_to_hb_transform (const hb_svg_transform_t &t) +{ + return hb_transform_t<> (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); +} + +static void +svg_clip_path_emit (hb_draw_funcs_t *dfuncs, + void *draw_data, + void *user_data) +{ + hb_raster_draw_t *rdr = (hb_raster_draw_t *) draw_data; + hb_svg_clip_emit_data_t *ed = (hb_svg_clip_emit_data_t *) user_data; + const hb_svg_clip_path_def_t *clip = ed->clip; + + for (unsigned i = 0; i < clip->shape_count; i++) + { + const hb_svg_clip_shape_t &s = ed->defs->clip_shapes[clip->first_shape + i]; + hb_transform_t<> t = ed->base_transform; + if (ed->has_bbox_transform) + t.multiply (ed->bbox_transform); + if (clip->has_clip_transform) + t.multiply (svg_to_hb_transform (clip->clip_transform)); + if (s.has_transform) + t.multiply (svg_to_hb_transform (s.transform)); + + hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); + + hb_svg_shape_emit_data_t shape = s.shape; + svg_shape_path_emit (dfuncs, draw_data, &shape); + } +} + +static bool +svg_parse_clip_path_id_ref (hb_svg_str_t s, char out_id[64]) +{ + s = s.trim (); + + if (s.len && s.data[0] == '#') + { + unsigned n = hb_min (s.len - 1, (unsigned) 63); + memcpy (out_id, s.data + 1, n); + out_id[n] = '\0'; + return n > 0; + } + + if (!svg_str_starts_with_ascii_ci (s, "url(")) + return false; + + const char *p = s.data + 4; + const char *end = s.data + s.len; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + + const char *q = p; + char quote = 0; + while (q < end) + { + char c = *q; + if (quote) + { + if (c == quote) quote = 0; + } + else + { + if (c == '"' || c == '\'') quote = c; + else if (c == ')') break; + } + q++; + } + if (q >= end || *q != ')') + return false; + + const char *id_b = p; + const char *id_e = q; + while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; + while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; + if (id_e > id_b && ((*id_b == '\'' && *(id_e - 1) == '\'') || (*id_b == '"' && *(id_e - 1) == '"'))) + { + id_b++; + id_e--; + } + while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; + while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; + if (id_b < id_e && *id_b == '#') id_b++; + if (id_b >= id_e) + return false; + + unsigned n = hb_min ((unsigned) (id_e - id_b), (unsigned) 63); + memcpy (out_id, id_b, n); + out_id[n] = '\0'; + return true; +} + +bool +svg_push_clip_path_ref (hb_raster_paint_t *paint, + hb_svg_defs_t *defs, + hb_svg_str_t clip_path_str, + const hb_extents_t<> *object_bbox) +{ + if (clip_path_str.is_null ()) return false; + hb_svg_str_t trimmed = clip_path_str.trim (); + if (!trimmed.len || trimmed.eq ("none")) return false; + + char clip_id[64]; + if (!svg_parse_clip_path_id_ref (trimmed, clip_id)) + return false; + + const hb_svg_clip_path_def_t *clip = defs->find_clip_path (clip_id); + if (!clip || !clip->shape_count) return false; + + hb_svg_clip_emit_data_t ed; + ed.defs = defs; + ed.clip = clip; + ed.base_transform = paint->current_effective_transform (); + + if (!clip->units_user_space) + { + if (!object_bbox || object_bbox->is_empty ()) + return false; + float w = object_bbox->xmax - object_bbox->xmin; + float h = object_bbox->ymax - object_bbox->ymin; + if (w <= 0 || h <= 0) + return false; + ed.has_bbox_transform = true; + ed.bbox_transform = hb_transform_t<> (w, 0, 0, h, object_bbox->xmin, object_bbox->ymin); + } + + hb_raster_paint_push_clip_path (paint, svg_clip_path_emit, &ed); + return true; +} diff --git a/src/hb-raster-svg-clip.hh b/src/hb-raster-svg-clip.hh new file mode 100644 index 000000000..f11bc48b2 --- /dev/null +++ b/src/hb-raster-svg-clip.hh @@ -0,0 +1,48 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_CLIP_HH +#define HB_RASTER_SVG_CLIP_HH + +#include "hb.hh" + +#include "hb-paint.hh" +#include "hb-raster-svg-defs.hh" + +struct hb_raster_paint_t; + +HB_INTERNAL void +svg_process_clip_path_def (hb_svg_defs_t *defs, + hb_svg_xml_parser_t &parser, + hb_svg_token_type_t tok); + +HB_INTERNAL bool +svg_push_clip_path_ref (hb_raster_paint_t *paint, + hb_svg_defs_t *defs, + hb_svg_str_t clip_path_str, + const hb_extents_t<> *object_bbox); + +#endif /* HB_RASTER_SVG_CLIP_HH */ diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index d598cebd1..8e7aabe7d 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -34,6 +34,7 @@ #include "hb-raster-svg-parse.hh" #include "hb-raster-svg-defs.hh" #include "hb-raster-svg-gradient.hh" +#include "hb-raster-svg-clip.hh" #include "OT/Color/svg/svg.hh" #include "hb-draw.h" #include "hb-ot-color.h" @@ -730,78 +731,7 @@ static void svg_render_element (hb_svg_render_context_t *ctx, /* Gradient def parsing lives in hb-raster-svg-gradient.* */ -static void -svg_process_clip_path_def (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_token_type_t tok) -{ - hb_svg_clip_path_def_t clip; - hb_svg_str_t id = parser.find_attr ("id"); - hb_svg_str_t units = parser.find_attr ("clipPathUnits"); - if (units.eq ("objectBoundingBox")) - clip.units_user_space = false; - else - clip.units_user_space = true; - - hb_svg_str_t cp_transform = parser.find_attr ("transform"); - if (cp_transform.len) - { - clip.has_clip_transform = true; - svg_parse_transform (cp_transform, &clip.clip_transform); - } - - clip.first_shape = ctx->defs.clip_shapes.length; - clip.shape_count = 0; - - /* Collect top-level geometry children of . */ - if (tok == SVG_TOKEN_OPEN_TAG) - { - int cdepth = 1; - bool had_alloc_failure = false; - while (cdepth > 0) - { - hb_svg_token_type_t ct = parser.next (); - if (ct == SVG_TOKEN_EOF) break; - if (ct == SVG_TOKEN_CLOSE_TAG) { cdepth--; continue; } - if (ct == SVG_TOKEN_OPEN_TAG || ct == SVG_TOKEN_SELF_CLOSE_TAG) - { - if (cdepth == 1) - { - hb_svg_shape_emit_data_t shape; - if (svg_parse_shape_tag (parser, &shape)) - { - hb_svg_clip_shape_t clip_shape; - clip_shape.shape = shape; - hb_svg_str_t sh_transform = parser.find_attr ("transform"); - if (sh_transform.len) - { - clip_shape.has_transform = true; - svg_parse_transform (sh_transform, &clip_shape.transform); - } - ctx->defs.clip_shapes.push (clip_shape); - if (likely (!ctx->defs.clip_shapes.in_error ())) - clip.shape_count++; - else - had_alloc_failure = true; - } - } - if (ct == SVG_TOKEN_OPEN_TAG) - cdepth++; - } - } - if (had_alloc_failure) - id = {}; - } - - if (id.len && clip.shape_count) - { - char id_buf[64]; - unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); - memcpy (id_buf, id.data, n); - id_buf[n] = '\0'; - (void) ctx->defs.add_clip_path (id_buf, clip); - } -} +/* Clip-path defs and push helpers live in hb-raster-svg-clip.* */ /* Process children — gradients, clipPaths */ static void @@ -822,7 +752,6 @@ svg_process_defs (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser) if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { - /* Dispatch supported defs entries to focused helpers. */ if (parser.tag_name.eq ("linearGradient")) svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, ctx->pfuncs, ctx->paint, @@ -834,10 +763,9 @@ svg_process_defs (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser) ctx->foreground, hb_font_get_face (ctx->font), ctx->palette); else if (parser.tag_name.eq ("clipPath")) - svg_process_clip_path_def (ctx, parser, tok); + svg_process_clip_path_def (&ctx->defs, parser, tok); else { - /* Skip unknown defs children */ if (tok == SVG_TOKEN_OPEN_TAG) depth++; } @@ -880,84 +808,6 @@ svg_find_element_by_id (const hb_svg_render_context_t *ctx, return false; } -struct hb_svg_clip_emit_data_t -{ - const hb_svg_defs_t *defs; - const hb_svg_clip_path_def_t *clip; - hb_transform_t<> base_transform; - hb_transform_t<> bbox_transform; - bool has_bbox_transform = false; -}; - -static inline hb_transform_t<> -svg_to_hb_transform (const hb_svg_transform_t &t) -{ - return hb_transform_t<> (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); -} - -static void -svg_clip_path_emit (hb_draw_funcs_t *dfuncs, - void *draw_data, - void *user_data) -{ - hb_raster_draw_t *rdr = (hb_raster_draw_t *) draw_data; - hb_svg_clip_emit_data_t *ed = (hb_svg_clip_emit_data_t *) user_data; - const hb_svg_clip_path_def_t *clip = ed->clip; - - for (unsigned i = 0; i < clip->shape_count; i++) - { - const hb_svg_clip_shape_t &s = ed->defs->clip_shapes[clip->first_shape + i]; - hb_transform_t<> t = ed->base_transform; - if (ed->has_bbox_transform) - t.multiply (ed->bbox_transform); - if (clip->has_clip_transform) - t.multiply (svg_to_hb_transform (clip->clip_transform)); - if (s.has_transform) - t.multiply (svg_to_hb_transform (s.transform)); - - hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); - - hb_svg_shape_emit_data_t shape = s.shape; - svg_shape_path_emit (dfuncs, draw_data, &shape); - } -} - -static bool -svg_push_clip_path_ref (hb_svg_render_context_t *ctx, - hb_svg_str_t clip_path_str, - const hb_extents_t<> *object_bbox) -{ - if (clip_path_str.is_null ()) return false; - hb_svg_str_t trimmed = clip_path_str.trim (); - if (!trimmed.len || trimmed.eq ("none")) return false; - - char clip_id[64]; - if (!svg_parse_id_ref_with_fallback (trimmed, clip_id, nullptr, true)) - return false; - const hb_svg_clip_path_def_t *clip = ctx->defs.find_clip_path (clip_id); - if (!clip || !clip->shape_count) return false; - - hb_svg_clip_emit_data_t ed; - ed.defs = &ctx->defs; - ed.clip = clip; - ed.base_transform = ctx->paint->current_effective_transform (); - - if (!clip->units_user_space) - { - if (!object_bbox || object_bbox->is_empty ()) - return false; - float w = object_bbox->xmax - object_bbox->xmin; - float h = object_bbox->ymax - object_bbox->ymin; - if (w <= 0 || h <= 0) - return false; - ed.has_bbox_transform = true; - ed.bbox_transform = hb_transform_t<> (w, 0, 0, h, object_bbox->xmin, object_bbox->ymin); - } - - hb_raster_paint_push_clip_path (ctx->paint, svg_clip_path_emit, &ed); - return true; -} - static void svg_bbox_move_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data, @@ -1058,7 +908,8 @@ svg_render_shape (hb_svg_render_context_t *ctx, hb_extents_t<> bbox; bool has_bbox = svg_compute_shape_bbox (shape, &bbox); - has_clip_path = svg_push_clip_path_ref (ctx, state.clip_path, has_bbox ? &bbox : nullptr); + has_clip_path = svg_push_clip_path_ref (ctx->paint, &ctx->defs, state.clip_path, + has_bbox ? &bbox : nullptr); /* Clip with shape path, then fill */ hb_raster_paint_push_clip_path (ctx->paint, svg_shape_path_emit, &shape); @@ -1127,7 +978,7 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (has_viewbox && vb_w > 0 && vb_h > 0) ctx->push_transform (1, 0, 0, 1, -vb_x, -vb_y); - has_clip = svg_push_clip_path_ref (ctx, clip_path_str, nullptr); + has_clip = svg_push_clip_path_ref (ctx->paint, &ctx->defs, clip_path_str, nullptr); if (!self_closing) { @@ -1534,7 +1385,7 @@ hb_raster_svg_render (hb_raster_paint_t *paint, ctx.foreground, hb_font_get_face (ctx.font), ctx.palette); else if (defs_parser.tag_name.eq ("clipPath")) - svg_process_clip_path_def (&ctx, defs_parser, tok); + svg_process_clip_path_def (&ctx.defs, defs_parser, tok); } } } diff --git a/src/meson.build b/src/meson.build index 64fe646c3..08c87ee83 100644 --- a/src/meson.build +++ b/src/meson.build @@ -711,6 +711,8 @@ hb_raster_sources = [ 'hb-raster-svg-defs.hh', 'hb-raster-svg-gradient.cc', 'hb-raster-svg-gradient.hh', + 'hb-raster-svg-clip.cc', + 'hb-raster-svg-clip.hh', 'hb-raster-svg-parse.cc', 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', From b8f607cbae5b9d24a34f7ffc5d82b3d627fafd6f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:01:54 -0300 Subject: [PATCH 020/105] [raster] Split SVG bbox helpers into hb-raster-svg-bbox --- src/hb-raster-svg-bbox.cc | 105 ++++++++++++++++++++++++++++++++++++++ src/hb-raster-svg-bbox.hh | 39 ++++++++++++++ src/hb-raster-svg.cc | 78 +--------------------------- src/meson.build | 2 + 4 files changed, 147 insertions(+), 77 deletions(-) create mode 100644 src/hb-raster-svg-bbox.cc create mode 100644 src/hb-raster-svg-bbox.hh diff --git a/src/hb-raster-svg-bbox.cc b/src/hb-raster-svg-bbox.cc new file mode 100644 index 000000000..777ac8c30 --- /dev/null +++ b/src/hb-raster-svg-bbox.cc @@ -0,0 +1,105 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-bbox.hh" + +static void +svg_bbox_move_to (hb_draw_funcs_t *dfuncs HB_UNUSED, + void *draw_data, + hb_draw_state_t *st HB_UNUSED, + float to_x, float to_y, + void *user_data HB_UNUSED) +{ + ((hb_extents_t<> *) draw_data)->add_point (to_x, to_y); +} + +static void +svg_bbox_line_to (hb_draw_funcs_t *dfuncs HB_UNUSED, + void *draw_data, + hb_draw_state_t *st HB_UNUSED, + float to_x, float to_y, + void *user_data HB_UNUSED) +{ + ((hb_extents_t<> *) draw_data)->add_point (to_x, to_y); +} + +static void +svg_bbox_quadratic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, + void *draw_data, + hb_draw_state_t *st HB_UNUSED, + float control_x, float control_y, + float to_x, float to_y, + void *user_data HB_UNUSED) +{ + hb_extents_t<> *ext = (hb_extents_t<> *) draw_data; + ext->add_point (control_x, control_y); + ext->add_point (to_x, to_y); +} + +static void +svg_bbox_cubic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, + void *draw_data, + hb_draw_state_t *st HB_UNUSED, + float control1_x, float control1_y, + float control2_x, float control2_y, + float to_x, float to_y, + void *user_data HB_UNUSED) +{ + hb_extents_t<> *ext = (hb_extents_t<> *) draw_data; + ext->add_point (control1_x, control1_y); + ext->add_point (control2_x, control2_y); + ext->add_point (to_x, to_y); +} + +static hb_draw_funcs_t * +svg_bbox_draw_funcs () +{ + static hb_draw_funcs_t *funcs = nullptr; + if (unlikely (!funcs)) + { + funcs = hb_draw_funcs_create (); + hb_draw_funcs_set_move_to_func (funcs, svg_bbox_move_to, nullptr, nullptr); + hb_draw_funcs_set_line_to_func (funcs, svg_bbox_line_to, nullptr, nullptr); + hb_draw_funcs_set_quadratic_to_func (funcs, svg_bbox_quadratic_to, nullptr, nullptr); + hb_draw_funcs_set_cubic_to_func (funcs, svg_bbox_cubic_to, nullptr, nullptr); + hb_draw_funcs_make_immutable (funcs); + } + return funcs; +} + +bool +svg_compute_shape_bbox (const hb_svg_shape_emit_data_t &shape, + hb_extents_t<> *bbox) +{ + hb_extents_t<> ext; + hb_svg_shape_emit_data_t tmp = shape; + svg_shape_path_emit (svg_bbox_draw_funcs (), &ext, &tmp); + if (ext.is_empty ()) return false; + *bbox = ext; + return true; +} diff --git a/src/hb-raster-svg-bbox.hh b/src/hb-raster-svg-bbox.hh new file mode 100644 index 000000000..d6fb3a566 --- /dev/null +++ b/src/hb-raster-svg-bbox.hh @@ -0,0 +1,39 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_BBOX_HH +#define HB_RASTER_SVG_BBOX_HH + +#include "hb.hh" + +#include "hb-geometry.hh" +#include "hb-raster-svg-parse.hh" + +HB_INTERNAL bool +svg_compute_shape_bbox (const hb_svg_shape_emit_data_t &shape, + hb_extents_t<> *bbox); + +#endif /* HB_RASTER_SVG_BBOX_HH */ diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 8e7aabe7d..caa6f5858 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -35,6 +35,7 @@ #include "hb-raster-svg-defs.hh" #include "hb-raster-svg-gradient.hh" #include "hb-raster-svg-clip.hh" +#include "hb-raster-svg-bbox.hh" #include "OT/Color/svg/svg.hh" #include "hb-draw.h" #include "hb-ot-color.h" @@ -808,83 +809,6 @@ svg_find_element_by_id (const hb_svg_render_context_t *ctx, return false; } -static void -svg_bbox_move_to (hb_draw_funcs_t *dfuncs HB_UNUSED, - void *draw_data, - hb_draw_state_t *st HB_UNUSED, - float to_x, float to_y, - void *user_data HB_UNUSED) -{ - ((hb_extents_t<> *) draw_data)->add_point (to_x, to_y); -} - -static void -svg_bbox_line_to (hb_draw_funcs_t *dfuncs HB_UNUSED, - void *draw_data, - hb_draw_state_t *st HB_UNUSED, - float to_x, float to_y, - void *user_data HB_UNUSED) -{ - ((hb_extents_t<> *) draw_data)->add_point (to_x, to_y); -} - -static void -svg_bbox_quadratic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, - void *draw_data, - hb_draw_state_t *st HB_UNUSED, - float control_x, float control_y, - float to_x, float to_y, - void *user_data HB_UNUSED) -{ - hb_extents_t<> *ext = (hb_extents_t<> *) draw_data; - ext->add_point (control_x, control_y); - ext->add_point (to_x, to_y); -} - -static void -svg_bbox_cubic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, - void *draw_data, - hb_draw_state_t *st HB_UNUSED, - float control1_x, float control1_y, - float control2_x, float control2_y, - float to_x, float to_y, - void *user_data HB_UNUSED) -{ - hb_extents_t<> *ext = (hb_extents_t<> *) draw_data; - ext->add_point (control1_x, control1_y); - ext->add_point (control2_x, control2_y); - ext->add_point (to_x, to_y); -} - -static hb_draw_funcs_t * -svg_bbox_draw_funcs () -{ - static hb_draw_funcs_t *funcs = nullptr; - if (unlikely (!funcs)) - { - funcs = hb_draw_funcs_create (); - hb_draw_funcs_set_move_to_func (funcs, svg_bbox_move_to, nullptr, nullptr); - hb_draw_funcs_set_line_to_func (funcs, svg_bbox_line_to, nullptr, nullptr); - hb_draw_funcs_set_quadratic_to_func (funcs, svg_bbox_quadratic_to, nullptr, nullptr); - hb_draw_funcs_set_cubic_to_func (funcs, svg_bbox_cubic_to, nullptr, nullptr); - hb_draw_funcs_make_immutable (funcs); - } - return funcs; -} - -static bool -svg_compute_shape_bbox (const hb_svg_shape_emit_data_t &shape, - hb_extents_t<> *bbox) -{ - hb_extents_t<> ext; - hb_svg_shape_emit_data_t tmp = shape; - svg_shape_path_emit (svg_bbox_draw_funcs (), &ext, &tmp); - if (ext.is_empty ()) return false; - *bbox = ext; - return true; -} - - /* Render a single shape element */ static void svg_render_shape (hb_svg_render_context_t *ctx, diff --git a/src/meson.build b/src/meson.build index 08c87ee83..cc0586cc3 100644 --- a/src/meson.build +++ b/src/meson.build @@ -713,6 +713,8 @@ hb_raster_sources = [ 'hb-raster-svg-gradient.hh', 'hb-raster-svg-clip.cc', 'hb-raster-svg-clip.hh', + 'hb-raster-svg-bbox.cc', + 'hb-raster-svg-bbox.hh', 'hb-raster-svg-parse.cc', 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', From aaae77ccac27fa0b7f0c12258d558d618d89ebb6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:04:19 -0300 Subject: [PATCH 021/105] [raster] Split SVG fill helpers into hb-raster-svg-fill --- src/hb-raster-svg-fill.cc | 305 ++++++++++++++++++++++++++++++++++++++ src/hb-raster-svg-fill.hh | 53 +++++++ src/hb-raster-svg.cc | 226 +--------------------------- src/meson.build | 2 + 4 files changed, 366 insertions(+), 220 deletions(-) create mode 100644 src/hb-raster-svg-fill.cc create mode 100644 src/hb-raster-svg-fill.hh diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc new file mode 100644 index 000000000..d92ef2c4b --- /dev/null +++ b/src/hb-raster-svg-fill.cc @@ -0,0 +1,305 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-fill.hh" + +#include "hb-raster-svg-base.hh" +#include "hb-raster-svg-gradient.hh" + +#include + +struct hb_svg_color_line_data_t +{ + const hb_svg_gradient_t *grad; + hb_paint_extend_t extend; + float alpha_scale; + hb_color_t current_color; +}; + +static unsigned +svg_color_line_get_stops (hb_color_line_t *color_line HB_UNUSED, + void *color_line_data, + unsigned int start, + unsigned int *count, + hb_color_stop_t *color_stops, + void *user_data HB_UNUSED) +{ + hb_svg_color_line_data_t *cl = (hb_svg_color_line_data_t *) color_line_data; + const hb_svg_gradient_t *grad = cl->grad; + unsigned total = grad->stops.length; + + if (count) + { + unsigned n = hb_min (*count, total > start ? total - start : 0u); + for (unsigned i = 0; i < n; i++) + { + const hb_svg_gradient_stop_t &stop = grad->stops[start + i]; + color_stops[i].offset = stop.offset; + color_stops[i].is_foreground = false; + hb_color_t c = stop.is_current_color ? cl->current_color : stop.color; + if (stop.is_current_color) + { + uint8_t a = (uint8_t) hb_raster_div255 (hb_color_get_alpha (c) * hb_color_get_alpha (stop.color)); + c = HB_COLOR (hb_color_get_blue (c), + hb_color_get_green (c), + hb_color_get_red (c), + a); + } + if (cl->alpha_scale < 1.f) + { + uint8_t a = (uint8_t) hb_clamp ((int) (hb_color_get_alpha (c) * cl->alpha_scale + 0.5f), 0, 255); + c = HB_COLOR (hb_color_get_blue (c), + hb_color_get_green (c), + hb_color_get_red (c), + a); + } + color_stops[i].color = c; + } + *count = n; + } + return total; +} + +static hb_paint_extend_t +svg_color_line_get_extend (hb_color_line_t *color_line HB_UNUSED, + void *color_line_data, + void *user_data HB_UNUSED) +{ + hb_svg_color_line_data_t *cl = (hb_svg_color_line_data_t *) color_line_data; + return cl->extend; +} + +static bool +svg_parse_paint_url_with_fallback (hb_svg_str_t s, + char out_id[64], + hb_svg_str_t *fallback) +{ + if (fallback) *fallback = {}; + s = s.trim (); + if (!svg_str_starts_with_ascii_ci (s, "url(")) + return false; + + const char *p = s.data + 4; + const char *end = s.data + s.len; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + + const char *q = p; + char quote = 0; + while (q < end) + { + char c = *q; + if (quote) + { + if (c == quote) quote = 0; + } + else + { + if (c == '"' || c == '\'') quote = c; + else if (c == ')') break; + } + q++; + } + if (q >= end || *q != ')') + return false; + + const char *id_b = p; + const char *id_e = q; + while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; + while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; + if (id_e > id_b && ((*id_b == '\'' && *(id_e - 1) == '\'') || (*id_b == '"' && *(id_e - 1) == '"'))) + { + id_b++; + id_e--; + } + while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; + while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; + if (id_b < id_e && *id_b == '#') id_b++; + if (id_b >= id_e) + return false; + + unsigned n = hb_min ((unsigned) (id_e - id_b), (unsigned) 63); + memcpy (out_id, id_b, n); + out_id[n] = '\0'; + + if (fallback) + { + const char *f = q + 1; + while (f < end && (*f == ' ' || *f == '\t' || *f == '\n' || *f == '\r')) f++; + if (f < end) *fallback = {f, (unsigned) (end - f)}; + } + return true; +} + +void +svg_emit_fill (const hb_svg_fill_context_t *ctx, + hb_svg_str_t fill_str, + float fill_opacity, + const hb_extents_t<> *object_bbox, + hb_color_t current_color) +{ + bool is_none = false; + + char url_id[64]; + hb_svg_str_t fallback_paint; + bool has_url_paint = svg_parse_paint_url_with_fallback (fill_str, url_id, &fallback_paint); + + const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs->find_gradient (url_id) : nullptr; + if (has_url_paint && !grad) + { + if (fallback_paint.len) + svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); + return; + } + if (grad) + { + const hb_svg_gradient_t *chain[8]; + unsigned chain_len = 0; + const hb_svg_gradient_t *cur = grad; + while (cur && chain_len < ARRAY_LENGTH (chain)) + { + chain[chain_len++] = cur; + if (!cur->href_id[0]) break; + const hb_svg_gradient_t *next = ctx->defs->find_gradient (cur->href_id); + if (!next) break; + bool is_cycle = false; + for (unsigned i = 0; i < chain_len; i++) + if (chain[i] == next) + { + is_cycle = true; + break; + } + if (is_cycle) break; + cur = next; + } + if (!chain_len) return; + + hb_svg_gradient_t effective = *chain[chain_len - 1]; + for (int i = (int) chain_len - 2; i >= 0; i--) + { + const hb_svg_gradient_t *g = chain[i]; + effective.type = g->type; + if (g->stops.length) effective.stops = g->stops; + if (g->has_spread) { effective.spread = g->spread; effective.has_spread = true; } + if (g->has_units_user_space) + { + effective.units_user_space = g->units_user_space; + effective.has_units_user_space = true; + } + if (g->has_gradient_transform) + { + effective.gradient_transform = g->gradient_transform; + effective.has_gradient_transform = true; + } + if (g->has_x1) { effective.x1 = g->x1; effective.has_x1 = true; } + if (g->has_y1) { effective.y1 = g->y1; effective.has_y1 = true; } + if (g->has_x2) { effective.x2 = g->x2; effective.has_x2 = true; } + if (g->has_y2) { effective.y2 = g->y2; effective.has_y2 = true; } + if (g->has_cx) { effective.cx = g->cx; effective.has_cx = true; } + if (g->has_cy) { effective.cy = g->cy; effective.has_cy = true; } + if (g->has_r) { effective.r = g->r; effective.has_r = true; } + if (g->has_fx) { effective.fx = g->fx; effective.has_fx = true; } + if (g->has_fy) { effective.fy = g->fy; effective.has_fy = true; } + } + if (!effective.stops.length) + return; + + hb_svg_color_line_data_t cl_data; + cl_data.grad = &effective; + cl_data.extend = effective.spread; + cl_data.alpha_scale = hb_clamp (fill_opacity, 0.f, 1.f); + cl_data.current_color = current_color; + + hb_color_line_t cl = { + &cl_data, + svg_color_line_get_stops, nullptr, + svg_color_line_get_extend, nullptr + }; + + bool has_bbox_transform = !effective.units_user_space && object_bbox && !object_bbox->is_empty (); + if (has_bbox_transform) + { + float w = object_bbox->xmax - object_bbox->xmin; + float h = object_bbox->ymax - object_bbox->ymin; + if (w > 0 && h > 0) + hb_paint_push_transform (ctx->pfuncs, ctx->paint, w, 0, 0, h, object_bbox->xmin, object_bbox->ymin); + else + has_bbox_transform = false; + } + + if (effective.has_gradient_transform) + hb_paint_push_transform (ctx->pfuncs, ctx->paint, + effective.gradient_transform.xx, + effective.gradient_transform.yx, + effective.gradient_transform.xy, + effective.gradient_transform.yy, + effective.gradient_transform.dx, + effective.gradient_transform.dy); + + if (effective.type == SVG_GRADIENT_LINEAR) + { + hb_paint_linear_gradient (ctx->pfuncs, ctx->paint, &cl, + effective.x1, effective.y1, + effective.x2, effective.y2, + effective.x2 - (effective.y2 - effective.y1), + effective.y2 + (effective.x2 - effective.x1)); + } + else + { + float fx = effective.has_fx ? effective.fx : effective.cx; + float fy = effective.has_fy ? effective.fy : effective.cy; + + hb_paint_radial_gradient (ctx->pfuncs, ctx->paint, &cl, + fx, fy, 0.f, + effective.cx, effective.cy, effective.r); + } + + if (effective.has_gradient_transform) + hb_paint_pop_transform (ctx->pfuncs, ctx->paint); + if (has_bbox_transform) + hb_paint_pop_transform (ctx->pfuncs, ctx->paint); + + return; + } + + hb_color_t color = svg_parse_color (fill_str, + ctx->pfuncs, + ctx->paint, + current_color, + hb_font_get_face (ctx->font), + ctx->palette, + &is_none); + if (is_none) return; + + if (fill_opacity < 1.f) + color = HB_COLOR (hb_color_get_blue (color), + hb_color_get_green (color), + hb_color_get_red (color), + (uint8_t) (hb_color_get_alpha (color) * fill_opacity + 0.5f)); + + hb_paint_color (ctx->pfuncs, ctx->paint, false, color); +} diff --git a/src/hb-raster-svg-fill.hh b/src/hb-raster-svg-fill.hh new file mode 100644 index 000000000..7adeaefa3 --- /dev/null +++ b/src/hb-raster-svg-fill.hh @@ -0,0 +1,53 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_FILL_HH +#define HB_RASTER_SVG_FILL_HH + +#include "hb.hh" + +#include "hb-face.hh" +#include "hb-raster.h" +#include "hb-raster-paint.hh" +#include "hb-raster-svg-defs.hh" + +struct hb_svg_fill_context_t +{ + hb_raster_paint_t *paint; + hb_paint_funcs_t *pfuncs; + hb_font_t *font; + unsigned palette; + hb_svg_defs_t *defs; +}; + +HB_INTERNAL void +svg_emit_fill (const hb_svg_fill_context_t *ctx, + hb_svg_str_t fill_str, + float fill_opacity, + const hb_extents_t<> *object_bbox, + hb_color_t current_color); + +#endif /* HB_RASTER_SVG_FILL_HH */ diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index caa6f5858..b56418662 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -36,6 +36,7 @@ #include "hb-raster-svg-gradient.hh" #include "hb-raster-svg-clip.hh" #include "hb-raster-svg-bbox.hh" +#include "hb-raster-svg-fill.hh" #include "OT/Color/svg/svg.hh" #include "hb-draw.h" #include "hb-ot-color.h" @@ -371,73 +372,7 @@ svg_parse_id_ref_with_fallback (hb_svg_str_t s, bool allow_fragment_direct); /* - * 10. Gradient handler — construct hb_color_line_t - */ - -struct hb_svg_color_line_data_t -{ - const hb_svg_gradient_t *grad; - hb_paint_extend_t extend; - float alpha_scale; - hb_color_t current_color; -}; - -static unsigned -svg_color_line_get_stops (hb_color_line_t *color_line HB_UNUSED, - void *color_line_data, - unsigned int start, - unsigned int *count, - hb_color_stop_t *color_stops, - void *user_data HB_UNUSED) -{ - hb_svg_color_line_data_t *cl = (hb_svg_color_line_data_t *) color_line_data; - const hb_svg_gradient_t *grad = cl->grad; - unsigned total = grad->stops.length; - - if (count) - { - unsigned n = hb_min (*count, total > start ? total - start : 0u); - for (unsigned i = 0; i < n; i++) - { - const hb_svg_gradient_stop_t &stop = grad->stops[start + i]; - color_stops[i].offset = stop.offset; - color_stops[i].is_foreground = false; - hb_color_t c = stop.is_current_color ? cl->current_color : stop.color; - if (stop.is_current_color) - { - uint8_t a = (uint8_t) hb_raster_div255 (hb_color_get_alpha (c) * hb_color_get_alpha (stop.color)); - c = HB_COLOR (hb_color_get_blue (c), - hb_color_get_green (c), - hb_color_get_red (c), - a); - } - if (cl->alpha_scale < 1.f) - { - uint8_t a = (uint8_t) hb_clamp ((int) (hb_color_get_alpha (c) * cl->alpha_scale + 0.5f), 0, 255); - c = HB_COLOR (hb_color_get_blue (c), - hb_color_get_green (c), - hb_color_get_red (c), - a); - } - color_stops[i].color = c; - } - *count = n; - } - return total; -} - -static hb_paint_extend_t -svg_color_line_get_extend (hb_color_line_t *color_line HB_UNUSED, - void *color_line_data, - void *user_data HB_UNUSED) -{ - hb_svg_color_line_data_t *cl = (hb_svg_color_line_data_t *) color_line_data; - return cl->extend; -} - - -/* - * 11. SVG rendering context + * 10. SVG rendering context */ #define SVG_MAX_DEPTH 32 @@ -570,158 +505,6 @@ svg_parse_id_ref_with_fallback (hb_svg_str_t s, * Emit fill (solid or gradient) */ -static void -svg_emit_fill (hb_svg_render_context_t *ctx, - hb_svg_str_t fill_str, - float fill_opacity, - const hb_extents_t<> *object_bbox, - hb_color_t current_color) -{ - bool is_none = false; - - char url_id[64]; - hb_svg_str_t fallback_paint; - bool has_url_paint = svg_parse_id_ref_with_fallback (fill_str, url_id, &fallback_paint, false); - - /* Check for gradient reference */ - const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs.find_gradient (url_id) : nullptr; - if (has_url_paint && !grad) - { - if (fallback_paint.len) - svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); - return; - } - if (grad) - { - /* Resolve href chain and inherit unspecified attributes from references. */ - const hb_svg_gradient_t *chain[8]; - unsigned chain_len = 0; - const hb_svg_gradient_t *cur = grad; - while (cur && chain_len < ARRAY_LENGTH (chain)) - { - chain[chain_len++] = cur; - if (!cur->href_id[0]) break; - const hb_svg_gradient_t *next = ctx->defs.find_gradient (cur->href_id); - if (!next) break; - bool is_cycle = false; - for (unsigned i = 0; i < chain_len; i++) - if (chain[i] == next) - { - is_cycle = true; - break; - } - if (is_cycle) break; - cur = next; - } - if (!chain_len) return; - - hb_svg_gradient_t effective = *chain[chain_len - 1]; - for (int i = (int) chain_len - 2; i >= 0; i--) - { - const hb_svg_gradient_t *g = chain[i]; - effective.type = g->type; - if (g->stops.length) effective.stops = g->stops; - if (g->has_spread) { effective.spread = g->spread; effective.has_spread = true; } - if (g->has_units_user_space) - { - effective.units_user_space = g->units_user_space; - effective.has_units_user_space = true; - } - if (g->has_gradient_transform) - { - effective.gradient_transform = g->gradient_transform; - effective.has_gradient_transform = true; - } - if (g->has_x1) { effective.x1 = g->x1; effective.has_x1 = true; } - if (g->has_y1) { effective.y1 = g->y1; effective.has_y1 = true; } - if (g->has_x2) { effective.x2 = g->x2; effective.has_x2 = true; } - if (g->has_y2) { effective.y2 = g->y2; effective.has_y2 = true; } - if (g->has_cx) { effective.cx = g->cx; effective.has_cx = true; } - if (g->has_cy) { effective.cy = g->cy; effective.has_cy = true; } - if (g->has_r) { effective.r = g->r; effective.has_r = true; } - if (g->has_fx) { effective.fx = g->fx; effective.has_fx = true; } - if (g->has_fy) { effective.fy = g->fy; effective.has_fy = true; } - } - if (!effective.stops.length) - return; - - hb_svg_color_line_data_t cl_data; - cl_data.grad = &effective; - cl_data.extend = effective.spread; - cl_data.alpha_scale = hb_clamp (fill_opacity, 0.f, 1.f); - cl_data.current_color = current_color; - - hb_color_line_t cl = { - &cl_data, - svg_color_line_get_stops, nullptr, - svg_color_line_get_extend, nullptr - }; - - bool has_bbox_transform = !effective.units_user_space && object_bbox && !object_bbox->is_empty (); - if (has_bbox_transform) - { - float w = object_bbox->xmax - object_bbox->xmin; - float h = object_bbox->ymax - object_bbox->ymin; - if (w > 0 && h > 0) - ctx->push_transform (w, 0, 0, h, object_bbox->xmin, object_bbox->ymin); - else - has_bbox_transform = false; - } - - if (effective.has_gradient_transform) - ctx->push_transform (effective.gradient_transform.xx, - effective.gradient_transform.yx, - effective.gradient_transform.xy, - effective.gradient_transform.yy, - effective.gradient_transform.dx, - effective.gradient_transform.dy); - - if (effective.type == SVG_GRADIENT_LINEAR) - { - hb_paint_linear_gradient (ctx->pfuncs, ctx->paint, &cl, - effective.x1, effective.y1, - effective.x2, effective.y2, - effective.x2 - (effective.y2 - effective.y1), - effective.y2 + (effective.x2 - effective.x1)); - } - else /* SVG_GRADIENT_RADIAL */ - { - float fx = effective.has_fx ? effective.fx : effective.cx; - float fy = effective.has_fy ? effective.fy : effective.cy; - - hb_paint_radial_gradient (ctx->pfuncs, ctx->paint, &cl, - fx, fy, 0.f, - effective.cx, effective.cy, effective.r); - } - - if (effective.has_gradient_transform) - ctx->pop_transform (); - if (has_bbox_transform) - ctx->pop_transform (); - - return; - } - - /* Solid color */ - hb_color_t color = svg_parse_color (fill_str, - ctx->pfuncs, - ctx->paint, - current_color, - hb_font_get_face (ctx->font), - ctx->palette, - &is_none); - if (is_none) return; - - if (fill_opacity < 1.f) - color = HB_COLOR (hb_color_get_blue (color), - hb_color_get_green (color), - hb_color_get_red (color), - (uint8_t) (hb_color_get_alpha (color) * fill_opacity + 0.5f)); - - ctx->paint_color (color); -} - - /* * 11. Element renderer — recursive SVG rendering */ @@ -847,7 +630,10 @@ svg_render_shape (hb_svg_render_context_t *ctx, ctx->paint_color (black); } else - svg_emit_fill (ctx, state.fill, state.fill_opacity, has_bbox ? &bbox : nullptr, state.color); + { + hb_svg_fill_context_t fill_ctx = {ctx->paint, ctx->pfuncs, ctx->font, ctx->palette, &ctx->defs}; + svg_emit_fill (&fill_ctx, state.fill, state.fill_opacity, has_bbox ? &bbox : nullptr, state.color); + } ctx->pop_clip (); if (has_clip_path) diff --git a/src/meson.build b/src/meson.build index cc0586cc3..8adc3512f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -715,6 +715,8 @@ hb_raster_sources = [ 'hb-raster-svg-clip.hh', 'hb-raster-svg-bbox.cc', 'hb-raster-svg-bbox.hh', + 'hb-raster-svg-fill.cc', + 'hb-raster-svg-fill.hh', 'hb-raster-svg-parse.cc', 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', From 0b4f03d470c6af3e9c792f41dbd6c860a843edae Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:07:16 -0300 Subject: [PATCH 022/105] [raster] Split SVG use helpers into hb-raster-svg-use --- src/hb-raster-svg-use.cc | 186 ++++++++++++++++++++++++++++++++++++++ src/hb-raster-svg-use.hh | 62 +++++++++++++ src/hb-raster-svg.cc | 188 +++------------------------------------ src/meson.build | 2 + 4 files changed, 264 insertions(+), 174 deletions(-) create mode 100644 src/hb-raster-svg-use.cc create mode 100644 src/hb-raster-svg-use.hh diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc new file mode 100644 index 000000000..43e75ae19 --- /dev/null +++ b/src/hb-raster-svg-use.cc @@ -0,0 +1,186 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-use.hh" + +#include "hb-raster-svg-base.hh" + +#include + +hb_svg_str_t +svg_find_href_attr (const hb_svg_xml_parser_t &parser) +{ + hb_svg_str_t href = parser.find_attr ("href"); + if (href.is_null ()) + href = parser.find_attr ("xlink:href"); + return href; +} + +static bool +svg_find_element_by_id (const hb_svg_use_context_t *ctx, + const char *id, + const char **found) +{ + *found = nullptr; + if (ctx->doc_cache) + { + unsigned start = 0, end = 0; + if (ctx->svg_accel->doc_cache_find_id_cstr (ctx->doc_cache, id, &start, &end)) + { + if (start < ctx->doc_len && end <= ctx->doc_len && start < end) + { + *found = ctx->doc_start + start; + return true; + } + } + } + + hb_svg_xml_parser_t search (ctx->doc_start, ctx->doc_len); + while (true) + { + hb_svg_token_type_t tok = search.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok != SVG_TOKEN_OPEN_TAG && tok != SVG_TOKEN_SELF_CLOSE_TAG) continue; + hb_svg_str_t attr_id = search.find_attr ("id"); + if (attr_id.len && attr_id.eq_cstr (id)) + { + *found = search.tag_start; + return true; + } + } + return false; +} + +static bool +svg_parse_id_ref (hb_svg_str_t s, char out_id[64]) +{ + s = s.trim (); + + if (s.len && s.data[0] == '#') + { + unsigned n = hb_min (s.len - 1, (unsigned) 63); + memcpy (out_id, s.data + 1, n); + out_id[n] = '\0'; + return n > 0; + } + + if (!svg_str_starts_with_ascii_ci (s, "url(")) + return false; + + const char *p = s.data + 4; + const char *end = s.data + s.len; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + + const char *q = p; + char quote = 0; + while (q < end) + { + char c = *q; + if (quote) + { + if (c == quote) quote = 0; + } + else + { + if (c == '"' || c == '\'') quote = c; + else if (c == ')') break; + } + q++; + } + if (q >= end || *q != ')') + return false; + + const char *id_b = p; + const char *id_e = q; + while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; + while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; + if (id_e > id_b && ((*id_b == '\'' && *(id_e - 1) == '\'') || (*id_b == '"' && *(id_e - 1) == '"'))) + { + id_b++; + id_e--; + } + while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; + while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; + if (id_b < id_e && *id_b == '#') id_b++; + if (id_b >= id_e) + return false; + + unsigned n = hb_min ((unsigned) (id_e - id_b), (unsigned) 63); + memcpy (out_id, id_b, n); + out_id[n] = '\0'; + return true; +} + +void +svg_render_use_element (const hb_svg_use_context_t *ctx, + hb_svg_xml_parser_t &parser, + const void *state, + hb_svg_str_t transform_str, + hb_svg_use_render_cb_t render_cb, + void *render_user) +{ + hb_svg_str_t href = svg_find_href_attr (parser); + + char ref_id[64]; + if (!svg_parse_id_ref (href, ref_id)) + return; + + float use_x = svg_parse_float (parser.find_attr ("x")); + float use_y = svg_parse_float (parser.find_attr ("y")); + + bool has_translate = (use_x != 0.f || use_y != 0.f); + bool has_use_transform = transform_str.len > 0; + + if (has_use_transform) + { + hb_svg_transform_t t; + svg_parse_transform (transform_str, &t); + hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); + } + + if (has_translate) + hb_paint_push_transform (ctx->pfuncs, ctx->paint, 1, 0, 0, 1, use_x, use_y); + + const char *found = nullptr; + svg_find_element_by_id (ctx, ref_id, &found); + + if (found) + { + unsigned remaining = ctx->doc_start + ctx->doc_len - found; + hb_svg_xml_parser_t ref_parser (found, remaining); + hb_svg_token_type_t tok = ref_parser.next (); + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + render_cb (render_user, ref_parser, state); + } + + if (has_translate) + hb_paint_pop_transform (ctx->pfuncs, ctx->paint); + + if (has_use_transform) + hb_paint_pop_transform (ctx->pfuncs, ctx->paint); +} diff --git a/src/hb-raster-svg-use.hh b/src/hb-raster-svg-use.hh new file mode 100644 index 000000000..0c135f83b --- /dev/null +++ b/src/hb-raster-svg-use.hh @@ -0,0 +1,62 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_USE_HH +#define HB_RASTER_SVG_USE_HH + +#include "hb.hh" + +#include "OT/Color/svg/svg.hh" +#include "hb-raster-svg-parse.hh" +#include "hb-raster-paint.hh" + +struct hb_svg_use_context_t +{ + hb_raster_paint_t *paint; + hb_paint_funcs_t *pfuncs; + + const char *doc_start; + unsigned doc_len; + const OT::SVG::accelerator_t *svg_accel; + const OT::SVG::svg_doc_cache_t *doc_cache; +}; + +typedef void (*hb_svg_use_render_cb_t) (void *render_user, + hb_svg_xml_parser_t &parser, + const void *state); + +HB_INTERNAL hb_svg_str_t +svg_find_href_attr (const hb_svg_xml_parser_t &parser); + +HB_INTERNAL void +svg_render_use_element (const hb_svg_use_context_t *ctx, + hb_svg_xml_parser_t &parser, + const void *state, + hb_svg_str_t transform_str, + hb_svg_use_render_cb_t render_cb, + void *render_user); + +#endif /* HB_RASTER_SVG_USE_HH */ diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index b56418662..411e3ab20 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -37,6 +37,7 @@ #include "hb-raster-svg-clip.hh" #include "hb-raster-svg-bbox.hh" #include "hb-raster-svg-fill.hh" +#include "hb-raster-svg-use.hh" #include "OT/Color/svg/svg.hh" #include "hb-draw.h" #include "hb-ot-color.h" @@ -361,16 +362,6 @@ svg_parse_color (hb_svg_str_t s, } -/* - * 9. Defs store - */ - -static bool -svg_parse_id_ref_with_fallback (hb_svg_str_t s, - char out_id[64], - hb_svg_str_t *fallback, - bool allow_fragment_direct); - /* * 10. SVG rendering context */ @@ -429,82 +420,6 @@ struct hb_svg_cascade_t float opacity = 1.f; }; -static bool -svg_parse_id_ref_with_fallback (hb_svg_str_t s, - char out_id[64], - hb_svg_str_t *fallback, - bool allow_fragment_direct) -{ - if (fallback) *fallback = {}; - s = s.trim (); - - if (allow_fragment_direct && s.len && s.data[0] == '#') - { - unsigned n = hb_min (s.len - 1, (unsigned) 63); - memcpy (out_id, s.data + 1, n); - out_id[n] = '\0'; - return n > 0; - } - - if (!svg_str_starts_with_ascii_ci (s, "url(")) - return false; - - const char *p = s.data + 4; - const char *end = s.data + s.len; - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - - const char *q = p; - char quote = 0; - while (q < end) - { - char c = *q; - if (quote) - { - if (c == quote) quote = 0; - } - else - { - if (c == '"' || c == '\'') quote = c; - else if (c == ')') break; - } - q++; - } - if (q >= end || *q != ')') - return false; - - const char *id_b = p; - const char *id_e = q; - while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; - while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; - if (id_e > id_b && ((*id_b == '\'' && *(id_e - 1) == '\'') || (*id_b == '"' && *(id_e - 1) == '"'))) - { - id_b++; - id_e--; - } - while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; - while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; - if (id_b < id_e && *id_b == '#') id_b++; - if (id_b >= id_e) - return false; - - unsigned n = hb_min ((unsigned) (id_e - id_b), (unsigned) 63); - memcpy (out_id, id_b, n); - out_id[n] = '\0'; - - if (fallback) - { - const char *f = q + 1; - while (f < end && (*f == ' ' || *f == '\t' || *f == '\n' || *f == '\r')) f++; - if (f < end) *fallback = {f, (unsigned) (end - f)}; - } - return true; -} - - -/* - * Emit fill (solid or gradient) - */ - /* * 11. Element renderer — recursive SVG rendering */ @@ -557,41 +472,6 @@ svg_process_defs (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser) } } -static bool -svg_find_element_by_id (const hb_svg_render_context_t *ctx, - const char *id, - const char **found) -{ - *found = nullptr; - if (ctx->doc_cache) - { - unsigned start = 0, end = 0; - if (ctx->svg_accel->doc_cache_find_id_cstr (ctx->doc_cache, id, &start, &end)) - { - if (start < ctx->doc_len && end <= ctx->doc_len && start < end) - { - *found = ctx->doc_start + start; - return true; - } - } - } - - hb_svg_xml_parser_t search (ctx->doc_start, ctx->doc_len); - while (true) - { - hb_svg_token_type_t tok = search.next (); - if (tok == SVG_TOKEN_EOF) break; - if (tok != SVG_TOKEN_OPEN_TAG && tok != SVG_TOKEN_SELF_CLOSE_TAG) continue; - hb_svg_str_t attr_id = search.find_attr ("id"); - if (attr_id.len && attr_id.eq_cstr (id)) - { - *found = search.tag_start; - return true; - } - } - return false; -} - /* Render a single shape element */ static void svg_render_shape (hb_svg_render_context_t *ctx, @@ -866,60 +746,14 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, return false; } -static hb_svg_str_t -svg_find_href_attr (const hb_svg_xml_parser_t &parser) -{ - hb_svg_str_t href = parser.find_attr ("href"); - if (href.is_null ()) - href = parser.find_attr ("xlink:href"); - return href; -} - static void -svg_render_use_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - const hb_svg_cascade_t &state, - hb_svg_str_t transform_str) +svg_render_use_callback (void *render_user, + hb_svg_xml_parser_t &parser, + const void *state) { - hb_svg_str_t href = svg_find_href_attr (parser); - - char ref_id[64]; - if (!svg_parse_id_ref_with_fallback (href, ref_id, nullptr, true)) - return; - - float use_x = svg_parse_float (parser.find_attr ("x")); - float use_y = svg_parse_float (parser.find_attr ("y")); - - bool has_translate = (use_x != 0.f || use_y != 0.f); - bool has_use_transform = transform_str.len > 0; - - if (has_use_transform) - { - hb_svg_transform_t t; - svg_parse_transform (transform_str, &t); - ctx->push_transform (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); - } - - if (has_translate) - ctx->push_transform (1, 0, 0, 1, use_x, use_y); - - const char *found = nullptr; - svg_find_element_by_id (ctx, ref_id, &found); - - if (found) - { - unsigned remaining = ctx->doc_start + ctx->doc_len - found; - hb_svg_xml_parser_t ref_parser (found, remaining); - hb_svg_token_type_t tok = ref_parser.next (); - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - svg_render_element (ctx, ref_parser, state); - } - - if (has_translate) - ctx->pop_transform (); - - if (has_use_transform) - ctx->pop_transform (); + svg_render_element ((hb_svg_render_context_t *) render_user, + parser, + *(const hb_svg_cascade_t *) state); } static void @@ -1017,7 +851,13 @@ svg_render_element (hb_svg_render_context_t *ctx, else if (svg_render_primitive_shape_element (ctx, parser, tag, state, transform_str)) ; else if (tag.eq ("use")) - svg_render_use_element (ctx, parser, state, transform_str); + { + hb_svg_use_context_t use_ctx = {ctx->paint, ctx->pfuncs, + ctx->doc_start, ctx->doc_len, + ctx->svg_accel, ctx->doc_cache}; + svg_render_use_element (&use_ctx, parser, &state, transform_str, + svg_render_use_callback, ctx); + } ctx->depth--; diff --git a/src/meson.build b/src/meson.build index 8adc3512f..489f78211 100644 --- a/src/meson.build +++ b/src/meson.build @@ -717,6 +717,8 @@ hb_raster_sources = [ 'hb-raster-svg-bbox.hh', 'hb-raster-svg-fill.cc', 'hb-raster-svg-fill.hh', + 'hb-raster-svg-use.cc', + 'hb-raster-svg-use.hh', 'hb-raster-svg-parse.cc', 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', From 9f18f09012c70d4f63665cbec155bd6e331ea8f2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:08:19 -0300 Subject: [PATCH 023/105] [raster] Split SVG renderer into hb-raster-svg-render --- src/hb-raster-svg-render.cc | 680 ++++++++++++++++++++++++++++++++++++ src/hb-raster-svg.cc | 633 --------------------------------- src/meson.build | 1 + 3 files changed, 681 insertions(+), 633 deletions(-) create mode 100644 src/hb-raster-svg-render.cc diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc new file mode 100644 index 000000000..979ac6e58 --- /dev/null +++ b/src/hb-raster-svg-render.cc @@ -0,0 +1,680 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-face.hh" +#include "hb-raster.h" +#include "hb-raster-paint.hh" +#include "hb-raster-svg.hh" +#include "hb-raster-svg-base.hh" +#include "hb-raster-svg-parse.hh" +#include "hb-raster-svg-defs.hh" +#include "hb-raster-svg-gradient.hh" +#include "hb-raster-svg-clip.hh" +#include "hb-raster-svg-bbox.hh" +#include "hb-raster-svg-fill.hh" +#include "hb-raster-svg-use.hh" +#include "OT/Color/svg/svg.hh" +#include "hb-draw.h" +#include "hb-ot-color.h" + +#include +#include + + +/* + * 10. SVG rendering context + */ + +#define SVG_MAX_DEPTH 32 + +struct hb_svg_render_context_t +{ + hb_raster_paint_t *paint; + hb_paint_funcs_t *pfuncs; + hb_font_t *font; + unsigned palette; + hb_color_t foreground; + hb_svg_defs_t defs; + int depth = 0; + + /* The full SVG document for resolution */ + const char *doc_start; + unsigned doc_len; + const OT::SVG::accelerator_t *svg_accel = nullptr; + const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; + + void push_transform (float xx, float yx, float xy, float yy, float dx, float dy) + { + hb_paint_push_transform (pfuncs, paint, xx, yx, xy, yy, dx, dy); + } + void pop_transform () + { + hb_paint_pop_transform (pfuncs, paint); + } + void push_group () + { + hb_paint_push_group (pfuncs, paint); + } + void pop_group (hb_paint_composite_mode_t mode) + { + hb_paint_pop_group (pfuncs, paint, mode); + } + void paint_color (hb_color_t color) + { + hb_paint_color (pfuncs, paint, false, color); + } + void pop_clip () + { + hb_paint_pop_clip (pfuncs, paint); + } +}; + +struct hb_svg_cascade_t +{ + hb_svg_str_t fill; + float fill_opacity = 1.f; + hb_svg_str_t clip_path; + hb_color_t color = HB_COLOR (0, 0, 0, 255); + bool visibility = true; + float opacity = 1.f; +}; + +/* + * 11. Element renderer — recursive SVG rendering + */ + +static void svg_render_element (hb_svg_render_context_t *ctx, + hb_svg_xml_parser_t &parser, + const struct hb_svg_cascade_t &inherited); + +/* Gradient def parsing lives in hb-raster-svg-gradient.* */ + +/* Clip-path defs and push helpers live in hb-raster-svg-clip.* */ + +/* Process children — gradients, clipPaths */ +static void +svg_process_defs (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser) +{ + int depth = 1; + + while (depth > 0) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + + if (tok == SVG_TOKEN_CLOSE_TAG) + { + depth--; + continue; + } + + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + if (parser.tag_name.eq ("linearGradient")) + svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, + ctx->pfuncs, ctx->paint, + ctx->foreground, hb_font_get_face (ctx->font), + ctx->palette); + else if (parser.tag_name.eq ("radialGradient")) + svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, + ctx->pfuncs, ctx->paint, + ctx->foreground, hb_font_get_face (ctx->font), + ctx->palette); + else if (parser.tag_name.eq ("clipPath")) + svg_process_clip_path_def (&ctx->defs, parser, tok); + else + { + if (tok == SVG_TOKEN_OPEN_TAG) + depth++; + } + } + } +} + +/* Render a single shape element */ +static void +svg_render_shape (hb_svg_render_context_t *ctx, + hb_svg_shape_emit_data_t &shape, + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str) +{ + bool has_transform = transform_str.len > 0; + bool has_opacity = state.opacity < 1.f; + bool has_clip_path = false; + + if (has_opacity) + ctx->push_group (); + + if (has_transform) + { + hb_svg_transform_t t; + svg_parse_transform (transform_str, &t); + ctx->push_transform (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); + } + + hb_extents_t<> bbox; + bool has_bbox = svg_compute_shape_bbox (shape, &bbox); + has_clip_path = svg_push_clip_path_ref (ctx->paint, &ctx->defs, state.clip_path, + has_bbox ? &bbox : nullptr); + + /* Clip with shape path, then fill */ + hb_raster_paint_push_clip_path (ctx->paint, svg_shape_path_emit, &shape); + + /* Default fill is black */ + if (state.fill.is_null ()) + { + hb_color_t black = HB_COLOR (0, 0, 0, 255); + if (state.fill_opacity < 1.f) + black = HB_COLOR (0, 0, 0, (uint8_t) (255 * state.fill_opacity + 0.5f)); + ctx->paint_color (black); + } + else + { + hb_svg_fill_context_t fill_ctx = {ctx->paint, ctx->pfuncs, ctx->font, ctx->palette, &ctx->defs}; + svg_emit_fill (&fill_ctx, state.fill, state.fill_opacity, has_bbox ? &bbox : nullptr, state.color); + } + + ctx->pop_clip (); + if (has_clip_path) + ctx->pop_clip (); + + if (has_transform) + ctx->pop_transform (); + + if (has_opacity) + ctx->pop_group (HB_PAINT_COMPOSITE_MODE_SRC_OVER); +} + +static void +svg_render_container_element (hb_svg_render_context_t *ctx, + hb_svg_xml_parser_t &parser, + hb_svg_str_t tag, + bool self_closing, + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str, + hb_svg_str_t clip_path_str) +{ + bool has_transform = transform_str.len > 0; + bool has_opacity = state.opacity < 1.f; + bool has_clip = false; + bool has_viewbox = false; + float vb_x = 0, vb_y = 0, vb_w = 0, vb_h = 0; + + if (tag.eq ("svg")) + { + hb_svg_str_t viewbox_str = parser.find_attr ("viewBox"); + if (viewbox_str.len) + { + has_viewbox = true; + hb_svg_float_parser_t vb_fp (viewbox_str); + vb_x = vb_fp.next_float (); + vb_y = vb_fp.next_float (); + vb_w = vb_fp.next_float (); + vb_h = vb_fp.next_float (); + } + } + + if (has_opacity) + ctx->push_group (); + + if (has_transform) + { + hb_svg_transform_t t; + svg_parse_transform (transform_str, &t); + ctx->push_transform (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); + } + + if (has_viewbox && vb_w > 0 && vb_h > 0) + ctx->push_transform (1, 0, 0, 1, -vb_x, -vb_y); + + has_clip = svg_push_clip_path_ref (ctx->paint, &ctx->defs, clip_path_str, nullptr); + + if (!self_closing) + { + int depth = 1; + while (depth > 0) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + + if (tok == SVG_TOKEN_CLOSE_TAG) + { + depth--; + continue; + } + + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + hb_svg_str_t child_tag = parser.tag_name; + if (parser.tag_name.eq ("defs")) + { + if (tok != SVG_TOKEN_SELF_CLOSE_TAG) + svg_process_defs (ctx, parser); + continue; + } + svg_render_element (ctx, parser, state); + if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && + !child_tag.eq ("svg") && !child_tag.eq ("use")) + { + /* Skip children of non-container elements we don't handle. */ + int skip_depth = 1; + while (skip_depth > 0) + { + hb_svg_token_type_t st = parser.next (); + if (st == SVG_TOKEN_EOF) break; + if (st == SVG_TOKEN_CLOSE_TAG) skip_depth--; + else if (st == SVG_TOKEN_OPEN_TAG) skip_depth++; + } + } + } + } + } + + if (has_viewbox && vb_w > 0 && vb_h > 0) + ctx->pop_transform (); + + if (has_clip) + ctx->pop_clip (); + + if (has_transform) + ctx->pop_transform (); + + if (has_opacity) + ctx->pop_group (HB_PAINT_COMPOSITE_MODE_SRC_OVER); +} + +static bool +svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, + hb_svg_xml_parser_t &parser, + hb_svg_str_t tag, + const hb_svg_cascade_t &state, + hb_svg_str_t transform_str) +{ + if (tag.eq ("path")) + { + hb_svg_str_t d = parser.find_attr ("d"); + if (d.len) + { + hb_svg_shape_emit_data_t shape; + shape.type = hb_svg_shape_emit_data_t::SHAPE_PATH; + shape.str_data = d; + svg_render_shape (ctx, shape, state, transform_str); + } + return true; + } + if (tag.eq ("rect")) + { + float x = svg_parse_float (parser.find_attr ("x")); + float y = svg_parse_float (parser.find_attr ("y")); + float w = svg_parse_float (parser.find_attr ("width")); + float h = svg_parse_float (parser.find_attr ("height")); + float rx = svg_parse_float (parser.find_attr ("rx")); + float ry = svg_parse_float (parser.find_attr ("ry")); + + if (w > 0 && h > 0) + { + hb_svg_shape_emit_data_t shape; + shape.type = hb_svg_shape_emit_data_t::SHAPE_RECT; + shape.params[0] = x; + shape.params[1] = y; + shape.params[2] = w; + shape.params[3] = h; + shape.params[4] = rx; + shape.params[5] = ry; + svg_render_shape (ctx, shape, state, transform_str); + } + return true; + } + if (tag.eq ("circle")) + { + float cx = svg_parse_float (parser.find_attr ("cx")); + float cy = svg_parse_float (parser.find_attr ("cy")); + float r = svg_parse_float (parser.find_attr ("r")); + + if (r > 0) + { + hb_svg_shape_emit_data_t shape; + shape.type = hb_svg_shape_emit_data_t::SHAPE_CIRCLE; + shape.params[0] = cx; + shape.params[1] = cy; + shape.params[2] = r; + svg_render_shape (ctx, shape, state, transform_str); + } + return true; + } + if (tag.eq ("ellipse")) + { + float cx = svg_parse_float (parser.find_attr ("cx")); + float cy = svg_parse_float (parser.find_attr ("cy")); + float rx = svg_parse_float (parser.find_attr ("rx")); + float ry = svg_parse_float (parser.find_attr ("ry")); + + if (rx > 0 && ry > 0) + { + hb_svg_shape_emit_data_t shape; + shape.type = hb_svg_shape_emit_data_t::SHAPE_ELLIPSE; + shape.params[0] = cx; + shape.params[1] = cy; + shape.params[2] = rx; + shape.params[3] = ry; + svg_render_shape (ctx, shape, state, transform_str); + } + return true; + } + if (tag.eq ("line")) + { + float x1 = svg_parse_float (parser.find_attr ("x1")); + float y1 = svg_parse_float (parser.find_attr ("y1")); + float x2 = svg_parse_float (parser.find_attr ("x2")); + float y2 = svg_parse_float (parser.find_attr ("y2")); + + hb_svg_shape_emit_data_t shape; + shape.type = hb_svg_shape_emit_data_t::SHAPE_LINE; + shape.params[0] = x1; + shape.params[1] = y1; + shape.params[2] = x2; + shape.params[3] = y2; + svg_render_shape (ctx, shape, state, transform_str); + return true; + } + if (tag.eq ("polyline")) + { + hb_svg_str_t points = parser.find_attr ("points"); + if (points.len) + { + hb_svg_shape_emit_data_t shape; + shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; + shape.str_data = points; + svg_render_shape (ctx, shape, state, transform_str); + } + return true; + } + if (tag.eq ("polygon")) + { + hb_svg_str_t points = parser.find_attr ("points"); + if (points.len) + { + hb_svg_shape_emit_data_t shape; + shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; + shape.str_data = points; + svg_render_shape (ctx, shape, state, transform_str); + } + return true; + } + + return false; +} + +static void +svg_render_use_callback (void *render_user, + hb_svg_xml_parser_t &parser, + const void *state) +{ + svg_render_element ((hb_svg_render_context_t *) render_user, + parser, + *(const hb_svg_cascade_t *) state); +} + +static void +svg_skip_subtree (hb_svg_xml_parser_t &parser) +{ + int depth = 1; + while (depth > 0) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok == SVG_TOKEN_CLOSE_TAG) depth--; + else if (tok == SVG_TOKEN_OPEN_TAG) depth++; + } +} + +/* Render one element (may be a container or shape) */ +static void +svg_render_element (hb_svg_render_context_t *ctx, + hb_svg_xml_parser_t &parser, + const hb_svg_cascade_t &inherited) +{ + if (ctx->depth >= SVG_MAX_DEPTH) return; + + const HB_UNUSED unsigned transform_depth = ctx->paint->transform_stack.length; + const HB_UNUSED unsigned clip_depth = ctx->paint->clip_stack.length; + const HB_UNUSED unsigned surface_depth = ctx->paint->surface_stack.length; + + ctx->depth++; + + hb_svg_str_t tag = parser.tag_name; + bool self_closing = parser.self_closing; + + /* Extract common attributes */ + hb_svg_str_t style = parser.find_attr ("style"); + hb_svg_style_props_t style_props; + svg_parse_style_props (style, &style_props); + hb_svg_str_t fill_attr = svg_pick_attr_or_style (parser, style_props.fill, "fill"); + hb_svg_str_t fill_opacity_str = svg_pick_attr_or_style (parser, style_props.fill_opacity, "fill-opacity"); + hb_svg_str_t opacity_str = svg_pick_attr_or_style (parser, style_props.opacity, "opacity"); + hb_svg_str_t transform_str = svg_pick_attr_or_style (parser, style_props.transform, "transform"); + hb_svg_str_t clip_path_attr = svg_pick_attr_or_style (parser, style_props.clip_path, "clip-path"); + hb_svg_str_t display_str = svg_pick_attr_or_style (parser, style_props.display, "display"); + hb_svg_str_t color_str = svg_pick_attr_or_style (parser, style_props.color, "color"); + hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, style_props.visibility, "visibility"); + + hb_svg_cascade_t state = inherited; + state.fill = (fill_attr.is_null () || svg_str_is_inherit (fill_attr)) ? inherited.fill : fill_attr; + state.fill_opacity = (fill_opacity_str.len && !svg_str_is_inherit (fill_opacity_str)) + ? svg_parse_float_clamped01 (fill_opacity_str) + : inherited.fill_opacity; + state.opacity = (opacity_str.len && !svg_str_is_inherit (opacity_str) && !svg_str_is_none (opacity_str)) + ? svg_parse_float_clamped01 (opacity_str) + : (svg_str_is_inherit (opacity_str) ? inherited.opacity : 1.f); + state.clip_path = (clip_path_attr.is_null () || svg_str_is_inherit (clip_path_attr)) + ? inherited.clip_path + : clip_path_attr; + if (svg_str_is_inherit (transform_str) || svg_str_is_none (transform_str)) + transform_str = {}; + state.color = inherited.color; + bool is_none = false; + if (color_str.len && !svg_str_eq_ascii_ci (color_str.trim (), "inherit")) + state.color = svg_parse_color (color_str, ctx->pfuncs, ctx->paint, + inherited.color, hb_font_get_face (ctx->font), + ctx->palette, &is_none); + state.visibility = inherited.visibility; + hb_svg_str_t visibility_trim = visibility_str.trim (); + if (visibility_trim.len && !svg_str_eq_ascii_ci (visibility_trim, "inherit")) + state.visibility = !(svg_str_eq_ascii_ci (visibility_trim, "hidden") || + svg_str_eq_ascii_ci (visibility_trim, "collapse")); + + if (svg_str_eq_ascii_ci (display_str.trim (), "none")) + { + if (!self_closing) + svg_skip_subtree (parser); + ctx->depth--; + assert (ctx->paint->transform_stack.length == transform_depth); + assert (ctx->paint->clip_stack.length == clip_depth); + assert (ctx->paint->surface_stack.length == surface_depth); + return; + } + if (!state.visibility) + { + if (!self_closing) + svg_skip_subtree (parser); + ctx->depth--; + assert (ctx->paint->transform_stack.length == transform_depth); + assert (ctx->paint->clip_stack.length == clip_depth); + assert (ctx->paint->surface_stack.length == surface_depth); + return; + } + + if (tag.eq ("g") || tag.eq ("svg")) + svg_render_container_element (ctx, parser, tag, self_closing, + state, transform_str, state.clip_path); + else if (svg_render_primitive_shape_element (ctx, parser, tag, state, transform_str)) + ; + else if (tag.eq ("use")) + { + hb_svg_use_context_t use_ctx = {ctx->paint, ctx->pfuncs, + ctx->doc_start, ctx->doc_len, + ctx->svg_accel, ctx->doc_cache}; + svg_render_use_element (&use_ctx, parser, &state, transform_str, + svg_render_use_callback, ctx); + } + + ctx->depth--; + + assert (ctx->paint->transform_stack.length == transform_depth); + assert (ctx->paint->clip_stack.length == clip_depth); + assert (ctx->paint->surface_stack.length == surface_depth); +} + + +/* + * 12. Entry point + */ + +hb_bool_t +hb_raster_svg_render (hb_raster_paint_t *paint, + hb_blob_t *blob, + hb_codepoint_t glyph, + hb_font_t *font, + unsigned palette, + hb_color_t foreground) +{ + unsigned data_len; + const char *data = hb_blob_get_data (blob, &data_len); + if (!data || !data_len) return false; + + hb_face_t *face = hb_font_get_face (font); + const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; + unsigned doc_index = 0; + hb_codepoint_t start_glyph = HB_CODEPOINT_INVALID; + hb_codepoint_t end_glyph = HB_CODEPOINT_INVALID; + + if (face && + hb_ot_color_glyph_get_svg_document_index (face, glyph, &doc_index) && + hb_ot_color_get_svg_document_glyph_range (face, doc_index, &start_glyph, &end_glyph)) + doc_cache = face->table.SVG->get_or_create_doc_cache (blob, data, data_len, + doc_index, start_glyph, end_glyph); + + if (doc_cache) + data = face->table.SVG->doc_cache_get_svg (doc_cache, &data_len); + + hb_paint_funcs_t *pfuncs = hb_raster_paint_get_funcs (); + + hb_svg_render_context_t ctx; + ctx.paint = paint; + ctx.pfuncs = pfuncs; + ctx.font = font; + ctx.palette = palette; + ctx.foreground = foreground; + ctx.doc_start = data; + ctx.doc_len = data_len; + ctx.svg_accel = face ? face->table.SVG.get () : nullptr; + ctx.doc_cache = doc_cache; + + hb_svg_cascade_t initial_state; + initial_state.color = foreground; + + /* First pass: collect defs (gradients, clip-paths) from the entire document, + * including entries that are authored outside . */ + { + hb_svg_xml_parser_t defs_parser (data, data_len); + while (true) + { + hb_svg_token_type_t tok = defs_parser.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + if (defs_parser.tag_name.eq ("linearGradient")) + svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_LINEAR, + ctx.pfuncs, ctx.paint, + ctx.foreground, hb_font_get_face (ctx.font), + ctx.palette); + else if (defs_parser.tag_name.eq ("radialGradient")) + svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_RADIAL, + ctx.pfuncs, ctx.paint, + ctx.foreground, hb_font_get_face (ctx.font), + ctx.palette); + else if (defs_parser.tag_name.eq ("clipPath")) + svg_process_clip_path_def (&ctx.defs, defs_parser, tok); + } + } + } + + bool found_glyph = false; + unsigned glyph_start = 0, glyph_end = 0; + if (doc_cache && face->table.SVG->doc_cache_get_glyph_span (doc_cache, glyph, &glyph_start, &glyph_end)) + { + hb_svg_xml_parser_t parser (data + glyph_start, glyph_end - glyph_start); + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); + ctx.push_transform (1, 0, 0, -1, 0, 0); + svg_render_element (&ctx, parser, initial_state); + ctx.pop_transform (); + hb_paint_pop_transform (ctx.pfuncs, ctx.paint); + found_glyph = true; + } + } + else + { + /* Fallback for malformed/uncached docs: linear scan by glyph id. */ + char glyph_id_str[32]; + snprintf (glyph_id_str, sizeof (glyph_id_str), "glyph%u", glyph); + hb_svg_xml_parser_t parser (data, data_len); + while (true) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + hb_svg_str_t id = parser.find_attr ("id"); + if (id.len) + { + char id_buf[64]; + unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); + memcpy (id_buf, id.data, n); + id_buf[n] = '\0'; + + if (strcmp (id_buf, glyph_id_str) == 0) + { + hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); + ctx.push_transform (1, 0, 0, -1, 0, 0); + svg_render_element (&ctx, parser, initial_state); + ctx.pop_transform (); + hb_paint_pop_transform (ctx.pfuncs, ctx.paint); + found_glyph = true; + break; + } + } + } + } + } + + return found_glyph; +} diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg.cc index 411e3ab20..8977129f8 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg.cc @@ -361,636 +361,3 @@ svg_parse_color (hb_svg_str_t s, return HB_COLOR (0, 0, 0, 255); } - -/* - * 10. SVG rendering context - */ - -#define SVG_MAX_DEPTH 32 - -struct hb_svg_render_context_t -{ - hb_raster_paint_t *paint; - hb_paint_funcs_t *pfuncs; - hb_font_t *font; - unsigned palette; - hb_color_t foreground; - hb_svg_defs_t defs; - int depth = 0; - - /* The full SVG document for resolution */ - const char *doc_start; - unsigned doc_len; - const OT::SVG::accelerator_t *svg_accel = nullptr; - const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; - - void push_transform (float xx, float yx, float xy, float yy, float dx, float dy) - { - hb_paint_push_transform (pfuncs, paint, xx, yx, xy, yy, dx, dy); - } - void pop_transform () - { - hb_paint_pop_transform (pfuncs, paint); - } - void push_group () - { - hb_paint_push_group (pfuncs, paint); - } - void pop_group (hb_paint_composite_mode_t mode) - { - hb_paint_pop_group (pfuncs, paint, mode); - } - void paint_color (hb_color_t color) - { - hb_paint_color (pfuncs, paint, false, color); - } - void pop_clip () - { - hb_paint_pop_clip (pfuncs, paint); - } -}; - -struct hb_svg_cascade_t -{ - hb_svg_str_t fill; - float fill_opacity = 1.f; - hb_svg_str_t clip_path; - hb_color_t color = HB_COLOR (0, 0, 0, 255); - bool visibility = true; - float opacity = 1.f; -}; - -/* - * 11. Element renderer — recursive SVG rendering - */ - -static void svg_render_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - const struct hb_svg_cascade_t &inherited); - -/* Gradient def parsing lives in hb-raster-svg-gradient.* */ - -/* Clip-path defs and push helpers live in hb-raster-svg-clip.* */ - -/* Process children — gradients, clipPaths */ -static void -svg_process_defs (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser) -{ - int depth = 1; - - while (depth > 0) - { - hb_svg_token_type_t tok = parser.next (); - if (tok == SVG_TOKEN_EOF) break; - - if (tok == SVG_TOKEN_CLOSE_TAG) - { - depth--; - continue; - } - - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - { - if (parser.tag_name.eq ("linearGradient")) - svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, - ctx->pfuncs, ctx->paint, - ctx->foreground, hb_font_get_face (ctx->font), - ctx->palette); - else if (parser.tag_name.eq ("radialGradient")) - svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, - ctx->pfuncs, ctx->paint, - ctx->foreground, hb_font_get_face (ctx->font), - ctx->palette); - else if (parser.tag_name.eq ("clipPath")) - svg_process_clip_path_def (&ctx->defs, parser, tok); - else - { - if (tok == SVG_TOKEN_OPEN_TAG) - depth++; - } - } - } -} - -/* Render a single shape element */ -static void -svg_render_shape (hb_svg_render_context_t *ctx, - hb_svg_shape_emit_data_t &shape, - const hb_svg_cascade_t &state, - hb_svg_str_t transform_str) -{ - bool has_transform = transform_str.len > 0; - bool has_opacity = state.opacity < 1.f; - bool has_clip_path = false; - - if (has_opacity) - ctx->push_group (); - - if (has_transform) - { - hb_svg_transform_t t; - svg_parse_transform (transform_str, &t); - ctx->push_transform (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); - } - - hb_extents_t<> bbox; - bool has_bbox = svg_compute_shape_bbox (shape, &bbox); - has_clip_path = svg_push_clip_path_ref (ctx->paint, &ctx->defs, state.clip_path, - has_bbox ? &bbox : nullptr); - - /* Clip with shape path, then fill */ - hb_raster_paint_push_clip_path (ctx->paint, svg_shape_path_emit, &shape); - - /* Default fill is black */ - if (state.fill.is_null ()) - { - hb_color_t black = HB_COLOR (0, 0, 0, 255); - if (state.fill_opacity < 1.f) - black = HB_COLOR (0, 0, 0, (uint8_t) (255 * state.fill_opacity + 0.5f)); - ctx->paint_color (black); - } - else - { - hb_svg_fill_context_t fill_ctx = {ctx->paint, ctx->pfuncs, ctx->font, ctx->palette, &ctx->defs}; - svg_emit_fill (&fill_ctx, state.fill, state.fill_opacity, has_bbox ? &bbox : nullptr, state.color); - } - - ctx->pop_clip (); - if (has_clip_path) - ctx->pop_clip (); - - if (has_transform) - ctx->pop_transform (); - - if (has_opacity) - ctx->pop_group (HB_PAINT_COMPOSITE_MODE_SRC_OVER); -} - -static void -svg_render_container_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_str_t tag, - bool self_closing, - const hb_svg_cascade_t &state, - hb_svg_str_t transform_str, - hb_svg_str_t clip_path_str) -{ - bool has_transform = transform_str.len > 0; - bool has_opacity = state.opacity < 1.f; - bool has_clip = false; - bool has_viewbox = false; - float vb_x = 0, vb_y = 0, vb_w = 0, vb_h = 0; - - if (tag.eq ("svg")) - { - hb_svg_str_t viewbox_str = parser.find_attr ("viewBox"); - if (viewbox_str.len) - { - has_viewbox = true; - hb_svg_float_parser_t vb_fp (viewbox_str); - vb_x = vb_fp.next_float (); - vb_y = vb_fp.next_float (); - vb_w = vb_fp.next_float (); - vb_h = vb_fp.next_float (); - } - } - - if (has_opacity) - ctx->push_group (); - - if (has_transform) - { - hb_svg_transform_t t; - svg_parse_transform (transform_str, &t); - ctx->push_transform (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); - } - - if (has_viewbox && vb_w > 0 && vb_h > 0) - ctx->push_transform (1, 0, 0, 1, -vb_x, -vb_y); - - has_clip = svg_push_clip_path_ref (ctx->paint, &ctx->defs, clip_path_str, nullptr); - - if (!self_closing) - { - int depth = 1; - while (depth > 0) - { - hb_svg_token_type_t tok = parser.next (); - if (tok == SVG_TOKEN_EOF) break; - - if (tok == SVG_TOKEN_CLOSE_TAG) - { - depth--; - continue; - } - - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - { - hb_svg_str_t child_tag = parser.tag_name; - if (parser.tag_name.eq ("defs")) - { - if (tok != SVG_TOKEN_SELF_CLOSE_TAG) - svg_process_defs (ctx, parser); - continue; - } - svg_render_element (ctx, parser, state); - if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && - !child_tag.eq ("svg") && !child_tag.eq ("use")) - { - /* Skip children of non-container elements we don't handle. */ - int skip_depth = 1; - while (skip_depth > 0) - { - hb_svg_token_type_t st = parser.next (); - if (st == SVG_TOKEN_EOF) break; - if (st == SVG_TOKEN_CLOSE_TAG) skip_depth--; - else if (st == SVG_TOKEN_OPEN_TAG) skip_depth++; - } - } - } - } - } - - if (has_viewbox && vb_w > 0 && vb_h > 0) - ctx->pop_transform (); - - if (has_clip) - ctx->pop_clip (); - - if (has_transform) - ctx->pop_transform (); - - if (has_opacity) - ctx->pop_group (HB_PAINT_COMPOSITE_MODE_SRC_OVER); -} - -static bool -svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - hb_svg_str_t tag, - const hb_svg_cascade_t &state, - hb_svg_str_t transform_str) -{ - if (tag.eq ("path")) - { - hb_svg_str_t d = parser.find_attr ("d"); - if (d.len) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_PATH; - shape.str_data = d; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("rect")) - { - float x = svg_parse_float (parser.find_attr ("x")); - float y = svg_parse_float (parser.find_attr ("y")); - float w = svg_parse_float (parser.find_attr ("width")); - float h = svg_parse_float (parser.find_attr ("height")); - float rx = svg_parse_float (parser.find_attr ("rx")); - float ry = svg_parse_float (parser.find_attr ("ry")); - - if (w > 0 && h > 0) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_RECT; - shape.params[0] = x; - shape.params[1] = y; - shape.params[2] = w; - shape.params[3] = h; - shape.params[4] = rx; - shape.params[5] = ry; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("circle")) - { - float cx = svg_parse_float (parser.find_attr ("cx")); - float cy = svg_parse_float (parser.find_attr ("cy")); - float r = svg_parse_float (parser.find_attr ("r")); - - if (r > 0) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_CIRCLE; - shape.params[0] = cx; - shape.params[1] = cy; - shape.params[2] = r; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("ellipse")) - { - float cx = svg_parse_float (parser.find_attr ("cx")); - float cy = svg_parse_float (parser.find_attr ("cy")); - float rx = svg_parse_float (parser.find_attr ("rx")); - float ry = svg_parse_float (parser.find_attr ("ry")); - - if (rx > 0 && ry > 0) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_ELLIPSE; - shape.params[0] = cx; - shape.params[1] = cy; - shape.params[2] = rx; - shape.params[3] = ry; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("line")) - { - float x1 = svg_parse_float (parser.find_attr ("x1")); - float y1 = svg_parse_float (parser.find_attr ("y1")); - float x2 = svg_parse_float (parser.find_attr ("x2")); - float y2 = svg_parse_float (parser.find_attr ("y2")); - - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_LINE; - shape.params[0] = x1; - shape.params[1] = y1; - shape.params[2] = x2; - shape.params[3] = y2; - svg_render_shape (ctx, shape, state, transform_str); - return true; - } - if (tag.eq ("polyline")) - { - hb_svg_str_t points = parser.find_attr ("points"); - if (points.len) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; - shape.str_data = points; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("polygon")) - { - hb_svg_str_t points = parser.find_attr ("points"); - if (points.len) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; - shape.str_data = points; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - - return false; -} - -static void -svg_render_use_callback (void *render_user, - hb_svg_xml_parser_t &parser, - const void *state) -{ - svg_render_element ((hb_svg_render_context_t *) render_user, - parser, - *(const hb_svg_cascade_t *) state); -} - -static void -svg_skip_subtree (hb_svg_xml_parser_t &parser) -{ - int depth = 1; - while (depth > 0) - { - hb_svg_token_type_t tok = parser.next (); - if (tok == SVG_TOKEN_EOF) break; - if (tok == SVG_TOKEN_CLOSE_TAG) depth--; - else if (tok == SVG_TOKEN_OPEN_TAG) depth++; - } -} - -/* Render one element (may be a container or shape) */ -static void -svg_render_element (hb_svg_render_context_t *ctx, - hb_svg_xml_parser_t &parser, - const hb_svg_cascade_t &inherited) -{ - if (ctx->depth >= SVG_MAX_DEPTH) return; - - const HB_UNUSED unsigned transform_depth = ctx->paint->transform_stack.length; - const HB_UNUSED unsigned clip_depth = ctx->paint->clip_stack.length; - const HB_UNUSED unsigned surface_depth = ctx->paint->surface_stack.length; - - ctx->depth++; - - hb_svg_str_t tag = parser.tag_name; - bool self_closing = parser.self_closing; - - /* Extract common attributes */ - hb_svg_str_t style = parser.find_attr ("style"); - hb_svg_style_props_t style_props; - svg_parse_style_props (style, &style_props); - hb_svg_str_t fill_attr = svg_pick_attr_or_style (parser, style_props.fill, "fill"); - hb_svg_str_t fill_opacity_str = svg_pick_attr_or_style (parser, style_props.fill_opacity, "fill-opacity"); - hb_svg_str_t opacity_str = svg_pick_attr_or_style (parser, style_props.opacity, "opacity"); - hb_svg_str_t transform_str = svg_pick_attr_or_style (parser, style_props.transform, "transform"); - hb_svg_str_t clip_path_attr = svg_pick_attr_or_style (parser, style_props.clip_path, "clip-path"); - hb_svg_str_t display_str = svg_pick_attr_or_style (parser, style_props.display, "display"); - hb_svg_str_t color_str = svg_pick_attr_or_style (parser, style_props.color, "color"); - hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, style_props.visibility, "visibility"); - - hb_svg_cascade_t state = inherited; - state.fill = (fill_attr.is_null () || svg_str_is_inherit (fill_attr)) ? inherited.fill : fill_attr; - state.fill_opacity = (fill_opacity_str.len && !svg_str_is_inherit (fill_opacity_str)) - ? svg_parse_float_clamped01 (fill_opacity_str) - : inherited.fill_opacity; - state.opacity = (opacity_str.len && !svg_str_is_inherit (opacity_str) && !svg_str_is_none (opacity_str)) - ? svg_parse_float_clamped01 (opacity_str) - : (svg_str_is_inherit (opacity_str) ? inherited.opacity : 1.f); - state.clip_path = (clip_path_attr.is_null () || svg_str_is_inherit (clip_path_attr)) - ? inherited.clip_path - : clip_path_attr; - if (svg_str_is_inherit (transform_str) || svg_str_is_none (transform_str)) - transform_str = {}; - state.color = inherited.color; - bool is_none = false; - if (color_str.len && !svg_str_eq_ascii_ci (color_str.trim (), "inherit")) - state.color = svg_parse_color (color_str, ctx->pfuncs, ctx->paint, - inherited.color, hb_font_get_face (ctx->font), - ctx->palette, &is_none); - state.visibility = inherited.visibility; - hb_svg_str_t visibility_trim = visibility_str.trim (); - if (visibility_trim.len && !svg_str_eq_ascii_ci (visibility_trim, "inherit")) - state.visibility = !(svg_str_eq_ascii_ci (visibility_trim, "hidden") || - svg_str_eq_ascii_ci (visibility_trim, "collapse")); - - if (svg_str_eq_ascii_ci (display_str.trim (), "none")) - { - if (!self_closing) - svg_skip_subtree (parser); - ctx->depth--; - assert (ctx->paint->transform_stack.length == transform_depth); - assert (ctx->paint->clip_stack.length == clip_depth); - assert (ctx->paint->surface_stack.length == surface_depth); - return; - } - if (!state.visibility) - { - if (!self_closing) - svg_skip_subtree (parser); - ctx->depth--; - assert (ctx->paint->transform_stack.length == transform_depth); - assert (ctx->paint->clip_stack.length == clip_depth); - assert (ctx->paint->surface_stack.length == surface_depth); - return; - } - - if (tag.eq ("g") || tag.eq ("svg")) - svg_render_container_element (ctx, parser, tag, self_closing, - state, transform_str, state.clip_path); - else if (svg_render_primitive_shape_element (ctx, parser, tag, state, transform_str)) - ; - else if (tag.eq ("use")) - { - hb_svg_use_context_t use_ctx = {ctx->paint, ctx->pfuncs, - ctx->doc_start, ctx->doc_len, - ctx->svg_accel, ctx->doc_cache}; - svg_render_use_element (&use_ctx, parser, &state, transform_str, - svg_render_use_callback, ctx); - } - - ctx->depth--; - - assert (ctx->paint->transform_stack.length == transform_depth); - assert (ctx->paint->clip_stack.length == clip_depth); - assert (ctx->paint->surface_stack.length == surface_depth); -} - - -/* - * 12. Entry point - */ - -hb_bool_t -hb_raster_svg_render (hb_raster_paint_t *paint, - hb_blob_t *blob, - hb_codepoint_t glyph, - hb_font_t *font, - unsigned palette, - hb_color_t foreground) -{ - unsigned data_len; - const char *data = hb_blob_get_data (blob, &data_len); - if (!data || !data_len) return false; - - hb_face_t *face = hb_font_get_face (font); - const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; - unsigned doc_index = 0; - hb_codepoint_t start_glyph = HB_CODEPOINT_INVALID; - hb_codepoint_t end_glyph = HB_CODEPOINT_INVALID; - - if (face && - hb_ot_color_glyph_get_svg_document_index (face, glyph, &doc_index) && - hb_ot_color_get_svg_document_glyph_range (face, doc_index, &start_glyph, &end_glyph)) - doc_cache = face->table.SVG->get_or_create_doc_cache (blob, data, data_len, - doc_index, start_glyph, end_glyph); - - if (doc_cache) - data = face->table.SVG->doc_cache_get_svg (doc_cache, &data_len); - - hb_paint_funcs_t *pfuncs = hb_raster_paint_get_funcs (); - - hb_svg_render_context_t ctx; - ctx.paint = paint; - ctx.pfuncs = pfuncs; - ctx.font = font; - ctx.palette = palette; - ctx.foreground = foreground; - ctx.doc_start = data; - ctx.doc_len = data_len; - ctx.svg_accel = face ? face->table.SVG.get () : nullptr; - ctx.doc_cache = doc_cache; - - hb_svg_cascade_t initial_state; - initial_state.color = foreground; - - /* First pass: collect defs (gradients, clip-paths) from the entire document, - * including entries that are authored outside . */ - { - hb_svg_xml_parser_t defs_parser (data, data_len); - while (true) - { - hb_svg_token_type_t tok = defs_parser.next (); - if (tok == SVG_TOKEN_EOF) break; - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - { - if (defs_parser.tag_name.eq ("linearGradient")) - svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_LINEAR, - ctx.pfuncs, ctx.paint, - ctx.foreground, hb_font_get_face (ctx.font), - ctx.palette); - else if (defs_parser.tag_name.eq ("radialGradient")) - svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_RADIAL, - ctx.pfuncs, ctx.paint, - ctx.foreground, hb_font_get_face (ctx.font), - ctx.palette); - else if (defs_parser.tag_name.eq ("clipPath")) - svg_process_clip_path_def (&ctx.defs, defs_parser, tok); - } - } - } - - bool found_glyph = false; - unsigned glyph_start = 0, glyph_end = 0; - if (doc_cache && face->table.SVG->doc_cache_get_glyph_span (doc_cache, glyph, &glyph_start, &glyph_end)) - { - hb_svg_xml_parser_t parser (data + glyph_start, glyph_end - glyph_start); - hb_svg_token_type_t tok = parser.next (); - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - { - hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); - ctx.push_transform (1, 0, 0, -1, 0, 0); - svg_render_element (&ctx, parser, initial_state); - ctx.pop_transform (); - hb_paint_pop_transform (ctx.pfuncs, ctx.paint); - found_glyph = true; - } - } - else - { - /* Fallback for malformed/uncached docs: linear scan by glyph id. */ - char glyph_id_str[32]; - snprintf (glyph_id_str, sizeof (glyph_id_str), "glyph%u", glyph); - hb_svg_xml_parser_t parser (data, data_len); - while (true) - { - hb_svg_token_type_t tok = parser.next (); - if (tok == SVG_TOKEN_EOF) break; - - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - { - hb_svg_str_t id = parser.find_attr ("id"); - if (id.len) - { - char id_buf[64]; - unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); - memcpy (id_buf, id.data, n); - id_buf[n] = '\0'; - - if (strcmp (id_buf, glyph_id_str) == 0) - { - hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); - ctx.push_transform (1, 0, 0, -1, 0, 0); - svg_render_element (&ctx, parser, initial_state); - ctx.pop_transform (); - hb_paint_pop_transform (ctx.pfuncs, ctx.paint); - found_glyph = true; - break; - } - } - } - } - } - - return found_glyph; -} diff --git a/src/meson.build b/src/meson.build index 489f78211..37bc35d36 100644 --- a/src/meson.build +++ b/src/meson.build @@ -719,6 +719,7 @@ hb_raster_sources = [ 'hb-raster-svg-fill.hh', 'hb-raster-svg-use.cc', 'hb-raster-svg-use.hh', + 'hb-raster-svg-render.cc', 'hb-raster-svg-parse.cc', 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', From 5c2882b5c8855d21dfe2b2487643542a4b09bd38 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:11:48 -0300 Subject: [PATCH 024/105] [raster] Split SVG color parser and remove hb-raster-svg.cc --- ...b-raster-svg.cc => hb-raster-svg-color.cc} | 31 +------------ src/hb-raster-svg-color.hh | 44 +++++++++++++++++++ src/hb-raster-svg-gradient.hh | 10 +---- src/meson.build | 3 +- 4 files changed, 49 insertions(+), 39 deletions(-) rename src/{hb-raster-svg.cc => hb-raster-svg-color.cc} (94%) create mode 100644 src/hb-raster-svg-color.hh diff --git a/src/hb-raster-svg.cc b/src/hb-raster-svg-color.cc similarity index 94% rename from src/hb-raster-svg.cc rename to src/hb-raster-svg-color.cc index 8977129f8..1cb7a1711 100644 --- a/src/hb-raster-svg.cc +++ b/src/hb-raster-svg-color.cc @@ -26,40 +26,14 @@ #include "hb.hh" -#include "hb-face.hh" +#include "hb-raster-svg-color.hh" + #include "hb-raster.h" -#include "hb-raster-paint.hh" -#include "hb-raster-svg.hh" #include "hb-raster-svg-base.hh" -#include "hb-raster-svg-parse.hh" -#include "hb-raster-svg-defs.hh" -#include "hb-raster-svg-gradient.hh" -#include "hb-raster-svg-clip.hh" -#include "hb-raster-svg-bbox.hh" -#include "hb-raster-svg-fill.hh" -#include "hb-raster-svg-use.hh" -#include "OT/Color/svg/svg.hh" -#include "hb-draw.h" #include "hb-ot-color.h" #include #include -#include - - -/* - * 1. String / style helpers live in hb-raster-svg-base.* - */ - - -/* - * 2. XML tokenizer / transform / shape parsing live in hb-raster-svg-parse.* - */ - - -/* - * 3. SVG Color parser - */ struct hb_svg_named_color_t { @@ -360,4 +334,3 @@ svg_parse_color (hb_svg_str_t s, return HB_COLOR (0, 0, 0, 255); } - diff --git a/src/hb-raster-svg-color.hh b/src/hb-raster-svg-color.hh new file mode 100644 index 000000000..22b9fe505 --- /dev/null +++ b/src/hb-raster-svg-color.hh @@ -0,0 +1,44 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_COLOR_HH +#define HB_RASTER_SVG_COLOR_HH + +#include "hb.hh" + +#include "hb-face.hh" +#include "hb-raster-svg-base.hh" + +HB_INTERNAL hb_color_t +svg_parse_color (hb_svg_str_t s, + hb_paint_funcs_t *pfuncs, + void *paint_data, + hb_color_t foreground, + hb_face_t *face, + unsigned palette, + bool *is_none); + +#endif /* HB_RASTER_SVG_COLOR_HH */ diff --git a/src/hb-raster-svg-gradient.hh b/src/hb-raster-svg-gradient.hh index 1a2667278..0b49b8bf3 100644 --- a/src/hb-raster-svg-gradient.hh +++ b/src/hb-raster-svg-gradient.hh @@ -30,17 +30,9 @@ #include "hb.hh" #include "hb-face.hh" +#include "hb-raster-svg-color.hh" #include "hb-raster-svg-defs.hh" -HB_INTERNAL hb_color_t -svg_parse_color (hb_svg_str_t s, - hb_paint_funcs_t *pfuncs, - void *paint_data, - hb_color_t foreground, - hb_face_t *face, - unsigned palette, - bool *is_none); - HB_INTERNAL void svg_process_gradient_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, diff --git a/src/meson.build b/src/meson.build index 37bc35d36..8a95bd883 100644 --- a/src/meson.build +++ b/src/meson.build @@ -720,11 +720,12 @@ hb_raster_sources = [ 'hb-raster-svg-use.cc', 'hb-raster-svg-use.hh', 'hb-raster-svg-render.cc', + 'hb-raster-svg-color.cc', + 'hb-raster-svg-color.hh', 'hb-raster-svg-parse.cc', 'hb-raster-svg-parse.hh', 'hb-raster-paint.cc', 'hb-raster-paint.hh', - 'hb-raster-svg.cc', 'hb-raster-svg.hh', 'hb-static.cc', ] From 601c9814d042911eb343996591ace73099a25531 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:14:46 -0300 Subject: [PATCH 025/105] [raster] Share SVG render/fill/use context structs --- src/hb-raster-svg-context.hh | 108 +++++++++++++++++++++++++++++++++++ src/hb-raster-svg-fill.hh | 14 +---- src/hb-raster-svg-render.cc | 57 +----------------- src/hb-raster-svg-use.hh | 14 +---- src/meson.build | 1 + 5 files changed, 112 insertions(+), 82 deletions(-) create mode 100644 src/hb-raster-svg-context.hh diff --git a/src/hb-raster-svg-context.hh b/src/hb-raster-svg-context.hh new file mode 100644 index 000000000..b71e73c4c --- /dev/null +++ b/src/hb-raster-svg-context.hh @@ -0,0 +1,108 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_CONTEXT_HH +#define HB_RASTER_SVG_CONTEXT_HH + +#include "hb.hh" + +#include "OT/Color/svg/svg.hh" +#include "hb-raster-paint.hh" +#include "hb-raster-svg-base.hh" +#include "hb-raster-svg-defs.hh" + +struct hb_svg_fill_context_t +{ + hb_raster_paint_t *paint; + hb_paint_funcs_t *pfuncs; + hb_font_t *font; + unsigned palette; + hb_svg_defs_t *defs; +}; + +struct hb_svg_use_context_t +{ + hb_raster_paint_t *paint; + hb_paint_funcs_t *pfuncs; + + const char *doc_start; + unsigned doc_len; + const OT::SVG::accelerator_t *svg_accel; + const OT::SVG::svg_doc_cache_t *doc_cache; +}; + +struct hb_svg_render_context_t +{ + hb_raster_paint_t *paint; + hb_paint_funcs_t *pfuncs; + hb_font_t *font; + unsigned palette; + hb_color_t foreground; + hb_svg_defs_t defs; + int depth = 0; + + const char *doc_start; + unsigned doc_len; + const OT::SVG::accelerator_t *svg_accel = nullptr; + const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; + + void push_transform (float xx, float yx, float xy, float yy, float dx, float dy) + { + hb_paint_push_transform (pfuncs, paint, xx, yx, xy, yy, dx, dy); + } + void pop_transform () + { + hb_paint_pop_transform (pfuncs, paint); + } + void push_group () + { + hb_paint_push_group (pfuncs, paint); + } + void pop_group (hb_paint_composite_mode_t mode) + { + hb_paint_pop_group (pfuncs, paint, mode); + } + void paint_color (hb_color_t color) + { + hb_paint_color (pfuncs, paint, false, color); + } + void pop_clip () + { + hb_paint_pop_clip (pfuncs, paint); + } +}; + +struct hb_svg_cascade_t +{ + hb_svg_str_t fill; + float fill_opacity = 1.f; + hb_svg_str_t clip_path; + hb_color_t color = HB_COLOR (0, 0, 0, 255); + bool visibility = true; + float opacity = 1.f; +}; + +#endif /* HB_RASTER_SVG_CONTEXT_HH */ diff --git a/src/hb-raster-svg-fill.hh b/src/hb-raster-svg-fill.hh index 7adeaefa3..b8840164d 100644 --- a/src/hb-raster-svg-fill.hh +++ b/src/hb-raster-svg-fill.hh @@ -29,19 +29,7 @@ #include "hb.hh" -#include "hb-face.hh" -#include "hb-raster.h" -#include "hb-raster-paint.hh" -#include "hb-raster-svg-defs.hh" - -struct hb_svg_fill_context_t -{ - hb_raster_paint_t *paint; - hb_paint_funcs_t *pfuncs; - hb_font_t *font; - unsigned palette; - hb_svg_defs_t *defs; -}; +#include "hb-raster-svg-context.hh" HB_INTERNAL void svg_emit_fill (const hb_svg_fill_context_t *ctx, diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 979ac6e58..76ecc9b5e 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -33,6 +33,7 @@ #include "hb-raster-svg-base.hh" #include "hb-raster-svg-parse.hh" #include "hb-raster-svg-defs.hh" +#include "hb-raster-svg-context.hh" #include "hb-raster-svg-gradient.hh" #include "hb-raster-svg-clip.hh" #include "hb-raster-svg-bbox.hh" @@ -46,64 +47,8 @@ #include -/* - * 10. SVG rendering context - */ - #define SVG_MAX_DEPTH 32 -struct hb_svg_render_context_t -{ - hb_raster_paint_t *paint; - hb_paint_funcs_t *pfuncs; - hb_font_t *font; - unsigned palette; - hb_color_t foreground; - hb_svg_defs_t defs; - int depth = 0; - - /* The full SVG document for resolution */ - const char *doc_start; - unsigned doc_len; - const OT::SVG::accelerator_t *svg_accel = nullptr; - const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; - - void push_transform (float xx, float yx, float xy, float yy, float dx, float dy) - { - hb_paint_push_transform (pfuncs, paint, xx, yx, xy, yy, dx, dy); - } - void pop_transform () - { - hb_paint_pop_transform (pfuncs, paint); - } - void push_group () - { - hb_paint_push_group (pfuncs, paint); - } - void pop_group (hb_paint_composite_mode_t mode) - { - hb_paint_pop_group (pfuncs, paint, mode); - } - void paint_color (hb_color_t color) - { - hb_paint_color (pfuncs, paint, false, color); - } - void pop_clip () - { - hb_paint_pop_clip (pfuncs, paint); - } -}; - -struct hb_svg_cascade_t -{ - hb_svg_str_t fill; - float fill_opacity = 1.f; - hb_svg_str_t clip_path; - hb_color_t color = HB_COLOR (0, 0, 0, 255); - bool visibility = true; - float opacity = 1.f; -}; - /* * 11. Element renderer — recursive SVG rendering */ diff --git a/src/hb-raster-svg-use.hh b/src/hb-raster-svg-use.hh index 0c135f83b..393c4d349 100644 --- a/src/hb-raster-svg-use.hh +++ b/src/hb-raster-svg-use.hh @@ -29,20 +29,8 @@ #include "hb.hh" -#include "OT/Color/svg/svg.hh" +#include "hb-raster-svg-context.hh" #include "hb-raster-svg-parse.hh" -#include "hb-raster-paint.hh" - -struct hb_svg_use_context_t -{ - hb_raster_paint_t *paint; - hb_paint_funcs_t *pfuncs; - - const char *doc_start; - unsigned doc_len; - const OT::SVG::accelerator_t *svg_accel; - const OT::SVG::svg_doc_cache_t *doc_cache; -}; typedef void (*hb_svg_use_render_cb_t) (void *render_user, hb_svg_xml_parser_t &parser, diff --git a/src/meson.build b/src/meson.build index 8a95bd883..a91c9e8c3 100644 --- a/src/meson.build +++ b/src/meson.build @@ -717,6 +717,7 @@ hb_raster_sources = [ 'hb-raster-svg-bbox.hh', 'hb-raster-svg-fill.cc', 'hb-raster-svg-fill.hh', + 'hb-raster-svg-context.hh', 'hb-raster-svg-use.cc', 'hb-raster-svg-use.hh', 'hb-raster-svg-render.cc', From d98af91ba1e22980865c367b76e4c24729e057cb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:15:44 -0300 Subject: [PATCH 026/105] [raster] Normalize SVG helper APIs to hb_raster_svg_* --- src/hb-raster-svg-bbox.cc | 4 ++-- src/hb-raster-svg-bbox.hh | 2 +- src/hb-raster-svg-clip.cc | 12 ++++++------ src/hb-raster-svg-clip.hh | 4 ++-- src/hb-raster-svg-color.cc | 4 ++-- src/hb-raster-svg-color.hh | 2 +- src/hb-raster-svg-fill.cc | 6 +++--- src/hb-raster-svg-fill.hh | 2 +- src/hb-raster-svg-gradient.cc | 10 +++++----- src/hb-raster-svg-gradient.hh | 2 +- src/hb-raster-svg-parse.cc | 10 +++++----- src/hb-raster-svg-parse.hh | 8 ++++---- src/hb-raster-svg-render.cc | 30 +++++++++++++++--------------- src/hb-raster-svg-use.cc | 8 ++++---- src/hb-raster-svg-use.hh | 4 ++-- 15 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/hb-raster-svg-bbox.cc b/src/hb-raster-svg-bbox.cc index 777ac8c30..d229e31cf 100644 --- a/src/hb-raster-svg-bbox.cc +++ b/src/hb-raster-svg-bbox.cc @@ -93,12 +93,12 @@ svg_bbox_draw_funcs () } bool -svg_compute_shape_bbox (const hb_svg_shape_emit_data_t &shape, +hb_raster_svg_compute_shape_bbox (const hb_svg_shape_emit_data_t &shape, hb_extents_t<> *bbox) { hb_extents_t<> ext; hb_svg_shape_emit_data_t tmp = shape; - svg_shape_path_emit (svg_bbox_draw_funcs (), &ext, &tmp); + hb_raster_svg_shape_path_emit (svg_bbox_draw_funcs (), &ext, &tmp); if (ext.is_empty ()) return false; *bbox = ext; return true; diff --git a/src/hb-raster-svg-bbox.hh b/src/hb-raster-svg-bbox.hh index d6fb3a566..46802e6d7 100644 --- a/src/hb-raster-svg-bbox.hh +++ b/src/hb-raster-svg-bbox.hh @@ -33,7 +33,7 @@ #include "hb-raster-svg-parse.hh" HB_INTERNAL bool -svg_compute_shape_bbox (const hb_svg_shape_emit_data_t &shape, +hb_raster_svg_compute_shape_bbox (const hb_svg_shape_emit_data_t &shape, hb_extents_t<> *bbox); #endif /* HB_RASTER_SVG_BBOX_HH */ diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 4300427be..17e7f6e89 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -36,7 +36,7 @@ #include void -svg_process_clip_path_def (hb_svg_defs_t *defs, +hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, hb_svg_token_type_t tok) { @@ -52,7 +52,7 @@ svg_process_clip_path_def (hb_svg_defs_t *defs, if (cp_transform.len) { clip.has_clip_transform = true; - svg_parse_transform (cp_transform, &clip.clip_transform); + hb_raster_svg_parse_transform (cp_transform, &clip.clip_transform); } clip.first_shape = defs->clip_shapes.length; @@ -72,7 +72,7 @@ svg_process_clip_path_def (hb_svg_defs_t *defs, if (cdepth == 1) { hb_svg_shape_emit_data_t shape; - if (svg_parse_shape_tag (parser, &shape)) + if (hb_raster_svg_parse_shape_tag (parser, &shape)) { hb_svg_clip_shape_t clip_shape; clip_shape.shape = shape; @@ -80,7 +80,7 @@ svg_process_clip_path_def (hb_svg_defs_t *defs, if (sh_transform.len) { clip_shape.has_transform = true; - svg_parse_transform (sh_transform, &clip_shape.transform); + hb_raster_svg_parse_transform (sh_transform, &clip_shape.transform); } defs->clip_shapes.push (clip_shape); if (likely (!defs->clip_shapes.in_error ())) @@ -145,7 +145,7 @@ svg_clip_path_emit (hb_draw_funcs_t *dfuncs, hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); hb_svg_shape_emit_data_t shape = s.shape; - svg_shape_path_emit (dfuncs, draw_data, &shape); + hb_raster_svg_shape_path_emit (dfuncs, draw_data, &shape); } } @@ -210,7 +210,7 @@ svg_parse_clip_path_id_ref (hb_svg_str_t s, char out_id[64]) } bool -svg_push_clip_path_ref (hb_raster_paint_t *paint, +hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, hb_svg_defs_t *defs, hb_svg_str_t clip_path_str, const hb_extents_t<> *object_bbox) diff --git a/src/hb-raster-svg-clip.hh b/src/hb-raster-svg-clip.hh index f11bc48b2..27a747cc5 100644 --- a/src/hb-raster-svg-clip.hh +++ b/src/hb-raster-svg-clip.hh @@ -35,12 +35,12 @@ struct hb_raster_paint_t; HB_INTERNAL void -svg_process_clip_path_def (hb_svg_defs_t *defs, +hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, hb_svg_token_type_t tok); HB_INTERNAL bool -svg_push_clip_path_ref (hb_raster_paint_t *paint, +hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, hb_svg_defs_t *defs, hb_svg_str_t clip_path_str, const hb_extents_t<> *object_bbox); diff --git a/src/hb-raster-svg-color.cc b/src/hb-raster-svg-color.cc index 1cb7a1711..4c64ddc99 100644 --- a/src/hb-raster-svg-color.cc +++ b/src/hb-raster-svg-color.cc @@ -204,7 +204,7 @@ hexval (char c) /* Parse SVG color value; returns HB_COLOR with alpha = 255. * Sets *is_none if "none". */ hb_color_t -svg_parse_color (hb_svg_str_t s, +hb_raster_svg_parse_color (hb_svg_str_t s, hb_paint_funcs_t *pfuncs, void *paint_data, hb_color_t foreground, @@ -261,7 +261,7 @@ svg_parse_color (hb_svg_str_t s, while (e > val_start && *(e - 1) != ')') e--; if (e > val_start) e--; hb_svg_str_t fallback = {val_start, (unsigned) (e - val_start)}; - return svg_parse_color (fallback, pfuncs, paint_data, foreground, face, palette, is_none); + return hb_raster_svg_parse_color (fallback, pfuncs, paint_data, foreground, face, palette, is_none); } return foreground; diff --git a/src/hb-raster-svg-color.hh b/src/hb-raster-svg-color.hh index 22b9fe505..893072e14 100644 --- a/src/hb-raster-svg-color.hh +++ b/src/hb-raster-svg-color.hh @@ -33,7 +33,7 @@ #include "hb-raster-svg-base.hh" HB_INTERNAL hb_color_t -svg_parse_color (hb_svg_str_t s, +hb_raster_svg_parse_color (hb_svg_str_t s, hb_paint_funcs_t *pfuncs, void *paint_data, hb_color_t foreground, diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index d92ef2c4b..0564fe40f 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -156,7 +156,7 @@ svg_parse_paint_url_with_fallback (hb_svg_str_t s, } void -svg_emit_fill (const hb_svg_fill_context_t *ctx, +hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, hb_svg_str_t fill_str, float fill_opacity, const hb_extents_t<> *object_bbox, @@ -172,7 +172,7 @@ svg_emit_fill (const hb_svg_fill_context_t *ctx, if (has_url_paint && !grad) { if (fallback_paint.len) - svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); + hb_raster_svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); return; } if (grad) @@ -286,7 +286,7 @@ svg_emit_fill (const hb_svg_fill_context_t *ctx, return; } - hb_color_t color = svg_parse_color (fill_str, + hb_color_t color = hb_raster_svg_parse_color (fill_str, ctx->pfuncs, ctx->paint, current_color, diff --git a/src/hb-raster-svg-fill.hh b/src/hb-raster-svg-fill.hh index b8840164d..ce4ceecd9 100644 --- a/src/hb-raster-svg-fill.hh +++ b/src/hb-raster-svg-fill.hh @@ -32,7 +32,7 @@ #include "hb-raster-svg-context.hh" HB_INTERNAL void -svg_emit_fill (const hb_svg_fill_context_t *ctx, +hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, hb_svg_str_t fill_str, float fill_opacity, const hb_extents_t<> *object_bbox, diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index 0617a0a3b..5d644632a 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -63,7 +63,7 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, if (color_str.len && !svg_str_is_inherit (color_str)) { is_current_color = svg_str_eq_ascii_ci (color_str.trim (), "currentColor"); - color = svg_parse_color (color_str, pfuncs, paint_data, foreground, face, palette, &is_none); + color = hb_raster_svg_parse_color (color_str, pfuncs, paint_data, foreground, face, palette, &is_none); } if (opacity_str.len && !svg_str_is_inherit (opacity_str)) @@ -84,7 +84,7 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, } static hb_svg_str_t -svg_find_href_attr (const hb_svg_xml_parser_t &parser) +hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser) { hb_svg_str_t href = parser.find_attr ("href"); if (href.is_null ()) @@ -142,10 +142,10 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, if (transform_str.len) { grad.has_gradient_transform = true; - svg_parse_transform (transform_str, &grad.gradient_transform); + hb_raster_svg_parse_transform (transform_str, &grad.gradient_transform); } - hb_svg_str_t href = svg_find_href_attr (parser); + hb_svg_str_t href = hb_raster_svg_find_href_attr (parser); if (href.len) (void) svg_parse_fragment_id (href, grad.href_id); } @@ -218,7 +218,7 @@ svg_parse_gradient_children (hb_svg_defs_t *defs, } void -svg_process_gradient_def (hb_svg_defs_t *defs, +hb_raster_svg_process_gradient_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, hb_svg_token_type_t tok, hb_svg_gradient_type_t type, diff --git a/src/hb-raster-svg-gradient.hh b/src/hb-raster-svg-gradient.hh index 0b49b8bf3..26853583b 100644 --- a/src/hb-raster-svg-gradient.hh +++ b/src/hb-raster-svg-gradient.hh @@ -34,7 +34,7 @@ #include "hb-raster-svg-defs.hh" HB_INTERNAL void -svg_process_gradient_def (hb_svg_defs_t *defs, +hb_raster_svg_process_gradient_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, hb_svg_token_type_t tok, hb_svg_gradient_type_t type, diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc index 26b4cbfbb..d456927bc 100644 --- a/src/hb-raster-svg-parse.cc +++ b/src/hb-raster-svg-parse.cc @@ -30,7 +30,7 @@ #include bool -svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) +hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) { hb_svg_float_parser_t fp (s); @@ -232,7 +232,7 @@ svg_arc_endpoint_to_center (float x1, float y1, float x2, float y2, } void -svg_parse_path_data (hb_svg_str_t d, hb_draw_funcs_t *dfuncs, void *draw_data) +hb_raster_svg_parse_path_data (hb_svg_str_t d, hb_draw_funcs_t *dfuncs, void *draw_data) { hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; hb_svg_float_parser_t fp (d); @@ -536,13 +536,13 @@ svg_polygon_to_path (hb_svg_str_t points, bool close, } void -svg_shape_path_emit (hb_draw_funcs_t *dfuncs, void *draw_data, void *user_data) +hb_raster_svg_shape_path_emit (hb_draw_funcs_t *dfuncs, void *draw_data, void *user_data) { hb_svg_shape_emit_data_t *shape = (hb_svg_shape_emit_data_t *) user_data; switch (shape->type) { case hb_svg_shape_emit_data_t::SHAPE_PATH: - svg_parse_path_data (shape->str_data, dfuncs, draw_data); + hb_raster_svg_parse_path_data (shape->str_data, dfuncs, draw_data); break; case hb_svg_shape_emit_data_t::SHAPE_RECT: svg_rect_to_path (shape->params[0], shape->params[1], @@ -574,7 +574,7 @@ svg_shape_path_emit (hb_draw_funcs_t *dfuncs, void *draw_data, void *user_data) } bool -svg_parse_shape_tag (hb_svg_xml_parser_t &parser, +hb_raster_svg_parse_shape_tag (hb_svg_xml_parser_t &parser, hb_svg_shape_emit_data_t *shape) { hb_svg_attr_view_t attrs (parser); diff --git a/src/hb-raster-svg-parse.hh b/src/hb-raster-svg-parse.hh index 9a8338319..7e3b2ae39 100644 --- a/src/hb-raster-svg-parse.hh +++ b/src/hb-raster-svg-parse.hh @@ -346,9 +346,9 @@ struct hb_svg_shape_emit_data_t float params[6]; }; -HB_INTERNAL bool svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out); -HB_INTERNAL void svg_parse_path_data (hb_svg_str_t d, hb_draw_funcs_t *dfuncs, void *draw_data); -HB_INTERNAL void svg_shape_path_emit (hb_draw_funcs_t *dfuncs, void *draw_data, void *user_data); -HB_INTERNAL bool svg_parse_shape_tag (hb_svg_xml_parser_t &parser, hb_svg_shape_emit_data_t *shape); +HB_INTERNAL bool hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out); +HB_INTERNAL void hb_raster_svg_parse_path_data (hb_svg_str_t d, hb_draw_funcs_t *dfuncs, void *draw_data); +HB_INTERNAL void hb_raster_svg_shape_path_emit (hb_draw_funcs_t *dfuncs, void *draw_data, void *user_data); +HB_INTERNAL bool hb_raster_svg_parse_shape_tag (hb_svg_xml_parser_t &parser, hb_svg_shape_emit_data_t *shape); #endif /* HB_RASTER_SVG_PARSE_HH */ diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 76ecc9b5e..ea8d4920e 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -81,17 +81,17 @@ svg_process_defs (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser) if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { if (parser.tag_name.eq ("linearGradient")) - svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, + hb_raster_svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, ctx->pfuncs, ctx->paint, ctx->foreground, hb_font_get_face (ctx->font), ctx->palette); else if (parser.tag_name.eq ("radialGradient")) - svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, + hb_raster_svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, ctx->pfuncs, ctx->paint, ctx->foreground, hb_font_get_face (ctx->font), ctx->palette); else if (parser.tag_name.eq ("clipPath")) - svg_process_clip_path_def (&ctx->defs, parser, tok); + hb_raster_svg_process_clip_path_def (&ctx->defs, parser, tok); else { if (tok == SVG_TOKEN_OPEN_TAG) @@ -118,17 +118,17 @@ svg_render_shape (hb_svg_render_context_t *ctx, if (has_transform) { hb_svg_transform_t t; - svg_parse_transform (transform_str, &t); + hb_raster_svg_parse_transform (transform_str, &t); ctx->push_transform (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); } hb_extents_t<> bbox; - bool has_bbox = svg_compute_shape_bbox (shape, &bbox); - has_clip_path = svg_push_clip_path_ref (ctx->paint, &ctx->defs, state.clip_path, + bool has_bbox = hb_raster_svg_compute_shape_bbox (shape, &bbox); + has_clip_path = hb_raster_svg_push_clip_path_ref (ctx->paint, &ctx->defs, state.clip_path, has_bbox ? &bbox : nullptr); /* Clip with shape path, then fill */ - hb_raster_paint_push_clip_path (ctx->paint, svg_shape_path_emit, &shape); + hb_raster_paint_push_clip_path (ctx->paint, hb_raster_svg_shape_path_emit, &shape); /* Default fill is black */ if (state.fill.is_null ()) @@ -141,7 +141,7 @@ svg_render_shape (hb_svg_render_context_t *ctx, else { hb_svg_fill_context_t fill_ctx = {ctx->paint, ctx->pfuncs, ctx->font, ctx->palette, &ctx->defs}; - svg_emit_fill (&fill_ctx, state.fill, state.fill_opacity, has_bbox ? &bbox : nullptr, state.color); + hb_raster_svg_emit_fill (&fill_ctx, state.fill, state.fill_opacity, has_bbox ? &bbox : nullptr, state.color); } ctx->pop_clip (); @@ -190,14 +190,14 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (has_transform) { hb_svg_transform_t t; - svg_parse_transform (transform_str, &t); + hb_raster_svg_parse_transform (transform_str, &t); ctx->push_transform (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); } if (has_viewbox && vb_w > 0 && vb_h > 0) ctx->push_transform (1, 0, 0, 1, -vb_x, -vb_y); - has_clip = svg_push_clip_path_ref (ctx->paint, &ctx->defs, clip_path_str, nullptr); + has_clip = hb_raster_svg_push_clip_path_ref (ctx->paint, &ctx->defs, clip_path_str, nullptr); if (!self_closing) { @@ -444,7 +444,7 @@ svg_render_element (hb_svg_render_context_t *ctx, state.color = inherited.color; bool is_none = false; if (color_str.len && !svg_str_eq_ascii_ci (color_str.trim (), "inherit")) - state.color = svg_parse_color (color_str, ctx->pfuncs, ctx->paint, + state.color = hb_raster_svg_parse_color (color_str, ctx->pfuncs, ctx->paint, inherited.color, hb_font_get_face (ctx->font), ctx->palette, &is_none); state.visibility = inherited.visibility; @@ -484,7 +484,7 @@ svg_render_element (hb_svg_render_context_t *ctx, hb_svg_use_context_t use_ctx = {ctx->paint, ctx->pfuncs, ctx->doc_start, ctx->doc_len, ctx->svg_accel, ctx->doc_cache}; - svg_render_use_element (&use_ctx, parser, &state, transform_str, + hb_raster_svg_render_use_element (&use_ctx, parser, &state, transform_str, svg_render_use_callback, ctx); } @@ -554,17 +554,17 @@ hb_raster_svg_render (hb_raster_paint_t *paint, if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { if (defs_parser.tag_name.eq ("linearGradient")) - svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_LINEAR, + hb_raster_svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_LINEAR, ctx.pfuncs, ctx.paint, ctx.foreground, hb_font_get_face (ctx.font), ctx.palette); else if (defs_parser.tag_name.eq ("radialGradient")) - svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_RADIAL, + hb_raster_svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_RADIAL, ctx.pfuncs, ctx.paint, ctx.foreground, hb_font_get_face (ctx.font), ctx.palette); else if (defs_parser.tag_name.eq ("clipPath")) - svg_process_clip_path_def (&ctx.defs, defs_parser, tok); + hb_raster_svg_process_clip_path_def (&ctx.defs, defs_parser, tok); } } } diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index 43e75ae19..a3003e745 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -33,7 +33,7 @@ #include hb_svg_str_t -svg_find_href_attr (const hb_svg_xml_parser_t &parser) +hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser) { hb_svg_str_t href = parser.find_attr ("href"); if (href.is_null ()) @@ -137,14 +137,14 @@ svg_parse_id_ref (hb_svg_str_t s, char out_id[64]) } void -svg_render_use_element (const hb_svg_use_context_t *ctx, +hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t &parser, const void *state, hb_svg_str_t transform_str, hb_svg_use_render_cb_t render_cb, void *render_user) { - hb_svg_str_t href = svg_find_href_attr (parser); + hb_svg_str_t href = hb_raster_svg_find_href_attr (parser); char ref_id[64]; if (!svg_parse_id_ref (href, ref_id)) @@ -159,7 +159,7 @@ svg_render_use_element (const hb_svg_use_context_t *ctx, if (has_use_transform) { hb_svg_transform_t t; - svg_parse_transform (transform_str, &t); + hb_raster_svg_parse_transform (transform_str, &t); hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); } diff --git a/src/hb-raster-svg-use.hh b/src/hb-raster-svg-use.hh index 393c4d349..96f9ea0ae 100644 --- a/src/hb-raster-svg-use.hh +++ b/src/hb-raster-svg-use.hh @@ -37,10 +37,10 @@ typedef void (*hb_svg_use_render_cb_t) (void *render_user, const void *state); HB_INTERNAL hb_svg_str_t -svg_find_href_attr (const hb_svg_xml_parser_t &parser); +hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser); HB_INTERNAL void -svg_render_use_element (const hb_svg_use_context_t *ctx, +hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t &parser, const void *state, hb_svg_str_t transform_str, From c6b1598e488574c9cdf454c5a47dbedf37e1e377 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:16:40 -0300 Subject: [PATCH 027/105] [raster] Reduce SVG module include coupling --- src/hb-raster-svg-fill.cc | 2 +- src/hb-raster-svg-gradient.hh | 1 - src/hb-raster-svg-render.cc | 6 ------ 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 0564fe40f..66b7f6e26 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -29,7 +29,7 @@ #include "hb-raster-svg-fill.hh" #include "hb-raster-svg-base.hh" -#include "hb-raster-svg-gradient.hh" +#include "hb-raster-svg-color.hh" #include diff --git a/src/hb-raster-svg-gradient.hh b/src/hb-raster-svg-gradient.hh index 26853583b..0b1e6fd0c 100644 --- a/src/hb-raster-svg-gradient.hh +++ b/src/hb-raster-svg-gradient.hh @@ -29,7 +29,6 @@ #include "hb.hh" -#include "hb-face.hh" #include "hb-raster-svg-color.hh" #include "hb-raster-svg-defs.hh" diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index ea8d4920e..e237f6e33 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -27,21 +27,15 @@ #include "hb.hh" #include "hb-face.hh" -#include "hb-raster.h" -#include "hb-raster-paint.hh" #include "hb-raster-svg.hh" #include "hb-raster-svg-base.hh" #include "hb-raster-svg-parse.hh" -#include "hb-raster-svg-defs.hh" #include "hb-raster-svg-context.hh" #include "hb-raster-svg-gradient.hh" #include "hb-raster-svg-clip.hh" #include "hb-raster-svg-bbox.hh" #include "hb-raster-svg-fill.hh" #include "hb-raster-svg-use.hh" -#include "OT/Color/svg/svg.hh" -#include "hb-draw.h" -#include "hb-ot-color.h" #include #include From 860beb08b610d77562a4fe507e048bb7197d7dd8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:18:49 -0300 Subject: [PATCH 028/105] [raster] Split SVG defs scanning from render --- src/hb-raster-svg-defs-scan.cc | 100 +++++++++++++++++++++++++++++++++ src/hb-raster-svg-defs-scan.hh | 53 +++++++++++++++++ src/hb-raster-svg-render.cc | 81 +++++--------------------- src/meson.build | 2 + 4 files changed, 170 insertions(+), 66 deletions(-) create mode 100644 src/hb-raster-svg-defs-scan.cc create mode 100644 src/hb-raster-svg-defs-scan.hh diff --git a/src/hb-raster-svg-defs-scan.cc b/src/hb-raster-svg-defs-scan.cc new file mode 100644 index 000000000..e4d037695 --- /dev/null +++ b/src/hb-raster-svg-defs-scan.cc @@ -0,0 +1,100 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb.hh" + +#include "hb-raster-svg-defs-scan.hh" + +#include "hb-raster-svg-clip.hh" +#include "hb-raster-svg-gradient.hh" + +void +hb_raster_svg_process_defs_element (const hb_svg_defs_scan_context_t *ctx, + hb_svg_xml_parser_t &parser) +{ + int depth = 1; + + while (depth > 0) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + + if (tok == SVG_TOKEN_CLOSE_TAG) + { + depth--; + continue; + } + + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + if (parser.tag_name.eq ("linearGradient")) + hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, + ctx->pfuncs, ctx->paint_data, + ctx->foreground, ctx->face, + ctx->palette); + else if (parser.tag_name.eq ("radialGradient")) + hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, + ctx->pfuncs, ctx->paint_data, + ctx->foreground, ctx->face, + ctx->palette); + else if (parser.tag_name.eq ("clipPath")) + hb_raster_svg_process_clip_path_def (ctx->defs, parser, tok); + else + { + if (tok == SVG_TOKEN_OPEN_TAG) + depth++; + } + } + } +} + +void +hb_raster_svg_collect_defs (const hb_svg_defs_scan_context_t *ctx, + const char *data, + unsigned data_len) +{ + hb_svg_xml_parser_t parser (data, data_len); + while (true) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + if (parser.tag_name.eq ("linearGradient")) + hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, + ctx->pfuncs, ctx->paint_data, + ctx->foreground, ctx->face, + ctx->palette); + else if (parser.tag_name.eq ("radialGradient")) + hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, + ctx->pfuncs, ctx->paint_data, + ctx->foreground, ctx->face, + ctx->palette); + else if (parser.tag_name.eq ("clipPath")) + hb_raster_svg_process_clip_path_def (ctx->defs, parser, tok); + } + } +} diff --git a/src/hb-raster-svg-defs-scan.hh b/src/hb-raster-svg-defs-scan.hh new file mode 100644 index 000000000..a150d630a --- /dev/null +++ b/src/hb-raster-svg-defs-scan.hh @@ -0,0 +1,53 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Author(s): Behdad Esfahbod + */ + +#ifndef HB_RASTER_SVG_DEFS_SCAN_HH +#define HB_RASTER_SVG_DEFS_SCAN_HH + +#include "hb.hh" + +#include "hb-raster-svg-defs.hh" + +struct hb_svg_defs_scan_context_t +{ + hb_svg_defs_t *defs; + hb_paint_funcs_t *pfuncs; + void *paint_data; + hb_color_t foreground; + hb_face_t *face; + unsigned palette; +}; + +HB_INTERNAL void +hb_raster_svg_process_defs_element (const hb_svg_defs_scan_context_t *ctx, + hb_svg_xml_parser_t &parser); + +HB_INTERNAL void +hb_raster_svg_collect_defs (const hb_svg_defs_scan_context_t *ctx, + const char *data, + unsigned data_len); + +#endif /* HB_RASTER_SVG_DEFS_SCAN_HH */ diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index e237f6e33..dbebe7ea5 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -31,6 +31,7 @@ #include "hb-raster-svg-base.hh" #include "hb-raster-svg-parse.hh" #include "hb-raster-svg-context.hh" +#include "hb-raster-svg-defs-scan.hh" #include "hb-raster-svg-gradient.hh" #include "hb-raster-svg-clip.hh" #include "hb-raster-svg-bbox.hh" @@ -55,46 +56,6 @@ static void svg_render_element (hb_svg_render_context_t *ctx, /* Clip-path defs and push helpers live in hb-raster-svg-clip.* */ -/* Process children — gradients, clipPaths */ -static void -svg_process_defs (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser) -{ - int depth = 1; - - while (depth > 0) - { - hb_svg_token_type_t tok = parser.next (); - if (tok == SVG_TOKEN_EOF) break; - - if (tok == SVG_TOKEN_CLOSE_TAG) - { - depth--; - continue; - } - - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - { - if (parser.tag_name.eq ("linearGradient")) - hb_raster_svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, - ctx->pfuncs, ctx->paint, - ctx->foreground, hb_font_get_face (ctx->font), - ctx->palette); - else if (parser.tag_name.eq ("radialGradient")) - hb_raster_svg_process_gradient_def (&ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, - ctx->pfuncs, ctx->paint, - ctx->foreground, hb_font_get_face (ctx->font), - ctx->palette); - else if (parser.tag_name.eq ("clipPath")) - hb_raster_svg_process_clip_path_def (&ctx->defs, parser, tok); - else - { - if (tok == SVG_TOKEN_OPEN_TAG) - depth++; - } - } - } -} - /* Render a single shape element */ static void svg_render_shape (hb_svg_render_context_t *ctx, @@ -213,7 +174,14 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (parser.tag_name.eq ("defs")) { if (tok != SVG_TOKEN_SELF_CLOSE_TAG) - svg_process_defs (ctx, parser); + { + hb_svg_defs_scan_context_t scan_ctx = { + &ctx->defs, ctx->pfuncs, ctx->paint, + ctx->foreground, hb_font_get_face (ctx->font), + ctx->palette + }; + hb_raster_svg_process_defs_element (&scan_ctx, parser); + } continue; } svg_render_element (ctx, parser, state); @@ -537,31 +505,12 @@ hb_raster_svg_render (hb_raster_paint_t *paint, hb_svg_cascade_t initial_state; initial_state.color = foreground; - /* First pass: collect defs (gradients, clip-paths) from the entire document, - * including entries that are authored outside . */ - { - hb_svg_xml_parser_t defs_parser (data, data_len); - while (true) - { - hb_svg_token_type_t tok = defs_parser.next (); - if (tok == SVG_TOKEN_EOF) break; - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - { - if (defs_parser.tag_name.eq ("linearGradient")) - hb_raster_svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_LINEAR, - ctx.pfuncs, ctx.paint, - ctx.foreground, hb_font_get_face (ctx.font), - ctx.palette); - else if (defs_parser.tag_name.eq ("radialGradient")) - hb_raster_svg_process_gradient_def (&ctx.defs, defs_parser, tok, SVG_GRADIENT_RADIAL, - ctx.pfuncs, ctx.paint, - ctx.foreground, hb_font_get_face (ctx.font), - ctx.palette); - else if (defs_parser.tag_name.eq ("clipPath")) - hb_raster_svg_process_clip_path_def (&ctx.defs, defs_parser, tok); - } - } - } + hb_svg_defs_scan_context_t scan_ctx = { + &ctx.defs, ctx.pfuncs, ctx.paint, + ctx.foreground, hb_font_get_face (ctx.font), + ctx.palette + }; + hb_raster_svg_collect_defs (&scan_ctx, data, data_len); bool found_glyph = false; unsigned glyph_start = 0, glyph_end = 0; diff --git a/src/meson.build b/src/meson.build index a91c9e8c3..e07dcc1c6 100644 --- a/src/meson.build +++ b/src/meson.build @@ -709,6 +709,8 @@ hb_raster_sources = [ 'hb-raster-svg-base.hh', 'hb-raster-svg-defs.cc', 'hb-raster-svg-defs.hh', + 'hb-raster-svg-defs-scan.cc', + 'hb-raster-svg-defs-scan.hh', 'hb-raster-svg-gradient.cc', 'hb-raster-svg-gradient.hh', 'hb-raster-svg-clip.cc', From f560bbb9e4e3549d25b340c19ceda3a561497e89 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:22:10 -0300 Subject: [PATCH 029/105] [raster] Guard SVG cycles with hb_decycler_t --- src/hb-raster-svg-context.hh | 3 +++ src/hb-raster-svg-render.cc | 3 ++- src/hb-raster-svg-use.cc | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/hb-raster-svg-context.hh b/src/hb-raster-svg-context.hh index b71e73c4c..bf68fe9f1 100644 --- a/src/hb-raster-svg-context.hh +++ b/src/hb-raster-svg-context.hh @@ -30,6 +30,7 @@ #include "hb.hh" #include "OT/Color/svg/svg.hh" +#include "hb-decycler.hh" #include "hb-raster-paint.hh" #include "hb-raster-svg-base.hh" #include "hb-raster-svg-defs.hh" @@ -52,6 +53,7 @@ struct hb_svg_use_context_t unsigned doc_len; const OT::SVG::accelerator_t *svg_accel; const OT::SVG::svg_doc_cache_t *doc_cache; + hb_decycler_t *use_decycler; }; struct hb_svg_render_context_t @@ -68,6 +70,7 @@ struct hb_svg_render_context_t unsigned doc_len; const OT::SVG::accelerator_t *svg_accel = nullptr; const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; + hb_decycler_t use_decycler; void push_transform (float xx, float yx, float xy, float yy, float dx, float dy) { diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index dbebe7ea5..97884aa85 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -445,7 +445,8 @@ svg_render_element (hb_svg_render_context_t *ctx, { hb_svg_use_context_t use_ctx = {ctx->paint, ctx->pfuncs, ctx->doc_start, ctx->doc_len, - ctx->svg_accel, ctx->doc_cache}; + ctx->svg_accel, ctx->doc_cache, + &ctx->use_decycler}; hb_raster_svg_render_use_element (&use_ctx, parser, &state, transform_str, svg_render_use_callback, ctx); } diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index a3003e745..688e6dbaa 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -171,6 +171,10 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, if (found) { + hb_decycler_node_t node (*ctx->use_decycler); + if (unlikely (!node.visit ((uintptr_t) found))) + return; + unsigned remaining = ctx->doc_start + ctx->doc_len - found; hb_svg_xml_parser_t ref_parser (found, remaining); hb_svg_token_type_t tok = ref_parser.next (); From 503d216ef22820a9267ca9036cfb3a80d55c44f9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:24:37 -0300 Subject: [PATCH 030/105] [raster] Share SVG href and id-ref parsing helpers --- src/hb-raster-svg-base.cc | 84 +++++++++++++++++++++++++++++++++++ src/hb-raster-svg-base.hh | 6 +++ src/hb-raster-svg-clip.cc | 75 +++++-------------------------- src/hb-raster-svg-fill.cc | 60 +++---------------------- src/hb-raster-svg-gradient.cc | 33 ++++---------- src/hb-raster-svg-use.cc | 80 +++------------------------------ src/hb-raster-svg-use.hh | 3 -- 7 files changed, 122 insertions(+), 219 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index 1d25047f8..2338b5555 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -27,6 +27,7 @@ #include "hb.hh" #include "hb-raster-svg-base.hh" +#include "hb-raster-svg-parse.hh" static inline char svg_ascii_lower (char c) @@ -125,3 +126,86 @@ svg_parse_number_or_percent (hb_svg_str_t s, bool *is_percent) } return s.to_float (); } + +hb_svg_str_t +hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser) +{ + hb_svg_str_t href = parser.find_attr ("href"); + if (href.is_null ()) + href = parser.find_attr ("xlink:href"); + return href; +} + +bool +hb_raster_svg_parse_id_ref (hb_svg_str_t s, + hb_svg_str_t *out_id, + hb_svg_str_t *out_tail) +{ + if (out_id) *out_id = {}; + if (out_tail) *out_tail = {}; + s = s.trim (); + + if (s.len && s.data[0] == '#') + { + hb_svg_str_t id = {s.data + 1, s.len - 1}; + id = id.trim (); + if (!id.len) + return false; + if (out_id) *out_id = id; + return true; + } + + if (!svg_str_starts_with_ascii_ci (s, "url(")) + return false; + + const char *p = s.data + 4; + const char *end = s.data + s.len; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + + const char *q = p; + char quote = 0; + while (q < end) + { + char c = *q; + if (quote) + { + if (c == quote) quote = 0; + } + else + { + if (c == '"' || c == '\'') quote = c; + else if (c == ')') break; + } + q++; + } + if (q >= end || *q != ')') + return false; + + hb_svg_str_t id = {(const char *) p, (unsigned) (q - p)}; + id = id.trim (); + if (id.len >= 2 && + ((id.data[0] == '\'' && id.data[id.len - 1] == '\'') || + (id.data[0] == '"' && id.data[id.len - 1] == '"'))) + { + id.data++; + id.len -= 2; + } + id = id.trim (); + if (id.len && id.data[0] == '#') + { + id.data++; + id.len--; + } + id = id.trim (); + if (!id.len) + return false; + + if (out_id) *out_id = id; + if (out_tail) + { + const char *tail = q + 1; + while (tail < end && (*tail == ' ' || *tail == '\t' || *tail == '\n' || *tail == '\r')) tail++; + *out_tail = {(const char *) tail, (unsigned) (end - tail)}; + } + return true; +} diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 2ec04b468..7934f024f 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -117,10 +117,16 @@ struct hb_svg_style_props_t hb_svg_str_t stop_opacity; }; +struct hb_svg_xml_parser_t; + HB_INTERNAL bool svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit); HB_INTERNAL bool svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit); HB_INTERNAL void svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out); HB_INTERNAL float svg_parse_number_or_percent (hb_svg_str_t s, bool *is_percent); +HB_INTERNAL hb_svg_str_t hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser); +HB_INTERNAL bool hb_raster_svg_parse_id_ref (hb_svg_str_t s, + hb_svg_str_t *out_id, + hb_svg_str_t *out_tail); static inline float svg_parse_float_clamped01 (hb_svg_str_t s) { diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 17e7f6e89..b726c7910 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -33,8 +33,6 @@ #include "hb-raster-svg.hh" #include "hb-raster-svg-base.hh" -#include - void hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, @@ -149,66 +147,6 @@ svg_clip_path_emit (hb_draw_funcs_t *dfuncs, } } -static bool -svg_parse_clip_path_id_ref (hb_svg_str_t s, char out_id[64]) -{ - s = s.trim (); - - if (s.len && s.data[0] == '#') - { - unsigned n = hb_min (s.len - 1, (unsigned) 63); - memcpy (out_id, s.data + 1, n); - out_id[n] = '\0'; - return n > 0; - } - - if (!svg_str_starts_with_ascii_ci (s, "url(")) - return false; - - const char *p = s.data + 4; - const char *end = s.data + s.len; - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - - const char *q = p; - char quote = 0; - while (q < end) - { - char c = *q; - if (quote) - { - if (c == quote) quote = 0; - } - else - { - if (c == '"' || c == '\'') quote = c; - else if (c == ')') break; - } - q++; - } - if (q >= end || *q != ')') - return false; - - const char *id_b = p; - const char *id_e = q; - while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; - while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; - if (id_e > id_b && ((*id_b == '\'' && *(id_e - 1) == '\'') || (*id_b == '"' && *(id_e - 1) == '"'))) - { - id_b++; - id_e--; - } - while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; - while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; - if (id_b < id_e && *id_b == '#') id_b++; - if (id_b >= id_e) - return false; - - unsigned n = hb_min ((unsigned) (id_e - id_b), (unsigned) 63); - memcpy (out_id, id_b, n); - out_id[n] = '\0'; - return true; -} - bool hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, hb_svg_defs_t *defs, @@ -219,11 +157,18 @@ hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, hb_svg_str_t trimmed = clip_path_str.trim (); if (!trimmed.len || trimmed.eq ("none")) return false; - char clip_id[64]; - if (!svg_parse_clip_path_id_ref (trimmed, clip_id)) + hb_svg_str_t clip_id; + if (!hb_raster_svg_parse_id_ref (trimmed, &clip_id, nullptr)) + return false; + if (svg_str_starts_with_ascii_ci (trimmed, "url(") && + !svg_str_starts_with_ascii_ci (trimmed, "url(#")) return false; - const hb_svg_clip_path_def_t *clip = defs->find_clip_path (clip_id); + char clip_id_buf[64]; + unsigned clip_id_len = hb_min (clip_id.len, (unsigned) sizeof (clip_id_buf) - 1); + hb_memcpy (clip_id_buf, clip_id.data, clip_id_len); + clip_id_buf[clip_id_len] = '\0'; + const hb_svg_clip_path_def_t *clip = defs->find_clip_path (clip_id_buf); if (!clip || !clip->shape_count) return false; hb_svg_clip_emit_data_t ed; diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 66b7f6e26..4e0b8f060 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -31,8 +31,6 @@ #include "hb-raster-svg-base.hh" #include "hb-raster-svg-color.hh" -#include - struct hb_svg_color_line_data_t { const hb_svg_gradient_t *grad; @@ -99,60 +97,16 @@ svg_parse_paint_url_with_fallback (hb_svg_str_t s, char out_id[64], hb_svg_str_t *fallback) { - if (fallback) *fallback = {}; - s = s.trim (); - if (!svg_str_starts_with_ascii_ci (s, "url(")) + hb_svg_str_t id; + if (!hb_raster_svg_parse_id_ref (s, &id, fallback)) + return false; + if (!svg_str_starts_with_ascii_ci (s.trim (), "url(")) return false; - const char *p = s.data + 4; - const char *end = s.data + s.len; - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - - const char *q = p; - char quote = 0; - while (q < end) - { - char c = *q; - if (quote) - { - if (c == quote) quote = 0; - } - else - { - if (c == '"' || c == '\'') quote = c; - else if (c == ')') break; - } - q++; - } - if (q >= end || *q != ')') - return false; - - const char *id_b = p; - const char *id_e = q; - while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; - while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; - if (id_e > id_b && ((*id_b == '\'' && *(id_e - 1) == '\'') || (*id_b == '"' && *(id_e - 1) == '"'))) - { - id_b++; - id_e--; - } - while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; - while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; - if (id_b < id_e && *id_b == '#') id_b++; - if (id_b >= id_e) - return false; - - unsigned n = hb_min ((unsigned) (id_e - id_b), (unsigned) 63); - memcpy (out_id, id_b, n); + unsigned n = hb_min (id.len, (unsigned) 63); + hb_memcpy (out_id, id.data, n); out_id[n] = '\0'; - - if (fallback) - { - const char *f = q + 1; - while (f < end && (*f == ' ' || *f == '\t' || *f == '\n' || *f == '\r')) f++; - if (f < end) *fallback = {f, (unsigned) (end - f)}; - } - return true; + return n > 0; } void diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index 5d644632a..960615667 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -30,8 +30,6 @@ #include "hb-raster-svg-base.hh" -#include - static bool svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, hb_svg_gradient_t &grad, @@ -83,27 +81,6 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, return !grad.stops.in_error (); } -static hb_svg_str_t -hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser) -{ - hb_svg_str_t href = parser.find_attr ("href"); - if (href.is_null ()) - href = parser.find_attr ("xlink:href"); - return href; -} - -static bool -svg_parse_fragment_id (hb_svg_str_t s, char out_id[64]) -{ - s = s.trim (); - if (!s.len || s.data[0] != '#') - return false; - unsigned n = hb_min (s.len - 1, (unsigned) 63); - memcpy (out_id, s.data + 1, n); - out_id[n] = '\0'; - return n > 0; -} - static void svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, hb_svg_gradient_t &grad) @@ -147,7 +124,15 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, hb_svg_str_t href = hb_raster_svg_find_href_attr (parser); if (href.len) - (void) svg_parse_fragment_id (href, grad.href_id); + { + hb_svg_str_t href_id; + if (hb_raster_svg_parse_id_ref (href, &href_id, nullptr)) + { + unsigned n = hb_min (href_id.len, (unsigned) sizeof (grad.href_id) - 1); + hb_memcpy (grad.href_id, href_id.data, n); + grad.href_id[n] = '\0'; + } + } } static void diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index 688e6dbaa..dada659d4 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -32,25 +32,17 @@ #include -hb_svg_str_t -hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser) -{ - hb_svg_str_t href = parser.find_attr ("href"); - if (href.is_null ()) - href = parser.find_attr ("xlink:href"); - return href; -} - static bool svg_find_element_by_id (const hb_svg_use_context_t *ctx, - const char *id, + hb_svg_str_t id, const char **found) { *found = nullptr; if (ctx->doc_cache) { unsigned start = 0, end = 0; - if (ctx->svg_accel->doc_cache_find_id_cstr (ctx->doc_cache, id, &start, &end)) + OT::SVG::svg_id_span_t key = {id.data, id.len}; + if (ctx->svg_accel->doc_cache_find_id_span (ctx->doc_cache, key, &start, &end)) { if (start < ctx->doc_len && end <= ctx->doc_len && start < end) { @@ -67,7 +59,7 @@ svg_find_element_by_id (const hb_svg_use_context_t *ctx, if (tok == SVG_TOKEN_EOF) break; if (tok != SVG_TOKEN_OPEN_TAG && tok != SVG_TOKEN_SELF_CLOSE_TAG) continue; hb_svg_str_t attr_id = search.find_attr ("id"); - if (attr_id.len && attr_id.eq_cstr (id)) + if (attr_id.len == id.len && 0 == memcmp (attr_id.data, id.data, id.len)) { *found = search.tag_start; return true; @@ -76,66 +68,6 @@ svg_find_element_by_id (const hb_svg_use_context_t *ctx, return false; } -static bool -svg_parse_id_ref (hb_svg_str_t s, char out_id[64]) -{ - s = s.trim (); - - if (s.len && s.data[0] == '#') - { - unsigned n = hb_min (s.len - 1, (unsigned) 63); - memcpy (out_id, s.data + 1, n); - out_id[n] = '\0'; - return n > 0; - } - - if (!svg_str_starts_with_ascii_ci (s, "url(")) - return false; - - const char *p = s.data + 4; - const char *end = s.data + s.len; - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - - const char *q = p; - char quote = 0; - while (q < end) - { - char c = *q; - if (quote) - { - if (c == quote) quote = 0; - } - else - { - if (c == '"' || c == '\'') quote = c; - else if (c == ')') break; - } - q++; - } - if (q >= end || *q != ')') - return false; - - const char *id_b = p; - const char *id_e = q; - while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; - while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; - if (id_e > id_b && ((*id_b == '\'' && *(id_e - 1) == '\'') || (*id_b == '"' && *(id_e - 1) == '"'))) - { - id_b++; - id_e--; - } - while (id_b < id_e && (*id_b == ' ' || *id_b == '\t' || *id_b == '\n' || *id_b == '\r')) id_b++; - while (id_e > id_b && (*(id_e - 1) == ' ' || *(id_e - 1) == '\t' || *(id_e - 1) == '\n' || *(id_e - 1) == '\r')) id_e--; - if (id_b < id_e && *id_b == '#') id_b++; - if (id_b >= id_e) - return false; - - unsigned n = hb_min ((unsigned) (id_e - id_b), (unsigned) 63); - memcpy (out_id, id_b, n); - out_id[n] = '\0'; - return true; -} - void hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t &parser, @@ -146,8 +78,8 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, { hb_svg_str_t href = hb_raster_svg_find_href_attr (parser); - char ref_id[64]; - if (!svg_parse_id_ref (href, ref_id)) + hb_svg_str_t ref_id; + if (!hb_raster_svg_parse_id_ref (href, &ref_id, nullptr)) return; float use_x = svg_parse_float (parser.find_attr ("x")); diff --git a/src/hb-raster-svg-use.hh b/src/hb-raster-svg-use.hh index 96f9ea0ae..74b67c09e 100644 --- a/src/hb-raster-svg-use.hh +++ b/src/hb-raster-svg-use.hh @@ -36,9 +36,6 @@ typedef void (*hb_svg_use_render_cb_t) (void *render_user, hb_svg_xml_parser_t &parser, const void *state); -HB_INTERNAL hb_svg_str_t -hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser); - HB_INTERNAL void hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t &parser, From f90d1391eec51b50216b25d3c5f6821c2c1b7c57 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:25:58 -0300 Subject: [PATCH 031/105] [raster] Use span IDs for SVG defs and references --- src/hb-raster-svg-clip.cc | 14 ++------ src/hb-raster-svg-defs.cc | 64 ++++++++++------------------------- src/hb-raster-svg-defs.hh | 20 ++++------- src/hb-raster-svg-fill.cc | 18 +++++----- src/hb-raster-svg-gradient.cc | 14 ++------ 5 files changed, 35 insertions(+), 95 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index b726c7910..1318eaf4f 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -96,13 +96,7 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, } if (id.len && clip.shape_count) - { - char id_buf[64]; - unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); - memcpy (id_buf, id.data, n); - id_buf[n] = '\0'; - (void) defs->add_clip_path (id_buf, clip); - } + (void) defs->add_clip_path (hb_bytes_t (id.data, id.len), clip); } struct hb_svg_clip_emit_data_t @@ -164,11 +158,7 @@ hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, !svg_str_starts_with_ascii_ci (trimmed, "url(#")) return false; - char clip_id_buf[64]; - unsigned clip_id_len = hb_min (clip_id.len, (unsigned) sizeof (clip_id_buf) - 1); - hb_memcpy (clip_id_buf, clip_id.data, clip_id_len); - clip_id_buf[clip_id_len] = '\0'; - const hb_svg_clip_path_def_t *clip = defs->find_clip_path (clip_id_buf); + const hb_svg_clip_path_def_t *clip = defs->find_clip_path (hb_bytes_t (clip_id.data, clip_id.len)); if (!clip || !clip->shape_count) return false; hb_svg_clip_emit_data_t ed; diff --git a/src/hb-raster-svg-defs.cc b/src/hb-raster-svg-defs.cc index 6baa5fdf7..1cce2f97f 100644 --- a/src/hb-raster-svg-defs.cc +++ b/src/hb-raster-svg-defs.cc @@ -38,18 +38,20 @@ hb_svg_defs_t::~hb_svg_defs_t () bool hb_svg_defs_t::add_id_mapping (hb_hashmap_t *map, - const char *id, + hb_bytes_t id, unsigned idx) { - hb_bytes_t key = hb_bytes_t (id, (unsigned) strlen (id)); - if (map->has (key)) + if (!id.length) + return false; + if (map->has (id)) return true; - unsigned n = (unsigned) strlen (id); + unsigned n = (unsigned) id.length; char *owned = (char *) hb_malloc (n + 1); if (unlikely (!owned)) return false; - hb_memcpy (owned, id, n + 1); + hb_memcpy (owned, id.arrayZ, n); + owned[n] = '\0'; if (unlikely (!owned_id_strings.push (owned))) { hb_free (owned); @@ -63,85 +65,53 @@ hb_svg_defs_t::add_id_mapping (hb_hashmap_t *map, } bool -hb_svg_defs_t::add_gradient (const char *id, const hb_svg_gradient_t &grad) +hb_svg_defs_t::add_gradient (hb_bytes_t id, const hb_svg_gradient_t &grad) { unsigned idx = gradients.length; gradients.push (grad); if (unlikely (gradients.in_error ())) return false; - hb_svg_def_t def; - unsigned n = hb_min (strlen (id), (size_t) sizeof (def.id) - 1); - memcpy (def.id, id, n); - def.id[n] = '\0'; - def.type = hb_svg_def_t::DEF_GRADIENT; - def.index = idx; - defs.push (def); - if (unlikely (defs.in_error ())) + if (unlikely (!add_id_mapping (&gradient_by_id, id, idx))) { gradients.pop (); return false; } - if (unlikely (!add_id_mapping (&gradient_by_id, def.id, idx))) - { - defs.pop (); - gradients.pop (); - return false; - } return true; } const hb_svg_gradient_t * -hb_svg_defs_t::find_gradient (const char *id) const +hb_svg_defs_t::find_gradient (hb_bytes_t id) const { + if (!id.length) return nullptr; unsigned *idx = nullptr; - if (id && gradient_by_id.has (hb_bytes_t (id, (unsigned) strlen (id)), &idx)) + if (gradient_by_id.has (id, &idx)) return &gradients[*idx]; - for (unsigned i = 0; i < defs.length; i++) - if (defs[i].type == hb_svg_def_t::DEF_GRADIENT && - strcmp (defs[i].id, id) == 0) - return &gradients[defs[i].index]; return nullptr; } bool -hb_svg_defs_t::add_clip_path (const char *id, const hb_svg_clip_path_def_t &clip) +hb_svg_defs_t::add_clip_path (hb_bytes_t id, const hb_svg_clip_path_def_t &clip) { unsigned idx = clip_paths.length; clip_paths.push (clip); if (unlikely (clip_paths.in_error ())) return false; - hb_svg_def_t def; - unsigned n = hb_min (strlen (id), (size_t) sizeof (def.id) - 1); - memcpy (def.id, id, n); - def.id[n] = '\0'; - def.type = hb_svg_def_t::DEF_CLIP_PATH; - def.index = idx; - defs.push (def); - if (unlikely (defs.in_error ())) + if (unlikely (!add_id_mapping (&clip_path_by_id, id, idx))) { clip_paths.pop (); return false; } - if (unlikely (!add_id_mapping (&clip_path_by_id, def.id, idx))) - { - defs.pop (); - clip_paths.pop (); - return false; - } return true; } const hb_svg_clip_path_def_t * -hb_svg_defs_t::find_clip_path (const char *id) const +hb_svg_defs_t::find_clip_path (hb_bytes_t id) const { + if (!id.length) return nullptr; unsigned *idx = nullptr; - if (id && clip_path_by_id.has (hb_bytes_t (id, (unsigned) strlen (id)), &idx)) + if (clip_path_by_id.has (id, &idx)) return &clip_paths[*idx]; - for (unsigned i = 0; i < defs.length; i++) - if (defs[i].type == hb_svg_def_t::DEF_CLIP_PATH && - strcmp (defs[i].id, id) == 0) - return &clip_paths[defs[i].index]; return nullptr; } diff --git a/src/hb-raster-svg-defs.hh b/src/hb-raster-svg-defs.hh index e93bfbb25..617060ec9 100644 --- a/src/hb-raster-svg-defs.hh +++ b/src/hb-raster-svg-defs.hh @@ -66,7 +66,7 @@ struct hb_svg_gradient_t hb_vector_t stops; - char href_id[64] = {}; + hb_bytes_t href_id = {}; }; struct hb_svg_clip_path_def_t @@ -85,19 +85,11 @@ struct hb_svg_clip_shape_t bool has_transform = false; }; -struct hb_svg_def_t -{ - char id[64]; - enum { DEF_GRADIENT, DEF_CLIP_PATH } type; - unsigned index; -}; - struct hb_svg_defs_t { hb_vector_t gradients; hb_vector_t clip_shapes; hb_vector_t clip_paths; - hb_vector_t defs; hb_hashmap_t gradient_by_id; hb_hashmap_t clip_path_by_id; hb_vector_t owned_id_strings; @@ -105,12 +97,12 @@ struct hb_svg_defs_t ~hb_svg_defs_t (); bool add_id_mapping (hb_hashmap_t *map, - const char *id, + hb_bytes_t id, unsigned idx); - bool add_gradient (const char *id, const hb_svg_gradient_t &grad); - const hb_svg_gradient_t *find_gradient (const char *id) const; - bool add_clip_path (const char *id, const hb_svg_clip_path_def_t &clip); - const hb_svg_clip_path_def_t *find_clip_path (const char *id) const; + bool add_gradient (hb_bytes_t id, const hb_svg_gradient_t &grad); + const hb_svg_gradient_t *find_gradient (hb_bytes_t id) const; + bool add_clip_path (hb_bytes_t id, const hb_svg_clip_path_def_t &clip); + const hb_svg_clip_path_def_t *find_clip_path (hb_bytes_t id) const; }; #endif /* HB_RASTER_SVG_DEFS_HH */ diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 4e0b8f060..032f1e1bf 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -94,19 +94,17 @@ svg_color_line_get_extend (hb_color_line_t *color_line HB_UNUSED, static bool svg_parse_paint_url_with_fallback (hb_svg_str_t s, - char out_id[64], + hb_svg_str_t *out_id, hb_svg_str_t *fallback) { + if (out_id) *out_id = {}; hb_svg_str_t id; if (!hb_raster_svg_parse_id_ref (s, &id, fallback)) return false; if (!svg_str_starts_with_ascii_ci (s.trim (), "url(")) return false; - - unsigned n = hb_min (id.len, (unsigned) 63); - hb_memcpy (out_id, id.data, n); - out_id[n] = '\0'; - return n > 0; + if (out_id) *out_id = id; + return id.len > 0; } void @@ -118,11 +116,11 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, { bool is_none = false; - char url_id[64]; + hb_svg_str_t url_id; hb_svg_str_t fallback_paint; - bool has_url_paint = svg_parse_paint_url_with_fallback (fill_str, url_id, &fallback_paint); + bool has_url_paint = svg_parse_paint_url_with_fallback (fill_str, &url_id, &fallback_paint); - const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs->find_gradient (url_id) : nullptr; + const hb_svg_gradient_t *grad = has_url_paint ? ctx->defs->find_gradient (hb_bytes_t (url_id.data, url_id.len)) : nullptr; if (has_url_paint && !grad) { if (fallback_paint.len) @@ -137,7 +135,7 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, while (cur && chain_len < ARRAY_LENGTH (chain)) { chain[chain_len++] = cur; - if (!cur->href_id[0]) break; + if (!cur->href_id.length) break; const hb_svg_gradient_t *next = ctx->defs->find_gradient (cur->href_id); if (!next) break; bool is_cycle = false; diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index 960615667..f390986e0 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -127,11 +127,7 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, { hb_svg_str_t href_id; if (hb_raster_svg_parse_id_ref (href, &href_id, nullptr)) - { - unsigned n = hb_min (href_id.len, (unsigned) sizeof (grad.href_id) - 1); - hb_memcpy (grad.href_id, href_id.data, n); - grad.href_id[n] = '\0'; - } + grad.href_id = hb_bytes_t (href_id.data, href_id.len); } } @@ -226,11 +222,5 @@ hb_raster_svg_process_gradient_def (hb_svg_defs_t *defs, palette); if (id.len) - { - char id_buf[64]; - unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); - memcpy (id_buf, id.data, n); - id_buf[n] = '\0'; - (void) defs->add_gradient (id_buf, grad); - } + (void) defs->add_gradient (hb_bytes_t (id.data, id.len), grad); } From 7eec97483bc169fc4abbe0014ef55faac809e2c7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:26:33 -0300 Subject: [PATCH 032/105] [raster] De-duplicate SVG defs scan dispatch --- src/hb-raster-svg-defs-scan.cc | 64 ++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/hb-raster-svg-defs-scan.cc b/src/hb-raster-svg-defs-scan.cc index e4d037695..27d5258b3 100644 --- a/src/hb-raster-svg-defs-scan.cc +++ b/src/hb-raster-svg-defs-scan.cc @@ -31,6 +31,35 @@ #include "hb-raster-svg-clip.hh" #include "hb-raster-svg-gradient.hh" +static inline bool +hb_raster_svg_process_defs_child_tag (const hb_svg_defs_scan_context_t *ctx, + hb_svg_xml_parser_t &parser, + hb_svg_token_type_t tok) +{ + if (parser.tag_name.eq ("linearGradient")) + { + hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, + ctx->pfuncs, ctx->paint_data, + ctx->foreground, ctx->face, + ctx->palette); + return true; + } + if (parser.tag_name.eq ("radialGradient")) + { + hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, + ctx->pfuncs, ctx->paint_data, + ctx->foreground, ctx->face, + ctx->palette); + return true; + } + if (parser.tag_name.eq ("clipPath")) + { + hb_raster_svg_process_clip_path_def (ctx->defs, parser, tok); + return true; + } + return false; +} + void hb_raster_svg_process_defs_element (const hb_svg_defs_scan_context_t *ctx, hb_svg_xml_parser_t &parser) @@ -50,23 +79,9 @@ hb_raster_svg_process_defs_element (const hb_svg_defs_scan_context_t *ctx, if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { - if (parser.tag_name.eq ("linearGradient")) - hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, - ctx->pfuncs, ctx->paint_data, - ctx->foreground, ctx->face, - ctx->palette); - else if (parser.tag_name.eq ("radialGradient")) - hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, - ctx->pfuncs, ctx->paint_data, - ctx->foreground, ctx->face, - ctx->palette); - else if (parser.tag_name.eq ("clipPath")) - hb_raster_svg_process_clip_path_def (ctx->defs, parser, tok); - else - { - if (tok == SVG_TOKEN_OPEN_TAG) - depth++; - } + if (!hb_raster_svg_process_defs_child_tag (ctx, parser, tok) && + tok == SVG_TOKEN_OPEN_TAG) + depth++; } } } @@ -82,19 +97,6 @@ hb_raster_svg_collect_defs (const hb_svg_defs_scan_context_t *ctx, hb_svg_token_type_t tok = parser.next (); if (tok == SVG_TOKEN_EOF) break; if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - { - if (parser.tag_name.eq ("linearGradient")) - hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_LINEAR, - ctx->pfuncs, ctx->paint_data, - ctx->foreground, ctx->face, - ctx->palette); - else if (parser.tag_name.eq ("radialGradient")) - hb_raster_svg_process_gradient_def (ctx->defs, parser, tok, SVG_GRADIENT_RADIAL, - ctx->pfuncs, ctx->paint_data, - ctx->foreground, ctx->face, - ctx->palette); - else if (parser.tag_name.eq ("clipPath")) - hb_raster_svg_process_clip_path_def (ctx->defs, parser, tok); - } + hb_raster_svg_process_defs_child_tag (ctx, parser, tok); } } From c24c7b7cd1aa2f24f236c1b3f6c560c83c376cca Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:29:19 -0300 Subject: [PATCH 033/105] [raster] Improve SVG viewport behavior --- src/hb-raster-svg-render.cc | 4 ++-- src/hb-raster-svg-use.cc | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 97884aa85..5f978f156 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -125,7 +125,7 @@ svg_render_container_element (hb_svg_render_context_t *ctx, bool has_viewbox = false; float vb_x = 0, vb_y = 0, vb_w = 0, vb_h = 0; - if (tag.eq ("svg")) + if (tag.eq ("svg") || tag.eq ("symbol")) { hb_svg_str_t viewbox_str = parser.find_attr ("viewBox"); if (viewbox_str.len) @@ -436,7 +436,7 @@ svg_render_element (hb_svg_render_context_t *ctx, return; } - if (tag.eq ("g") || tag.eq ("svg")) + if (tag.eq ("g") || tag.eq ("svg") || tag.eq ("symbol")) svg_render_container_element (ctx, parser, tag, self_closing, state, transform_str, state.clip_path); else if (svg_render_primitive_shape_element (ctx, parser, tag, state, transform_str)) diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index dada659d4..9f06b7a58 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -68,6 +68,26 @@ svg_find_element_by_id (const hb_svg_use_context_t *ctx, return false; } +static inline bool +svg_parse_viewbox (hb_svg_str_t viewbox_str, + float *x, float *y, float *w, float *h) +{ + if (!viewbox_str.len) + return false; + hb_svg_float_parser_t vb_fp (viewbox_str); + float vb_x = vb_fp.next_float (); + float vb_y = vb_fp.next_float (); + float vb_w = vb_fp.next_float (); + float vb_h = vb_fp.next_float (); + if (vb_w <= 0.f || vb_h <= 0.f) + return false; + if (x) *x = vb_x; + if (y) *y = vb_y; + if (w) *w = vb_w; + if (h) *h = vb_h; + return true; +} + void hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t &parser, @@ -84,6 +104,8 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, float use_x = svg_parse_float (parser.find_attr ("x")); float use_y = svg_parse_float (parser.find_attr ("y")); + float use_w = svg_parse_float (parser.find_attr ("width")); + float use_h = svg_parse_float (parser.find_attr ("height")); bool has_translate = (use_x != 0.f || use_y != 0.f); bool has_use_transform = transform_str.len > 0; @@ -111,7 +133,22 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t ref_parser (found, remaining); hb_svg_token_type_t tok = ref_parser.next (); if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + { + bool has_viewport_scale = false; + if ((ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) && + use_w > 0.f && use_h > 0.f) + { + float vb_w = 0.f, vb_h = 0.f; + if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), nullptr, nullptr, &vb_w, &vb_h)) + { + hb_paint_push_transform (ctx->pfuncs, ctx->paint, use_w / vb_w, 0, 0, use_h / vb_h, 0, 0); + has_viewport_scale = true; + } + } render_cb (render_user, ref_parser, state); + if (has_viewport_scale) + hb_paint_pop_transform (ctx->pfuncs, ctx->paint); + } } if (has_translate) From ce91e95c9d228eb12b070f5a0c940499fb7e30d8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:29:48 -0300 Subject: [PATCH 034/105] [raster] Remove fixed SVG gradient href-chain limit --- src/hb-raster-svg-fill.cc | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 032f1e1bf..2ea590cbc 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -129,31 +129,34 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, } if (grad) { - const hb_svg_gradient_t *chain[8]; - unsigned chain_len = 0; + hb_vector_t chain; const hb_svg_gradient_t *cur = grad; - while (cur && chain_len < ARRAY_LENGTH (chain)) + while (cur) { - chain[chain_len++] = cur; - if (!cur->href_id.length) break; - const hb_svg_gradient_t *next = ctx->defs->find_gradient (cur->href_id); - if (!next) break; bool is_cycle = false; - for (unsigned i = 0; i < chain_len; i++) - if (chain[i] == next) + for (unsigned i = 0; i < chain.length; i++) + if (chain.arrayZ[i] == cur) { is_cycle = true; break; } - if (is_cycle) break; + if (is_cycle) + break; + chain.push (cur); + if (unlikely (chain.in_error ())) + return; + + if (!cur->href_id.length) break; + const hb_svg_gradient_t *next = ctx->defs->find_gradient (cur->href_id); + if (!next) break; cur = next; } - if (!chain_len) return; + if (!chain.length) return; - hb_svg_gradient_t effective = *chain[chain_len - 1]; - for (int i = (int) chain_len - 2; i >= 0; i--) + hb_svg_gradient_t effective = *chain.arrayZ[chain.length - 1]; + for (int i = (int) chain.length - 2; i >= 0; i--) { - const hb_svg_gradient_t *g = chain[i]; + const hb_svg_gradient_t *g = chain.arrayZ[i]; effective.type = g->type; if (g->stops.length) effective.stops = g->stops; if (g->has_spread) { effective.spread = g->spread; effective.has_spread = true; } From 5f4deabc9570f9b4c5226b0f3525715ea42d2851 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:30:15 -0300 Subject: [PATCH 035/105] [raster] Sanitize non-finite SVG numeric parses --- src/hb-raster-svg-base.hh | 4 +++- src/hb-raster-svg-parse.hh | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 7934f024f..1ba1b62f0 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -29,6 +29,7 @@ #include "hb.hh" +#include #include #include @@ -67,7 +68,8 @@ struct hb_svg_str_t unsigned n = hb_min (len, (unsigned) sizeof (buf) - 1); memcpy (buf, data, n); buf[n] = '\0'; - return strtof (buf, nullptr); + float v = strtof (buf, nullptr); + return isfinite (v) ? v : 0.f; } int to_int () const diff --git a/src/hb-raster-svg-parse.hh b/src/hb-raster-svg-parse.hh index 7e3b2ae39..ac4bab27b 100644 --- a/src/hb-raster-svg-parse.hh +++ b/src/hb-raster-svg-parse.hh @@ -319,7 +319,8 @@ struct hb_svg_float_parser_t return 0.f; } buf[n] = '\0'; - return strtof (buf, nullptr); + float v = strtof (buf, nullptr); + return isfinite (v) ? v : 0.f; } bool next_flag () From 28dc8f6a6ccd8c4dd93c35f55ad9b7072b6b9926 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:31:41 -0300 Subject: [PATCH 036/105] [raster] Require local fragment IDs for SVG refs --- src/hb-raster-svg-base.cc | 43 +++++++++++++++++++++++++++++++++++ src/hb-raster-svg-base.hh | 3 +++ src/hb-raster-svg-clip.cc | 5 +--- src/hb-raster-svg-fill.cc | 2 +- src/hb-raster-svg-gradient.cc | 2 +- src/hb-raster-svg-use.cc | 2 +- 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index 2338b5555..793c0c4ec 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -209,3 +209,46 @@ hb_raster_svg_parse_id_ref (hb_svg_str_t s, } return true; } + +bool +hb_raster_svg_parse_local_id_ref (hb_svg_str_t s, + hb_svg_str_t *out_id, + hb_svg_str_t *out_tail) +{ + if (out_id) *out_id = {}; + if (out_tail) *out_tail = {}; + s = s.trim (); + + if (s.len && s.data[0] == '#') + { + hb_svg_str_t id = {s.data + 1, s.len - 1}; + id = id.trim (); + if (!id.len) + return false; + if (out_id) *out_id = id; + return true; + } + + if (!svg_str_starts_with_ascii_ci (s, "url(")) + return false; + + hb_svg_str_t id; + if (!hb_raster_svg_parse_id_ref (s, &id, out_tail)) + return false; + + const char *p = s.data + 4; + const char *end = s.data + s.len; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + if (p < end && (*p == '\'' || *p == '"')) + { + p++; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + if (p >= end || *p != '#') + return false; + } + else if (p >= end || *p != '#') + return false; + + if (out_id) *out_id = id; + return true; +} diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 1ba1b62f0..52cec2263 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -129,6 +129,9 @@ HB_INTERNAL hb_svg_str_t hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t HB_INTERNAL bool hb_raster_svg_parse_id_ref (hb_svg_str_t s, hb_svg_str_t *out_id, hb_svg_str_t *out_tail); +HB_INTERNAL bool hb_raster_svg_parse_local_id_ref (hb_svg_str_t s, + hb_svg_str_t *out_id, + hb_svg_str_t *out_tail); static inline float svg_parse_float_clamped01 (hb_svg_str_t s) { diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 1318eaf4f..2f4b7c38f 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -152,10 +152,7 @@ hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, if (!trimmed.len || trimmed.eq ("none")) return false; hb_svg_str_t clip_id; - if (!hb_raster_svg_parse_id_ref (trimmed, &clip_id, nullptr)) - return false; - if (svg_str_starts_with_ascii_ci (trimmed, "url(") && - !svg_str_starts_with_ascii_ci (trimmed, "url(#")) + if (!hb_raster_svg_parse_local_id_ref (trimmed, &clip_id, nullptr)) return false; const hb_svg_clip_path_def_t *clip = defs->find_clip_path (hb_bytes_t (clip_id.data, clip_id.len)); diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 2ea590cbc..f2ffbc008 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -99,7 +99,7 @@ svg_parse_paint_url_with_fallback (hb_svg_str_t s, { if (out_id) *out_id = {}; hb_svg_str_t id; - if (!hb_raster_svg_parse_id_ref (s, &id, fallback)) + if (!hb_raster_svg_parse_local_id_ref (s, &id, fallback)) return false; if (!svg_str_starts_with_ascii_ci (s.trim (), "url(")) return false; diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index f390986e0..7da4bfefd 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -126,7 +126,7 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, if (href.len) { hb_svg_str_t href_id; - if (hb_raster_svg_parse_id_ref (href, &href_id, nullptr)) + if (hb_raster_svg_parse_local_id_ref (href, &href_id, nullptr)) grad.href_id = hb_bytes_t (href_id.data, href_id.len); } } diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index 9f06b7a58..b11d7ad44 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -99,7 +99,7 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_str_t href = hb_raster_svg_find_href_attr (parser); hb_svg_str_t ref_id; - if (!hb_raster_svg_parse_id_ref (href, &ref_id, nullptr)) + if (!hb_raster_svg_parse_local_id_ref (href, &ref_id, nullptr)) return; float use_x = svg_parse_float (parser.find_attr ("x")); From ef73c6ef52b6276a5d35ce99c9e7944e0e722c59 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:33:27 -0300 Subject: [PATCH 037/105] [raster] Apply empty SVG clipPath definitions as empty clips --- src/hb-raster-svg-clip.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 2f4b7c38f..31ca8e660 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -95,7 +95,7 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, id = {}; } - if (id.len && clip.shape_count) + if (id.len) (void) defs->add_clip_path (hb_bytes_t (id.data, id.len), clip); } @@ -156,7 +156,7 @@ hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, return false; const hb_svg_clip_path_def_t *clip = defs->find_clip_path (hb_bytes_t (clip_id.data, clip_id.len)); - if (!clip || !clip->shape_count) return false; + if (!clip) return false; hb_svg_clip_emit_data_t ed; ed.defs = defs; From 966a1c2849477351ecf3b7b816d7d66e5cca406f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:34:06 -0300 Subject: [PATCH 038/105] [raster] Handle nested transforms in SVG clipPath content --- src/hb-raster-svg-clip.cc | 64 ++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 31ca8e660..b8e833060 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -58,6 +58,11 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, if (tok == SVG_TOKEN_OPEN_TAG) { + const unsigned SVG_MAX_CLIP_DEPTH = 64; + hb_svg_transform_t inherited[SVG_MAX_CLIP_DEPTH]; + inherited[0] = hb_svg_transform_t (); + inherited[1] = hb_svg_transform_t (); + int cdepth = 1; bool had_alloc_failure = false; while (cdepth > 0) @@ -67,28 +72,51 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, if (ct == SVG_TOKEN_CLOSE_TAG) { cdepth--; continue; } if (ct == SVG_TOKEN_OPEN_TAG || ct == SVG_TOKEN_SELF_CLOSE_TAG) { - if (cdepth == 1) + hb_svg_transform_t effective = (unsigned) cdepth < SVG_MAX_CLIP_DEPTH + ? inherited[cdepth] + : hb_svg_transform_t (); + + hb_svg_str_t sh_transform = parser.find_attr ("transform"); + bool has_local_transform = sh_transform.len > 0; + if (has_local_transform) { - hb_svg_shape_emit_data_t shape; - if (hb_raster_svg_parse_shape_tag (parser, &shape)) - { - hb_svg_clip_shape_t clip_shape; - clip_shape.shape = shape; - hb_svg_str_t sh_transform = parser.find_attr ("transform"); - if (sh_transform.len) - { - clip_shape.has_transform = true; - hb_raster_svg_parse_transform (sh_transform, &clip_shape.transform); - } - defs->clip_shapes.push (clip_shape); - if (likely (!defs->clip_shapes.in_error ())) - clip.shape_count++; - else - had_alloc_failure = true; - } + hb_svg_transform_t local; + hb_raster_svg_parse_transform (sh_transform, &local); + effective.multiply (local); } + + hb_svg_shape_emit_data_t shape; + if (hb_raster_svg_parse_shape_tag (parser, &shape)) + { + hb_svg_clip_shape_t clip_shape; + clip_shape.shape = shape; + bool has_effective_transform = has_local_transform; + if ((unsigned) cdepth < SVG_MAX_CLIP_DEPTH && cdepth > 1) + { + const hb_svg_transform_t &parent = inherited[cdepth]; + has_effective_transform = has_effective_transform || + parent.xx != 1.f || parent.yx != 0.f || + parent.xy != 0.f || parent.yy != 1.f || + parent.dx != 0.f || parent.dy != 0.f; + } + if (has_effective_transform) + { + clip_shape.has_transform = true; + clip_shape.transform = effective; + } + defs->clip_shapes.push (clip_shape); + if (likely (!defs->clip_shapes.in_error ())) + clip.shape_count++; + else + had_alloc_failure = true; + } + if (ct == SVG_TOKEN_OPEN_TAG) + { + if ((unsigned) (cdepth + 1) < SVG_MAX_CLIP_DEPTH) + inherited[cdepth + 1] = effective; cdepth++; + } } } if (had_alloc_failure) From e89ebd73b3319e7488663db3836fee335e5db4a6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:34:21 -0300 Subject: [PATCH 039/105] [raster] Honor style transform in SVG clipPath parsing --- src/hb-raster-svg-clip.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index b8e833060..bb08f06d9 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -46,7 +46,9 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, else clip.units_user_space = true; - hb_svg_str_t cp_transform = parser.find_attr ("transform"); + hb_svg_style_props_t root_style_props; + svg_parse_style_props (parser.find_attr ("style"), &root_style_props); + hb_svg_str_t cp_transform = svg_pick_attr_or_style (parser, root_style_props.transform, "transform"); if (cp_transform.len) { clip.has_clip_transform = true; @@ -76,7 +78,9 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, ? inherited[cdepth] : hb_svg_transform_t (); - hb_svg_str_t sh_transform = parser.find_attr ("transform"); + hb_svg_style_props_t style_props; + svg_parse_style_props (parser.find_attr ("style"), &style_props); + hb_svg_str_t sh_transform = svg_pick_attr_or_style (parser, style_props.transform, "transform"); bool has_local_transform = sh_transform.len > 0; if (has_local_transform) { From d57bd72030cd9debcec3192d91260489a495093f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:35:19 -0300 Subject: [PATCH 040/105] [raster] Respect visibility when collecting clipPath shapes --- src/hb-raster-svg-clip.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index bb08f06d9..adee02ee5 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -74,6 +74,29 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, if (ct == SVG_TOKEN_CLOSE_TAG) { cdepth--; continue; } if (ct == SVG_TOKEN_OPEN_TAG || ct == SVG_TOKEN_SELF_CLOSE_TAG) { + hb_svg_style_props_t visibility_style_props; + svg_parse_style_props (parser.find_attr ("style"), &visibility_style_props); + hb_svg_str_t display_str = svg_pick_attr_or_style (parser, visibility_style_props.display, "display"); + hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, visibility_style_props.visibility, "visibility"); + bool is_hidden = svg_str_eq_ascii_ci (display_str.trim (), "none") || + svg_str_eq_ascii_ci (visibility_str.trim (), "hidden") || + svg_str_eq_ascii_ci (visibility_str.trim (), "collapse"); + if (is_hidden) + { + if (ct == SVG_TOKEN_OPEN_TAG) + { + int skip_depth = 1; + while (skip_depth > 0) + { + hb_svg_token_type_t st = parser.next (); + if (st == SVG_TOKEN_EOF) break; + if (st == SVG_TOKEN_CLOSE_TAG) skip_depth--; + else if (st == SVG_TOKEN_OPEN_TAG) skip_depth++; + } + } + continue; + } + hb_svg_transform_t effective = (unsigned) cdepth < SVG_MAX_CLIP_DEPTH ? inherited[cdepth] : hb_svg_transform_t (); From 124d92f9072795fa04aaff965e6690445c16e465 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:37:19 -0300 Subject: [PATCH 041/105] [raster] Support shapes inside SVG clipPath --- src/hb-raster-svg-clip.cc | 168 +++++++++++++++++++++++++++------ src/hb-raster-svg-clip.hh | 7 +- src/hb-raster-svg-defs-scan.cc | 4 +- src/hb-raster-svg-defs-scan.hh | 5 + src/hb-raster-svg-render.cc | 8 +- 5 files changed, 157 insertions(+), 35 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index adee02ee5..0da66c52b 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -33,10 +33,79 @@ #include "hb-raster-svg.hh" #include "hb-raster-svg-base.hh" +#include + +static inline bool +svg_transform_is_identity (const hb_svg_transform_t &t) +{ + return t.xx == 1.f && t.yx == 0.f && + t.xy == 0.f && t.yy == 1.f && + t.dx == 0.f && t.dy == 0.f; +} + +static bool +svg_parse_element_transform (hb_svg_xml_parser_t &parser, + hb_svg_transform_t *out) +{ + hb_svg_style_props_t style_props; + svg_parse_style_props (parser.find_attr ("style"), &style_props); + hb_svg_str_t transform = svg_pick_attr_or_style (parser, style_props.transform, "transform"); + if (!transform.len) + return false; + hb_raster_svg_parse_transform (transform, out); + return true; +} + +static bool +svg_find_element_by_id (const char *doc_start, + unsigned doc_len, + const OT::SVG::accelerator_t *svg_accel, + const OT::SVG::svg_doc_cache_t *doc_cache, + hb_svg_str_t id, + const char **found) +{ + *found = nullptr; + if (!doc_start || !doc_len || !id.len) + return false; + + if (doc_cache && svg_accel) + { + unsigned start = 0, end = 0; + OT::SVG::svg_id_span_t key = {id.data, id.len}; + if (svg_accel->doc_cache_find_id_span (doc_cache, key, &start, &end)) + { + if (start < doc_len && end <= doc_len && start < end) + { + *found = doc_start + start; + return true; + } + } + } + + hb_svg_xml_parser_t search (doc_start, doc_len); + while (true) + { + hb_svg_token_type_t tok = search.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok != SVG_TOKEN_OPEN_TAG && tok != SVG_TOKEN_SELF_CLOSE_TAG) continue; + hb_svg_str_t attr_id = search.find_attr ("id"); + if (attr_id.len == id.len && 0 == memcmp (attr_id.data, id.data, id.len)) + { + *found = search.tag_start; + return true; + } + } + return false; +} + void hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, - hb_svg_token_type_t tok) + hb_svg_token_type_t tok, + const char *doc_start, + unsigned doc_len, + const OT::SVG::accelerator_t *svg_accel, + const OT::SVG::svg_doc_cache_t *doc_cache) { hb_svg_clip_path_def_t clip; hb_svg_str_t id = parser.find_attr ("id"); @@ -46,13 +115,11 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, else clip.units_user_space = true; - hb_svg_style_props_t root_style_props; - svg_parse_style_props (parser.find_attr ("style"), &root_style_props); - hb_svg_str_t cp_transform = svg_pick_attr_or_style (parser, root_style_props.transform, "transform"); - if (cp_transform.len) + hb_svg_transform_t cp_t; + if (svg_parse_element_transform (parser, &cp_t)) { clip.has_clip_transform = true; - hb_raster_svg_parse_transform (cp_transform, &clip.clip_transform); + clip.clip_transform = cp_t; } clip.first_shape = defs->clip_shapes.length; @@ -101,41 +168,80 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, ? inherited[cdepth] : hb_svg_transform_t (); - hb_svg_style_props_t style_props; - svg_parse_style_props (parser.find_attr ("style"), &style_props); - hb_svg_str_t sh_transform = svg_pick_attr_or_style (parser, style_props.transform, "transform"); - bool has_local_transform = sh_transform.len > 0; + hb_svg_transform_t local; + bool has_local_transform = svg_parse_element_transform (parser, &local); if (has_local_transform) { - hb_svg_transform_t local; - hb_raster_svg_parse_transform (sh_transform, &local); effective.multiply (local); } - hb_svg_shape_emit_data_t shape; - if (hb_raster_svg_parse_shape_tag (parser, &shape)) + if (parser.tag_name.eq ("use")) { - hb_svg_clip_shape_t clip_shape; - clip_shape.shape = shape; - bool has_effective_transform = has_local_transform; - if ((unsigned) cdepth < SVG_MAX_CLIP_DEPTH && cdepth > 1) + hb_svg_str_t href = hb_raster_svg_find_href_attr (parser); + hb_svg_str_t ref_id; + if (hb_raster_svg_parse_local_id_ref (href, &ref_id, nullptr)) { - const hb_svg_transform_t &parent = inherited[cdepth]; - has_effective_transform = has_effective_transform || - parent.xx != 1.f || parent.yx != 0.f || - parent.xy != 0.f || parent.yy != 1.f || - parent.dx != 0.f || parent.dy != 0.f; + const char *found = nullptr; + if (svg_find_element_by_id (doc_start, doc_len, svg_accel, doc_cache, + ref_id, &found)) + { + unsigned remaining = doc_len - (unsigned) (found - doc_start); + hb_svg_xml_parser_t ref_parser (found, remaining); + hb_svg_token_type_t rt = ref_parser.next (); + if (rt == SVG_TOKEN_OPEN_TAG || rt == SVG_TOKEN_SELF_CLOSE_TAG) + { + hb_svg_shape_emit_data_t ref_shape; + if (hb_raster_svg_parse_shape_tag (ref_parser, &ref_shape)) + { + hb_svg_transform_t use_t = effective; + float use_x = svg_parse_float (parser.find_attr ("x")); + float use_y = svg_parse_float (parser.find_attr ("y")); + if (use_x != 0.f || use_y != 0.f) + { + hb_svg_transform_t tr; + tr.dx = use_x; + tr.dy = use_y; + use_t.multiply (tr); + } + hb_svg_transform_t ref_t; + if (svg_parse_element_transform (ref_parser, &ref_t)) + use_t.multiply (ref_t); + + hb_svg_clip_shape_t clip_shape; + clip_shape.shape = ref_shape; + if (!svg_transform_is_identity (use_t)) + { + clip_shape.has_transform = true; + clip_shape.transform = use_t; + } + defs->clip_shapes.push (clip_shape); + if (likely (!defs->clip_shapes.in_error ())) + clip.shape_count++; + else + had_alloc_failure = true; + } + } + } } - if (has_effective_transform) + } + else + { + hb_svg_shape_emit_data_t shape; + if (hb_raster_svg_parse_shape_tag (parser, &shape)) { - clip_shape.has_transform = true; - clip_shape.transform = effective; + hb_svg_clip_shape_t clip_shape; + clip_shape.shape = shape; + if (!svg_transform_is_identity (effective)) + { + clip_shape.has_transform = true; + clip_shape.transform = effective; + } + defs->clip_shapes.push (clip_shape); + if (likely (!defs->clip_shapes.in_error ())) + clip.shape_count++; + else + had_alloc_failure = true; } - defs->clip_shapes.push (clip_shape); - if (likely (!defs->clip_shapes.in_error ())) - clip.shape_count++; - else - had_alloc_failure = true; } if (ct == SVG_TOKEN_OPEN_TAG) diff --git a/src/hb-raster-svg-clip.hh b/src/hb-raster-svg-clip.hh index 27a747cc5..0b89cc8f0 100644 --- a/src/hb-raster-svg-clip.hh +++ b/src/hb-raster-svg-clip.hh @@ -29,6 +29,7 @@ #include "hb.hh" +#include "OT/Color/svg/svg.hh" #include "hb-paint.hh" #include "hb-raster-svg-defs.hh" @@ -37,7 +38,11 @@ struct hb_raster_paint_t; HB_INTERNAL void hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, - hb_svg_token_type_t tok); + hb_svg_token_type_t tok, + const char *doc_start, + unsigned doc_len, + const OT::SVG::accelerator_t *svg_accel, + const OT::SVG::svg_doc_cache_t *doc_cache); HB_INTERNAL bool hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, diff --git a/src/hb-raster-svg-defs-scan.cc b/src/hb-raster-svg-defs-scan.cc index 27d5258b3..ba806b18f 100644 --- a/src/hb-raster-svg-defs-scan.cc +++ b/src/hb-raster-svg-defs-scan.cc @@ -54,7 +54,9 @@ hb_raster_svg_process_defs_child_tag (const hb_svg_defs_scan_context_t *ctx, } if (parser.tag_name.eq ("clipPath")) { - hb_raster_svg_process_clip_path_def (ctx->defs, parser, tok); + hb_raster_svg_process_clip_path_def (ctx->defs, parser, tok, + ctx->doc_start, ctx->doc_len, + ctx->svg_accel, ctx->doc_cache); return true; } return false; diff --git a/src/hb-raster-svg-defs-scan.hh b/src/hb-raster-svg-defs-scan.hh index a150d630a..bddad1f74 100644 --- a/src/hb-raster-svg-defs-scan.hh +++ b/src/hb-raster-svg-defs-scan.hh @@ -29,6 +29,7 @@ #include "hb.hh" +#include "OT/Color/svg/svg.hh" #include "hb-raster-svg-defs.hh" struct hb_svg_defs_scan_context_t @@ -39,6 +40,10 @@ struct hb_svg_defs_scan_context_t hb_color_t foreground; hb_face_t *face; unsigned palette; + const char *doc_start; + unsigned doc_len; + const OT::SVG::accelerator_t *svg_accel; + const OT::SVG::svg_doc_cache_t *doc_cache; }; HB_INTERNAL void diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 5f978f156..efcc2d89a 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -178,7 +178,9 @@ svg_render_container_element (hb_svg_render_context_t *ctx, hb_svg_defs_scan_context_t scan_ctx = { &ctx->defs, ctx->pfuncs, ctx->paint, ctx->foreground, hb_font_get_face (ctx->font), - ctx->palette + ctx->palette, + ctx->doc_start, ctx->doc_len, + ctx->svg_accel, ctx->doc_cache }; hb_raster_svg_process_defs_element (&scan_ctx, parser); } @@ -509,7 +511,9 @@ hb_raster_svg_render (hb_raster_paint_t *paint, hb_svg_defs_scan_context_t scan_ctx = { &ctx.defs, ctx.pfuncs, ctx.paint, ctx.foreground, hb_font_get_face (ctx.font), - ctx.palette + ctx.palette, + ctx.doc_start, ctx.doc_len, + ctx.svg_accel, ctx.doc_cache }; hb_raster_svg_collect_defs (&scan_ctx, data, data_len); From 2f6fdda0c3bc34815152c3da338aaf53faf1a4a0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:39:16 -0300 Subject: [PATCH 042/105] [raster] Guard clipPath reference cycles with hb_decycler_t --- src/hb-raster-svg-clip.cc | 147 ++++++++++++++++++++++---------------- 1 file changed, 87 insertions(+), 60 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 0da66c52b..14591ffc6 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -32,6 +32,7 @@ #include "hb-raster-paint.hh" #include "hb-raster-svg.hh" #include "hb-raster-svg-base.hh" +#include "hb-decycler.hh" #include @@ -98,6 +99,87 @@ svg_find_element_by_id (const char *doc_start, return false; } +static inline void +svg_clip_append_shape (hb_svg_defs_t *defs, + hb_svg_clip_path_def_t *clip, + const hb_svg_shape_emit_data_t &shape, + const hb_svg_transform_t &transform, + bool *had_alloc_failure) +{ + hb_svg_clip_shape_t clip_shape; + clip_shape.shape = shape; + if (!svg_transform_is_identity (transform)) + { + clip_shape.has_transform = true; + clip_shape.transform = transform; + } + defs->clip_shapes.push (clip_shape); + if (likely (!defs->clip_shapes.in_error ())) + clip->shape_count++; + else if (had_alloc_failure) + *had_alloc_failure = true; +} + +static void +svg_clip_collect_use_target (hb_svg_defs_t *defs, + hb_svg_clip_path_def_t *clip, + hb_svg_xml_parser_t &use_parser, + const hb_svg_transform_t &base_transform, + const char *doc_start, + unsigned doc_len, + const OT::SVG::accelerator_t *svg_accel, + const OT::SVG::svg_doc_cache_t *doc_cache, + hb_decycler_t &use_decycler, + unsigned depth, + bool *had_alloc_failure) +{ + const unsigned SVG_MAX_CLIP_USE_DEPTH = 64; + if (depth >= SVG_MAX_CLIP_USE_DEPTH) + return; + + hb_svg_str_t href = hb_raster_svg_find_href_attr (use_parser); + hb_svg_str_t ref_id; + if (!hb_raster_svg_parse_local_id_ref (href, &ref_id, nullptr)) + return; + + const char *found = nullptr; + if (!svg_find_element_by_id (doc_start, doc_len, svg_accel, doc_cache, ref_id, &found)) + return; + + hb_decycler_node_t node (use_decycler); + if (unlikely (!node.visit ((uintptr_t) found))) + return; + + hb_svg_transform_t effective = base_transform; + float use_x = svg_parse_float (use_parser.find_attr ("x")); + float use_y = svg_parse_float (use_parser.find_attr ("y")); + if (use_x != 0.f || use_y != 0.f) + { + hb_svg_transform_t tr; + tr.dx = use_x; + tr.dy = use_y; + effective.multiply (tr); + } + + unsigned remaining = doc_len - (unsigned) (found - doc_start); + hb_svg_xml_parser_t ref_parser (found, remaining); + hb_svg_token_type_t rt = ref_parser.next (); + if (rt != SVG_TOKEN_OPEN_TAG && rt != SVG_TOKEN_SELF_CLOSE_TAG) + return; + + hb_svg_transform_t ref_t; + if (svg_parse_element_transform (ref_parser, &ref_t)) + effective.multiply (ref_t); + + hb_svg_shape_emit_data_t ref_shape; + if (hb_raster_svg_parse_shape_tag (ref_parser, &ref_shape)) + svg_clip_append_shape (defs, clip, ref_shape, effective, had_alloc_failure); + else if (ref_parser.tag_name.eq ("use")) + svg_clip_collect_use_target (defs, clip, ref_parser, effective, + doc_start, doc_len, svg_accel, doc_cache, + use_decycler, depth + 1, had_alloc_failure); +} + void hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, hb_svg_xml_parser_t &parser, @@ -131,6 +213,7 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, hb_svg_transform_t inherited[SVG_MAX_CLIP_DEPTH]; inherited[0] = hb_svg_transform_t (); inherited[1] = hb_svg_transform_t (); + hb_decycler_t use_decycler; int cdepth = 1; bool had_alloc_failure = false; @@ -177,71 +260,15 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, if (parser.tag_name.eq ("use")) { - hb_svg_str_t href = hb_raster_svg_find_href_attr (parser); - hb_svg_str_t ref_id; - if (hb_raster_svg_parse_local_id_ref (href, &ref_id, nullptr)) - { - const char *found = nullptr; - if (svg_find_element_by_id (doc_start, doc_len, svg_accel, doc_cache, - ref_id, &found)) - { - unsigned remaining = doc_len - (unsigned) (found - doc_start); - hb_svg_xml_parser_t ref_parser (found, remaining); - hb_svg_token_type_t rt = ref_parser.next (); - if (rt == SVG_TOKEN_OPEN_TAG || rt == SVG_TOKEN_SELF_CLOSE_TAG) - { - hb_svg_shape_emit_data_t ref_shape; - if (hb_raster_svg_parse_shape_tag (ref_parser, &ref_shape)) - { - hb_svg_transform_t use_t = effective; - float use_x = svg_parse_float (parser.find_attr ("x")); - float use_y = svg_parse_float (parser.find_attr ("y")); - if (use_x != 0.f || use_y != 0.f) - { - hb_svg_transform_t tr; - tr.dx = use_x; - tr.dy = use_y; - use_t.multiply (tr); - } - hb_svg_transform_t ref_t; - if (svg_parse_element_transform (ref_parser, &ref_t)) - use_t.multiply (ref_t); - - hb_svg_clip_shape_t clip_shape; - clip_shape.shape = ref_shape; - if (!svg_transform_is_identity (use_t)) - { - clip_shape.has_transform = true; - clip_shape.transform = use_t; - } - defs->clip_shapes.push (clip_shape); - if (likely (!defs->clip_shapes.in_error ())) - clip.shape_count++; - else - had_alloc_failure = true; - } - } - } - } + svg_clip_collect_use_target (defs, &clip, parser, effective, + doc_start, doc_len, svg_accel, doc_cache, + use_decycler, 0, &had_alloc_failure); } else { hb_svg_shape_emit_data_t shape; if (hb_raster_svg_parse_shape_tag (parser, &shape)) - { - hb_svg_clip_shape_t clip_shape; - clip_shape.shape = shape; - if (!svg_transform_is_identity (effective)) - { - clip_shape.has_transform = true; - clip_shape.transform = effective; - } - defs->clip_shapes.push (clip_shape); - if (likely (!defs->clip_shapes.in_error ())) - clip.shape_count++; - else - had_alloc_failure = true; - } + svg_clip_append_shape (defs, &clip, shape, effective, &had_alloc_failure); } if (ct == SVG_TOKEN_OPEN_TAG) From 0b03fe5ad60b4eff0aa496e363a351f3fba72fff Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:40:16 -0300 Subject: [PATCH 043/105] [raster] Traverse container targets for clipPath refs --- src/hb-raster-svg-clip.cc | 125 +++++++++++++++++++++++++++++++++++--- 1 file changed, 117 insertions(+), 8 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 14591ffc6..b88d2d470 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -120,6 +120,44 @@ svg_clip_append_shape (hb_svg_defs_t *defs, *had_alloc_failure = true; } +static void +svg_skip_subtree (hb_svg_xml_parser_t &parser) +{ + int depth = 1; + while (depth > 0) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok == SVG_TOKEN_CLOSE_TAG) depth--; + else if (tok == SVG_TOKEN_OPEN_TAG) depth++; + } +} + +static inline bool +svg_is_hidden_element (hb_svg_xml_parser_t &parser) +{ + hb_svg_style_props_t style_props; + svg_parse_style_props (parser.find_attr ("style"), &style_props); + hb_svg_str_t display_str = svg_pick_attr_or_style (parser, style_props.display, "display"); + hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, style_props.visibility, "visibility"); + return svg_str_eq_ascii_ci (display_str.trim (), "none") || + svg_str_eq_ascii_ci (visibility_str.trim (), "hidden") || + svg_str_eq_ascii_ci (visibility_str.trim (), "collapse"); +} + +static void +svg_clip_collect_ref_element (hb_svg_defs_t *defs, + hb_svg_clip_path_def_t *clip, + hb_svg_xml_parser_t &parser, + const hb_svg_transform_t &base_transform, + const char *doc_start, + unsigned doc_len, + const OT::SVG::accelerator_t *svg_accel, + const OT::SVG::svg_doc_cache_t *doc_cache, + hb_decycler_t &use_decycler, + unsigned depth, + bool *had_alloc_failure); + static void svg_clip_collect_use_target (hb_svg_defs_t *defs, hb_svg_clip_path_def_t *clip, @@ -167,17 +205,88 @@ svg_clip_collect_use_target (hb_svg_defs_t *defs, if (rt != SVG_TOKEN_OPEN_TAG && rt != SVG_TOKEN_SELF_CLOSE_TAG) return; - hb_svg_transform_t ref_t; - if (svg_parse_element_transform (ref_parser, &ref_t)) - effective.multiply (ref_t); + svg_clip_collect_ref_element (defs, clip, ref_parser, effective, + doc_start, doc_len, svg_accel, doc_cache, + use_decycler, depth + 1, had_alloc_failure); +} - hb_svg_shape_emit_data_t ref_shape; - if (hb_raster_svg_parse_shape_tag (ref_parser, &ref_shape)) - svg_clip_append_shape (defs, clip, ref_shape, effective, had_alloc_failure); - else if (ref_parser.tag_name.eq ("use")) - svg_clip_collect_use_target (defs, clip, ref_parser, effective, +static void +svg_clip_collect_ref_element (hb_svg_defs_t *defs, + hb_svg_clip_path_def_t *clip, + hb_svg_xml_parser_t &parser, + const hb_svg_transform_t &base_transform, + const char *doc_start, + unsigned doc_len, + const OT::SVG::accelerator_t *svg_accel, + const OT::SVG::svg_doc_cache_t *doc_cache, + hb_decycler_t &use_decycler, + unsigned depth, + bool *had_alloc_failure) +{ + const unsigned SVG_MAX_CLIP_REF_DEPTH = 64; + if (depth >= SVG_MAX_CLIP_REF_DEPTH) + { + if (!parser.self_closing) + svg_skip_subtree (parser); + return; + } + + if (svg_is_hidden_element (parser)) + { + if (!parser.self_closing) + svg_skip_subtree (parser); + return; + } + + hb_svg_transform_t effective = base_transform; + hb_svg_transform_t local_t; + if (svg_parse_element_transform (parser, &local_t)) + effective.multiply (local_t); + + hb_svg_shape_emit_data_t shape; + if (hb_raster_svg_parse_shape_tag (parser, &shape)) + { + svg_clip_append_shape (defs, clip, shape, effective, had_alloc_failure); + if (!parser.self_closing) + svg_skip_subtree (parser); + return; + } + + if (parser.tag_name.eq ("use")) + { + svg_clip_collect_use_target (defs, clip, parser, effective, doc_start, doc_len, svg_accel, doc_cache, use_decycler, depth + 1, had_alloc_failure); + if (!parser.self_closing) + svg_skip_subtree (parser); + return; + } + + bool is_container = parser.tag_name.eq ("g") || + parser.tag_name.eq ("svg") || + parser.tag_name.eq ("symbol"); + if (!is_container || parser.self_closing) + { + if (!parser.self_closing) + svg_skip_subtree (parser); + return; + } + + int inner_depth = 1; + while (inner_depth > 0) + { + hb_svg_token_type_t tok = parser.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok == SVG_TOKEN_CLOSE_TAG) + { + inner_depth--; + continue; + } + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + svg_clip_collect_ref_element (defs, clip, parser, effective, + doc_start, doc_len, svg_accel, doc_cache, + use_decycler, depth + 1, had_alloc_failure); + } } void From 68be92bdef65cc5108e5e62c119c42734b433343 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:40:48 -0300 Subject: [PATCH 044/105] [raster] Honor preserveAspectRatio for SVG viewports --- src/hb-raster-svg-use.cc | 90 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index b11d7ad44..fd0382931 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -88,6 +88,86 @@ svg_parse_viewbox (hb_svg_str_t viewbox_str, return true; } +static inline float +svg_align_offset (hb_svg_str_t align, + float leftover, + char axis) +{ + if (leftover <= 0.f) return 0.f; + if ((axis == 'x' && align.starts_with ("xMin")) || + (axis == 'y' && (align.eq ("xMinYMin") || align.eq ("xMidYMin") || align.eq ("xMaxYMin")))) + return 0.f; + if ((axis == 'x' && align.starts_with ("xMax")) || + (axis == 'y' && (align.eq ("xMinYMax") || align.eq ("xMidYMax") || align.eq ("xMaxYMax")))) + return leftover; + return leftover * 0.5f; +} + +static bool +svg_compute_viewbox_transform (float viewport_w, + float viewport_h, + float vb_x, + float vb_y, + float vb_w, + float vb_h, + hb_svg_str_t preserve_aspect_ratio, + hb_svg_transform_t *out) +{ + if (!(viewport_w > 0.f && viewport_h > 0.f && vb_w > 0.f && vb_h > 0.f)) + return false; + + hb_svg_str_t par = preserve_aspect_ratio.trim (); + if (!par.len) + par = hb_svg_str_t ("xMidYMid meet", 12); + + bool is_none = false; + bool is_slice = false; + if (svg_str_starts_with_ascii_ci (par, "none")) + is_none = true; + else if (par.len >= 5) + { + if (svg_str_starts_with_ascii_ci (par.trim (), "x")) + { + const char *p = par.data; + const char *end = par.data + par.len; + while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + if (p < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (p, (unsigned) (end - p)), "slice")) + is_slice = true; + } + } + + hb_svg_transform_t t; + if (is_none) + { + t.xx = viewport_w / vb_w; + t.yy = viewport_h / vb_h; + t.dx = -vb_x * t.xx; + t.dy = -vb_y * t.yy; + *out = t; + return true; + } + + float sx = viewport_w / vb_w; + float sy = viewport_h / vb_h; + float s = is_slice ? hb_max (sx, sy) : hb_min (sx, sy); + float scaled_w = vb_w * s; + float scaled_h = vb_h * s; + float leftover_x = viewport_w - scaled_w; + float leftover_y = viewport_h - scaled_h; + + hb_svg_str_t align = par.trim (); + if (!align.starts_with ("x")) + align = hb_svg_str_t ("xMidYMid", 8); + + t.xx = s; + t.yy = s; + t.dx = svg_align_offset (align, leftover_x, 'x') - vb_x * s; + t.dy = svg_align_offset (align, leftover_y, 'y') - vb_y * s; + *out = t; + return true; +} + void hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t &parser, @@ -138,10 +218,14 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, if ((ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) && use_w > 0.f && use_h > 0.f) { - float vb_w = 0.f, vb_h = 0.f; - if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), nullptr, nullptr, &vb_w, &vb_h)) + float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; + hb_svg_transform_t t; + if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && + svg_compute_viewbox_transform (use_w, use_h, vb_x, vb_y, vb_w, vb_h, + ref_parser.find_attr ("preserveAspectRatio"), + &t)) { - hb_paint_push_transform (ctx->pfuncs, ctx->paint, use_w / vb_w, 0, 0, use_h / vb_h, 0, 0); + hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); has_viewport_scale = true; } } From 5cad3ea5bc4fc2321d9663e01b6ea57dd5b4a114 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:41:13 -0300 Subject: [PATCH 045/105] [raster] Inherit visibility through nested clipPath content --- src/hb-raster-svg-clip.cc | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index b88d2d470..fa01a54c4 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -320,8 +320,11 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, { const unsigned SVG_MAX_CLIP_DEPTH = 64; hb_svg_transform_t inherited[SVG_MAX_CLIP_DEPTH]; + bool inherited_visibility[SVG_MAX_CLIP_DEPTH]; inherited[0] = hb_svg_transform_t (); inherited[1] = hb_svg_transform_t (); + inherited_visibility[0] = true; + inherited_visibility[1] = true; hb_decycler_t use_decycler; int cdepth = 1; @@ -337,9 +340,24 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, svg_parse_style_props (parser.find_attr ("style"), &visibility_style_props); hb_svg_str_t display_str = svg_pick_attr_or_style (parser, visibility_style_props.display, "display"); hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, visibility_style_props.visibility, "visibility"); - bool is_hidden = svg_str_eq_ascii_ci (display_str.trim (), "none") || - svg_str_eq_ascii_ci (visibility_str.trim (), "hidden") || - svg_str_eq_ascii_ci (visibility_str.trim (), "collapse"); + bool parent_visible = (unsigned) cdepth < SVG_MAX_CLIP_DEPTH + ? inherited_visibility[cdepth] + : true; + bool is_visible = parent_visible; + if (svg_str_eq_ascii_ci (display_str.trim (), "none")) + is_visible = false; + hb_svg_str_t vis_trim = visibility_str.trim (); + if (vis_trim.len) + { + if (svg_str_eq_ascii_ci (vis_trim, "inherit")) + is_visible = parent_visible; + else if (svg_str_eq_ascii_ci (vis_trim, "hidden") || + svg_str_eq_ascii_ci (vis_trim, "collapse")) + is_visible = false; + else if (svg_str_eq_ascii_ci (vis_trim, "visible")) + is_visible = true; + } + bool is_hidden = !is_visible; if (is_hidden) { if (ct == SVG_TOKEN_OPEN_TAG) @@ -383,7 +401,10 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, if (ct == SVG_TOKEN_OPEN_TAG) { if ((unsigned) (cdepth + 1) < SVG_MAX_CLIP_DEPTH) + { inherited[cdepth + 1] = effective; + inherited_visibility[cdepth + 1] = is_visible; + } cdepth++; } } From 48f9bbf814893caba79b813118624bde8e4a2c43 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:41:53 -0300 Subject: [PATCH 046/105] [raster] Refactor SVG clipPath collection helpers --- src/hb-raster-svg-clip.cc | 105 +++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index fa01a54c4..e870583a7 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -99,12 +99,22 @@ svg_find_element_by_id (const char *doc_start, return false; } +struct hb_svg_clip_collect_context_t +{ + hb_svg_defs_t *defs; + hb_svg_clip_path_def_t *clip; + const char *doc_start; + unsigned doc_len; + const OT::SVG::accelerator_t *svg_accel; + const OT::SVG::svg_doc_cache_t *doc_cache; + hb_decycler_t *use_decycler; + bool *had_alloc_failure; +}; + static inline void -svg_clip_append_shape (hb_svg_defs_t *defs, - hb_svg_clip_path_def_t *clip, +svg_clip_append_shape (hb_svg_clip_collect_context_t *ctx, const hb_svg_shape_emit_data_t &shape, - const hb_svg_transform_t &transform, - bool *had_alloc_failure) + const hb_svg_transform_t &transform) { hb_svg_clip_shape_t clip_shape; clip_shape.shape = shape; @@ -113,11 +123,11 @@ svg_clip_append_shape (hb_svg_defs_t *defs, clip_shape.has_transform = true; clip_shape.transform = transform; } - defs->clip_shapes.push (clip_shape); - if (likely (!defs->clip_shapes.in_error ())) - clip->shape_count++; - else if (had_alloc_failure) - *had_alloc_failure = true; + ctx->defs->clip_shapes.push (clip_shape); + if (likely (!ctx->defs->clip_shapes.in_error ())) + ctx->clip->shape_count++; + else if (ctx->had_alloc_failure) + *ctx->had_alloc_failure = true; } static void @@ -146,30 +156,18 @@ svg_is_hidden_element (hb_svg_xml_parser_t &parser) } static void -svg_clip_collect_ref_element (hb_svg_defs_t *defs, - hb_svg_clip_path_def_t *clip, +svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &parser, const hb_svg_transform_t &base_transform, - const char *doc_start, - unsigned doc_len, - const OT::SVG::accelerator_t *svg_accel, - const OT::SVG::svg_doc_cache_t *doc_cache, - hb_decycler_t &use_decycler, unsigned depth, - bool *had_alloc_failure); + bool local_decycler_only = false); static void -svg_clip_collect_use_target (hb_svg_defs_t *defs, - hb_svg_clip_path_def_t *clip, +svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &use_parser, const hb_svg_transform_t &base_transform, - const char *doc_start, - unsigned doc_len, - const OT::SVG::accelerator_t *svg_accel, - const OT::SVG::svg_doc_cache_t *doc_cache, - hb_decycler_t &use_decycler, unsigned depth, - bool *had_alloc_failure) + bool local_decycler_only = false) { const unsigned SVG_MAX_CLIP_USE_DEPTH = 64; if (depth >= SVG_MAX_CLIP_USE_DEPTH) @@ -181,12 +179,24 @@ svg_clip_collect_use_target (hb_svg_defs_t *defs, return; const char *found = nullptr; - if (!svg_find_element_by_id (doc_start, doc_len, svg_accel, doc_cache, ref_id, &found)) + if (!svg_find_element_by_id (ctx->doc_start, ctx->doc_len, + ctx->svg_accel, ctx->doc_cache, + ref_id, &found)) return; - hb_decycler_node_t node (use_decycler); - if (unlikely (!node.visit ((uintptr_t) found))) - return; + if (!local_decycler_only) + { + hb_decycler_node_t node (*ctx->use_decycler); + if (unlikely (!node.visit ((uintptr_t) found))) + return; + } + else + { + hb_decycler_t local_decycler; + hb_decycler_node_t node (local_decycler); + if (unlikely (!node.visit ((uintptr_t) found))) + return; + } hb_svg_transform_t effective = base_transform; float use_x = svg_parse_float (use_parser.find_attr ("x")); @@ -199,29 +209,21 @@ svg_clip_collect_use_target (hb_svg_defs_t *defs, effective.multiply (tr); } - unsigned remaining = doc_len - (unsigned) (found - doc_start); + unsigned remaining = ctx->doc_len - (unsigned) (found - ctx->doc_start); hb_svg_xml_parser_t ref_parser (found, remaining); hb_svg_token_type_t rt = ref_parser.next (); if (rt != SVG_TOKEN_OPEN_TAG && rt != SVG_TOKEN_SELF_CLOSE_TAG) return; - svg_clip_collect_ref_element (defs, clip, ref_parser, effective, - doc_start, doc_len, svg_accel, doc_cache, - use_decycler, depth + 1, had_alloc_failure); + svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1, local_decycler_only); } static void -svg_clip_collect_ref_element (hb_svg_defs_t *defs, - hb_svg_clip_path_def_t *clip, +svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &parser, const hb_svg_transform_t &base_transform, - const char *doc_start, - unsigned doc_len, - const OT::SVG::accelerator_t *svg_accel, - const OT::SVG::svg_doc_cache_t *doc_cache, - hb_decycler_t &use_decycler, unsigned depth, - bool *had_alloc_failure) + bool local_decycler_only) { const unsigned SVG_MAX_CLIP_REF_DEPTH = 64; if (depth >= SVG_MAX_CLIP_REF_DEPTH) @@ -246,7 +248,7 @@ svg_clip_collect_ref_element (hb_svg_defs_t *defs, hb_svg_shape_emit_data_t shape; if (hb_raster_svg_parse_shape_tag (parser, &shape)) { - svg_clip_append_shape (defs, clip, shape, effective, had_alloc_failure); + svg_clip_append_shape (ctx, shape, effective); if (!parser.self_closing) svg_skip_subtree (parser); return; @@ -254,9 +256,7 @@ svg_clip_collect_ref_element (hb_svg_defs_t *defs, if (parser.tag_name.eq ("use")) { - svg_clip_collect_use_target (defs, clip, parser, effective, - doc_start, doc_len, svg_accel, doc_cache, - use_decycler, depth + 1, had_alloc_failure); + svg_clip_collect_use_target (ctx, parser, effective, depth + 1, local_decycler_only); if (!parser.self_closing) svg_skip_subtree (parser); return; @@ -283,9 +283,7 @@ svg_clip_collect_ref_element (hb_svg_defs_t *defs, continue; } if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - svg_clip_collect_ref_element (defs, clip, parser, effective, - doc_start, doc_len, svg_accel, doc_cache, - use_decycler, depth + 1, had_alloc_failure); + svg_clip_collect_ref_element (ctx, parser, effective, depth + 1, local_decycler_only); } } @@ -329,6 +327,11 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, int cdepth = 1; bool had_alloc_failure = false; + hb_svg_clip_collect_context_t collect_ctx = { + defs, &clip, + doc_start, doc_len, svg_accel, doc_cache, + &use_decycler, &had_alloc_failure + }; while (cdepth > 0) { hb_svg_token_type_t ct = parser.next (); @@ -387,15 +390,13 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, if (parser.tag_name.eq ("use")) { - svg_clip_collect_use_target (defs, &clip, parser, effective, - doc_start, doc_len, svg_accel, doc_cache, - use_decycler, 0, &had_alloc_failure); + svg_clip_collect_use_target (&collect_ctx, parser, effective, 0); } else { hb_svg_shape_emit_data_t shape; if (hb_raster_svg_parse_shape_tag (parser, &shape)) - svg_clip_append_shape (defs, &clip, shape, effective, &had_alloc_failure); + svg_clip_append_shape (&collect_ctx, shape, effective); } if (ct == SVG_TOKEN_OPEN_TAG) From 11234bfea08b97f2a849f26d0b34e89f8d93d70f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:43:36 -0300 Subject: [PATCH 047/105] [raster] Apply viewBox mapping for clipPath svg/symbol refs --- src/hb-raster-svg-clip.cc | 111 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index e870583a7..00c339345 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -44,6 +44,103 @@ svg_transform_is_identity (const hb_svg_transform_t &t) t.dx == 0.f && t.dy == 0.f; } +static inline bool +svg_parse_viewbox (hb_svg_str_t viewbox_str, + float *x, float *y, float *w, float *h) +{ + if (!viewbox_str.len) + return false; + hb_svg_float_parser_t vb_fp (viewbox_str); + float vb_x = vb_fp.next_float (); + float vb_y = vb_fp.next_float (); + float vb_w = vb_fp.next_float (); + float vb_h = vb_fp.next_float (); + if (vb_w <= 0.f || vb_h <= 0.f) + return false; + if (x) *x = vb_x; + if (y) *y = vb_y; + if (w) *w = vb_w; + if (h) *h = vb_h; + return true; +} + +static inline float +svg_align_offset (hb_svg_str_t align, + float leftover, + char axis) +{ + if (leftover <= 0.f) return 0.f; + if ((axis == 'x' && align.starts_with ("xMin")) || + (axis == 'y' && (align.eq ("xMinYMin") || align.eq ("xMidYMin") || align.eq ("xMaxYMin")))) + return 0.f; + if ((axis == 'x' && align.starts_with ("xMax")) || + (axis == 'y' && (align.eq ("xMinYMax") || align.eq ("xMidYMax") || align.eq ("xMaxYMax")))) + return leftover; + return leftover * 0.5f; +} + +static bool +svg_compute_viewbox_transform (float viewport_w, + float viewport_h, + float vb_x, + float vb_y, + float vb_w, + float vb_h, + hb_svg_str_t preserve_aspect_ratio, + hb_svg_transform_t *out) +{ + if (!(viewport_w > 0.f && viewport_h > 0.f && vb_w > 0.f && vb_h > 0.f)) + return false; + + hb_svg_str_t par = preserve_aspect_ratio.trim (); + if (!par.len) + par = hb_svg_str_t ("xMidYMid meet", 12); + + bool is_none = false; + bool is_slice = false; + if (svg_str_starts_with_ascii_ci (par, "none")) + is_none = true; + else if (par.starts_with ("x")) + { + const char *p = par.data; + const char *end = par.data + par.len; + while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + if (p < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (p, (unsigned) (end - p)), "slice")) + is_slice = true; + } + + hb_svg_transform_t t; + if (is_none) + { + t.xx = viewport_w / vb_w; + t.yy = viewport_h / vb_h; + t.dx = -vb_x * t.xx; + t.dy = -vb_y * t.yy; + *out = t; + return true; + } + + float sx = viewport_w / vb_w; + float sy = viewport_h / vb_h; + float s = is_slice ? hb_max (sx, sy) : hb_min (sx, sy); + float scaled_w = vb_w * s; + float scaled_h = vb_h * s; + float leftover_x = viewport_w - scaled_w; + float leftover_y = viewport_h - scaled_h; + + hb_svg_str_t align = par.trim (); + if (!align.starts_with ("x")) + align = hb_svg_str_t ("xMidYMid", 8); + + t.xx = s; + t.yy = s; + t.dx = svg_align_offset (align, leftover_x, 'x') - vb_x * s; + t.dy = svg_align_offset (align, leftover_y, 'y') - vb_y * s; + *out = t; + return true; +} + static bool svg_parse_element_transform (hb_svg_xml_parser_t &parser, hb_svg_transform_t *out) @@ -201,6 +298,8 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, hb_svg_transform_t effective = base_transform; float use_x = svg_parse_float (use_parser.find_attr ("x")); float use_y = svg_parse_float (use_parser.find_attr ("y")); + float use_w = svg_parse_float (use_parser.find_attr ("width")); + float use_h = svg_parse_float (use_parser.find_attr ("height")); if (use_x != 0.f || use_y != 0.f) { hb_svg_transform_t tr; @@ -215,6 +314,18 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, if (rt != SVG_TOKEN_OPEN_TAG && rt != SVG_TOKEN_SELF_CLOSE_TAG) return; + if ((ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) && + use_w > 0.f && use_h > 0.f) + { + float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; + hb_svg_transform_t vb_t; + if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && + svg_compute_viewbox_transform (use_w, use_h, vb_x, vb_y, vb_w, vb_h, + ref_parser.find_attr ("preserveAspectRatio"), + &vb_t)) + effective.multiply (vb_t); + } + svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1, local_decycler_only); } From 6e7a6138cec1e2898b8f6015806d0f5d1100856c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:44:23 -0300 Subject: [PATCH 048/105] [raster] Ignore geometry in clipPath collection --- src/hb-raster-svg-clip.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 00c339345..9be8747e2 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -351,6 +351,14 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, return; } + /* Definitions are not directly renderable clip geometry. */ + if (parser.tag_name.eq ("defs")) + { + if (!parser.self_closing) + svg_skip_subtree (parser); + return; + } + hb_svg_transform_t effective = base_transform; hb_svg_transform_t local_t; if (svg_parse_element_transform (parser, &local_t)) @@ -450,6 +458,22 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, if (ct == SVG_TOKEN_CLOSE_TAG) { cdepth--; continue; } if (ct == SVG_TOKEN_OPEN_TAG || ct == SVG_TOKEN_SELF_CLOSE_TAG) { + if (parser.tag_name.eq ("defs")) + { + if (ct == SVG_TOKEN_OPEN_TAG) + { + int skip_depth = 1; + while (skip_depth > 0) + { + hb_svg_token_type_t st = parser.next (); + if (st == SVG_TOKEN_EOF) break; + if (st == SVG_TOKEN_CLOSE_TAG) skip_depth--; + else if (st == SVG_TOKEN_OPEN_TAG) skip_depth++; + } + } + continue; + } + hb_svg_style_props_t visibility_style_props; svg_parse_style_props (parser.find_attr ("style"), &visibility_style_props); hb_svg_str_t display_str = svg_pick_attr_or_style (parser, visibility_style_props.display, "display"); From 2622fe92e86c72ec64214b240fb87ad8f8ab6511 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:44:45 -0300 Subject: [PATCH 049/105] [raster] Validate objectBoundingBox dimensions before mapping --- src/hb-raster-svg-clip.cc | 3 ++- src/hb-raster-svg-fill.cc | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 9be8747e2..275f58733 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -34,6 +34,7 @@ #include "hb-raster-svg-base.hh" #include "hb-decycler.hh" +#include #include static inline bool @@ -623,7 +624,7 @@ hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, return false; float w = object_bbox->xmax - object_bbox->xmin; float h = object_bbox->ymax - object_bbox->ymin; - if (w <= 0 || h <= 0) + if (!(isfinite (w) && isfinite (h)) || w <= 0.f || h <= 0.f) return false; ed.has_bbox_transform = true; ed.bbox_transform = hb_transform_t<> (w, 0, 0, h, object_bbox->xmin, object_bbox->ymin); diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index f2ffbc008..6eff84da2 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -31,6 +31,8 @@ #include "hb-raster-svg-base.hh" #include "hb-raster-svg-color.hh" +#include + struct hb_svg_color_line_data_t { const hb_svg_gradient_t *grad; @@ -200,7 +202,7 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, { float w = object_bbox->xmax - object_bbox->xmin; float h = object_bbox->ymax - object_bbox->ymin; - if (w > 0 && h > 0) + if (isfinite (w) && isfinite (h) && w > 0.f && h > 0.f) hb_paint_push_transform (ctx->pfuncs, ctx->paint, w, 0, 0, h, object_bbox->xmin, object_bbox->ymin); else has_bbox_transform = false; From 4a4f9f2b7a4174ede3b07bdf10a2db598a529fbe Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:45:04 -0300 Subject: [PATCH 050/105] [raster] Treat symbol as container in SVG child-skip logic --- src/hb-raster-svg-render.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index efcc2d89a..5cb0c81ac 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -188,7 +188,8 @@ svg_render_container_element (hb_svg_render_context_t *ctx, } svg_render_element (ctx, parser, state); if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && - !child_tag.eq ("svg") && !child_tag.eq ("use")) + !child_tag.eq ("svg") && !child_tag.eq ("symbol") && + !child_tag.eq ("use")) { /* Skip children of non-container elements we don't handle. */ int skip_depth = 1; From 53bfaeabdd94a8cc590d6955623a642eb993c865 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:46:33 -0300 Subject: [PATCH 051/105] [raster] Always unwind transforms on SVG cycle skip --- src/hb-raster-svg-use.cc | 42 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index fd0382931..9041db33f 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -205,33 +205,37 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, if (found) { + bool can_render = true; hb_decycler_node_t node (*ctx->use_decycler); if (unlikely (!node.visit ((uintptr_t) found))) - return; + can_render = false; - unsigned remaining = ctx->doc_start + ctx->doc_len - found; - hb_svg_xml_parser_t ref_parser (found, remaining); - hb_svg_token_type_t tok = ref_parser.next (); - if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) + if (can_render) { - bool has_viewport_scale = false; - if ((ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) && - use_w > 0.f && use_h > 0.f) + unsigned remaining = ctx->doc_start + ctx->doc_len - found; + hb_svg_xml_parser_t ref_parser (found, remaining); + hb_svg_token_type_t tok = ref_parser.next (); + if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { - float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; - hb_svg_transform_t t; - if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && - svg_compute_viewbox_transform (use_w, use_h, vb_x, vb_y, vb_w, vb_h, - ref_parser.find_attr ("preserveAspectRatio"), - &t)) + bool has_viewport_scale = false; + if ((ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) && + use_w > 0.f && use_h > 0.f) { - hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); - has_viewport_scale = true; + float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; + hb_svg_transform_t t; + if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && + svg_compute_viewbox_transform (use_w, use_h, vb_x, vb_y, vb_w, vb_h, + ref_parser.find_attr ("preserveAspectRatio"), + &t)) + { + hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); + has_viewport_scale = true; + } } + render_cb (render_user, ref_parser, state); + if (has_viewport_scale) + hb_paint_pop_transform (ctx->pfuncs, ctx->paint); } - render_cb (render_user, ref_parser, state); - if (has_viewport_scale) - hb_paint_pop_transform (ctx->pfuncs, ctx->paint); } } From 2fbc1a1ac251feb4b2134cf4fcdbeb3b89cf994d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:46:52 -0300 Subject: [PATCH 052/105] [raster] Use SVG paint fallback for unresolved gradient chains --- src/hb-raster-svg-fill.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 6eff84da2..107ac3e9d 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -146,14 +146,23 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, break; chain.push (cur); if (unlikely (chain.in_error ())) + { + if (fallback_paint.len) + hb_raster_svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); return; + } if (!cur->href_id.length) break; const hb_svg_gradient_t *next = ctx->defs->find_gradient (cur->href_id); if (!next) break; cur = next; } - if (!chain.length) return; + if (!chain.length) + { + if (fallback_paint.len) + hb_raster_svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); + return; + } hb_svg_gradient_t effective = *chain.arrayZ[chain.length - 1]; for (int i = (int) chain.length - 2; i >= 0; i--) @@ -183,7 +192,11 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, if (g->has_fy) { effective.fy = g->fy; effective.has_fy = true; } } if (!effective.stops.length) + { + if (fallback_paint.len) + hb_raster_svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); return; + } hb_svg_color_line_data_t cl_data; cl_data.grad = &effective; From 50ef120ef3a3ea2fe761f049ba406a2f86c4e19c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:47:58 -0300 Subject: [PATCH 053/105] [raster] Fallback to target viewport size for SVG mapping --- src/hb-raster-svg-clip.cc | 12 +++++++++--- src/hb-raster-svg-use.cc | 26 ++++++++++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 275f58733..bf7be5294 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -315,13 +315,19 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, if (rt != SVG_TOKEN_OPEN_TAG && rt != SVG_TOKEN_SELF_CLOSE_TAG) return; - if ((ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) && - use_w > 0.f && use_h > 0.f) + if (ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) { + float viewport_w = use_w; + float viewport_h = use_h; + if (viewport_w <= 0.f) + viewport_w = svg_parse_float (ref_parser.find_attr ("width")); + if (viewport_h <= 0.f) + viewport_h = svg_parse_float (ref_parser.find_attr ("height")); + float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t vb_t; if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && - svg_compute_viewbox_transform (use_w, use_h, vb_x, vb_y, vb_w, vb_h, + svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, ref_parser.find_attr ("preserveAspectRatio"), &vb_t)) effective.multiply (vb_t); diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index 9041db33f..b0f6426cc 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -217,17 +217,23 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_token_type_t tok = ref_parser.next (); if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { - bool has_viewport_scale = false; - if ((ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) && - use_w > 0.f && use_h > 0.f) + bool has_viewport_scale = false; + if (ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) + { + float viewport_w = use_w; + float viewport_h = use_h; + if (viewport_w <= 0.f) + viewport_w = svg_parse_float (ref_parser.find_attr ("width")); + if (viewport_h <= 0.f) + viewport_h = svg_parse_float (ref_parser.find_attr ("height")); + + float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; + hb_svg_transform_t t; + if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && + svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, + ref_parser.find_attr ("preserveAspectRatio"), + &t)) { - float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; - hb_svg_transform_t t; - if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && - svg_compute_viewbox_transform (use_w, use_h, vb_x, vb_y, vb_w, vb_h, - ref_parser.find_attr ("preserveAspectRatio"), - &t)) - { hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); has_viewport_scale = true; } From eb0e29d3c0b6e967a96073b6b2b0217312388660 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:48:21 -0300 Subject: [PATCH 054/105] [raster] Simplify clipPath decycler flow --- src/hb-raster-svg-clip.cc | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index bf7be5294..5349f9f7c 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -257,15 +257,13 @@ static void svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &parser, const hb_svg_transform_t &base_transform, - unsigned depth, - bool local_decycler_only = false); + unsigned depth); static void svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &use_parser, const hb_svg_transform_t &base_transform, - unsigned depth, - bool local_decycler_only = false) + unsigned depth) { const unsigned SVG_MAX_CLIP_USE_DEPTH = 64; if (depth >= SVG_MAX_CLIP_USE_DEPTH) @@ -282,19 +280,9 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, ref_id, &found)) return; - if (!local_decycler_only) - { - hb_decycler_node_t node (*ctx->use_decycler); - if (unlikely (!node.visit ((uintptr_t) found))) - return; - } - else - { - hb_decycler_t local_decycler; - hb_decycler_node_t node (local_decycler); - if (unlikely (!node.visit ((uintptr_t) found))) - return; - } + hb_decycler_node_t node (*ctx->use_decycler); + if (unlikely (!node.visit ((uintptr_t) found))) + return; hb_svg_transform_t effective = base_transform; float use_x = svg_parse_float (use_parser.find_attr ("x")); @@ -333,15 +321,14 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, effective.multiply (vb_t); } - svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1, local_decycler_only); + svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1); } static void svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &parser, const hb_svg_transform_t &base_transform, - unsigned depth, - bool local_decycler_only) + unsigned depth) { const unsigned SVG_MAX_CLIP_REF_DEPTH = 64; if (depth >= SVG_MAX_CLIP_REF_DEPTH) @@ -382,7 +369,7 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, if (parser.tag_name.eq ("use")) { - svg_clip_collect_use_target (ctx, parser, effective, depth + 1, local_decycler_only); + svg_clip_collect_use_target (ctx, parser, effective, depth + 1); if (!parser.self_closing) svg_skip_subtree (parser); return; @@ -409,7 +396,7 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, continue; } if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - svg_clip_collect_ref_element (ctx, parser, effective, depth + 1, local_decycler_only); + svg_clip_collect_ref_element (ctx, parser, effective, depth + 1); } } From 31d2d6efe77a6beab095355c5c1a7756fd017147 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:49:27 -0300 Subject: [PATCH 055/105] [raster] Fix preserveAspectRatio alignment token parsing --- src/hb-raster-svg-clip.cc | 17 ++++++++++------- src/hb-raster-svg-use.cc | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 5349f9f7c..4dc0f9431 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -99,15 +99,19 @@ svg_compute_viewbox_transform (float viewport_w, bool is_none = false; bool is_slice = false; + hb_svg_str_t align = par; + const char *p = par.data; + const char *end = par.data + par.len; + while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; + align = hb_svg_str_t (par.data, (unsigned) (p - par.data)).trim (); + if (svg_str_starts_with_ascii_ci (par, "none")) is_none = true; - else if (par.starts_with ("x")) + else if (align.starts_with ("x")) { - const char *p = par.data; - const char *end = par.data + par.len; - while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - if (p < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (p, (unsigned) (end - p)), "slice")) + const char *mode = p; + while (mode < end && (*mode == ' ' || *mode == '\t' || *mode == '\n' || *mode == '\r')) mode++; + if (mode < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (mode, (unsigned) (end - mode)), "slice")) is_slice = true; } @@ -130,7 +134,6 @@ svg_compute_viewbox_transform (float viewport_w, float leftover_x = viewport_w - scaled_w; float leftover_y = viewport_h - scaled_h; - hb_svg_str_t align = par.trim (); if (!align.starts_with ("x")) align = hb_svg_str_t ("xMidYMid", 8); diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index b0f6426cc..73165c648 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -122,19 +122,20 @@ svg_compute_viewbox_transform (float viewport_w, bool is_none = false; bool is_slice = false; + hb_svg_str_t align = par; + const char *p = par.data; + const char *end = par.data + par.len; + while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; + align = hb_svg_str_t (par.data, (unsigned) (p - par.data)).trim (); + if (svg_str_starts_with_ascii_ci (par, "none")) is_none = true; - else if (par.len >= 5) + else if (align.starts_with ("x")) { - if (svg_str_starts_with_ascii_ci (par.trim (), "x")) - { - const char *p = par.data; - const char *end = par.data + par.len; - while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; - while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; - if (p < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (p, (unsigned) (end - p)), "slice")) - is_slice = true; - } + const char *mode = p; + while (mode < end && (*mode == ' ' || *mode == '\t' || *mode == '\n' || *mode == '\r')) mode++; + if (mode < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (mode, (unsigned) (end - mode)), "slice")) + is_slice = true; } hb_svg_transform_t t; @@ -156,7 +157,6 @@ svg_compute_viewbox_transform (float viewport_w, float leftover_x = viewport_w - scaled_w; float leftover_y = viewport_h - scaled_h; - hb_svg_str_t align = par.trim (); if (!align.starts_with ("x")) align = hb_svg_str_t ("xMidYMid", 8); From f49a8393f19b4ff0736d30c923bfe915d3ff282a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:51:03 -0300 Subject: [PATCH 056/105] [raster] Share SVG viewBox transform helpers --- src/hb-raster-svg-base.cc | 103 ++++++++++++++++++++++++++++++++++++ src/hb-raster-svg-base.hh | 14 +++++ src/hb-raster-svg-clip.cc | 108 ++------------------------------------ src/hb-raster-svg-use.cc | 108 ++------------------------------------ 4 files changed, 125 insertions(+), 208 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index 793c0c4ec..de8065854 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -252,3 +252,106 @@ hb_raster_svg_parse_local_id_ref (hb_svg_str_t s, if (out_id) *out_id = id; return true; } + +bool +hb_raster_svg_parse_viewbox (hb_svg_str_t viewbox_str, + float *x, + float *y, + float *w, + float *h) +{ + if (!viewbox_str.len) + return false; + hb_svg_float_parser_t vb_fp (viewbox_str); + float vb_x = vb_fp.next_float (); + float vb_y = vb_fp.next_float (); + float vb_w = vb_fp.next_float (); + float vb_h = vb_fp.next_float (); + if (vb_w <= 0.f || vb_h <= 0.f) + return false; + if (x) *x = vb_x; + if (y) *y = vb_y; + if (w) *w = vb_w; + if (h) *h = vb_h; + return true; +} + +static inline float +svg_align_offset (hb_svg_str_t align, + float leftover, + char axis) +{ + if (leftover <= 0.f) return 0.f; + if ((axis == 'x' && align.starts_with ("xMin")) || + (axis == 'y' && (align.eq ("xMinYMin") || align.eq ("xMidYMin") || align.eq ("xMaxYMin")))) + return 0.f; + if ((axis == 'x' && align.starts_with ("xMax")) || + (axis == 'y' && (align.eq ("xMinYMax") || align.eq ("xMidYMax") || align.eq ("xMaxYMax")))) + return leftover; + return leftover * 0.5f; +} + +bool +hb_raster_svg_compute_viewbox_transform (float viewport_w, + float viewport_h, + float vb_x, + float vb_y, + float vb_w, + float vb_h, + hb_svg_str_t preserve_aspect_ratio, + hb_svg_transform_t *out) +{ + if (!(viewport_w > 0.f && viewport_h > 0.f && vb_w > 0.f && vb_h > 0.f)) + return false; + + hb_svg_str_t par = preserve_aspect_ratio.trim (); + if (!par.len) + par = hb_svg_str_t ("xMidYMid meet", 12); + + bool is_none = false; + bool is_slice = false; + hb_svg_str_t align = par; + const char *p = par.data; + const char *end = par.data + par.len; + while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; + align = hb_svg_str_t (par.data, (unsigned) (p - par.data)).trim (); + + if (svg_str_starts_with_ascii_ci (par, "none")) + is_none = true; + else if (align.starts_with ("x")) + { + const char *mode = p; + while (mode < end && (*mode == ' ' || *mode == '\t' || *mode == '\n' || *mode == '\r')) mode++; + if (mode < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (mode, (unsigned) (end - mode)), "slice")) + is_slice = true; + } + + hb_svg_transform_t t; + if (is_none) + { + t.xx = viewport_w / vb_w; + t.yy = viewport_h / vb_h; + t.dx = -vb_x * t.xx; + t.dy = -vb_y * t.yy; + *out = t; + return true; + } + + float sx = viewport_w / vb_w; + float sy = viewport_h / vb_h; + float s = is_slice ? hb_max (sx, sy) : hb_min (sx, sy); + float scaled_w = vb_w * s; + float scaled_h = vb_h * s; + float leftover_x = viewport_w - scaled_w; + float leftover_y = viewport_h - scaled_h; + + if (!align.starts_with ("x")) + align = hb_svg_str_t ("xMidYMid", 8); + + t.xx = s; + t.yy = s; + t.dx = svg_align_offset (align, leftover_x, 'x') - vb_x * s; + t.dy = svg_align_offset (align, leftover_y, 'y') - vb_y * s; + *out = t; + return true; +} diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 52cec2263..d20e178ba 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -120,6 +120,7 @@ struct hb_svg_style_props_t }; struct hb_svg_xml_parser_t; +struct hb_svg_transform_t; HB_INTERNAL bool svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit); HB_INTERNAL bool svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit); @@ -132,6 +133,19 @@ HB_INTERNAL bool hb_raster_svg_parse_id_ref (hb_svg_str_t s, HB_INTERNAL bool hb_raster_svg_parse_local_id_ref (hb_svg_str_t s, hb_svg_str_t *out_id, hb_svg_str_t *out_tail); +HB_INTERNAL bool hb_raster_svg_parse_viewbox (hb_svg_str_t viewbox_str, + float *x, + float *y, + float *w, + float *h); +HB_INTERNAL bool hb_raster_svg_compute_viewbox_transform (float viewport_w, + float viewport_h, + float vb_x, + float vb_y, + float vb_w, + float vb_h, + hb_svg_str_t preserve_aspect_ratio, + hb_svg_transform_t *out); static inline float svg_parse_float_clamped01 (hb_svg_str_t s) { diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 4dc0f9431..002ccd791 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -45,106 +45,6 @@ svg_transform_is_identity (const hb_svg_transform_t &t) t.dx == 0.f && t.dy == 0.f; } -static inline bool -svg_parse_viewbox (hb_svg_str_t viewbox_str, - float *x, float *y, float *w, float *h) -{ - if (!viewbox_str.len) - return false; - hb_svg_float_parser_t vb_fp (viewbox_str); - float vb_x = vb_fp.next_float (); - float vb_y = vb_fp.next_float (); - float vb_w = vb_fp.next_float (); - float vb_h = vb_fp.next_float (); - if (vb_w <= 0.f || vb_h <= 0.f) - return false; - if (x) *x = vb_x; - if (y) *y = vb_y; - if (w) *w = vb_w; - if (h) *h = vb_h; - return true; -} - -static inline float -svg_align_offset (hb_svg_str_t align, - float leftover, - char axis) -{ - if (leftover <= 0.f) return 0.f; - if ((axis == 'x' && align.starts_with ("xMin")) || - (axis == 'y' && (align.eq ("xMinYMin") || align.eq ("xMidYMin") || align.eq ("xMaxYMin")))) - return 0.f; - if ((axis == 'x' && align.starts_with ("xMax")) || - (axis == 'y' && (align.eq ("xMinYMax") || align.eq ("xMidYMax") || align.eq ("xMaxYMax")))) - return leftover; - return leftover * 0.5f; -} - -static bool -svg_compute_viewbox_transform (float viewport_w, - float viewport_h, - float vb_x, - float vb_y, - float vb_w, - float vb_h, - hb_svg_str_t preserve_aspect_ratio, - hb_svg_transform_t *out) -{ - if (!(viewport_w > 0.f && viewport_h > 0.f && vb_w > 0.f && vb_h > 0.f)) - return false; - - hb_svg_str_t par = preserve_aspect_ratio.trim (); - if (!par.len) - par = hb_svg_str_t ("xMidYMid meet", 12); - - bool is_none = false; - bool is_slice = false; - hb_svg_str_t align = par; - const char *p = par.data; - const char *end = par.data + par.len; - while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; - align = hb_svg_str_t (par.data, (unsigned) (p - par.data)).trim (); - - if (svg_str_starts_with_ascii_ci (par, "none")) - is_none = true; - else if (align.starts_with ("x")) - { - const char *mode = p; - while (mode < end && (*mode == ' ' || *mode == '\t' || *mode == '\n' || *mode == '\r')) mode++; - if (mode < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (mode, (unsigned) (end - mode)), "slice")) - is_slice = true; - } - - hb_svg_transform_t t; - if (is_none) - { - t.xx = viewport_w / vb_w; - t.yy = viewport_h / vb_h; - t.dx = -vb_x * t.xx; - t.dy = -vb_y * t.yy; - *out = t; - return true; - } - - float sx = viewport_w / vb_w; - float sy = viewport_h / vb_h; - float s = is_slice ? hb_max (sx, sy) : hb_min (sx, sy); - float scaled_w = vb_w * s; - float scaled_h = vb_h * s; - float leftover_x = viewport_w - scaled_w; - float leftover_y = viewport_h - scaled_h; - - if (!align.starts_with ("x")) - align = hb_svg_str_t ("xMidYMid", 8); - - t.xx = s; - t.yy = s; - t.dx = svg_align_offset (align, leftover_x, 'x') - vb_x * s; - t.dy = svg_align_offset (align, leftover_y, 'y') - vb_y * s; - *out = t; - return true; -} - static bool svg_parse_element_transform (hb_svg_xml_parser_t &parser, hb_svg_transform_t *out) @@ -317,10 +217,10 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t vb_t; - if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && - svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, - ref_parser.find_attr ("preserveAspectRatio"), - &vb_t)) + if (hb_raster_svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && + hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, + ref_parser.find_attr ("preserveAspectRatio"), + &vb_t)) effective.multiply (vb_t); } diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index 73165c648..be2b14f09 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -68,106 +68,6 @@ svg_find_element_by_id (const hb_svg_use_context_t *ctx, return false; } -static inline bool -svg_parse_viewbox (hb_svg_str_t viewbox_str, - float *x, float *y, float *w, float *h) -{ - if (!viewbox_str.len) - return false; - hb_svg_float_parser_t vb_fp (viewbox_str); - float vb_x = vb_fp.next_float (); - float vb_y = vb_fp.next_float (); - float vb_w = vb_fp.next_float (); - float vb_h = vb_fp.next_float (); - if (vb_w <= 0.f || vb_h <= 0.f) - return false; - if (x) *x = vb_x; - if (y) *y = vb_y; - if (w) *w = vb_w; - if (h) *h = vb_h; - return true; -} - -static inline float -svg_align_offset (hb_svg_str_t align, - float leftover, - char axis) -{ - if (leftover <= 0.f) return 0.f; - if ((axis == 'x' && align.starts_with ("xMin")) || - (axis == 'y' && (align.eq ("xMinYMin") || align.eq ("xMidYMin") || align.eq ("xMaxYMin")))) - return 0.f; - if ((axis == 'x' && align.starts_with ("xMax")) || - (axis == 'y' && (align.eq ("xMinYMax") || align.eq ("xMidYMax") || align.eq ("xMaxYMax")))) - return leftover; - return leftover * 0.5f; -} - -static bool -svg_compute_viewbox_transform (float viewport_w, - float viewport_h, - float vb_x, - float vb_y, - float vb_w, - float vb_h, - hb_svg_str_t preserve_aspect_ratio, - hb_svg_transform_t *out) -{ - if (!(viewport_w > 0.f && viewport_h > 0.f && vb_w > 0.f && vb_h > 0.f)) - return false; - - hb_svg_str_t par = preserve_aspect_ratio.trim (); - if (!par.len) - par = hb_svg_str_t ("xMidYMid meet", 12); - - bool is_none = false; - bool is_slice = false; - hb_svg_str_t align = par; - const char *p = par.data; - const char *end = par.data + par.len; - while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; - align = hb_svg_str_t (par.data, (unsigned) (p - par.data)).trim (); - - if (svg_str_starts_with_ascii_ci (par, "none")) - is_none = true; - else if (align.starts_with ("x")) - { - const char *mode = p; - while (mode < end && (*mode == ' ' || *mode == '\t' || *mode == '\n' || *mode == '\r')) mode++; - if (mode < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (mode, (unsigned) (end - mode)), "slice")) - is_slice = true; - } - - hb_svg_transform_t t; - if (is_none) - { - t.xx = viewport_w / vb_w; - t.yy = viewport_h / vb_h; - t.dx = -vb_x * t.xx; - t.dy = -vb_y * t.yy; - *out = t; - return true; - } - - float sx = viewport_w / vb_w; - float sy = viewport_h / vb_h; - float s = is_slice ? hb_max (sx, sy) : hb_min (sx, sy); - float scaled_w = vb_w * s; - float scaled_h = vb_h * s; - float leftover_x = viewport_w - scaled_w; - float leftover_y = viewport_h - scaled_h; - - if (!align.starts_with ("x")) - align = hb_svg_str_t ("xMidYMid", 8); - - t.xx = s; - t.yy = s; - t.dx = svg_align_offset (align, leftover_x, 'x') - vb_x * s; - t.dy = svg_align_offset (align, leftover_y, 'y') - vb_y * s; - *out = t; - return true; -} - void hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t &parser, @@ -229,10 +129,10 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t t; - if (svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && - svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, - ref_parser.find_attr ("preserveAspectRatio"), - &t)) + if (hb_raster_svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && + hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, + ref_parser.find_attr ("preserveAspectRatio"), + &t)) { hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); has_viewport_scale = true; From 094dc4a0fb2621b12c86eb49bf6dbcf3ff6f4da4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:51:37 -0300 Subject: [PATCH 057/105] [raster] Honor x/y positioning on nested SVG containers --- src/hb-raster-svg-render.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 5cb0c81ac..42131c59b 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -123,10 +123,19 @@ svg_render_container_element (hb_svg_render_context_t *ctx, bool has_opacity = state.opacity < 1.f; bool has_clip = false; bool has_viewbox = false; + bool has_svg_translate = false; + float svg_x = 0.f, svg_y = 0.f; float vb_x = 0, vb_y = 0, vb_w = 0, vb_h = 0; if (tag.eq ("svg") || tag.eq ("symbol")) { + if (tag.eq ("svg")) + { + svg_x = svg_parse_float (parser.find_attr ("x")); + svg_y = svg_parse_float (parser.find_attr ("y")); + has_svg_translate = (svg_x != 0.f || svg_y != 0.f); + } + hb_svg_str_t viewbox_str = parser.find_attr ("viewBox"); if (viewbox_str.len) { @@ -149,6 +158,9 @@ svg_render_container_element (hb_svg_render_context_t *ctx, ctx->push_transform (t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); } + if (has_svg_translate) + ctx->push_transform (1, 0, 0, 1, svg_x, svg_y); + if (has_viewbox && vb_w > 0 && vb_h > 0) ctx->push_transform (1, 0, 0, 1, -vb_x, -vb_y); @@ -208,6 +220,9 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (has_viewbox && vb_w > 0 && vb_h > 0) ctx->pop_transform (); + if (has_svg_translate) + ctx->pop_transform (); + if (has_clip) ctx->pop_clip (); From cc4b5d71549227cd563eb2a313569d4c7b6096d8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:51:53 -0300 Subject: [PATCH 058/105] [raster] Apply nested SVG x/y offsets in clipPath traversal --- src/hb-raster-svg-clip.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 002ccd791..80598fd6c 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -260,6 +260,18 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_transform_t local_t; if (svg_parse_element_transform (parser, &local_t)) effective.multiply (local_t); + if (parser.tag_name.eq ("svg")) + { + float svg_x = svg_parse_float (parser.find_attr ("x")); + float svg_y = svg_parse_float (parser.find_attr ("y")); + if (svg_x != 0.f || svg_y != 0.f) + { + hb_svg_transform_t tr; + tr.dx = svg_x; + tr.dy = svg_y; + effective.multiply (tr); + } + } hb_svg_shape_emit_data_t shape; if (hb_raster_svg_parse_shape_tag (parser, &shape)) From e835a2ff485c2ae3e222fa7494d0e340c1559b80 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:52:22 -0300 Subject: [PATCH 059/105] [raster] Apply full viewBox mapping for nested SVG containers --- src/hb-raster-svg-render.cc | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 42131c59b..a26fbe2ad 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -123,8 +123,11 @@ svg_render_container_element (hb_svg_render_context_t *ctx, bool has_opacity = state.opacity < 1.f; bool has_clip = false; bool has_viewbox = false; + bool has_viewbox_transform = false; bool has_svg_translate = false; float svg_x = 0.f, svg_y = 0.f; + float viewport_w = 0.f, viewport_h = 0.f; + hb_svg_transform_t viewbox_t; float vb_x = 0, vb_y = 0, vb_w = 0, vb_h = 0; if (tag.eq ("svg") || tag.eq ("symbol")) @@ -145,6 +148,22 @@ svg_render_container_element (hb_svg_render_context_t *ctx, vb_y = vb_fp.next_float (); vb_w = vb_fp.next_float (); vb_h = vb_fp.next_float (); + + if (tag.eq ("svg")) + { + viewport_w = svg_parse_float (parser.find_attr ("width")); + viewport_h = svg_parse_float (parser.find_attr ("height")); + if (!(viewport_w > 0.f && viewport_h > 0.f)) + { + viewport_w = vb_w; + viewport_h = vb_h; + } + has_viewbox_transform = + hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, + vb_x, vb_y, vb_w, vb_h, + parser.find_attr ("preserveAspectRatio"), + &viewbox_t); + } } } @@ -161,7 +180,10 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (has_svg_translate) ctx->push_transform (1, 0, 0, 1, svg_x, svg_y); - if (has_viewbox && vb_w > 0 && vb_h > 0) + if (has_viewbox_transform) + ctx->push_transform (viewbox_t.xx, viewbox_t.yx, viewbox_t.xy, viewbox_t.yy, + viewbox_t.dx, viewbox_t.dy); + else if (has_viewbox && vb_w > 0 && vb_h > 0) ctx->push_transform (1, 0, 0, 1, -vb_x, -vb_y); has_clip = hb_raster_svg_push_clip_path_ref (ctx->paint, &ctx->defs, clip_path_str, nullptr); @@ -217,7 +239,7 @@ svg_render_container_element (hb_svg_render_context_t *ctx, } } - if (has_viewbox && vb_w > 0 && vb_h > 0) + if (has_viewbox_transform || (has_viewbox && vb_w > 0 && vb_h > 0)) ctx->pop_transform (); if (has_svg_translate) From 9086843c6421cef20f4259497e8fdd0a5080d82d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:52:40 -0300 Subject: [PATCH 060/105] [raster] Apply nested SVG viewBox mapping in clipPath refs --- src/hb-raster-svg-clip.cc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 80598fd6c..15ee63a02 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -271,6 +271,25 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, tr.dy = svg_y; effective.multiply (tr); } + + float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; + if (hb_raster_svg_parse_viewbox (parser.find_attr ("viewBox"), + &vb_x, &vb_y, &vb_w, &vb_h)) + { + float viewport_w = svg_parse_float (parser.find_attr ("width")); + float viewport_h = svg_parse_float (parser.find_attr ("height")); + if (!(viewport_w > 0.f && viewport_h > 0.f)) + { + viewport_w = vb_w; + viewport_h = vb_h; + } + hb_svg_transform_t vb_t; + if (hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, + vb_x, vb_y, vb_w, vb_h, + parser.find_attr ("preserveAspectRatio"), + &vb_t)) + effective.multiply (vb_t); + } } hb_svg_shape_emit_data_t shape; From 71e16a895d1d6a1428d2588aecff5f264fa6105c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:54:24 -0300 Subject: [PATCH 061/105] [raster] Avoid duplicate viewBox mapping for targets --- src/hb-raster-svg-context.hh | 1 + src/hb-raster-svg-render.cc | 19 +++++++++++++++---- src/hb-raster-svg-use.cc | 4 ++-- src/hb-raster-svg-use.hh | 3 ++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/hb-raster-svg-context.hh b/src/hb-raster-svg-context.hh index bf68fe9f1..dd91d3e15 100644 --- a/src/hb-raster-svg-context.hh +++ b/src/hb-raster-svg-context.hh @@ -71,6 +71,7 @@ struct hb_svg_render_context_t const OT::SVG::accelerator_t *svg_accel = nullptr; const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; hb_decycler_t use_decycler; + bool suppress_viewbox_once = false; void push_transform (float xx, float yx, float xy, float yy, float dx, float dy) { diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index a26fbe2ad..eb449124f 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -167,6 +167,13 @@ svg_render_container_element (hb_svg_render_context_t *ctx, } } + if (ctx->suppress_viewbox_once) + { + has_viewbox = false; + has_viewbox_transform = false; + ctx->suppress_viewbox_once = false; + } + if (has_opacity) ctx->push_group (); @@ -380,11 +387,15 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, static void svg_render_use_callback (void *render_user, hb_svg_xml_parser_t &parser, - const void *state) + const void *state, + bool viewport_mapped) { - svg_render_element ((hb_svg_render_context_t *) render_user, - parser, - *(const hb_svg_cascade_t *) state); + hb_svg_render_context_t *ctx = (hb_svg_render_context_t *) render_user; + bool old_suppress = ctx->suppress_viewbox_once; + if (viewport_mapped) + ctx->suppress_viewbox_once = true; + svg_render_element (ctx, parser, *(const hb_svg_cascade_t *) state); + ctx->suppress_viewbox_once = old_suppress; } static void diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index be2b14f09..66c8cc994 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -117,7 +117,7 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_token_type_t tok = ref_parser.next (); if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { - bool has_viewport_scale = false; + bool has_viewport_scale = false; if (ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) { float viewport_w = use_w; @@ -138,7 +138,7 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, has_viewport_scale = true; } } - render_cb (render_user, ref_parser, state); + render_cb (render_user, ref_parser, state, has_viewport_scale); if (has_viewport_scale) hb_paint_pop_transform (ctx->pfuncs, ctx->paint); } diff --git a/src/hb-raster-svg-use.hh b/src/hb-raster-svg-use.hh index 74b67c09e..aa81f4a01 100644 --- a/src/hb-raster-svg-use.hh +++ b/src/hb-raster-svg-use.hh @@ -34,7 +34,8 @@ typedef void (*hb_svg_use_render_cb_t) (void *render_user, hb_svg_xml_parser_t &parser, - const void *state); + const void *state, + bool viewport_mapped); HB_INTERNAL void hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, From 7a3de1a8119fffe6d70b9821a7f8c69caa483801 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:54:52 -0300 Subject: [PATCH 062/105] [raster] Avoid duplicate viewBox mapping in clipPath refs --- src/hb-raster-svg-clip.cc | 44 ++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 15ee63a02..db04ef721 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -160,7 +160,8 @@ static void svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &parser, const hb_svg_transform_t &base_transform, - unsigned depth); + unsigned depth, + bool suppress_viewbox_once = false); static void svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, @@ -206,6 +207,7 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, if (rt != SVG_TOKEN_OPEN_TAG && rt != SVG_TOKEN_SELF_CLOSE_TAG) return; + bool viewport_mapped = false; if (ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) { float viewport_w = use_w; @@ -221,17 +223,21 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, ref_parser.find_attr ("preserveAspectRatio"), &vb_t)) + { effective.multiply (vb_t); + viewport_mapped = true; + } } - svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1); + svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1, viewport_mapped); } static void svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &parser, const hb_svg_transform_t &base_transform, - unsigned depth) + unsigned depth, + bool suppress_viewbox_once) { const unsigned SVG_MAX_CLIP_REF_DEPTH = 64; if (depth >= SVG_MAX_CLIP_REF_DEPTH) @@ -272,23 +278,27 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, effective.multiply (tr); } - float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; - if (hb_raster_svg_parse_viewbox (parser.find_attr ("viewBox"), - &vb_x, &vb_y, &vb_w, &vb_h)) + if (!suppress_viewbox_once) { - float viewport_w = svg_parse_float (parser.find_attr ("width")); - float viewport_h = svg_parse_float (parser.find_attr ("height")); - if (!(viewport_w > 0.f && viewport_h > 0.f)) + float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; + if (hb_raster_svg_parse_viewbox (parser.find_attr ("viewBox"), + &vb_x, &vb_y, &vb_w, &vb_h)) { - viewport_w = vb_w; - viewport_h = vb_h; + float viewport_w = svg_parse_float (parser.find_attr ("width")); + float viewport_h = svg_parse_float (parser.find_attr ("height")); + if (!(viewport_w > 0.f && viewport_h > 0.f)) + { + viewport_w = vb_w; + viewport_h = vb_h; + } + hb_svg_transform_t vb_t; + if (hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, + vb_x, vb_y, vb_w, vb_h, + parser.find_attr ("preserveAspectRatio"), + &vb_t)) + effective.multiply (vb_t); } - hb_svg_transform_t vb_t; - if (hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, - vb_x, vb_y, vb_w, vb_h, - parser.find_attr ("preserveAspectRatio"), - &vb_t)) - effective.multiply (vb_t); + suppress_viewbox_once = false; } } From 4f829c9e38d7f8b5b55f57bd8ce1f8fc5c42acae Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:56:05 -0300 Subject: [PATCH 063/105] [raster] Guard SVG id-cache lookup on accel presence --- src/hb-raster-svg-use.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index 66c8cc994..4bbd43458 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -38,7 +38,7 @@ svg_find_element_by_id (const hb_svg_use_context_t *ctx, const char **found) { *found = nullptr; - if (ctx->doc_cache) + if (ctx->doc_cache && ctx->svg_accel) { unsigned start = 0, end = 0; OT::SVG::svg_id_span_t key = {id.data, id.len}; From 4d421068e325e51a77bb9c6a04f4cef92ca6c501 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:56:34 -0300 Subject: [PATCH 064/105] [raster] Inherit visibility in clipPath target traversal --- src/hb-raster-svg-clip.cc | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index db04ef721..0a635fabe 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -145,15 +145,24 @@ svg_skip_subtree (hb_svg_xml_parser_t &parser) } static inline bool -svg_is_hidden_element (hb_svg_xml_parser_t &parser) +svg_resolve_element_visibility (hb_svg_xml_parser_t &parser, + bool parent_visible) { hb_svg_style_props_t style_props; svg_parse_style_props (parser.find_attr ("style"), &style_props); hb_svg_str_t display_str = svg_pick_attr_or_style (parser, style_props.display, "display"); hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, style_props.visibility, "visibility"); - return svg_str_eq_ascii_ci (display_str.trim (), "none") || - svg_str_eq_ascii_ci (visibility_str.trim (), "hidden") || - svg_str_eq_ascii_ci (visibility_str.trim (), "collapse"); + if (svg_str_eq_ascii_ci (display_str.trim (), "none")) + return false; + hb_svg_str_t vis_trim = visibility_str.trim (); + if (!vis_trim.len || svg_str_eq_ascii_ci (vis_trim, "inherit")) + return parent_visible; + if (svg_str_eq_ascii_ci (vis_trim, "hidden") || + svg_str_eq_ascii_ci (vis_trim, "collapse")) + return false; + if (svg_str_eq_ascii_ci (vis_trim, "visible")) + return true; + return parent_visible; } static void @@ -161,7 +170,8 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &parser, const hb_svg_transform_t &base_transform, unsigned depth, - bool suppress_viewbox_once = false); + bool suppress_viewbox_once = false, + bool parent_visible = true); static void svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, @@ -229,7 +239,7 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, } } - svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1, viewport_mapped); + svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1, viewport_mapped, true); } static void @@ -237,7 +247,8 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, hb_svg_xml_parser_t &parser, const hb_svg_transform_t &base_transform, unsigned depth, - bool suppress_viewbox_once) + bool suppress_viewbox_once, + bool parent_visible) { const unsigned SVG_MAX_CLIP_REF_DEPTH = 64; if (depth >= SVG_MAX_CLIP_REF_DEPTH) @@ -247,7 +258,8 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, return; } - if (svg_is_hidden_element (parser)) + bool is_visible = svg_resolve_element_visibility (parser, parent_visible); + if (!is_visible) { if (!parser.self_closing) svg_skip_subtree (parser); @@ -340,7 +352,7 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, continue; } if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - svg_clip_collect_ref_element (ctx, parser, effective, depth + 1); + svg_clip_collect_ref_element (ctx, parser, effective, depth + 1, false, is_visible); } } From 8a0f99815ddb4aafd90c4b3ae25b7b882922cc95 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:57:17 -0300 Subject: [PATCH 065/105] [raster] Render symbol elements only when referenced by --- src/hb-raster-svg-context.hh | 1 + src/hb-raster-svg-render.cc | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/hb-raster-svg-context.hh b/src/hb-raster-svg-context.hh index dd91d3e15..5882cab17 100644 --- a/src/hb-raster-svg-context.hh +++ b/src/hb-raster-svg-context.hh @@ -72,6 +72,7 @@ struct hb_svg_render_context_t const OT::SVG::svg_doc_cache_t *doc_cache = nullptr; hb_decycler_t use_decycler; bool suppress_viewbox_once = false; + bool allow_symbol_render_once = false; void push_transform (float xx, float yx, float xy, float yy, float dx, float dy) { diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index eb449124f..2e5d3a4d9 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -392,10 +392,14 @@ svg_render_use_callback (void *render_user, { hb_svg_render_context_t *ctx = (hb_svg_render_context_t *) render_user; bool old_suppress = ctx->suppress_viewbox_once; + bool old_allow_symbol = ctx->allow_symbol_render_once; if (viewport_mapped) ctx->suppress_viewbox_once = true; + if (parser.tag_name.eq ("symbol")) + ctx->allow_symbol_render_once = true; svg_render_element (ctx, parser, *(const hb_svg_cascade_t *) state); ctx->suppress_viewbox_once = old_suppress; + ctx->allow_symbol_render_once = old_allow_symbol; } static void @@ -487,6 +491,19 @@ svg_render_element (hb_svg_render_context_t *ctx, return; } + if (tag.eq ("symbol") && !ctx->allow_symbol_render_once) + { + if (!self_closing) + svg_skip_subtree (parser); + ctx->depth--; + assert (ctx->paint->transform_stack.length == transform_depth); + assert (ctx->paint->clip_stack.length == clip_depth); + assert (ctx->paint->surface_stack.length == surface_depth); + return; + } + if (tag.eq ("symbol")) + ctx->allow_symbol_render_once = false; + if (tag.eq ("g") || tag.eq ("svg") || tag.eq ("symbol")) svg_render_container_element (ctx, parser, tag, self_closing, state, transform_str, state.clip_path); From 39a6473638744b5638c6205543899a1fce8d9571 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:57:40 -0300 Subject: [PATCH 066/105] [raster] Ignore direct symbol nodes inside clipPath defs --- src/hb-raster-svg-clip.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 0a635fabe..80f17a741 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -408,6 +408,22 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, if (ct == SVG_TOKEN_CLOSE_TAG) { cdepth--; continue; } if (ct == SVG_TOKEN_OPEN_TAG || ct == SVG_TOKEN_SELF_CLOSE_TAG) { + if (parser.tag_name.eq ("symbol")) + { + if (ct == SVG_TOKEN_OPEN_TAG) + { + int skip_depth = 1; + while (skip_depth > 0) + { + hb_svg_token_type_t st = parser.next (); + if (st == SVG_TOKEN_EOF) break; + if (st == SVG_TOKEN_CLOSE_TAG) skip_depth--; + else if (st == SVG_TOKEN_OPEN_TAG) skip_depth++; + } + } + continue; + } + if (parser.tag_name.eq ("defs")) { if (ct == SVG_TOKEN_OPEN_TAG) From ba437844f3c34381fdc76d27dee7d9808984a44b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:58:11 -0300 Subject: [PATCH 067/105] [raster] Fallback viewport to viewBox when sizes are absent --- src/hb-raster-svg-clip.cc | 19 +++++++++++++------ src/hb-raster-svg-use.cc | 15 +++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 80f17a741..7f7112d79 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -229,13 +229,20 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t vb_t; - if (hb_raster_svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && - hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, - ref_parser.find_attr ("preserveAspectRatio"), - &vb_t)) + if (hb_raster_svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h)) { - effective.multiply (vb_t); - viewport_mapped = true; + if (!(viewport_w > 0.f && viewport_h > 0.f)) + { + viewport_w = vb_w; + viewport_h = vb_h; + } + if (hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, + ref_parser.find_attr ("preserveAspectRatio"), + &vb_t)) + { + effective.multiply (vb_t); + viewport_mapped = true; + } } } diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index 4bbd43458..ab04c75bf 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -129,15 +129,22 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t t; - if (hb_raster_svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h) && - hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, - ref_parser.find_attr ("preserveAspectRatio"), - &t)) + if (hb_raster_svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h)) { + if (!(viewport_w > 0.f && viewport_h > 0.f)) + { + viewport_w = vb_w; + viewport_h = vb_h; + } + if (hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, + ref_parser.find_attr ("preserveAspectRatio"), + &t)) + { hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); has_viewport_scale = true; } } + } render_cb (render_user, ref_parser, state, has_viewport_scale); if (has_viewport_scale) hb_paint_pop_transform (ctx->pfuncs, ctx->paint); From c53ee98cfce81534acdffe4cd8582873c5293f43 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:58:32 -0300 Subject: [PATCH 068/105] [raster] Accept defer prefix in preserveAspectRatio parsing --- src/hb-raster-svg-base.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index de8065854..b2b46785f 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -305,6 +305,13 @@ hb_raster_svg_compute_viewbox_transform (float viewport_w, return false; hb_svg_str_t par = preserve_aspect_ratio.trim (); + if (svg_str_starts_with_ascii_ci (par, "defer")) + { + const char *p = par.data + 5; + const char *end = par.data + par.len; + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) p++; + par = hb_svg_str_t (p, (unsigned) (end - p)); + } if (!par.len) par = hb_svg_str_t ("xMidYMid meet", 12); From df7e8af129d0ec530832c6716f61f32c71986081 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:59:21 -0300 Subject: [PATCH 069/105] [raster] Restrict clipPath symbol traversal to direct targets --- src/hb-raster-svg-clip.cc | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 7f7112d79..4c0b2e462 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -171,7 +171,8 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, const hb_svg_transform_t &base_transform, unsigned depth, bool suppress_viewbox_once = false, - bool parent_visible = true); + bool parent_visible = true, + bool allow_symbol_once = false); static void svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, @@ -246,7 +247,9 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, } } - svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1, viewport_mapped, true); + bool allow_symbol = ref_parser.tag_name.eq ("symbol"); + svg_clip_collect_ref_element (ctx, ref_parser, effective, depth + 1, + viewport_mapped, true, allow_symbol); } static void @@ -255,7 +258,8 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, const hb_svg_transform_t &base_transform, unsigned depth, bool suppress_viewbox_once, - bool parent_visible) + bool parent_visible, + bool allow_symbol_once) { const unsigned SVG_MAX_CLIP_REF_DEPTH = 64; if (depth >= SVG_MAX_CLIP_REF_DEPTH) @@ -280,6 +284,14 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, svg_skip_subtree (parser); return; } + if (parser.tag_name.eq ("symbol") && !allow_symbol_once) + { + if (!parser.self_closing) + svg_skip_subtree (parser); + return; + } + if (parser.tag_name.eq ("symbol")) + allow_symbol_once = false; hb_svg_transform_t effective = base_transform; hb_svg_transform_t local_t; @@ -359,7 +371,7 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, continue; } if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) - svg_clip_collect_ref_element (ctx, parser, effective, depth + 1, false, is_visible); + svg_clip_collect_ref_element (ctx, parser, effective, depth + 1, false, is_visible, false); } } From 59d2524bb55e9215dad97557b75838c9cd7e3765 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:00:24 -0300 Subject: [PATCH 070/105] [raster] Share SVG id target lookup helper --- src/hb-raster-svg-base.cc | 42 +++++++++++++++++++++++++++++++++ src/hb-raster-svg-base.hh | 7 ++++++ src/hb-raster-svg-clip.cc | 49 +++------------------------------------ src/hb-raster-svg-use.cc | 41 ++------------------------------ 4 files changed, 54 insertions(+), 85 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index b2b46785f..eb8062c7d 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -253,6 +253,48 @@ hb_raster_svg_parse_local_id_ref (hb_svg_str_t s, return true; } +bool +hb_raster_svg_find_element_by_id (const char *doc_start, + unsigned doc_len, + const OT::SVG::accelerator_t *svg_accel, + const OT::SVG::svg_doc_cache_t *doc_cache, + hb_svg_str_t id, + const char **found) +{ + *found = nullptr; + if (!doc_start || !doc_len || !id.len) + return false; + + if (doc_cache && svg_accel) + { + unsigned start = 0, end = 0; + OT::SVG::svg_id_span_t key = {id.data, id.len}; + if (svg_accel->doc_cache_find_id_span (doc_cache, key, &start, &end)) + { + if (start < doc_len && end <= doc_len && start < end) + { + *found = doc_start + start; + return true; + } + } + } + + hb_svg_xml_parser_t search (doc_start, doc_len); + while (true) + { + hb_svg_token_type_t tok = search.next (); + if (tok == SVG_TOKEN_EOF) break; + if (tok != SVG_TOKEN_OPEN_TAG && tok != SVG_TOKEN_SELF_CLOSE_TAG) continue; + hb_svg_str_t attr_id = search.find_attr ("id"); + if (attr_id.len == id.len && 0 == memcmp (attr_id.data, id.data, id.len)) + { + *found = search.tag_start; + return true; + } + } + return false; +} + bool hb_raster_svg_parse_viewbox (hb_svg_str_t viewbox_str, float *x, diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index d20e178ba..49aaece46 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -28,6 +28,7 @@ #define HB_RASTER_SVG_BASE_HH #include "hb.hh" +#include "OT/Color/svg/svg.hh" #include #include @@ -133,6 +134,12 @@ HB_INTERNAL bool hb_raster_svg_parse_id_ref (hb_svg_str_t s, HB_INTERNAL bool hb_raster_svg_parse_local_id_ref (hb_svg_str_t s, hb_svg_str_t *out_id, hb_svg_str_t *out_tail); +HB_INTERNAL bool hb_raster_svg_find_element_by_id (const char *doc_start, + unsigned doc_len, + const OT::SVG::accelerator_t *svg_accel, + const OT::SVG::svg_doc_cache_t *doc_cache, + hb_svg_str_t id, + const char **found); HB_INTERNAL bool hb_raster_svg_parse_viewbox (hb_svg_str_t viewbox_str, float *x, float *y, diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 4c0b2e462..508266ab3 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -35,7 +35,6 @@ #include "hb-decycler.hh" #include -#include static inline bool svg_transform_is_identity (const hb_svg_transform_t &t) @@ -58,48 +57,6 @@ svg_parse_element_transform (hb_svg_xml_parser_t &parser, return true; } -static bool -svg_find_element_by_id (const char *doc_start, - unsigned doc_len, - const OT::SVG::accelerator_t *svg_accel, - const OT::SVG::svg_doc_cache_t *doc_cache, - hb_svg_str_t id, - const char **found) -{ - *found = nullptr; - if (!doc_start || !doc_len || !id.len) - return false; - - if (doc_cache && svg_accel) - { - unsigned start = 0, end = 0; - OT::SVG::svg_id_span_t key = {id.data, id.len}; - if (svg_accel->doc_cache_find_id_span (doc_cache, key, &start, &end)) - { - if (start < doc_len && end <= doc_len && start < end) - { - *found = doc_start + start; - return true; - } - } - } - - hb_svg_xml_parser_t search (doc_start, doc_len); - while (true) - { - hb_svg_token_type_t tok = search.next (); - if (tok == SVG_TOKEN_EOF) break; - if (tok != SVG_TOKEN_OPEN_TAG && tok != SVG_TOKEN_SELF_CLOSE_TAG) continue; - hb_svg_str_t attr_id = search.find_attr ("id"); - if (attr_id.len == id.len && 0 == memcmp (attr_id.data, id.data, id.len)) - { - *found = search.tag_start; - return true; - } - } - return false; -} - struct hb_svg_clip_collect_context_t { hb_svg_defs_t *defs; @@ -190,9 +147,9 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, return; const char *found = nullptr; - if (!svg_find_element_by_id (ctx->doc_start, ctx->doc_len, - ctx->svg_accel, ctx->doc_cache, - ref_id, &found)) + if (!hb_raster_svg_find_element_by_id (ctx->doc_start, ctx->doc_len, + ctx->svg_accel, ctx->doc_cache, + ref_id, &found)) return; hb_decycler_node_t node (*ctx->use_decycler); diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index ab04c75bf..73458f2ef 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -30,44 +30,6 @@ #include "hb-raster-svg-base.hh" -#include - -static bool -svg_find_element_by_id (const hb_svg_use_context_t *ctx, - hb_svg_str_t id, - const char **found) -{ - *found = nullptr; - if (ctx->doc_cache && ctx->svg_accel) - { - unsigned start = 0, end = 0; - OT::SVG::svg_id_span_t key = {id.data, id.len}; - if (ctx->svg_accel->doc_cache_find_id_span (ctx->doc_cache, key, &start, &end)) - { - if (start < ctx->doc_len && end <= ctx->doc_len && start < end) - { - *found = ctx->doc_start + start; - return true; - } - } - } - - hb_svg_xml_parser_t search (ctx->doc_start, ctx->doc_len); - while (true) - { - hb_svg_token_type_t tok = search.next (); - if (tok == SVG_TOKEN_EOF) break; - if (tok != SVG_TOKEN_OPEN_TAG && tok != SVG_TOKEN_SELF_CLOSE_TAG) continue; - hb_svg_str_t attr_id = search.find_attr ("id"); - if (attr_id.len == id.len && 0 == memcmp (attr_id.data, id.data, id.len)) - { - *found = search.tag_start; - return true; - } - } - return false; -} - void hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_xml_parser_t &parser, @@ -101,7 +63,8 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_paint_push_transform (ctx->pfuncs, ctx->paint, 1, 0, 0, 1, use_x, use_y); const char *found = nullptr; - svg_find_element_by_id (ctx, ref_id, &found); + hb_raster_svg_find_element_by_id (ctx->doc_start, ctx->doc_len, ctx->svg_accel, ctx->doc_cache, + ref_id, &found); if (found) { From 3bcaf95acfaf9b58e179bea8e4162a668f7dd480 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:01:39 -0300 Subject: [PATCH 071/105] [raster] Support style x/y/width/height on SVG --- src/hb-raster-svg-base.cc | 4 ++++ src/hb-raster-svg-base.hh | 4 ++++ src/hb-raster-svg-clip.cc | 10 ++++++---- src/hb-raster-svg-use.cc | 10 ++++++---- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index eb8062c7d..d8fba127d 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -107,6 +107,10 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) else if (svg_str_eq_ascii_ci (prop_name, "offset")) out->offset = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "stop-color")) out->stop_color = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "stop-opacity")) out->stop_opacity = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "x")) out->x = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "y")) out->y = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "width")) out->width = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "height")) out->height = prop_value; if (p < end && *p == ';') p++; } diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 49aaece46..1e7051251 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -118,6 +118,10 @@ struct hb_svg_style_props_t hb_svg_str_t offset; hb_svg_str_t stop_color; hb_svg_str_t stop_opacity; + hb_svg_str_t x; + hb_svg_str_t y; + hb_svg_str_t width; + hb_svg_str_t height; }; struct hb_svg_xml_parser_t; diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 508266ab3..c4a92088a 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -157,10 +157,12 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, return; hb_svg_transform_t effective = base_transform; - float use_x = svg_parse_float (use_parser.find_attr ("x")); - float use_y = svg_parse_float (use_parser.find_attr ("y")); - float use_w = svg_parse_float (use_parser.find_attr ("width")); - float use_h = svg_parse_float (use_parser.find_attr ("height")); + hb_svg_style_props_t use_style_props; + svg_parse_style_props (use_parser.find_attr ("style"), &use_style_props); + float use_x = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.x, "x")); + float use_y = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.y, "y")); + float use_w = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.width, "width")); + float use_h = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.height, "height")); if (use_x != 0.f || use_y != 0.f) { hb_svg_transform_t tr; diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index 73458f2ef..fff614c55 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -44,10 +44,12 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, if (!hb_raster_svg_parse_local_id_ref (href, &ref_id, nullptr)) return; - float use_x = svg_parse_float (parser.find_attr ("x")); - float use_y = svg_parse_float (parser.find_attr ("y")); - float use_w = svg_parse_float (parser.find_attr ("width")); - float use_h = svg_parse_float (parser.find_attr ("height")); + hb_svg_style_props_t style_props; + svg_parse_style_props (parser.find_attr ("style"), &style_props); + float use_x = svg_parse_float (svg_pick_attr_or_style (parser, style_props.x, "x")); + float use_y = svg_parse_float (svg_pick_attr_or_style (parser, style_props.y, "y")); + float use_w = svg_parse_float (svg_pick_attr_or_style (parser, style_props.width, "width")); + float use_h = svg_parse_float (svg_pick_attr_or_style (parser, style_props.height, "height")); bool has_translate = (use_x != 0.f || use_y != 0.f); bool has_use_transform = transform_str.len > 0; From 4f75ddb6f3ef77e0c9825b579cd370364991a194 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:02:48 -0300 Subject: [PATCH 072/105] [raster] Support style geometry on nested SVG viewport elements --- src/hb-raster-svg-clip.cc | 20 +++++++++++--------- src/hb-raster-svg-render.cc | 11 +++++++---- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index c4a92088a..674164a3f 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -253,13 +253,15 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, allow_symbol_once = false; hb_svg_transform_t effective = base_transform; + hb_svg_style_props_t geom_style_props; + svg_parse_style_props (parser.find_attr ("style"), &geom_style_props); hb_svg_transform_t local_t; if (svg_parse_element_transform (parser, &local_t)) effective.multiply (local_t); if (parser.tag_name.eq ("svg")) { - float svg_x = svg_parse_float (parser.find_attr ("x")); - float svg_y = svg_parse_float (parser.find_attr ("y")); + float svg_x = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.x, "x")); + float svg_y = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.y, "y")); if (svg_x != 0.f || svg_y != 0.f) { hb_svg_transform_t tr; @@ -271,14 +273,14 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, if (!suppress_viewbox_once) { float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; - if (hb_raster_svg_parse_viewbox (parser.find_attr ("viewBox"), - &vb_x, &vb_y, &vb_w, &vb_h)) + if (hb_raster_svg_parse_viewbox (parser.find_attr ("viewBox"), + &vb_x, &vb_y, &vb_w, &vb_h)) + { + float viewport_w = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.width, "width")); + float viewport_h = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.height, "height")); + if (!(viewport_w > 0.f && viewport_h > 0.f)) { - float viewport_w = svg_parse_float (parser.find_attr ("width")); - float viewport_h = svg_parse_float (parser.find_attr ("height")); - if (!(viewport_w > 0.f && viewport_h > 0.f)) - { - viewport_w = vb_w; + viewport_w = vb_w; viewport_h = vb_h; } hb_svg_transform_t vb_t; diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 2e5d3a4d9..accf6d4ee 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -132,10 +132,13 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (tag.eq ("svg") || tag.eq ("symbol")) { + hb_svg_style_props_t geom_style_props; + svg_parse_style_props (parser.find_attr ("style"), &geom_style_props); + if (tag.eq ("svg")) { - svg_x = svg_parse_float (parser.find_attr ("x")); - svg_y = svg_parse_float (parser.find_attr ("y")); + svg_x = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.x, "x")); + svg_y = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.y, "y")); has_svg_translate = (svg_x != 0.f || svg_y != 0.f); } @@ -151,8 +154,8 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (tag.eq ("svg")) { - viewport_w = svg_parse_float (parser.find_attr ("width")); - viewport_h = svg_parse_float (parser.find_attr ("height")); + viewport_w = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.width, "width")); + viewport_h = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.height, "height")); if (!(viewport_w > 0.f && viewport_h > 0.f)) { viewport_w = vb_w; From 87ba0012537962fe92e033ad43c272a09f60d534 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:03:10 -0300 Subject: [PATCH 073/105] [raster] Reuse shared viewBox parser in SVG container rendering --- src/hb-raster-svg-render.cc | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index accf6d4ee..51a6d9a74 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -143,14 +143,9 @@ svg_render_container_element (hb_svg_render_context_t *ctx, } hb_svg_str_t viewbox_str = parser.find_attr ("viewBox"); - if (viewbox_str.len) + if (hb_raster_svg_parse_viewbox (viewbox_str, &vb_x, &vb_y, &vb_w, &vb_h)) { has_viewbox = true; - hb_svg_float_parser_t vb_fp (viewbox_str); - vb_x = vb_fp.next_float (); - vb_y = vb_fp.next_float (); - vb_w = vb_fp.next_float (); - vb_h = vb_fp.next_float (); if (tag.eq ("svg")) { From d52b612f45b500fcd9f320edaf889587140d6be4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:04:34 -0300 Subject: [PATCH 074/105] [raster] Parse shape geometry properties from SVG style --- src/hb-raster-svg-base.cc | 11 ++++++++++ src/hb-raster-svg-base.hh | 11 ++++++++++ src/hb-raster-svg-parse.cc | 42 ++++++++++++++++++++------------------ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index d8fba127d..20ef6ee68 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -111,6 +111,17 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) else if (svg_str_eq_ascii_ci (prop_name, "y")) out->y = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "width")) out->width = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "height")) out->height = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "cx")) out->cx = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "cy")) out->cy = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "r")) out->r = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "rx")) out->rx = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "ry")) out->ry = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "x1")) out->x1 = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "y1")) out->y1 = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "x2")) out->x2 = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "y2")) out->y2 = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "points")) out->points = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "d")) out->d = prop_value; if (p < end && *p == ';') p++; } diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 1e7051251..43bdfa06c 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -122,6 +122,17 @@ struct hb_svg_style_props_t hb_svg_str_t y; hb_svg_str_t width; hb_svg_str_t height; + hb_svg_str_t cx; + hb_svg_str_t cy; + hb_svg_str_t r; + hb_svg_str_t rx; + hb_svg_str_t ry; + hb_svg_str_t x1; + hb_svg_str_t y1; + hb_svg_str_t x2; + hb_svg_str_t y2; + hb_svg_str_t points; + hb_svg_str_t d; }; struct hb_svg_xml_parser_t; diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc index d456927bc..6ed4ff6b3 100644 --- a/src/hb-raster-svg-parse.cc +++ b/src/hb-raster-svg-parse.cc @@ -578,10 +578,12 @@ hb_raster_svg_parse_shape_tag (hb_svg_xml_parser_t &parser, hb_svg_shape_emit_data_t *shape) { hb_svg_attr_view_t attrs (parser); + hb_svg_style_props_t style_props; + svg_parse_style_props (attrs.get ("style"), &style_props); hb_svg_str_t tag = parser.tag_name; if (tag.eq ("path")) { - hb_svg_str_t d = attrs.get ("d"); + hb_svg_str_t d = svg_pick_attr_or_style (parser, style_props.d, "d"); if (!d.len) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_PATH; shape->str_data = d; @@ -589,36 +591,36 @@ hb_raster_svg_parse_shape_tag (hb_svg_xml_parser_t &parser, } if (tag.eq ("rect")) { - float w = svg_parse_float (attrs.get ("width")); - float h = svg_parse_float (attrs.get ("height")); + float w = svg_parse_float (svg_pick_attr_or_style (parser, style_props.width, "width")); + float h = svg_parse_float (svg_pick_attr_or_style (parser, style_props.height, "height")); if (w <= 0 || h <= 0) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_RECT; - shape->params[0] = svg_parse_float (attrs.get ("x")); - shape->params[1] = svg_parse_float (attrs.get ("y")); + shape->params[0] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.x, "x")); + shape->params[1] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.y, "y")); shape->params[2] = w; shape->params[3] = h; - shape->params[4] = svg_parse_float (attrs.get ("rx")); - shape->params[5] = svg_parse_float (attrs.get ("ry")); + shape->params[4] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.rx, "rx")); + shape->params[5] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.ry, "ry")); return true; } if (tag.eq ("circle")) { - float r = svg_parse_float (attrs.get ("r")); + float r = svg_parse_float (svg_pick_attr_or_style (parser, style_props.r, "r")); if (r <= 0) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_CIRCLE; - shape->params[0] = svg_parse_float (attrs.get ("cx")); - shape->params[1] = svg_parse_float (attrs.get ("cy")); + shape->params[0] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.cx, "cx")); + shape->params[1] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.cy, "cy")); shape->params[2] = r; return true; } if (tag.eq ("ellipse")) { - float rx = svg_parse_float (attrs.get ("rx")); - float ry = svg_parse_float (attrs.get ("ry")); + float rx = svg_parse_float (svg_pick_attr_or_style (parser, style_props.rx, "rx")); + float ry = svg_parse_float (svg_pick_attr_or_style (parser, style_props.ry, "ry")); if (rx <= 0 || ry <= 0) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_ELLIPSE; - shape->params[0] = svg_parse_float (attrs.get ("cx")); - shape->params[1] = svg_parse_float (attrs.get ("cy")); + shape->params[0] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.cx, "cx")); + shape->params[1] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.cy, "cy")); shape->params[2] = rx; shape->params[3] = ry; return true; @@ -626,15 +628,15 @@ hb_raster_svg_parse_shape_tag (hb_svg_xml_parser_t &parser, if (tag.eq ("line")) { shape->type = hb_svg_shape_emit_data_t::SHAPE_LINE; - shape->params[0] = svg_parse_float (attrs.get ("x1")); - shape->params[1] = svg_parse_float (attrs.get ("y1")); - shape->params[2] = svg_parse_float (attrs.get ("x2")); - shape->params[3] = svg_parse_float (attrs.get ("y2")); + shape->params[0] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.x1, "x1")); + shape->params[1] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.y1, "y1")); + shape->params[2] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.x2, "x2")); + shape->params[3] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.y2, "y2")); return true; } if (tag.eq ("polyline")) { - hb_svg_str_t points = attrs.get ("points"); + hb_svg_str_t points = svg_pick_attr_or_style (parser, style_props.points, "points"); if (!points.len) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; shape->str_data = points; @@ -642,7 +644,7 @@ hb_raster_svg_parse_shape_tag (hb_svg_xml_parser_t &parser, } if (tag.eq ("polygon")) { - hb_svg_str_t points = attrs.get ("points"); + hb_svg_str_t points = svg_pick_attr_or_style (parser, style_props.points, "points"); if (!points.len) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; shape->str_data = points; From 8523cf7a8284dcf391625ccb608f8f24f73fd928 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:05:05 -0300 Subject: [PATCH 075/105] [raster] Reuse shared shape parser for primitive SVG rendering --- src/hb-raster-svg-render.cc | 118 +++--------------------------------- 1 file changed, 7 insertions(+), 111 deletions(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 51a6d9a74..ba283359a 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -267,119 +267,15 @@ svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, const hb_svg_cascade_t &state, hb_svg_str_t transform_str) { - if (tag.eq ("path")) - { - hb_svg_str_t d = parser.find_attr ("d"); - if (d.len) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_PATH; - shape.str_data = d; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("rect")) - { - float x = svg_parse_float (parser.find_attr ("x")); - float y = svg_parse_float (parser.find_attr ("y")); - float w = svg_parse_float (parser.find_attr ("width")); - float h = svg_parse_float (parser.find_attr ("height")); - float rx = svg_parse_float (parser.find_attr ("rx")); - float ry = svg_parse_float (parser.find_attr ("ry")); + if (!(tag.eq ("path") || tag.eq ("rect") || tag.eq ("circle") || + tag.eq ("ellipse") || tag.eq ("line") || tag.eq ("polyline") || + tag.eq ("polygon"))) + return false; - if (w > 0 && h > 0) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_RECT; - shape.params[0] = x; - shape.params[1] = y; - shape.params[2] = w; - shape.params[3] = h; - shape.params[4] = rx; - shape.params[5] = ry; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("circle")) - { - float cx = svg_parse_float (parser.find_attr ("cx")); - float cy = svg_parse_float (parser.find_attr ("cy")); - float r = svg_parse_float (parser.find_attr ("r")); - - if (r > 0) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_CIRCLE; - shape.params[0] = cx; - shape.params[1] = cy; - shape.params[2] = r; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("ellipse")) - { - float cx = svg_parse_float (parser.find_attr ("cx")); - float cy = svg_parse_float (parser.find_attr ("cy")); - float rx = svg_parse_float (parser.find_attr ("rx")); - float ry = svg_parse_float (parser.find_attr ("ry")); - - if (rx > 0 && ry > 0) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_ELLIPSE; - shape.params[0] = cx; - shape.params[1] = cy; - shape.params[2] = rx; - shape.params[3] = ry; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("line")) - { - float x1 = svg_parse_float (parser.find_attr ("x1")); - float y1 = svg_parse_float (parser.find_attr ("y1")); - float x2 = svg_parse_float (parser.find_attr ("x2")); - float y2 = svg_parse_float (parser.find_attr ("y2")); - - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_LINE; - shape.params[0] = x1; - shape.params[1] = y1; - shape.params[2] = x2; - shape.params[3] = y2; + hb_svg_shape_emit_data_t shape; + if (hb_raster_svg_parse_shape_tag (parser, &shape)) svg_render_shape (ctx, shape, state, transform_str); - return true; - } - if (tag.eq ("polyline")) - { - hb_svg_str_t points = parser.find_attr ("points"); - if (points.len) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYLINE; - shape.str_data = points; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - if (tag.eq ("polygon")) - { - hb_svg_str_t points = parser.find_attr ("points"); - if (points.len) - { - hb_svg_shape_emit_data_t shape; - shape.type = hb_svg_shape_emit_data_t::SHAPE_POLYGON; - shape.str_data = points; - svg_render_shape (ctx, shape, state, transform_str); - } - return true; - } - - return false; + return true; } static void From d19699d089f4342c147df5e93cc5bbad16892eaf Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:05:44 -0300 Subject: [PATCH 076/105] [raster] Read target style width/height for SVG viewport fallback --- src/hb-raster-svg-clip.cc | 6 ++++-- src/hb-raster-svg-use.cc | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 674164a3f..3a915b84a 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -182,10 +182,12 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, { float viewport_w = use_w; float viewport_h = use_h; + hb_svg_style_props_t target_style_props; + svg_parse_style_props (ref_parser.find_attr ("style"), &target_style_props); if (viewport_w <= 0.f) - viewport_w = svg_parse_float (ref_parser.find_attr ("width")); + viewport_w = svg_parse_float (svg_pick_attr_or_style (ref_parser, target_style_props.width, "width")); if (viewport_h <= 0.f) - viewport_h = svg_parse_float (ref_parser.find_attr ("height")); + viewport_h = svg_parse_float (svg_pick_attr_or_style (ref_parser, target_style_props.height, "height")); float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t vb_t; diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index fff614c55..a79f383f7 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -87,10 +87,12 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, { float viewport_w = use_w; float viewport_h = use_h; + hb_svg_style_props_t target_style_props; + svg_parse_style_props (ref_parser.find_attr ("style"), &target_style_props); if (viewport_w <= 0.f) - viewport_w = svg_parse_float (ref_parser.find_attr ("width")); + viewport_w = svg_parse_float (svg_pick_attr_or_style (ref_parser, target_style_props.width, "width")); if (viewport_h <= 0.f) - viewport_h = svg_parse_float (ref_parser.find_attr ("height")); + viewport_h = svg_parse_float (svg_pick_attr_or_style (ref_parser, target_style_props.height, "height")); float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t t; From 3eff4faa4df00b65f885be47772608f78ac8e218 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:06:22 -0300 Subject: [PATCH 077/105] [raster] Reuse shared visibility resolver in clipPath parsing --- src/hb-raster-svg-clip.cc | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 3a915b84a..ba7aca5ea 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -422,27 +422,10 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, continue; } - hb_svg_style_props_t visibility_style_props; - svg_parse_style_props (parser.find_attr ("style"), &visibility_style_props); - hb_svg_str_t display_str = svg_pick_attr_or_style (parser, visibility_style_props.display, "display"); - hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, visibility_style_props.visibility, "visibility"); bool parent_visible = (unsigned) cdepth < SVG_MAX_CLIP_DEPTH ? inherited_visibility[cdepth] : true; - bool is_visible = parent_visible; - if (svg_str_eq_ascii_ci (display_str.trim (), "none")) - is_visible = false; - hb_svg_str_t vis_trim = visibility_str.trim (); - if (vis_trim.len) - { - if (svg_str_eq_ascii_ci (vis_trim, "inherit")) - is_visible = parent_visible; - else if (svg_str_eq_ascii_ci (vis_trim, "hidden") || - svg_str_eq_ascii_ci (vis_trim, "collapse")) - is_visible = false; - else if (svg_str_eq_ascii_ci (vis_trim, "visible")) - is_visible = true; - } + bool is_visible = svg_resolve_element_visibility (parser, parent_visible); bool is_hidden = !is_visible; if (is_hidden) { From bb8a6f73e33d3d53c187e816822a22abfc612423 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:06:49 -0300 Subject: [PATCH 078/105] [raster] Remove truncating id copy in SVG glyph fallback scan --- src/hb-raster-svg-render.cc | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index ba283359a..cd97a2795 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -497,7 +497,9 @@ hb_raster_svg_render (hb_raster_paint_t *paint, { /* Fallback for malformed/uncached docs: linear scan by glyph id. */ char glyph_id_str[32]; - snprintf (glyph_id_str, sizeof (glyph_id_str), "glyph%u", glyph); + int glyph_id_len = snprintf (glyph_id_str, sizeof (glyph_id_str), "glyph%u", glyph); + if (glyph_id_len <= 0 || (unsigned) glyph_id_len >= sizeof (glyph_id_str)) + return false; hb_svg_xml_parser_t parser (data, data_len); while (true) { @@ -509,12 +511,8 @@ hb_raster_svg_render (hb_raster_paint_t *paint, hb_svg_str_t id = parser.find_attr ("id"); if (id.len) { - char id_buf[64]; - unsigned n = hb_min (id.len, (unsigned) sizeof (id_buf) - 1); - memcpy (id_buf, id.data, n); - id_buf[n] = '\0'; - - if (strcmp (id_buf, glyph_id_str) == 0) + if (id.len == (unsigned) glyph_id_len && + 0 == memcmp (id.data, glyph_id_str, (unsigned) glyph_id_len)) { hb_paint_push_font_transform (ctx.pfuncs, ctx.paint, font); ctx.push_transform (1, 0, 0, -1, 0, 0); From 1198ff51d653e076d61b27d37b4fc77404c287f3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:08:17 -0300 Subject: [PATCH 079/105] [raster] Treat percent viewport sizes as unresolved in SVG mapping --- src/hb-raster-svg-base.cc | 8 ++++++++ src/hb-raster-svg-base.hh | 1 + src/hb-raster-svg-clip.cc | 12 ++++++------ src/hb-raster-svg-render.cc | 4 ++-- src/hb-raster-svg-use.cc | 8 ++++---- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index 20ef6ee68..cc4942b62 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -419,3 +419,11 @@ hb_raster_svg_compute_viewbox_transform (float viewport_w, *out = t; return true; } + +float +hb_raster_svg_parse_non_percent_length (hb_svg_str_t s) +{ + bool is_percent = false; + float v = svg_parse_number_or_percent (s, &is_percent); + return is_percent ? 0.f : v; +} diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 43bdfa06c..2d4cab549 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -168,6 +168,7 @@ HB_INTERNAL bool hb_raster_svg_compute_viewbox_transform (float viewport_w, float vb_h, hb_svg_str_t preserve_aspect_ratio, hb_svg_transform_t *out); +HB_INTERNAL float hb_raster_svg_parse_non_percent_length (hb_svg_str_t s); static inline float svg_parse_float_clamped01 (hb_svg_str_t s) { diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index ba7aca5ea..0f259e874 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -161,8 +161,8 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, svg_parse_style_props (use_parser.find_attr ("style"), &use_style_props); float use_x = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.x, "x")); float use_y = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.y, "y")); - float use_w = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.width, "width")); - float use_h = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.height, "height")); + float use_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.width, "width")); + float use_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.height, "height")); if (use_x != 0.f || use_y != 0.f) { hb_svg_transform_t tr; @@ -185,9 +185,9 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, hb_svg_style_props_t target_style_props; svg_parse_style_props (ref_parser.find_attr ("style"), &target_style_props); if (viewport_w <= 0.f) - viewport_w = svg_parse_float (svg_pick_attr_or_style (ref_parser, target_style_props.width, "width")); + viewport_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (ref_parser, target_style_props.width, "width")); if (viewport_h <= 0.f) - viewport_h = svg_parse_float (svg_pick_attr_or_style (ref_parser, target_style_props.height, "height")); + viewport_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (ref_parser, target_style_props.height, "height")); float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t vb_t; @@ -278,8 +278,8 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, if (hb_raster_svg_parse_viewbox (parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h)) { - float viewport_w = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.width, "width")); - float viewport_h = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.height, "height")); + float viewport_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.width, "width")); + float viewport_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.height, "height")); if (!(viewport_w > 0.f && viewport_h > 0.f)) { viewport_w = vb_w; diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index cd97a2795..415af7da3 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -149,8 +149,8 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (tag.eq ("svg")) { - viewport_w = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.width, "width")); - viewport_h = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.height, "height")); + viewport_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.width, "width")); + viewport_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.height, "height")); if (!(viewport_w > 0.f && viewport_h > 0.f)) { viewport_w = vb_w; diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index a79f383f7..ae61961b4 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -48,8 +48,8 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, svg_parse_style_props (parser.find_attr ("style"), &style_props); float use_x = svg_parse_float (svg_pick_attr_or_style (parser, style_props.x, "x")); float use_y = svg_parse_float (svg_pick_attr_or_style (parser, style_props.y, "y")); - float use_w = svg_parse_float (svg_pick_attr_or_style (parser, style_props.width, "width")); - float use_h = svg_parse_float (svg_pick_attr_or_style (parser, style_props.height, "height")); + float use_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.width, "width")); + float use_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.height, "height")); bool has_translate = (use_x != 0.f || use_y != 0.f); bool has_use_transform = transform_str.len > 0; @@ -90,9 +90,9 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_style_props_t target_style_props; svg_parse_style_props (ref_parser.find_attr ("style"), &target_style_props); if (viewport_w <= 0.f) - viewport_w = svg_parse_float (svg_pick_attr_or_style (ref_parser, target_style_props.width, "width")); + viewport_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (ref_parser, target_style_props.width, "width")); if (viewport_h <= 0.f) - viewport_h = svg_parse_float (svg_pick_attr_or_style (ref_parser, target_style_props.height, "height")); + viewport_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (ref_parser, target_style_props.height, "height")); float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t t; From 8fc7d8f2cc66e27b48d13ee2691e87c4f364f7da Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:09:33 -0300 Subject: [PATCH 080/105] [raster] Reject negative rect corner radii in SVG parsing --- src/hb-raster-svg-parse.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc index 6ed4ff6b3..ecd33b914 100644 --- a/src/hb-raster-svg-parse.cc +++ b/src/hb-raster-svg-parse.cc @@ -405,6 +405,8 @@ svg_rect_to_path (float x, float y, float w, float h, float rx, float ry, hb_draw_funcs_t *dfuncs, void *draw_data) { hb_draw_state_t st = HB_DRAW_STATE_DEFAULT; + if (rx < 0.f || ry < 0.f) + return; if (rx <= 0 && ry <= 0) { @@ -594,13 +596,16 @@ hb_raster_svg_parse_shape_tag (hb_svg_xml_parser_t &parser, float w = svg_parse_float (svg_pick_attr_or_style (parser, style_props.width, "width")); float h = svg_parse_float (svg_pick_attr_or_style (parser, style_props.height, "height")); if (w <= 0 || h <= 0) return false; + float rx = svg_parse_float (svg_pick_attr_or_style (parser, style_props.rx, "rx")); + float ry = svg_parse_float (svg_pick_attr_or_style (parser, style_props.ry, "ry")); + if (rx < 0.f || ry < 0.f) return false; shape->type = hb_svg_shape_emit_data_t::SHAPE_RECT; shape->params[0] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.x, "x")); shape->params[1] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.y, "y")); shape->params[2] = w; shape->params[3] = h; - shape->params[4] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.rx, "rx")); - shape->params[5] = svg_parse_float (svg_pick_attr_or_style (parser, style_props.ry, "ry")); + shape->params[4] = rx; + shape->params[5] = ry; return true; } if (tag.eq ("circle")) From be096d50bdd09564963914e3913abfab2be7d08e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:10:33 -0300 Subject: [PATCH 081/105] [raster] Treat percent x/y as unresolved in SVG viewport positioning --- src/hb-raster-svg-clip.cc | 8 ++++---- src/hb-raster-svg-render.cc | 4 ++-- src/hb-raster-svg-use.cc | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 0f259e874..32dbe428f 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -159,8 +159,8 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, hb_svg_transform_t effective = base_transform; hb_svg_style_props_t use_style_props; svg_parse_style_props (use_parser.find_attr ("style"), &use_style_props); - float use_x = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.x, "x")); - float use_y = svg_parse_float (svg_pick_attr_or_style (use_parser, use_style_props.y, "y")); + float use_x = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.x, "x")); + float use_y = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.y, "y")); float use_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.width, "width")); float use_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.height, "height")); if (use_x != 0.f || use_y != 0.f) @@ -262,8 +262,8 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, effective.multiply (local_t); if (parser.tag_name.eq ("svg")) { - float svg_x = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.x, "x")); - float svg_y = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.y, "y")); + float svg_x = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.x, "x")); + float svg_y = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.y, "y")); if (svg_x != 0.f || svg_y != 0.f) { hb_svg_transform_t tr; diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 415af7da3..b47e7d9d1 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -137,8 +137,8 @@ svg_render_container_element (hb_svg_render_context_t *ctx, if (tag.eq ("svg")) { - svg_x = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.x, "x")); - svg_y = svg_parse_float (svg_pick_attr_or_style (parser, geom_style_props.y, "y")); + svg_x = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.x, "x")); + svg_y = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, geom_style_props.y, "y")); has_svg_translate = (svg_x != 0.f || svg_y != 0.f); } diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index ae61961b4..d5e71a7c3 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -46,8 +46,8 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, hb_svg_style_props_t style_props; svg_parse_style_props (parser.find_attr ("style"), &style_props); - float use_x = svg_parse_float (svg_pick_attr_or_style (parser, style_props.x, "x")); - float use_y = svg_parse_float (svg_pick_attr_or_style (parser, style_props.y, "y")); + float use_x = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.x, "x")); + float use_y = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.y, "y")); float use_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.width, "width")); float use_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.height, "height")); From 3b9a860ce8d750e89e8ee5121d10f86b1abbdb07 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:10:59 -0300 Subject: [PATCH 082/105] [raster] Simplify primitive SVG render helper signature --- src/hb-raster-svg-render.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index b47e7d9d1..6fe098bd7 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -263,10 +263,10 @@ svg_render_container_element (hb_svg_render_context_t *ctx, static bool svg_render_primitive_shape_element (hb_svg_render_context_t *ctx, hb_svg_xml_parser_t &parser, - hb_svg_str_t tag, const hb_svg_cascade_t &state, hb_svg_str_t transform_str) { + hb_svg_str_t tag = parser.tag_name; if (!(tag.eq ("path") || tag.eq ("rect") || tag.eq ("circle") || tag.eq ("ellipse") || tag.eq ("line") || tag.eq ("polyline") || tag.eq ("polygon"))) @@ -401,7 +401,7 @@ svg_render_element (hb_svg_render_context_t *ctx, if (tag.eq ("g") || tag.eq ("svg") || tag.eq ("symbol")) svg_render_container_element (ctx, parser, tag, self_closing, state, transform_str, state.clip_path); - else if (svg_render_primitive_shape_element (ctx, parser, tag, state, transform_str)) + else if (svg_render_primitive_shape_element (ctx, parser, state, transform_str)) ; else if (tag.eq ("use")) { From 9988fd26d54ae41935b811be434dd621eee8ed59 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:13:46 -0300 Subject: [PATCH 083/105] [raster] Share use target viewport mapping helper --- src/hb-raster-svg-base.cc | 33 +++++++++++++++++++++++++++++++++ src/hb-raster-svg-base.hh | 4 ++++ src/hb-raster-svg-clip.cc | 31 ++++--------------------------- src/hb-raster-svg-use.cc | 29 +++-------------------------- 4 files changed, 44 insertions(+), 53 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index cc4942b62..59a28b785 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -420,6 +420,39 @@ hb_raster_svg_compute_viewbox_transform (float viewport_w, return true; } +bool +hb_raster_svg_compute_use_target_viewbox_transform (hb_svg_xml_parser_t &target_parser, + float use_w, + float use_h, + hb_svg_transform_t *out) +{ + if (!(target_parser.tag_name.eq ("svg") || target_parser.tag_name.eq ("symbol"))) + return false; + + float viewport_w = use_w; + float viewport_h = use_h; + hb_svg_style_props_t target_style_props; + svg_parse_style_props (target_parser.find_attr ("style"), &target_style_props); + if (viewport_w <= 0.f) + viewport_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (target_parser, target_style_props.width, "width")); + if (viewport_h <= 0.f) + viewport_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (target_parser, target_style_props.height, "height")); + + float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; + if (!hb_raster_svg_parse_viewbox (target_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h)) + return false; + + if (!(viewport_w > 0.f && viewport_h > 0.f)) + { + viewport_w = vb_w; + viewport_h = vb_h; + } + + return hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, + target_parser.find_attr ("preserveAspectRatio"), + out); +} + float hb_raster_svg_parse_non_percent_length (hb_svg_str_t s) { diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 2d4cab549..e6ca041e4 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -168,6 +168,10 @@ HB_INTERNAL bool hb_raster_svg_compute_viewbox_transform (float viewport_w, float vb_h, hb_svg_str_t preserve_aspect_ratio, hb_svg_transform_t *out); +HB_INTERNAL bool hb_raster_svg_compute_use_target_viewbox_transform (hb_svg_xml_parser_t &target_parser, + float use_w, + float use_h, + hb_svg_transform_t *out); HB_INTERNAL float hb_raster_svg_parse_non_percent_length (hb_svg_str_t s); static inline float svg_parse_float_clamped01 (hb_svg_str_t s) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 32dbe428f..e84374901 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -178,34 +178,11 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, return; bool viewport_mapped = false; - if (ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) + hb_svg_transform_t vb_t; + if (hb_raster_svg_compute_use_target_viewbox_transform (ref_parser, use_w, use_h, &vb_t)) { - float viewport_w = use_w; - float viewport_h = use_h; - hb_svg_style_props_t target_style_props; - svg_parse_style_props (ref_parser.find_attr ("style"), &target_style_props); - if (viewport_w <= 0.f) - viewport_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (ref_parser, target_style_props.width, "width")); - if (viewport_h <= 0.f) - viewport_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (ref_parser, target_style_props.height, "height")); - - float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; - hb_svg_transform_t vb_t; - if (hb_raster_svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h)) - { - if (!(viewport_w > 0.f && viewport_h > 0.f)) - { - viewport_w = vb_w; - viewport_h = vb_h; - } - if (hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, - ref_parser.find_attr ("preserveAspectRatio"), - &vb_t)) - { - effective.multiply (vb_t); - viewport_mapped = true; - } - } + effective.multiply (vb_t); + viewport_mapped = true; } bool allow_symbol = ref_parser.tag_name.eq ("symbol"); diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index d5e71a7c3..e69b02a29 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -83,35 +83,12 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, if (tok == SVG_TOKEN_OPEN_TAG || tok == SVG_TOKEN_SELF_CLOSE_TAG) { bool has_viewport_scale = false; - if (ref_parser.tag_name.eq ("svg") || ref_parser.tag_name.eq ("symbol")) - { - float viewport_w = use_w; - float viewport_h = use_h; - hb_svg_style_props_t target_style_props; - svg_parse_style_props (ref_parser.find_attr ("style"), &target_style_props); - if (viewport_w <= 0.f) - viewport_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (ref_parser, target_style_props.width, "width")); - if (viewport_h <= 0.f) - viewport_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (ref_parser, target_style_props.height, "height")); - - float vb_x = 0.f, vb_y = 0.f, vb_w = 0.f, vb_h = 0.f; hb_svg_transform_t t; - if (hb_raster_svg_parse_viewbox (ref_parser.find_attr ("viewBox"), &vb_x, &vb_y, &vb_w, &vb_h)) + if (hb_raster_svg_compute_use_target_viewbox_transform (ref_parser, use_w, use_h, &t)) { - if (!(viewport_w > 0.f && viewport_h > 0.f)) - { - viewport_w = vb_w; - viewport_h = vb_h; - } - if (hb_raster_svg_compute_viewbox_transform (viewport_w, viewport_h, vb_x, vb_y, vb_w, vb_h, - ref_parser.find_attr ("preserveAspectRatio"), - &t)) - { - hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); - has_viewport_scale = true; - } + hb_paint_push_transform (ctx->pfuncs, ctx->paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy); + has_viewport_scale = true; } - } render_cb (render_user, ref_parser, state, has_viewport_scale); if (has_viewport_scale) hb_paint_pop_transform (ctx->pfuncs, ctx->paint); From 9e44a08250048f33b1f5863997f965dc27076791 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:14:14 -0300 Subject: [PATCH 084/105] [raster] Share SVG use geometry parsing helper --- src/hb-raster-svg-base.cc | 16 ++++++++++++++++ src/hb-raster-svg-base.hh | 5 +++++ src/hb-raster-svg-clip.cc | 8 ++------ src/hb-raster-svg-use.cc | 8 ++------ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index 59a28b785..2272fec61 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -453,6 +453,22 @@ hb_raster_svg_compute_use_target_viewbox_transform (hb_svg_xml_parser_t &target_ out); } +void +hb_raster_svg_parse_use_geometry (hb_svg_xml_parser_t &parser, + float *x, + float *y, + float *w, + float *h) +{ + hb_svg_style_props_t style_props; + svg_parse_style_props (parser.find_attr ("style"), &style_props); + + if (x) *x = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.x, "x")); + if (y) *y = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.y, "y")); + if (w) *w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.width, "width")); + if (h) *h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.height, "height")); +} + float hb_raster_svg_parse_non_percent_length (hb_svg_str_t s) { diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index e6ca041e4..60d495be2 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -172,6 +172,11 @@ HB_INTERNAL bool hb_raster_svg_compute_use_target_viewbox_transform (hb_svg_xml_ float use_w, float use_h, hb_svg_transform_t *out); +HB_INTERNAL void hb_raster_svg_parse_use_geometry (hb_svg_xml_parser_t &parser, + float *x, + float *y, + float *w, + float *h); HB_INTERNAL float hb_raster_svg_parse_non_percent_length (hb_svg_str_t s); static inline float svg_parse_float_clamped01 (hb_svg_str_t s) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index e84374901..dfb4c4b57 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -157,12 +157,8 @@ svg_clip_collect_use_target (hb_svg_clip_collect_context_t *ctx, return; hb_svg_transform_t effective = base_transform; - hb_svg_style_props_t use_style_props; - svg_parse_style_props (use_parser.find_attr ("style"), &use_style_props); - float use_x = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.x, "x")); - float use_y = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.y, "y")); - float use_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.width, "width")); - float use_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (use_parser, use_style_props.height, "height")); + float use_x = 0.f, use_y = 0.f, use_w = 0.f, use_h = 0.f; + hb_raster_svg_parse_use_geometry (use_parser, &use_x, &use_y, &use_w, &use_h); if (use_x != 0.f || use_y != 0.f) { hb_svg_transform_t tr; diff --git a/src/hb-raster-svg-use.cc b/src/hb-raster-svg-use.cc index e69b02a29..b4aa5ab47 100644 --- a/src/hb-raster-svg-use.cc +++ b/src/hb-raster-svg-use.cc @@ -44,12 +44,8 @@ hb_raster_svg_render_use_element (const hb_svg_use_context_t *ctx, if (!hb_raster_svg_parse_local_id_ref (href, &ref_id, nullptr)) return; - hb_svg_style_props_t style_props; - svg_parse_style_props (parser.find_attr ("style"), &style_props); - float use_x = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.x, "x")); - float use_y = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.y, "y")); - float use_w = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.width, "width")); - float use_h = hb_raster_svg_parse_non_percent_length (svg_pick_attr_or_style (parser, style_props.height, "height")); + float use_x = 0.f, use_y = 0.f, use_w = 0.f, use_h = 0.f; + hb_raster_svg_parse_use_geometry (parser, &use_x, &use_y, &use_w, &use_h); bool has_translate = (use_x != 0.f || use_y != 0.f); bool has_use_transform = transform_str.len > 0; From 588faa2da2930320462b3be1793b6a1ab8ba0df9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:15:01 -0300 Subject: [PATCH 085/105] [raster] Treat SVG as a container in render and clip traversal --- src/hb-raster-svg-clip.cc | 1 + src/hb-raster-svg-render.cc | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index dfb4c4b57..089c202b5 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -287,6 +287,7 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, } bool is_container = parser.tag_name.eq ("g") || + parser.tag_name.eq ("a") || parser.tag_name.eq ("svg") || parser.tag_name.eq ("symbol"); if (!is_container || parser.self_closing) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 6fe098bd7..5b5e10bf6 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -227,6 +227,7 @@ svg_render_container_element (hb_svg_render_context_t *ctx, } svg_render_element (ctx, parser, state); if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && + !child_tag.eq ("a") && !child_tag.eq ("svg") && !child_tag.eq ("symbol") && !child_tag.eq ("use")) { @@ -398,7 +399,7 @@ svg_render_element (hb_svg_render_context_t *ctx, if (tag.eq ("symbol")) ctx->allow_symbol_render_once = false; - if (tag.eq ("g") || tag.eq ("svg") || tag.eq ("symbol")) + if (tag.eq ("g") || tag.eq ("a") || tag.eq ("svg") || tag.eq ("symbol")) svg_render_container_element (ctx, parser, tag, self_closing, state, transform_str, state.clip_path); else if (svg_render_primitive_shape_element (ctx, parser, state, transform_str)) From f52b058db23d32d6cdba5ab207992e1a92e799d8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:15:20 -0300 Subject: [PATCH 086/105] [raster] Remove unused hb_svg_str_t helpers --- src/hb-raster-svg-base.hh | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 60d495be2..0481c121f 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -56,12 +56,6 @@ struct hb_svg_str_t return len >= plen && memcmp (data, prefix, plen) == 0; } - bool eq_cstr (const char *s) const - { - unsigned slen = (unsigned) strlen (s); - return len == slen && memcmp (data, s, len) == 0; - } - float to_float () const { if (!data || !len) return 0.f; @@ -73,16 +67,6 @@ struct hb_svg_str_t return isfinite (v) ? v : 0.f; } - int to_int () const - { - if (!data || !len) return 0; - char buf[32]; - unsigned n = hb_min (len, (unsigned) sizeof (buf) - 1); - memcpy (buf, data, n); - buf[n] = '\0'; - return atoi (buf); - } - hb_svg_str_t trim_left () const { const char *p = data; From a4b05d135df94269796ad11b80dcc4973c62afb1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:15:54 -0300 Subject: [PATCH 087/105] [raster] Centralize SVG container tag checks --- src/hb-raster-svg-base.hh | 12 ++++++++++++ src/hb-raster-svg-clip.cc | 5 +---- src/hb-raster-svg-render.cc | 8 +++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 0481c121f..2ad38262a 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -180,4 +180,16 @@ svg_str_is_none (hb_svg_str_t s) return svg_str_eq_ascii_ci (s.trim (), "none"); } +static inline bool +hb_raster_svg_tag_is_container (hb_svg_str_t tag) +{ + return tag.eq ("g") || tag.eq ("a") || tag.eq ("svg") || tag.eq ("symbol"); +} + +static inline bool +hb_raster_svg_tag_is_container_or_use (hb_svg_str_t tag) +{ + return hb_raster_svg_tag_is_container (tag) || tag.eq ("use"); +} + #endif /* HB_RASTER_SVG_BASE_HH */ diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 089c202b5..2d5ccdd8c 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -286,10 +286,7 @@ svg_clip_collect_ref_element (hb_svg_clip_collect_context_t *ctx, return; } - bool is_container = parser.tag_name.eq ("g") || - parser.tag_name.eq ("a") || - parser.tag_name.eq ("svg") || - parser.tag_name.eq ("symbol"); + bool is_container = hb_raster_svg_tag_is_container (parser.tag_name); if (!is_container || parser.self_closing) { if (!parser.self_closing) diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 5b5e10bf6..25965de74 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -226,10 +226,8 @@ svg_render_container_element (hb_svg_render_context_t *ctx, continue; } svg_render_element (ctx, parser, state); - if (tok == SVG_TOKEN_OPEN_TAG && !child_tag.eq ("g") && - !child_tag.eq ("a") && - !child_tag.eq ("svg") && !child_tag.eq ("symbol") && - !child_tag.eq ("use")) + if (tok == SVG_TOKEN_OPEN_TAG && + !hb_raster_svg_tag_is_container_or_use (child_tag)) { /* Skip children of non-container elements we don't handle. */ int skip_depth = 1; @@ -399,7 +397,7 @@ svg_render_element (hb_svg_render_context_t *ctx, if (tag.eq ("symbol")) ctx->allow_symbol_render_once = false; - if (tag.eq ("g") || tag.eq ("a") || tag.eq ("svg") || tag.eq ("symbol")) + if (hb_raster_svg_tag_is_container (tag)) svg_render_container_element (ctx, parser, tag, self_closing, state, transform_str, state.clip_path); else if (svg_render_primitive_shape_element (ctx, parser, state, transform_str)) From 062fa57c854e8fba9ed04a1597f21d8898713b3a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:16:21 -0300 Subject: [PATCH 088/105] [raster] Use hb_decycler_t for gradient href cycle detection --- src/hb-raster-svg-fill.cc | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 107ac3e9d..41135296b 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -30,6 +30,7 @@ #include "hb-raster-svg-base.hh" #include "hb-raster-svg-color.hh" +#include "hb-decycler.hh" #include @@ -132,18 +133,14 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, if (grad) { hb_vector_t chain; + hb_decycler_t decycler; const hb_svg_gradient_t *cur = grad; while (cur) { - bool is_cycle = false; - for (unsigned i = 0; i < chain.length; i++) - if (chain.arrayZ[i] == cur) - { - is_cycle = true; - break; - } - if (is_cycle) + hb_decycler_node_t node (decycler); + if (unlikely (!node.visit ((uintptr_t) cur))) break; + chain.push (cur); if (unlikely (chain.in_error ())) { From a70d4189bccaabce752904d6a51c3152c78e25c6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:17:12 -0300 Subject: [PATCH 089/105] [raster] Support style fallback for gradient geometry and attrs --- src/hb-raster-svg-base.cc | 8 ++++++++ src/hb-raster-svg-base.hh | 5 +++++ src/hb-raster-svg-gradient.cc | 30 +++++++++++++++++------------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index 2272fec61..a2f8e32ce 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -107,6 +107,12 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) else if (svg_str_eq_ascii_ci (prop_name, "offset")) out->offset = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "stop-color")) out->stop_color = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "stop-opacity")) out->stop_opacity = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "spreadmethod") || + svg_str_eq_ascii_ci (prop_name, "spread-method")) out->spread_method = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "gradientunits") || + svg_str_eq_ascii_ci (prop_name, "gradient-units")) out->gradient_units = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "gradienttransform") || + svg_str_eq_ascii_ci (prop_name, "gradient-transform")) out->gradient_transform = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "x")) out->x = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "y")) out->y = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "width")) out->width = prop_value; @@ -114,6 +120,8 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) else if (svg_str_eq_ascii_ci (prop_name, "cx")) out->cx = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "cy")) out->cy = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "r")) out->r = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "fx")) out->fx = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "fy")) out->fy = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "rx")) out->rx = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "ry")) out->ry = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "x1")) out->x1 = prop_value; diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 2ad38262a..2992f9b71 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -102,6 +102,9 @@ struct hb_svg_style_props_t hb_svg_str_t offset; hb_svg_str_t stop_color; hb_svg_str_t stop_opacity; + hb_svg_str_t spread_method; + hb_svg_str_t gradient_units; + hb_svg_str_t gradient_transform; hb_svg_str_t x; hb_svg_str_t y; hb_svg_str_t width; @@ -109,6 +112,8 @@ struct hb_svg_style_props_t hb_svg_str_t cx; hb_svg_str_t cy; hb_svg_str_t r; + hb_svg_str_t fx; + hb_svg_str_t fy; hb_svg_str_t rx; hb_svg_str_t ry; hb_svg_str_t x1; diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index 7da4bfefd..3c9aef5fc 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -86,7 +86,10 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, hb_svg_gradient_t &grad) { hb_svg_attr_view_t attrs (parser); - hb_svg_str_t spread_str = attrs.get ("spreadMethod"); + hb_svg_style_props_t style_props; + svg_parse_style_props (attrs.get ("style"), &style_props); + + hb_svg_str_t spread_str = svg_pick_attr_or_style (parser, style_props.spread_method, "spreadMethod"); if (spread_str.eq ("reflect")) { grad.spread = HB_PAINT_EXTEND_REFLECT; @@ -103,7 +106,7 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, grad.has_spread = true; } - hb_svg_str_t units_str = attrs.get ("gradientUnits"); + hb_svg_str_t units_str = svg_pick_attr_or_style (parser, style_props.gradient_units, "gradientUnits"); if (units_str.eq ("userSpaceOnUse")) { grad.units_user_space = true; @@ -115,7 +118,7 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, grad.has_units_user_space = true; } - hb_svg_str_t transform_str = attrs.get ("gradientTransform"); + hb_svg_str_t transform_str = svg_pick_attr_or_style (parser, style_props.gradient_transform, "gradientTransform"); if (transform_str.len) { grad.has_gradient_transform = true; @@ -135,13 +138,14 @@ static void svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, hb_svg_gradient_t &grad) { - hb_svg_attr_view_t attrs (parser); + hb_svg_style_props_t style_props; + svg_parse_style_props (parser.find_attr ("style"), &style_props); if (grad.type == SVG_GRADIENT_LINEAR) { - hb_svg_str_t x1_str = attrs.get ("x1"); - hb_svg_str_t y1_str = attrs.get ("y1"); - hb_svg_str_t x2_str = attrs.get ("x2"); - hb_svg_str_t y2_str = attrs.get ("y2"); + hb_svg_str_t x1_str = svg_pick_attr_or_style (parser, style_props.x1, "x1"); + hb_svg_str_t y1_str = svg_pick_attr_or_style (parser, style_props.y1, "y1"); + hb_svg_str_t x2_str = svg_pick_attr_or_style (parser, style_props.x2, "x2"); + hb_svg_str_t y2_str = svg_pick_attr_or_style (parser, style_props.y2, "y2"); if (x1_str.len) { grad.x1 = svg_parse_number_or_percent (x1_str, nullptr); grad.has_x1 = true; } if (y1_str.len) { grad.y1 = svg_parse_number_or_percent (y1_str, nullptr); grad.has_y1 = true; } if (x2_str.len) { grad.x2 = svg_parse_number_or_percent (x2_str, nullptr); grad.has_x2 = true; } @@ -152,11 +156,11 @@ svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, } else { - hb_svg_str_t cx_str = attrs.get ("cx"); - hb_svg_str_t cy_str = attrs.get ("cy"); - hb_svg_str_t r_str = attrs.get ("r"); - hb_svg_str_t fx_str = attrs.get ("fx"); - hb_svg_str_t fy_str = attrs.get ("fy"); + hb_svg_str_t cx_str = svg_pick_attr_or_style (parser, style_props.cx, "cx"); + hb_svg_str_t cy_str = svg_pick_attr_or_style (parser, style_props.cy, "cy"); + hb_svg_str_t r_str = svg_pick_attr_or_style (parser, style_props.r, "r"); + hb_svg_str_t fx_str = svg_pick_attr_or_style (parser, style_props.fx, "fx"); + hb_svg_str_t fy_str = svg_pick_attr_or_style (parser, style_props.fy, "fy"); if (cx_str.len) { grad.cx = svg_parse_number_or_percent (cx_str, nullptr); grad.has_cx = true; } if (cy_str.len) { grad.cy = svg_parse_number_or_percent (cy_str, nullptr); grad.has_cy = true; } From 34438c8bdf852b870a74060ea07d78d27e4902fc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:17:42 -0300 Subject: [PATCH 090/105] [raster] Parse gradient spread/units values case-insensitively --- src/hb-raster-svg-gradient.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index 3c9aef5fc..f1ac24611 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -89,30 +89,30 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, hb_svg_style_props_t style_props; svg_parse_style_props (attrs.get ("style"), &style_props); - hb_svg_str_t spread_str = svg_pick_attr_or_style (parser, style_props.spread_method, "spreadMethod"); - if (spread_str.eq ("reflect")) + hb_svg_str_t spread_str = svg_pick_attr_or_style (parser, style_props.spread_method, "spreadMethod").trim (); + if (svg_str_eq_ascii_ci (spread_str, "reflect")) { grad.spread = HB_PAINT_EXTEND_REFLECT; grad.has_spread = true; } - else if (spread_str.eq ("repeat")) + else if (svg_str_eq_ascii_ci (spread_str, "repeat")) { grad.spread = HB_PAINT_EXTEND_REPEAT; grad.has_spread = true; } - else if (spread_str.eq ("pad")) + else if (svg_str_eq_ascii_ci (spread_str, "pad")) { grad.spread = HB_PAINT_EXTEND_PAD; grad.has_spread = true; } - hb_svg_str_t units_str = svg_pick_attr_or_style (parser, style_props.gradient_units, "gradientUnits"); - if (units_str.eq ("userSpaceOnUse")) + hb_svg_str_t units_str = svg_pick_attr_or_style (parser, style_props.gradient_units, "gradientUnits").trim (); + if (svg_str_eq_ascii_ci (units_str, "userSpaceOnUse")) { grad.units_user_space = true; grad.has_units_user_space = true; } - else if (units_str.eq ("objectBoundingBox")) + else if (svg_str_eq_ascii_ci (units_str, "objectBoundingBox")) { grad.units_user_space = false; grad.has_units_user_space = true; From 5427e20e20ed668a5a2b90ba3e9eb156bc642d12 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:17:56 -0300 Subject: [PATCH 091/105] [raster] Match preserveAspectRatio align tokens case-insensitively --- src/hb-raster-svg-base.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index a2f8e32ce..b4482ad46 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -347,11 +347,15 @@ svg_align_offset (hb_svg_str_t align, char axis) { if (leftover <= 0.f) return 0.f; - if ((axis == 'x' && align.starts_with ("xMin")) || - (axis == 'y' && (align.eq ("xMinYMin") || align.eq ("xMidYMin") || align.eq ("xMaxYMin")))) + if ((axis == 'x' && svg_str_starts_with_ascii_ci (align, "xMin")) || + (axis == 'y' && (svg_str_eq_ascii_ci (align, "xMinYMin") || + svg_str_eq_ascii_ci (align, "xMidYMin") || + svg_str_eq_ascii_ci (align, "xMaxYMin")))) return 0.f; - if ((axis == 'x' && align.starts_with ("xMax")) || - (axis == 'y' && (align.eq ("xMinYMax") || align.eq ("xMidYMax") || align.eq ("xMaxYMax")))) + if ((axis == 'x' && svg_str_starts_with_ascii_ci (align, "xMax")) || + (axis == 'y' && (svg_str_eq_ascii_ci (align, "xMinYMax") || + svg_str_eq_ascii_ci (align, "xMidYMax") || + svg_str_eq_ascii_ci (align, "xMaxYMax")))) return leftover; return leftover * 0.5f; } @@ -390,7 +394,7 @@ hb_raster_svg_compute_viewbox_transform (float viewport_w, if (svg_str_starts_with_ascii_ci (par, "none")) is_none = true; - else if (align.starts_with ("x")) + else if (svg_str_starts_with_ascii_ci (align, "x")) { const char *mode = p; while (mode < end && (*mode == ' ' || *mode == '\t' || *mode == '\n' || *mode == '\r')) mode++; @@ -417,7 +421,7 @@ hb_raster_svg_compute_viewbox_transform (float viewport_w, float leftover_x = viewport_w - scaled_w; float leftover_y = viewport_h - scaled_h; - if (!align.starts_with ("x")) + if (!svg_str_starts_with_ascii_ci (align, "x")) align = hb_svg_str_t ("xMidYMid", 8); t.xx = s; From 520928c4e4d018fb1e84aa12250c11a83cbb9ba3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:18:40 -0300 Subject: [PATCH 092/105] [raster] Add radial gradient fr support --- src/hb-raster-svg-base.cc | 1 + src/hb-raster-svg-base.hh | 1 + src/hb-raster-svg-defs.hh | 4 ++-- src/hb-raster-svg-fill.cc | 4 +++- src/hb-raster-svg-gradient.cc | 2 ++ 5 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index b4482ad46..73e6c53d6 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -122,6 +122,7 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) else if (svg_str_eq_ascii_ci (prop_name, "r")) out->r = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "fx")) out->fx = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "fy")) out->fy = prop_value; + else if (svg_str_eq_ascii_ci (prop_name, "fr")) out->fr = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "rx")) out->rx = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "ry")) out->ry = prop_value; else if (svg_str_eq_ascii_ci (prop_name, "x1")) out->x1 = prop_value; diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 2992f9b71..25a8a98dc 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -114,6 +114,7 @@ struct hb_svg_style_props_t hb_svg_str_t r; hb_svg_str_t fx; hb_svg_str_t fy; + hb_svg_str_t fr; hb_svg_str_t rx; hb_svg_str_t ry; hb_svg_str_t x1; diff --git a/src/hb-raster-svg-defs.hh b/src/hb-raster-svg-defs.hh index 617060ec9..ca1855658 100644 --- a/src/hb-raster-svg-defs.hh +++ b/src/hb-raster-svg-defs.hh @@ -60,9 +60,9 @@ struct hb_svg_gradient_t bool has_x1 = false, has_y1 = false, has_x2 = false, has_y2 = false; float cx = 0.5f, cy = 0.5f, r = 0.5f; - float fx = -1.f, fy = -1.f; + float fx = -1.f, fy = -1.f, fr = 0.f; bool has_cx = false, has_cy = false, has_r = false; - bool has_fx = false, has_fy = false; + bool has_fx = false, has_fy = false, has_fr = false; hb_vector_t stops; diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 41135296b..d0375e570 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -187,6 +187,7 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, if (g->has_r) { effective.r = g->r; effective.has_r = true; } if (g->has_fx) { effective.fx = g->fx; effective.has_fx = true; } if (g->has_fy) { effective.fy = g->fy; effective.has_fy = true; } + if (g->has_fr) { effective.fr = g->fr; effective.has_fr = true; } } if (!effective.stops.length) { @@ -239,9 +240,10 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, { float fx = effective.has_fx ? effective.fx : effective.cx; float fy = effective.has_fy ? effective.fy : effective.cy; + float fr = effective.has_fr ? effective.fr : 0.f; hb_paint_radial_gradient (ctx->pfuncs, ctx->paint, &cl, - fx, fy, 0.f, + fx, fy, fr, effective.cx, effective.cy, effective.r); } diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index f1ac24611..32af5695f 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -161,12 +161,14 @@ svg_parse_gradient_geometry_attrs (hb_svg_xml_parser_t &parser, hb_svg_str_t r_str = svg_pick_attr_or_style (parser, style_props.r, "r"); hb_svg_str_t fx_str = svg_pick_attr_or_style (parser, style_props.fx, "fx"); hb_svg_str_t fy_str = svg_pick_attr_or_style (parser, style_props.fy, "fy"); + hb_svg_str_t fr_str = svg_pick_attr_or_style (parser, style_props.fr, "fr"); if (cx_str.len) { grad.cx = svg_parse_number_or_percent (cx_str, nullptr); grad.has_cx = true; } if (cy_str.len) { grad.cy = svg_parse_number_or_percent (cy_str, nullptr); grad.has_cy = true; } if (r_str.len) { grad.r = svg_parse_number_or_percent (r_str, nullptr); grad.has_r = true; } if (fx_str.len) { grad.fx = svg_parse_number_or_percent (fx_str, nullptr); grad.has_fx = true; } if (fy_str.len) { grad.fy = svg_parse_number_or_percent (fy_str, nullptr); grad.has_fy = true; } + if (fr_str.len) { grad.fr = svg_parse_number_or_percent (fr_str, nullptr); grad.has_fr = true; } } } From d48d888b38b2fa544e6ede53b6ba285a5f36ef18 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:24:09 -0300 Subject: [PATCH 093/105] [raster] Handle hidden gradient stops and monotonic offsets --- src/hb-raster-svg-gradient.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index 32af5695f..9bc6c0ec1 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -50,10 +50,21 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, hb_svg_str_t offset_str = svg_pick_attr_or_style (parser, style_props.offset, "offset"); hb_svg_str_t color_str = svg_pick_attr_or_style (parser, style_props.stop_color, "stop-color"); hb_svg_str_t opacity_str = svg_pick_attr_or_style (parser, style_props.stop_opacity, "stop-opacity"); + hb_svg_str_t display_str = svg_pick_attr_or_style (parser, style_props.display, "display"); + hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, style_props.visibility, "visibility"); + + if (svg_str_eq_ascii_ci (display_str.trim (), "none")) + return true; + hb_svg_str_t visibility_trim = visibility_str.trim (); + if (svg_str_eq_ascii_ci (visibility_trim, "hidden") || + svg_str_eq_ascii_ci (visibility_trim, "collapse")) + return true; float offset = 0; if (offset_str.len) offset = hb_clamp (svg_parse_number_or_percent (offset_str, nullptr), 0.f, 1.f); + if (grad.stops.length) + offset = hb_max (offset, grad.stops.arrayZ[grad.stops.length - 1].offset); bool is_none = false; hb_color_t color = HB_COLOR (0, 0, 0, 255); From edf70c23d748c08fc2e20cf9cd6415a9a4496ea1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:24:20 -0300 Subject: [PATCH 094/105] [raster] Parse clipPathUnits values case-insensitively --- src/hb-raster-svg-clip.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 2d5ccdd8c..aabe33030 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -320,9 +320,11 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, { hb_svg_clip_path_def_t clip; hb_svg_str_t id = parser.find_attr ("id"); - hb_svg_str_t units = parser.find_attr ("clipPathUnits"); - if (units.eq ("objectBoundingBox")) + hb_svg_str_t units = parser.find_attr ("clipPathUnits").trim (); + if (svg_str_eq_ascii_ci (units, "objectBoundingBox")) clip.units_user_space = false; + else if (svg_str_eq_ascii_ci (units, "userSpaceOnUse")) + clip.units_user_space = true; else clip.units_user_space = true; From 5659d5dc894ad68ab8fc93e44329ac6557f6bd1b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:24:53 -0300 Subject: [PATCH 095/105] [raster] Simplify gradient style parsing path --- src/hb-raster-svg-gradient.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index 9bc6c0ec1..ae7c5820a 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -96,9 +96,8 @@ static void svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, hb_svg_gradient_t &grad) { - hb_svg_attr_view_t attrs (parser); hb_svg_style_props_t style_props; - svg_parse_style_props (attrs.get ("style"), &style_props); + svg_parse_style_props (parser.find_attr ("style"), &style_props); hb_svg_str_t spread_str = svg_pick_attr_or_style (parser, style_props.spread_method, "spreadMethod").trim (); if (svg_str_eq_ascii_ci (spread_str, "reflect")) From 9f21f45d3fbb3dd819963d8ea35ca92bfebb2de3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:25:28 -0300 Subject: [PATCH 096/105] [raster] Parse SVG transform function names case-insensitively --- src/hb-raster-svg-parse.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc index ecd33b914..8f2663ba2 100644 --- a/src/hb-raster-svg-parse.cc +++ b/src/hb-raster-svg-parse.cc @@ -52,7 +52,7 @@ hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) hb_svg_transform_t t; - if (func_name.eq ("matrix")) + if (svg_str_eq_ascii_ci (func_name, "matrix")) { t.xx = fp.next_float (); t.yx = fp.next_float (); @@ -61,14 +61,14 @@ hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) t.dx = fp.next_float (); t.dy = fp.next_float (); } - else if (func_name.eq ("translate")) + else if (svg_str_eq_ascii_ci (func_name, "translate")) { t.dx = fp.next_float (); fp.skip_ws_comma (); if (fp.p < fp.end && *fp.p != ')') t.dy = fp.next_float (); } - else if (func_name.eq ("scale")) + else if (svg_str_eq_ascii_ci (func_name, "scale")) { t.xx = fp.next_float (); fp.skip_ws_comma (); @@ -77,7 +77,7 @@ hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) else t.yy = t.xx; } - else if (func_name.eq ("rotate")) + else if (svg_str_eq_ascii_ci (func_name, "rotate")) { float angle = fp.next_float () * (float) M_PI / 180.f; float cs = cosf (angle), sn = sinf (angle); @@ -97,12 +97,12 @@ hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) t.xy = -sn; t.yy = cs; } } - else if (func_name.eq ("skewX")) + else if (svg_str_eq_ascii_ci (func_name, "skewX")) { float angle = fp.next_float () * (float) M_PI / 180.f; t.xy = tanf (angle); } - else if (func_name.eq ("skewY")) + else if (svg_str_eq_ascii_ci (func_name, "skewY")) { float angle = fp.next_float () * (float) M_PI / 180.f; t.yx = tanf (angle); From c9656667834731131c9dd42aa136aa374ca74db0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:25:43 -0300 Subject: [PATCH 097/105] [raster] Match clip-path 'none' keyword case-insensitively --- src/hb-raster-svg-clip.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index aabe33030..ce3e31324 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -507,7 +507,7 @@ hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, { if (clip_path_str.is_null ()) return false; hb_svg_str_t trimmed = clip_path_str.trim (); - if (!trimmed.len || trimmed.eq ("none")) return false; + if (!trimmed.len || svg_str_eq_ascii_ci (trimmed, "none")) return false; hb_svg_str_t clip_id; if (!hb_raster_svg_parse_local_id_ref (trimmed, &clip_id, nullptr)) From faa4c66c38c4f3ba977a8698eca63ec387ec3d17 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:26:29 -0300 Subject: [PATCH 098/105] [raster] Cap gradient href inheritance chain depth --- src/hb-raster-svg-fill.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index d0375e570..fc44da553 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -132,11 +132,15 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, } if (grad) { + const unsigned SVG_MAX_GRADIENT_REF_DEPTH = 1024; hb_vector_t chain; hb_decycler_t decycler; const hb_svg_gradient_t *cur = grad; while (cur) { + if (chain.length >= SVG_MAX_GRADIENT_REF_DEPTH) + break; + hb_decycler_node_t node (decycler); if (unlikely (!node.visit ((uintptr_t) cur))) break; From 5a3e4a775048a480aa1419e18a3c0ee46a3bc45e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:26:52 -0300 Subject: [PATCH 099/105] [raster] Add ASCII-insensitive compare methods to hb_svg_str_t --- src/hb-raster-svg-base.cc | 22 ++-------------------- src/hb-raster-svg-base.hh | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index 73e6c53d6..d05c462ee 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -29,34 +29,16 @@ #include "hb-raster-svg-base.hh" #include "hb-raster-svg-parse.hh" -static inline char -svg_ascii_lower (char c) -{ - if (c >= 'A' && c <= 'Z') - return c + ('a' - 'A'); - return c; -} - bool svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit) { - unsigned n = (unsigned) strlen (lit); - if (s.len != n) return false; - for (unsigned i = 0; i < n; i++) - if (svg_ascii_lower (s.data[i]) != svg_ascii_lower (lit[i])) - return false; - return true; + return s.eq_ascii_ci (lit); } bool svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit) { - unsigned n = (unsigned) strlen (lit); - if (s.len < n) return false; - for (unsigned i = 0; i < n; i++) - if (svg_ascii_lower (s.data[i]) != svg_ascii_lower (lit[i])) - return false; - return true; + return s.starts_with_ascii_ci (lit); } void diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index 25a8a98dc..e72e5d8f9 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -34,6 +34,14 @@ #include #include +static inline char +hb_svg_ascii_lower (char c) +{ + if (c >= 'A' && c <= 'Z') + return c + ('a' - 'A'); + return c; +} + struct hb_svg_str_t { const char *data; @@ -56,6 +64,26 @@ struct hb_svg_str_t return len >= plen && memcmp (data, prefix, plen) == 0; } + bool eq_ascii_ci (const char *lit) const + { + unsigned n = (unsigned) strlen (lit); + if (len != n) return false; + for (unsigned i = 0; i < n; i++) + if (hb_svg_ascii_lower (data[i]) != hb_svg_ascii_lower (lit[i])) + return false; + return true; + } + + bool starts_with_ascii_ci (const char *lit) const + { + unsigned n = (unsigned) strlen (lit); + if (len < n) return false; + for (unsigned i = 0; i < n; i++) + if (hb_svg_ascii_lower (data[i]) != hb_svg_ascii_lower (lit[i])) + return false; + return true; + } + float to_float () const { if (!data || !len) return 0.f; From b3a0ce0e164426bcdbed4b5897e617245bf2a76a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:28:01 -0300 Subject: [PATCH 100/105] [raster] Churn callsites to hb_svg_str_t ASCII-ci methods --- src/hb-raster-svg-base.cc | 112 +++++++++++++++------------------- src/hb-raster-svg-base.hh | 6 +- src/hb-raster-svg-clip.cc | 16 ++--- src/hb-raster-svg-color.cc | 6 +- src/hb-raster-svg-fill.cc | 2 +- src/hb-raster-svg-gradient.cc | 18 +++--- src/hb-raster-svg-parse.cc | 12 ++-- src/hb-raster-svg-render.cc | 10 +-- 8 files changed, 84 insertions(+), 98 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index d05c462ee..9e99c76b9 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -29,18 +29,6 @@ #include "hb-raster-svg-base.hh" #include "hb-raster-svg-parse.hh" -bool -svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit) -{ - return s.eq_ascii_ci (lit); -} - -bool -svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit) -{ - return s.starts_with_ascii_ci (lit); -} - void svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) { @@ -78,41 +66,41 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) hb_svg_str_t prop_name = {name_start, (unsigned) (name_end - name_start)}; hb_svg_str_t prop_value = {value_start, (unsigned) (value_end - value_start)}; - if (svg_str_eq_ascii_ci (prop_name, "fill")) out->fill = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "fill-opacity")) out->fill_opacity = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "opacity")) out->opacity = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "transform")) out->transform = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "clip-path")) out->clip_path = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "display")) out->display = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "color")) out->color = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "visibility")) out->visibility = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "offset")) out->offset = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "stop-color")) out->stop_color = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "stop-opacity")) out->stop_opacity = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "spreadmethod") || - svg_str_eq_ascii_ci (prop_name, "spread-method")) out->spread_method = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "gradientunits") || - svg_str_eq_ascii_ci (prop_name, "gradient-units")) out->gradient_units = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "gradienttransform") || - svg_str_eq_ascii_ci (prop_name, "gradient-transform")) out->gradient_transform = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "x")) out->x = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "y")) out->y = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "width")) out->width = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "height")) out->height = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "cx")) out->cx = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "cy")) out->cy = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "r")) out->r = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "fx")) out->fx = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "fy")) out->fy = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "fr")) out->fr = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "rx")) out->rx = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "ry")) out->ry = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "x1")) out->x1 = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "y1")) out->y1 = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "x2")) out->x2 = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "y2")) out->y2 = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "points")) out->points = prop_value; - else if (svg_str_eq_ascii_ci (prop_name, "d")) out->d = prop_value; + if (prop_name.eq_ascii_ci ("fill")) out->fill = prop_value; + else if (prop_name.eq_ascii_ci ("fill-opacity")) out->fill_opacity = prop_value; + else if (prop_name.eq_ascii_ci ("opacity")) out->opacity = prop_value; + else if (prop_name.eq_ascii_ci ("transform")) out->transform = prop_value; + else if (prop_name.eq_ascii_ci ("clip-path")) out->clip_path = prop_value; + else if (prop_name.eq_ascii_ci ("display")) out->display = prop_value; + else if (prop_name.eq_ascii_ci ("color")) out->color = prop_value; + else if (prop_name.eq_ascii_ci ("visibility")) out->visibility = prop_value; + else if (prop_name.eq_ascii_ci ("offset")) out->offset = prop_value; + else if (prop_name.eq_ascii_ci ("stop-color")) out->stop_color = prop_value; + else if (prop_name.eq_ascii_ci ("stop-opacity")) out->stop_opacity = prop_value; + else if (prop_name.eq_ascii_ci ("spreadmethod") || + prop_name.eq_ascii_ci ("spread-method")) out->spread_method = prop_value; + else if (prop_name.eq_ascii_ci ("gradientunits") || + prop_name.eq_ascii_ci ("gradient-units")) out->gradient_units = prop_value; + else if (prop_name.eq_ascii_ci ("gradienttransform") || + prop_name.eq_ascii_ci ("gradient-transform")) out->gradient_transform = prop_value; + else if (prop_name.eq_ascii_ci ("x")) out->x = prop_value; + else if (prop_name.eq_ascii_ci ("y")) out->y = prop_value; + else if (prop_name.eq_ascii_ci ("width")) out->width = prop_value; + else if (prop_name.eq_ascii_ci ("height")) out->height = prop_value; + else if (prop_name.eq_ascii_ci ("cx")) out->cx = prop_value; + else if (prop_name.eq_ascii_ci ("cy")) out->cy = prop_value; + else if (prop_name.eq_ascii_ci ("r")) out->r = prop_value; + else if (prop_name.eq_ascii_ci ("fx")) out->fx = prop_value; + else if (prop_name.eq_ascii_ci ("fy")) out->fy = prop_value; + else if (prop_name.eq_ascii_ci ("fr")) out->fr = prop_value; + else if (prop_name.eq_ascii_ci ("rx")) out->rx = prop_value; + else if (prop_name.eq_ascii_ci ("ry")) out->ry = prop_value; + else if (prop_name.eq_ascii_ci ("x1")) out->x1 = prop_value; + else if (prop_name.eq_ascii_ci ("y1")) out->y1 = prop_value; + else if (prop_name.eq_ascii_ci ("x2")) out->x2 = prop_value; + else if (prop_name.eq_ascii_ci ("y2")) out->y2 = prop_value; + else if (prop_name.eq_ascii_ci ("points")) out->points = prop_value; + else if (prop_name.eq_ascii_ci ("d")) out->d = prop_value; if (p < end && *p == ';') p++; } @@ -161,7 +149,7 @@ hb_raster_svg_parse_id_ref (hb_svg_str_t s, return true; } - if (!svg_str_starts_with_ascii_ci (s, "url(")) + if (!s.starts_with_ascii_ci ("url(")) return false; const char *p = s.data + 4; @@ -235,7 +223,7 @@ hb_raster_svg_parse_local_id_ref (hb_svg_str_t s, return true; } - if (!svg_str_starts_with_ascii_ci (s, "url(")) + if (!s.starts_with_ascii_ci ("url(")) return false; hb_svg_str_t id; @@ -330,15 +318,15 @@ svg_align_offset (hb_svg_str_t align, char axis) { if (leftover <= 0.f) return 0.f; - if ((axis == 'x' && svg_str_starts_with_ascii_ci (align, "xMin")) || - (axis == 'y' && (svg_str_eq_ascii_ci (align, "xMinYMin") || - svg_str_eq_ascii_ci (align, "xMidYMin") || - svg_str_eq_ascii_ci (align, "xMaxYMin")))) + if ((axis == 'x' && align.starts_with_ascii_ci ("xMin")) || + (axis == 'y' && (align.eq_ascii_ci ("xMinYMin") || + align.eq_ascii_ci ("xMidYMin") || + align.eq_ascii_ci ("xMaxYMin")))) return 0.f; - if ((axis == 'x' && svg_str_starts_with_ascii_ci (align, "xMax")) || - (axis == 'y' && (svg_str_eq_ascii_ci (align, "xMinYMax") || - svg_str_eq_ascii_ci (align, "xMidYMax") || - svg_str_eq_ascii_ci (align, "xMaxYMax")))) + if ((axis == 'x' && align.starts_with_ascii_ci ("xMax")) || + (axis == 'y' && (align.eq_ascii_ci ("xMinYMax") || + align.eq_ascii_ci ("xMidYMax") || + align.eq_ascii_ci ("xMaxYMax")))) return leftover; return leftover * 0.5f; } @@ -357,7 +345,7 @@ hb_raster_svg_compute_viewbox_transform (float viewport_w, return false; hb_svg_str_t par = preserve_aspect_ratio.trim (); - if (svg_str_starts_with_ascii_ci (par, "defer")) + if (par.starts_with_ascii_ci ("defer")) { const char *p = par.data + 5; const char *end = par.data + par.len; @@ -375,13 +363,13 @@ hb_raster_svg_compute_viewbox_transform (float viewport_w, while (p < end && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; align = hb_svg_str_t (par.data, (unsigned) (p - par.data)).trim (); - if (svg_str_starts_with_ascii_ci (par, "none")) + if (par.starts_with_ascii_ci ("none")) is_none = true; - else if (svg_str_starts_with_ascii_ci (align, "x")) + else if (align.starts_with_ascii_ci ("x")) { const char *mode = p; while (mode < end && (*mode == ' ' || *mode == '\t' || *mode == '\n' || *mode == '\r')) mode++; - if (mode < end && svg_str_starts_with_ascii_ci (hb_svg_str_t (mode, (unsigned) (end - mode)), "slice")) + if (mode < end && hb_svg_str_t (mode, (unsigned) (end - mode)).starts_with_ascii_ci ("slice")) is_slice = true; } @@ -404,7 +392,7 @@ hb_raster_svg_compute_viewbox_transform (float viewport_w, float leftover_x = viewport_w - scaled_w; float leftover_y = viewport_h - scaled_h; - if (!svg_str_starts_with_ascii_ci (align, "x")) + if (!align.starts_with_ascii_ci ("x")) align = hb_svg_str_t ("xMidYMid", 8); t.xx = s; diff --git a/src/hb-raster-svg-base.hh b/src/hb-raster-svg-base.hh index e72e5d8f9..c7bdc9bd2 100644 --- a/src/hb-raster-svg-base.hh +++ b/src/hb-raster-svg-base.hh @@ -156,8 +156,6 @@ struct hb_svg_style_props_t struct hb_svg_xml_parser_t; struct hb_svg_transform_t; -HB_INTERNAL bool svg_str_eq_ascii_ci (hb_svg_str_t s, const char *lit); -HB_INTERNAL bool svg_str_starts_with_ascii_ci (hb_svg_str_t s, const char *lit); HB_INTERNAL void svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out); HB_INTERNAL float svg_parse_number_or_percent (hb_svg_str_t s, bool *is_percent); HB_INTERNAL hb_svg_str_t hb_raster_svg_find_href_attr (const hb_svg_xml_parser_t &parser); @@ -205,13 +203,13 @@ svg_parse_float_clamped01 (hb_svg_str_t s) static inline bool svg_str_is_inherit (hb_svg_str_t s) { - return svg_str_eq_ascii_ci (s.trim (), "inherit"); + return s.trim ().eq_ascii_ci ("inherit"); } static inline bool svg_str_is_none (hb_svg_str_t s) { - return svg_str_eq_ascii_ci (s.trim (), "none"); + return s.trim ().eq_ascii_ci ("none"); } static inline bool diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index ce3e31324..ca4ceeecb 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -109,15 +109,15 @@ svg_resolve_element_visibility (hb_svg_xml_parser_t &parser, svg_parse_style_props (parser.find_attr ("style"), &style_props); hb_svg_str_t display_str = svg_pick_attr_or_style (parser, style_props.display, "display"); hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, style_props.visibility, "visibility"); - if (svg_str_eq_ascii_ci (display_str.trim (), "none")) + if (display_str.trim ().eq_ascii_ci ("none")) return false; hb_svg_str_t vis_trim = visibility_str.trim (); - if (!vis_trim.len || svg_str_eq_ascii_ci (vis_trim, "inherit")) + if (!vis_trim.len || vis_trim.eq_ascii_ci ("inherit")) return parent_visible; - if (svg_str_eq_ascii_ci (vis_trim, "hidden") || - svg_str_eq_ascii_ci (vis_trim, "collapse")) + if (vis_trim.eq_ascii_ci ("hidden") || + vis_trim.eq_ascii_ci ("collapse")) return false; - if (svg_str_eq_ascii_ci (vis_trim, "visible")) + if (vis_trim.eq_ascii_ci ("visible")) return true; return parent_visible; } @@ -321,9 +321,9 @@ hb_raster_svg_process_clip_path_def (hb_svg_defs_t *defs, hb_svg_clip_path_def_t clip; hb_svg_str_t id = parser.find_attr ("id"); hb_svg_str_t units = parser.find_attr ("clipPathUnits").trim (); - if (svg_str_eq_ascii_ci (units, "objectBoundingBox")) + if (units.eq_ascii_ci ("objectBoundingBox")) clip.units_user_space = false; - else if (svg_str_eq_ascii_ci (units, "userSpaceOnUse")) + else if (units.eq_ascii_ci ("userSpaceOnUse")) clip.units_user_space = true; else clip.units_user_space = true; @@ -507,7 +507,7 @@ hb_raster_svg_push_clip_path_ref (hb_raster_paint_t *paint, { if (clip_path_str.is_null ()) return false; hb_svg_str_t trimmed = clip_path_str.trim (); - if (!trimmed.len || svg_str_eq_ascii_ci (trimmed, "none")) return false; + if (!trimmed.len || trimmed.eq_ascii_ci ("none")) return false; hb_svg_str_t clip_id; if (!hb_raster_svg_parse_local_id_ref (trimmed, &clip_id, nullptr)) diff --git a/src/hb-raster-svg-color.cc b/src/hb-raster-svg-color.cc index 4c64ddc99..191d2106e 100644 --- a/src/hb-raster-svg-color.cc +++ b/src/hb-raster-svg-color.cc @@ -216,17 +216,17 @@ hb_raster_svg_parse_color (hb_svg_str_t s, s = s.trim (); if (!s.len) { *is_none = true; return HB_COLOR (0, 0, 0, 0); } - if (svg_str_eq_ascii_ci (s, "none") || svg_str_eq_ascii_ci (s, "transparent")) + if (s.eq_ascii_ci ("none") || s.eq_ascii_ci ("transparent")) { *is_none = true; return HB_COLOR (0, 0, 0, 0); } - if (svg_str_eq_ascii_ci (s, "currentColor")) + if (s.eq_ascii_ci ("currentColor")) return foreground; /* var(--colorN) → CPAL palette color */ - if (svg_str_starts_with_ascii_ci (s, "var(")) + if (s.starts_with_ascii_ci ("var(")) { const char *p = s.data + 4; const char *e = s.data + s.len; diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index fc44da553..e464cc22c 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -104,7 +104,7 @@ svg_parse_paint_url_with_fallback (hb_svg_str_t s, hb_svg_str_t id; if (!hb_raster_svg_parse_local_id_ref (s, &id, fallback)) return false; - if (!svg_str_starts_with_ascii_ci (s.trim (), "url(")) + if (!s.trim ().starts_with_ascii_ci ("url(")) return false; if (out_id) *out_id = id; return id.len > 0; diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index ae7c5820a..81c2562b6 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -53,11 +53,11 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, hb_svg_str_t display_str = svg_pick_attr_or_style (parser, style_props.display, "display"); hb_svg_str_t visibility_str = svg_pick_attr_or_style (parser, style_props.visibility, "visibility"); - if (svg_str_eq_ascii_ci (display_str.trim (), "none")) + if (display_str.trim ().eq_ascii_ci ("none")) return true; hb_svg_str_t visibility_trim = visibility_str.trim (); - if (svg_str_eq_ascii_ci (visibility_trim, "hidden") || - svg_str_eq_ascii_ci (visibility_trim, "collapse")) + if (visibility_trim.eq_ascii_ci ("hidden") || + visibility_trim.eq_ascii_ci ("collapse")) return true; float offset = 0; @@ -71,7 +71,7 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser, bool is_current_color = false; if (color_str.len && !svg_str_is_inherit (color_str)) { - is_current_color = svg_str_eq_ascii_ci (color_str.trim (), "currentColor"); + is_current_color = color_str.trim ().eq_ascii_ci ("currentColor"); color = hb_raster_svg_parse_color (color_str, pfuncs, paint_data, foreground, face, palette, &is_none); } @@ -100,29 +100,29 @@ svg_parse_gradient_attrs (hb_svg_xml_parser_t &parser, svg_parse_style_props (parser.find_attr ("style"), &style_props); hb_svg_str_t spread_str = svg_pick_attr_or_style (parser, style_props.spread_method, "spreadMethod").trim (); - if (svg_str_eq_ascii_ci (spread_str, "reflect")) + if (spread_str.eq_ascii_ci ("reflect")) { grad.spread = HB_PAINT_EXTEND_REFLECT; grad.has_spread = true; } - else if (svg_str_eq_ascii_ci (spread_str, "repeat")) + else if (spread_str.eq_ascii_ci ("repeat")) { grad.spread = HB_PAINT_EXTEND_REPEAT; grad.has_spread = true; } - else if (svg_str_eq_ascii_ci (spread_str, "pad")) + else if (spread_str.eq_ascii_ci ("pad")) { grad.spread = HB_PAINT_EXTEND_PAD; grad.has_spread = true; } hb_svg_str_t units_str = svg_pick_attr_or_style (parser, style_props.gradient_units, "gradientUnits").trim (); - if (svg_str_eq_ascii_ci (units_str, "userSpaceOnUse")) + if (units_str.eq_ascii_ci ("userSpaceOnUse")) { grad.units_user_space = true; grad.has_units_user_space = true; } - else if (svg_str_eq_ascii_ci (units_str, "objectBoundingBox")) + else if (units_str.eq_ascii_ci ("objectBoundingBox")) { grad.units_user_space = false; grad.has_units_user_space = true; diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc index 8f2663ba2..213aeb7bf 100644 --- a/src/hb-raster-svg-parse.cc +++ b/src/hb-raster-svg-parse.cc @@ -52,7 +52,7 @@ hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) hb_svg_transform_t t; - if (svg_str_eq_ascii_ci (func_name, "matrix")) + if (func_name.eq_ascii_ci ("matrix")) { t.xx = fp.next_float (); t.yx = fp.next_float (); @@ -61,14 +61,14 @@ hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) t.dx = fp.next_float (); t.dy = fp.next_float (); } - else if (svg_str_eq_ascii_ci (func_name, "translate")) + else if (func_name.eq_ascii_ci ("translate")) { t.dx = fp.next_float (); fp.skip_ws_comma (); if (fp.p < fp.end && *fp.p != ')') t.dy = fp.next_float (); } - else if (svg_str_eq_ascii_ci (func_name, "scale")) + else if (func_name.eq_ascii_ci ("scale")) { t.xx = fp.next_float (); fp.skip_ws_comma (); @@ -77,7 +77,7 @@ hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) else t.yy = t.xx; } - else if (svg_str_eq_ascii_ci (func_name, "rotate")) + else if (func_name.eq_ascii_ci ("rotate")) { float angle = fp.next_float () * (float) M_PI / 180.f; float cs = cosf (angle), sn = sinf (angle); @@ -97,12 +97,12 @@ hb_raster_svg_parse_transform (hb_svg_str_t s, hb_svg_transform_t *out) t.xy = -sn; t.yy = cs; } } - else if (svg_str_eq_ascii_ci (func_name, "skewX")) + else if (func_name.eq_ascii_ci ("skewX")) { float angle = fp.next_float () * (float) M_PI / 180.f; t.xy = tanf (angle); } - else if (svg_str_eq_ascii_ci (func_name, "skewY")) + else if (func_name.eq_ascii_ci ("skewY")) { float angle = fp.next_float () * (float) M_PI / 180.f; t.yx = tanf (angle); diff --git a/src/hb-raster-svg-render.cc b/src/hb-raster-svg-render.cc index 25965de74..aa0dd4bdf 100644 --- a/src/hb-raster-svg-render.cc +++ b/src/hb-raster-svg-render.cc @@ -353,17 +353,17 @@ svg_render_element (hb_svg_render_context_t *ctx, transform_str = {}; state.color = inherited.color; bool is_none = false; - if (color_str.len && !svg_str_eq_ascii_ci (color_str.trim (), "inherit")) + if (color_str.len && !color_str.trim ().eq_ascii_ci ("inherit")) state.color = hb_raster_svg_parse_color (color_str, ctx->pfuncs, ctx->paint, inherited.color, hb_font_get_face (ctx->font), ctx->palette, &is_none); state.visibility = inherited.visibility; hb_svg_str_t visibility_trim = visibility_str.trim (); - if (visibility_trim.len && !svg_str_eq_ascii_ci (visibility_trim, "inherit")) - state.visibility = !(svg_str_eq_ascii_ci (visibility_trim, "hidden") || - svg_str_eq_ascii_ci (visibility_trim, "collapse")); + if (visibility_trim.len && !visibility_trim.eq_ascii_ci ("inherit")) + state.visibility = !(visibility_trim.eq_ascii_ci ("hidden") || + visibility_trim.eq_ascii_ci ("collapse")); - if (svg_str_eq_ascii_ci (display_str.trim (), "none")) + if (display_str.trim ().eq_ascii_ci ("none")) { if (!self_closing) svg_skip_subtree (parser); From 16256caf0b0ac0c7da86dd6448ef1fab9adfc3eb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:29:40 -0300 Subject: [PATCH 101/105] [raster] Validate and clamp radial gradient radii --- src/hb-raster-svg-fill.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index e464cc22c..2c9af7450 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -242,9 +242,23 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx, } else { + if (!isfinite (effective.r) || effective.r < 0.f) + { + if (effective.has_gradient_transform) + hb_paint_pop_transform (ctx->pfuncs, ctx->paint); + if (has_bbox_transform) + hb_paint_pop_transform (ctx->pfuncs, ctx->paint); + if (fallback_paint.len) + hb_raster_svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color); + return; + } + float fx = effective.has_fx ? effective.fx : effective.cx; float fy = effective.has_fy ? effective.fy : effective.cy; float fr = effective.has_fr ? effective.fr : 0.f; + if (!isfinite (fr) || fr < 0.f) + fr = 0.f; + fr = hb_min (fr, effective.r); hb_paint_radial_gradient (ctx->pfuncs, ctx->paint, &cl, fx, fy, fr, From 8fa382dd67a6cf6e5e30ae27816d8790131d48ed Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:29:50 -0300 Subject: [PATCH 102/105] [raster] Guard arc angle math against zero-length vectors --- src/hb-raster-svg-parse.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc index 213aeb7bf..de8b5ceae 100644 --- a/src/hb-raster-svg-parse.cc +++ b/src/hb-raster-svg-parse.cc @@ -214,6 +214,8 @@ svg_arc_endpoint_to_center (float x1, float y1, float x2, float y2, auto angle = [] (float ux, float uy, float vx, float vy) -> float { float dot = ux * vx + uy * vy; float len = sqrtf ((ux * ux + uy * uy) * (vx * vx + vy * vy)); + if (!(len > 0.f) || !isfinite (len)) + return 0.f; float a = acosf (hb_clamp (dot / len, -1.f, 1.f)); if (ux * vy - uy * vx < 0.f) a = -a; return a; From bbd0635b9ac376d782850b308ec89b521838c020 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:30:26 -0300 Subject: [PATCH 103/105] [raster] Fast-path common style properties in SVG style parser --- src/hb-raster-svg-base.cc | 91 ++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 35 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index 9e99c76b9..e83cca726 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -66,41 +66,62 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) hb_svg_str_t prop_name = {name_start, (unsigned) (name_end - name_start)}; hb_svg_str_t prop_value = {value_start, (unsigned) (value_end - value_start)}; - if (prop_name.eq_ascii_ci ("fill")) out->fill = prop_value; - else if (prop_name.eq_ascii_ci ("fill-opacity")) out->fill_opacity = prop_value; - else if (prop_name.eq_ascii_ci ("opacity")) out->opacity = prop_value; - else if (prop_name.eq_ascii_ci ("transform")) out->transform = prop_value; - else if (prop_name.eq_ascii_ci ("clip-path")) out->clip_path = prop_value; - else if (prop_name.eq_ascii_ci ("display")) out->display = prop_value; - else if (prop_name.eq_ascii_ci ("color")) out->color = prop_value; - else if (prop_name.eq_ascii_ci ("visibility")) out->visibility = prop_value; - else if (prop_name.eq_ascii_ci ("offset")) out->offset = prop_value; - else if (prop_name.eq_ascii_ci ("stop-color")) out->stop_color = prop_value; - else if (prop_name.eq_ascii_ci ("stop-opacity")) out->stop_opacity = prop_value; - else if (prop_name.eq_ascii_ci ("spreadmethod") || - prop_name.eq_ascii_ci ("spread-method")) out->spread_method = prop_value; - else if (prop_name.eq_ascii_ci ("gradientunits") || - prop_name.eq_ascii_ci ("gradient-units")) out->gradient_units = prop_value; - else if (prop_name.eq_ascii_ci ("gradienttransform") || - prop_name.eq_ascii_ci ("gradient-transform")) out->gradient_transform = prop_value; - else if (prop_name.eq_ascii_ci ("x")) out->x = prop_value; - else if (prop_name.eq_ascii_ci ("y")) out->y = prop_value; - else if (prop_name.eq_ascii_ci ("width")) out->width = prop_value; - else if (prop_name.eq_ascii_ci ("height")) out->height = prop_value; - else if (prop_name.eq_ascii_ci ("cx")) out->cx = prop_value; - else if (prop_name.eq_ascii_ci ("cy")) out->cy = prop_value; - else if (prop_name.eq_ascii_ci ("r")) out->r = prop_value; - else if (prop_name.eq_ascii_ci ("fx")) out->fx = prop_value; - else if (prop_name.eq_ascii_ci ("fy")) out->fy = prop_value; - else if (prop_name.eq_ascii_ci ("fr")) out->fr = prop_value; - else if (prop_name.eq_ascii_ci ("rx")) out->rx = prop_value; - else if (prop_name.eq_ascii_ci ("ry")) out->ry = prop_value; - else if (prop_name.eq_ascii_ci ("x1")) out->x1 = prop_value; - else if (prop_name.eq_ascii_ci ("y1")) out->y1 = prop_value; - else if (prop_name.eq_ascii_ci ("x2")) out->x2 = prop_value; - else if (prop_name.eq_ascii_ci ("y2")) out->y2 = prop_value; - else if (prop_name.eq_ascii_ci ("points")) out->points = prop_value; - else if (prop_name.eq_ascii_ci ("d")) out->d = prop_value; + bool handled = false; + if (prop_name.len) + { + switch (hb_svg_ascii_lower (prop_name.data[0])) + { + case 'f': + if (prop_name.eq_ascii_ci ("fill")) { out->fill = prop_value; handled = true; } + else if (prop_name.eq_ascii_ci ("fill-opacity")) { out->fill_opacity = prop_value; handled = true; } + break; + case 'o': + if (prop_name.eq_ascii_ci ("opacity")) { out->opacity = prop_value; handled = true; } + else if (prop_name.eq_ascii_ci ("offset")) { out->offset = prop_value; handled = true; } + break; + case 't': + if (prop_name.eq_ascii_ci ("transform")) { out->transform = prop_value; handled = true; } + break; + case 'c': + if (prop_name.eq_ascii_ci ("clip-path")) { out->clip_path = prop_value; handled = true; } + else if (prop_name.eq_ascii_ci ("color")) { out->color = prop_value; handled = true; } + break; + case 'd': + if (prop_name.eq_ascii_ci ("display")) { out->display = prop_value; handled = true; } + else if (prop_name.eq_ascii_ci ("d")) { out->d = prop_value; handled = true; } + break; + case 'v': + if (prop_name.eq_ascii_ci ("visibility")) { out->visibility = prop_value; handled = true; } + break; + default: + break; + } + } + if (!handled && prop_name.eq_ascii_ci ("stop-color")) out->stop_color = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("stop-opacity")) out->stop_opacity = prop_value; + else if (!handled && (prop_name.eq_ascii_ci ("spreadmethod") || + prop_name.eq_ascii_ci ("spread-method"))) out->spread_method = prop_value; + else if (!handled && (prop_name.eq_ascii_ci ("gradientunits") || + prop_name.eq_ascii_ci ("gradient-units"))) out->gradient_units = prop_value; + else if (!handled && (prop_name.eq_ascii_ci ("gradienttransform") || + prop_name.eq_ascii_ci ("gradient-transform"))) out->gradient_transform = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("x")) out->x = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("y")) out->y = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("width")) out->width = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("height")) out->height = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("cx")) out->cx = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("cy")) out->cy = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("r")) out->r = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("fx")) out->fx = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("fy")) out->fy = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("fr")) out->fr = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("rx")) out->rx = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("ry")) out->ry = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("x1")) out->x1 = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("y1")) out->y1 = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("x2")) out->x2 = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("y2")) out->y2 = prop_value; + else if (!handled && prop_name.eq_ascii_ci ("points")) out->points = prop_value; if (p < end && *p == ';') p++; } From 9a56516501b249d6cc02e8870a0f26eb0e9c4965 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:32:04 -0300 Subject: [PATCH 104/105] [raster] Group style property dispatch by first letter --- src/hb-raster-svg-base.cc | 99 ++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 42 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index e83cca726..f717a84ff 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -66,62 +66,77 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) hb_svg_str_t prop_name = {name_start, (unsigned) (name_end - name_start)}; hb_svg_str_t prop_value = {value_start, (unsigned) (value_end - value_start)}; - bool handled = false; if (prop_name.len) { switch (hb_svg_ascii_lower (prop_name.data[0])) { - case 'f': - if (prop_name.eq_ascii_ci ("fill")) { out->fill = prop_value; handled = true; } - else if (prop_name.eq_ascii_ci ("fill-opacity")) { out->fill_opacity = prop_value; handled = true; } - break; - case 'o': - if (prop_name.eq_ascii_ci ("opacity")) { out->opacity = prop_value; handled = true; } - else if (prop_name.eq_ascii_ci ("offset")) { out->offset = prop_value; handled = true; } - break; - case 't': - if (prop_name.eq_ascii_ci ("transform")) { out->transform = prop_value; handled = true; } - break; case 'c': - if (prop_name.eq_ascii_ci ("clip-path")) { out->clip_path = prop_value; handled = true; } - else if (prop_name.eq_ascii_ci ("color")) { out->color = prop_value; handled = true; } + if (prop_name.eq_ascii_ci ("clip-path")) out->clip_path = prop_value; + else if (prop_name.eq_ascii_ci ("color")) out->color = prop_value; + else if (prop_name.eq_ascii_ci ("cx")) out->cx = prop_value; + else if (prop_name.eq_ascii_ci ("cy")) out->cy = prop_value; break; case 'd': - if (prop_name.eq_ascii_ci ("display")) { out->display = prop_value; handled = true; } - else if (prop_name.eq_ascii_ci ("d")) { out->d = prop_value; handled = true; } + if (prop_name.eq_ascii_ci ("display")) out->display = prop_value; + else if (prop_name.eq_ascii_ci ("d")) out->d = prop_value; + break; + case 'f': + if (prop_name.eq_ascii_ci ("fill")) out->fill = prop_value; + else if (prop_name.eq_ascii_ci ("fill-opacity")) out->fill_opacity = prop_value; + else if (prop_name.eq_ascii_ci ("fx")) out->fx = prop_value; + else if (prop_name.eq_ascii_ci ("fy")) out->fy = prop_value; + else if (prop_name.eq_ascii_ci ("fr")) out->fr = prop_value; + break; + case 'g': + if (prop_name.eq_ascii_ci ("gradientunits") || + prop_name.eq_ascii_ci ("gradient-units")) out->gradient_units = prop_value; + else if (prop_name.eq_ascii_ci ("gradienttransform") || + prop_name.eq_ascii_ci ("gradient-transform")) out->gradient_transform = prop_value; + break; + case 'h': + if (prop_name.eq_ascii_ci ("height")) out->height = prop_value; + break; + case 'o': + if (prop_name.eq_ascii_ci ("opacity")) out->opacity = prop_value; + else if (prop_name.eq_ascii_ci ("offset")) out->offset = prop_value; + break; + case 'p': + if (prop_name.eq_ascii_ci ("points")) out->points = prop_value; + break; + case 'r': + if (prop_name.eq_ascii_ci ("r")) out->r = prop_value; + else if (prop_name.eq_ascii_ci ("rx")) out->rx = prop_value; + else if (prop_name.eq_ascii_ci ("ry")) out->ry = prop_value; + break; + case 's': + if (prop_name.eq_ascii_ci ("stop-color")) out->stop_color = prop_value; + else if (prop_name.eq_ascii_ci ("stop-opacity")) out->stop_opacity = prop_value; + else if (prop_name.eq_ascii_ci ("spreadmethod") || + prop_name.eq_ascii_ci ("spread-method")) out->spread_method = prop_value; + break; + case 't': + if (prop_name.eq_ascii_ci ("transform")) out->transform = prop_value; break; case 'v': - if (prop_name.eq_ascii_ci ("visibility")) { out->visibility = prop_value; handled = true; } + if (prop_name.eq_ascii_ci ("visibility")) out->visibility = prop_value; + break; + case 'w': + if (prop_name.eq_ascii_ci ("width")) out->width = prop_value; + break; + case 'x': + if (prop_name.eq_ascii_ci ("x")) out->x = prop_value; + else if (prop_name.eq_ascii_ci ("x1")) out->x1 = prop_value; + else if (prop_name.eq_ascii_ci ("x2")) out->x2 = prop_value; + break; + case 'y': + if (prop_name.eq_ascii_ci ("y")) out->y = prop_value; + else if (prop_name.eq_ascii_ci ("y1")) out->y1 = prop_value; + else if (prop_name.eq_ascii_ci ("y2")) out->y2 = prop_value; break; default: break; } } - if (!handled && prop_name.eq_ascii_ci ("stop-color")) out->stop_color = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("stop-opacity")) out->stop_opacity = prop_value; - else if (!handled && (prop_name.eq_ascii_ci ("spreadmethod") || - prop_name.eq_ascii_ci ("spread-method"))) out->spread_method = prop_value; - else if (!handled && (prop_name.eq_ascii_ci ("gradientunits") || - prop_name.eq_ascii_ci ("gradient-units"))) out->gradient_units = prop_value; - else if (!handled && (prop_name.eq_ascii_ci ("gradienttransform") || - prop_name.eq_ascii_ci ("gradient-transform"))) out->gradient_transform = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("x")) out->x = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("y")) out->y = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("width")) out->width = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("height")) out->height = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("cx")) out->cx = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("cy")) out->cy = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("r")) out->r = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("fx")) out->fx = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("fy")) out->fy = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("fr")) out->fr = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("rx")) out->rx = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("ry")) out->ry = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("x1")) out->x1 = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("y1")) out->y1 = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("x2")) out->x2 = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("y2")) out->y2 = prop_value; - else if (!handled && prop_name.eq_ascii_ci ("points")) out->points = prop_value; if (p < end && *p == ';') p++; } From 6367026d6b77c8f2abac2f88b6a18592ec91e8da Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 01:33:17 -0300 Subject: [PATCH 105/105] [raster] Add length guards to style property dispatch --- src/hb-raster-svg-base.cc | 70 +++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/hb-raster-svg-base.cc b/src/hb-raster-svg-base.cc index f717a84ff..641b005d5 100644 --- a/src/hb-raster-svg-base.cc +++ b/src/hb-raster-svg-base.cc @@ -71,67 +71,67 @@ svg_parse_style_props (hb_svg_str_t style, hb_svg_style_props_t *out) switch (hb_svg_ascii_lower (prop_name.data[0])) { case 'c': - if (prop_name.eq_ascii_ci ("clip-path")) out->clip_path = prop_value; - else if (prop_name.eq_ascii_ci ("color")) out->color = prop_value; - else if (prop_name.eq_ascii_ci ("cx")) out->cx = prop_value; - else if (prop_name.eq_ascii_ci ("cy")) out->cy = prop_value; + if (prop_name.len == 9 && prop_name.eq_ascii_ci ("clip-path")) out->clip_path = prop_value; + else if (prop_name.len == 5 && prop_name.eq_ascii_ci ("color")) out->color = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("cx")) out->cx = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("cy")) out->cy = prop_value; break; case 'd': - if (prop_name.eq_ascii_ci ("display")) out->display = prop_value; - else if (prop_name.eq_ascii_ci ("d")) out->d = prop_value; + if (prop_name.len == 7 && prop_name.eq_ascii_ci ("display")) out->display = prop_value; + else if (prop_name.len == 1 && prop_name.eq_ascii_ci ("d")) out->d = prop_value; break; case 'f': - if (prop_name.eq_ascii_ci ("fill")) out->fill = prop_value; - else if (prop_name.eq_ascii_ci ("fill-opacity")) out->fill_opacity = prop_value; - else if (prop_name.eq_ascii_ci ("fx")) out->fx = prop_value; - else if (prop_name.eq_ascii_ci ("fy")) out->fy = prop_value; - else if (prop_name.eq_ascii_ci ("fr")) out->fr = prop_value; + if (prop_name.len == 4 && prop_name.eq_ascii_ci ("fill")) out->fill = prop_value; + else if (prop_name.len == 12 && prop_name.eq_ascii_ci ("fill-opacity")) out->fill_opacity = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("fx")) out->fx = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("fy")) out->fy = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("fr")) out->fr = prop_value; break; case 'g': - if (prop_name.eq_ascii_ci ("gradientunits") || - prop_name.eq_ascii_ci ("gradient-units")) out->gradient_units = prop_value; - else if (prop_name.eq_ascii_ci ("gradienttransform") || - prop_name.eq_ascii_ci ("gradient-transform")) out->gradient_transform = prop_value; + if ((prop_name.len == 13 && prop_name.eq_ascii_ci ("gradientunits")) || + (prop_name.len == 14 && prop_name.eq_ascii_ci ("gradient-units"))) out->gradient_units = prop_value; + else if ((prop_name.len == 17 && prop_name.eq_ascii_ci ("gradienttransform")) || + (prop_name.len == 18 && prop_name.eq_ascii_ci ("gradient-transform"))) out->gradient_transform = prop_value; break; case 'h': - if (prop_name.eq_ascii_ci ("height")) out->height = prop_value; + if (prop_name.len == 6 && prop_name.eq_ascii_ci ("height")) out->height = prop_value; break; case 'o': - if (prop_name.eq_ascii_ci ("opacity")) out->opacity = prop_value; - else if (prop_name.eq_ascii_ci ("offset")) out->offset = prop_value; + if (prop_name.len == 7 && prop_name.eq_ascii_ci ("opacity")) out->opacity = prop_value; + else if (prop_name.len == 6 && prop_name.eq_ascii_ci ("offset")) out->offset = prop_value; break; case 'p': - if (prop_name.eq_ascii_ci ("points")) out->points = prop_value; + if (prop_name.len == 6 && prop_name.eq_ascii_ci ("points")) out->points = prop_value; break; case 'r': - if (prop_name.eq_ascii_ci ("r")) out->r = prop_value; - else if (prop_name.eq_ascii_ci ("rx")) out->rx = prop_value; - else if (prop_name.eq_ascii_ci ("ry")) out->ry = prop_value; + if (prop_name.len == 1 && prop_name.eq_ascii_ci ("r")) out->r = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("rx")) out->rx = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("ry")) out->ry = prop_value; break; case 's': - if (prop_name.eq_ascii_ci ("stop-color")) out->stop_color = prop_value; - else if (prop_name.eq_ascii_ci ("stop-opacity")) out->stop_opacity = prop_value; - else if (prop_name.eq_ascii_ci ("spreadmethod") || - prop_name.eq_ascii_ci ("spread-method")) out->spread_method = prop_value; + if (prop_name.len == 10 && prop_name.eq_ascii_ci ("stop-color")) out->stop_color = prop_value; + else if (prop_name.len == 12 && prop_name.eq_ascii_ci ("stop-opacity")) out->stop_opacity = prop_value; + else if ((prop_name.len == 12 && prop_name.eq_ascii_ci ("spreadmethod")) || + (prop_name.len == 13 && prop_name.eq_ascii_ci ("spread-method"))) out->spread_method = prop_value; break; case 't': - if (prop_name.eq_ascii_ci ("transform")) out->transform = prop_value; + if (prop_name.len == 9 && prop_name.eq_ascii_ci ("transform")) out->transform = prop_value; break; case 'v': - if (prop_name.eq_ascii_ci ("visibility")) out->visibility = prop_value; + if (prop_name.len == 10 && prop_name.eq_ascii_ci ("visibility")) out->visibility = prop_value; break; case 'w': - if (prop_name.eq_ascii_ci ("width")) out->width = prop_value; + if (prop_name.len == 5 && prop_name.eq_ascii_ci ("width")) out->width = prop_value; break; case 'x': - if (prop_name.eq_ascii_ci ("x")) out->x = prop_value; - else if (prop_name.eq_ascii_ci ("x1")) out->x1 = prop_value; - else if (prop_name.eq_ascii_ci ("x2")) out->x2 = prop_value; + if (prop_name.len == 1 && prop_name.eq_ascii_ci ("x")) out->x = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("x1")) out->x1 = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("x2")) out->x2 = prop_value; break; case 'y': - if (prop_name.eq_ascii_ci ("y")) out->y = prop_value; - else if (prop_name.eq_ascii_ci ("y1")) out->y1 = prop_value; - else if (prop_name.eq_ascii_ci ("y2")) out->y2 = prop_value; + if (prop_name.len == 1 && prop_name.eq_ascii_ci ("y")) out->y = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("y1")) out->y1 = prop_value; + else if (prop_name.len == 2 && prop_name.eq_ascii_ci ("y2")) out->y2 = prop_value; break; default: break;