From b8f607cbae5b9d24a34f7ffc5d82b3d627fafd6f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 00:01:54 -0300 Subject: [PATCH] [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',