[raster] Add SVG table rasterizer

Implement a minimal SVG parser/renderer that translates OpenType SVG
table documents into paint commands through the existing hb_paint_funcs_t
interface. All pixel-level rendering (gradients, compositing, clipping)
is handled by the existing hb_raster_paint_t infrastructure.

Features:
- XML tokenizer and SVG path data parser (all commands including arcs)
- Shape-to-path converters (rect, circle, ellipse, line, polyline, polygon)
- Gradient support (linearGradient, radialGradient with stops and transforms)
- SVG color parser (#RGB, #RRGGBB, rgb(), named colors, currentColor)
- CPAL palette colors via var(--colorN) with fallback support
- Transform parser (matrix, translate, scale, rotate, skewX, skewY)
- Defs/use resolution, clipPath support, group opacity
- Hook into hb-raster utility for SVG font rendering

New files: hb-raster-svg.cc, hb-raster-svg.hh, hb-raster-paint.hh

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-02-25 11:10:35 -03:00
1 parent 6d23a8a211
commit 291bc6e1f2
6 files changed
+2578 -166

No files matched your search

+157 -165
View File
@@ -26,174 +26,12 @@
#include "hb.hh"
#include "hb-raster-image.hh"
#include "hb-geometry.hh"
#include "hb-raster-paint.hh"
#include "hb-raster-svg.hh"
#include "hb-machinery.hh"
#include <math.h>
/* hb_raster_clip_t — alpha mask for clipping */
struct hb_raster_clip_t
{
hb_vector_t<uint8_t> alpha; /* A8 mask, same extents as root surface */
unsigned width = 0;
unsigned height = 0;
unsigned stride = 0;
/* Fast path: simple rectangle (no alpha buffer needed) */
bool is_rect = true;
int rect_x0 = 0, rect_y0 = 0;
int rect_x1 = 0, rect_y1 = 0;
/* Bounding box of non-zero alpha region (valid for both rect and mask) */
unsigned min_x = 0, min_y = 0;
unsigned max_x = 0, max_y = 0;
void init_full (unsigned w, unsigned h)
{
width = w;
height = h;
stride = (w + 3u) & ~3u;
is_rect = true;
rect_x0 = 0;
rect_y0 = 0;
rect_x1 = (int) w;
rect_y1 = (int) h;
min_x = 0;
min_y = 0;
max_x = w;
max_y = h;
}
void update_bounds_from_rect ()
{
min_x = (unsigned) hb_max (rect_x0, 0);
min_y = (unsigned) hb_max (rect_y0, 0);
max_x = (unsigned) hb_max (hb_min (rect_x1, (int) width), 0);
max_y = (unsigned) hb_max (hb_min (rect_y1, (int) height), 0);
}
uint8_t get_alpha (unsigned x, unsigned y) const
{
if (is_rect)
return ((int) x >= rect_x0 && (int) x < rect_x1 &&
(int) y >= rect_y0 && (int) y < rect_y1) ? 255 : 0;
if (x >= width || y >= height) return 0;
return alpha[y * stride + x];
}
};
/* hb_raster_paint_t — color glyph paint context */
struct hb_raster_paint_t
{
hb_object_header_t header;
/* Configuration */
hb_transform_t<> base_transform = {1, 0, 0, 1, 0, 0};
float x_scale_factor = 1.f;
float y_scale_factor = 1.f;
hb_raster_extents_t fixed_extents = {};
bool has_extents = false;
hb_color_t foreground = HB_COLOR (0, 0, 0, 255);
/* Stacks */
hb_vector_t<hb_transform_t<>> transform_stack;
hb_vector_t<hb_raster_clip_t> clip_stack;
hb_vector_t<hb_raster_clip_t> clip_cache;
hb_vector_t<hb_raster_image_t *> surface_stack;
/* Cached surface pool (freelist for reuse across push/pop group) */
hb_vector_t<hb_raster_image_t *> surface_cache;
hb_vector_t<hb_color_stop_t> scratch_color_stops;
/* Internal rasterizer for clip-to-glyph */
hb_raster_draw_t *clip_rdr = nullptr;
/* Helpers */
hb_raster_image_t *acquire_surface ()
{
hb_raster_image_t *img;
if (surface_cache.length)
img = surface_cache.pop ();
else
{
img = hb_raster_image_create_or_fail ();
if (unlikely (!img)) return nullptr;
}
if (unlikely (!img->configure (HB_RASTER_FORMAT_BGRA32, fixed_extents)))
{
hb_raster_image_destroy (img);
return nullptr;
}
img->clear ();
return img;
}
void release_surface (hb_raster_image_t *img)
{
surface_cache.push (img);
}
hb_raster_clip_t acquire_clip (unsigned w, unsigned h)
{
hb_raster_clip_t clip;
if (clip_cache.length)
clip = clip_cache.pop ();
clip.width = w;
clip.height = h;
clip.stride = (w + 3u) & ~3u;
clip.is_rect = false;
return clip;
}
void release_clip (hb_raster_clip_t &&clip)
{
if (clip.alpha.arrayZ)
clip_cache.push (std::move (clip));
}
void release_all_clips ()
{
while (clip_stack.length)
release_clip (clip_stack.pop ());
}
hb_raster_image_t *current_surface ()
{
return surface_stack.length ? surface_stack.tail () : nullptr;
}
hb_raster_clip_t &current_clip ()
{
return clip_stack.tail ();
}
hb_transform_t<> &current_transform ()
{
return transform_stack.tail ();
}
void apply_scale_factor (hb_transform_t<> &t) const
{
t.xx /= x_scale_factor;
t.xy /= x_scale_factor;
t.x0 /= x_scale_factor;
t.yx /= y_scale_factor;
t.yy /= y_scale_factor;
t.y0 /= y_scale_factor;
}
hb_transform_t<> current_effective_transform ()
{
hb_transform_t<> t = current_transform ();
apply_scale_factor (t);
return t;
}
};
/*
* Pixel helpers (paint-specific)
@@ -427,6 +265,152 @@ hb_raster_paint_push_clip_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED,
c->clip_stack.push (std::move (new_clip));
}
/* Push clip from arbitrary path emitter (used by SVG rasterizer).
* Identical to push_clip_glyph but calls user func instead of hb_font_draw_glyph. */
void
hb_raster_paint_push_clip_path (hb_raster_paint_t *c,
hb_raster_svg_path_func_t func,
void *user_data)
{
ensure_initialized (c);
hb_raster_image_t *surf = c->current_surface ();
if (unlikely (!surf)) return;
unsigned w = surf->extents.width;
unsigned h = surf->extents.height;
hb_raster_clip_t new_clip = c->acquire_clip (w, h);
hb_raster_draw_t *rdr = c->clip_rdr;
hb_transform_t<> t = c->current_effective_transform ();
hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0);
func (hb_raster_draw_get_funcs (), rdr, user_data);
hb_raster_image_t *mask_img = hb_raster_draw_render (rdr);
if (unlikely (!mask_img))
{
new_clip.init_full (w, h);
new_clip.is_rect = true;
new_clip.rect_x0 = new_clip.rect_y0 = 0;
new_clip.rect_x1 = new_clip.rect_y1 = 0;
new_clip.min_x = new_clip.min_y = new_clip.max_x = new_clip.max_y = 0;
c->clip_stack.push (std::move (new_clip));
return;
}
if (unlikely (!new_clip.alpha.resize (new_clip.stride * h)))
{
hb_raster_draw_recycle_image (rdr, mask_img);
new_clip.init_full (w, h);
new_clip.is_rect = true;
new_clip.rect_x0 = new_clip.rect_y0 = 0;
new_clip.rect_x1 = new_clip.rect_y1 = 0;
new_clip.min_x = new_clip.min_y = new_clip.max_x = new_clip.max_y = 0;
c->clip_stack.push (std::move (new_clip));
return;
}
const uint8_t *mask_buf = hb_raster_image_get_buffer (mask_img);
hb_raster_extents_t mask_ext;
hb_raster_image_get_extents (mask_img, &mask_ext);
const hb_raster_clip_t &old_clip = c->current_clip ();
int mask_x0 = mask_ext.x_origin - surf->extents.x_origin;
int mask_y0 = mask_ext.y_origin - surf->extents.y_origin;
int mask_x1 = mask_x0 + (int) mask_ext.width;
int mask_y1 = mask_y0 + (int) mask_ext.height;
int ix0_i = hb_max ((int) old_clip.min_x, hb_max (mask_x0, 0));
int iy0_i = hb_max ((int) old_clip.min_y, hb_max (mask_y0, 0));
int ix1_i = hb_min ((int) old_clip.max_x, hb_min (mask_x1, (int) w));
int iy1_i = hb_min ((int) old_clip.max_y, hb_min (mask_y1, (int) h));
if (ix0_i >= ix1_i || iy0_i >= iy1_i)
{
hb_raster_draw_recycle_image (rdr, mask_img);
new_clip.init_full (w, h);
new_clip.is_rect = true;
new_clip.rect_x0 = new_clip.rect_y0 = 0;
new_clip.rect_x1 = new_clip.rect_y1 = 0;
new_clip.min_x = new_clip.min_y = new_clip.max_x = new_clip.max_y = 0;
c->clip_stack.push (std::move (new_clip));
return;
}
unsigned ix0 = (unsigned) ix0_i;
unsigned iy0 = (unsigned) iy0_i;
unsigned ix1 = (unsigned) ix1_i;
unsigned iy1 = (unsigned) iy1_i;
new_clip.min_x = w; new_clip.min_y = h;
new_clip.max_x = 0; new_clip.max_y = 0;
if (old_clip.is_rect)
{
for (unsigned y = iy0; y < iy1; y++)
{
const uint8_t *mask_row = mask_buf + (unsigned) ((int) y - mask_y0) * mask_ext.stride;
uint8_t *out_row = new_clip.alpha.arrayZ + y * new_clip.stride;
unsigned row_min = ix1;
unsigned row_max = ix0;
unsigned mx = (unsigned) ((int) ix0 - mask_x0);
for (unsigned x = ix0; x < ix1; x++)
{
uint8_t a = mask_row[mx++];
out_row[x] = a;
if (a && row_min == ix1)
{
row_min = x;
row_max = x + 1;
}
else if (a)
row_max = x + 1;
}
if (row_min < row_max)
{
new_clip.min_x = hb_min (new_clip.min_x, row_min);
new_clip.min_y = hb_min (new_clip.min_y, y);
new_clip.max_x = hb_max (new_clip.max_x, row_max);
new_clip.max_y = hb_max (new_clip.max_y, y + 1);
}
}
}
else
{
for (unsigned y = iy0; y < iy1; y++)
{
const uint8_t *old_row = old_clip.alpha.arrayZ + y * old_clip.stride;
const uint8_t *mask_row = mask_buf + (unsigned) ((int) y - mask_y0) * mask_ext.stride;
uint8_t *out_row = new_clip.alpha.arrayZ + y * new_clip.stride;
unsigned row_min = ix1;
unsigned row_max = ix0;
for (unsigned x = ix0; x < ix1; x++)
{
unsigned mx = (unsigned) ((int) x - mask_x0);
uint8_t a = hb_raster_div255 (mask_row[mx] * old_row[x]);
out_row[x] = a;
if (a)
{
row_min = hb_min (row_min, x);
row_max = x + 1;
}
}
if (row_min < row_max)
{
new_clip.min_x = hb_min (new_clip.min_x, row_min);
new_clip.min_y = hb_min (new_clip.min_y, y);
new_clip.max_x = hb_max (new_clip.max_x, row_max);
new_clip.max_y = hb_max (new_clip.max_y, y + 1);
}
}
}
hb_raster_draw_recycle_image (rdr, mask_img);
c->clip_stack.push (std::move (new_clip));
}
static void
hb_raster_paint_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED,
void *paint_data,
@@ -723,7 +707,11 @@ hb_raster_paint_image (hb_paint_funcs_t *pfuncs HB_UNUSED,
ensure_initialized (c);
/* Only handle raw BGRA32 for now */
/* Handle SVG format */
if (format == HB_PAINT_IMAGE_FORMAT_SVG)
return hb_raster_svg_render (c, blob, c->svg_glyph, c->svg_font, c->foreground);
/* Only handle raw BGRA32 otherwise */
if (format != HB_TAG ('B','G','R','A'))
return false;
@@ -2033,9 +2021,13 @@ hb_raster_paint_glyph (hb_raster_paint_t *paint,
}
hb_raster_paint_set_transform (paint, xx, yx, xy, yy, tx, ty);
paint->svg_glyph = glyph;
paint->svg_font = font;
hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph,
hb_raster_paint_get_funcs (), paint,
palette, foreground);
paint->svg_glyph = 0;
paint->svg_font = nullptr;
hb_raster_paint_set_transform (paint, xx, yx, xy, yy, dx, dy);
return ret;
}
+202
View File
@@ -0,0 +1,202 @@
/*
* 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_PAINT_HH
#define HB_RASTER_PAINT_HH
#include "hb.hh"
#include "hb-raster-image.hh"
#include "hb-geometry.hh"
/* hb_raster_clip_t — alpha mask for clipping */
struct hb_raster_clip_t
{
hb_vector_t<uint8_t> alpha; /* A8 mask, same extents as root surface */
unsigned width = 0;
unsigned height = 0;
unsigned stride = 0;
/* Fast path: simple rectangle (no alpha buffer needed) */
bool is_rect = true;
int rect_x0 = 0, rect_y0 = 0;
int rect_x1 = 0, rect_y1 = 0;
/* Bounding box of non-zero alpha region (valid for both rect and mask) */
unsigned min_x = 0, min_y = 0;
unsigned max_x = 0, max_y = 0;
void init_full (unsigned w, unsigned h)
{
width = w;
height = h;
stride = (w + 3u) & ~3u;
is_rect = true;
rect_x0 = 0;
rect_y0 = 0;
rect_x1 = (int) w;
rect_y1 = (int) h;
min_x = 0;
min_y = 0;
max_x = w;
max_y = h;
}
void update_bounds_from_rect ()
{
min_x = (unsigned) hb_max (rect_x0, 0);
min_y = (unsigned) hb_max (rect_y0, 0);
max_x = (unsigned) hb_max (hb_min (rect_x1, (int) width), 0);
max_y = (unsigned) hb_max (hb_min (rect_y1, (int) height), 0);
}
uint8_t get_alpha (unsigned x, unsigned y) const
{
if (is_rect)
return ((int) x >= rect_x0 && (int) x < rect_x1 &&
(int) y >= rect_y0 && (int) y < rect_y1) ? 255 : 0;
if (x >= width || y >= height) return 0;
return alpha[y * stride + x];
}
};
/* hb_raster_paint_t — color glyph paint context */
struct hb_raster_paint_t
{
hb_object_header_t header;
/* Configuration */
hb_transform_t<> base_transform = {1, 0, 0, 1, 0, 0};
float x_scale_factor = 1.f;
float y_scale_factor = 1.f;
hb_raster_extents_t fixed_extents = {};
bool has_extents = false;
hb_color_t foreground = HB_COLOR (0, 0, 0, 255);
/* SVG rendering state */
hb_codepoint_t svg_glyph = 0;
hb_font_t *svg_font = nullptr;
/* Stacks */
hb_vector_t<hb_transform_t<>> transform_stack;
hb_vector_t<hb_raster_clip_t> clip_stack;
hb_vector_t<hb_raster_clip_t> clip_cache;
hb_vector_t<hb_raster_image_t *> surface_stack;
/* Cached surface pool (freelist for reuse across push/pop group) */
hb_vector_t<hb_raster_image_t *> surface_cache;
hb_vector_t<hb_color_stop_t> scratch_color_stops;
/* Internal rasterizer for clip-to-glyph */
hb_raster_draw_t *clip_rdr = nullptr;
/* Helpers */
hb_raster_image_t *acquire_surface ()
{
hb_raster_image_t *img;
if (surface_cache.length)
img = surface_cache.pop ();
else
{
img = hb_raster_image_create_or_fail ();
if (unlikely (!img)) return nullptr;
}
if (unlikely (!img->configure (HB_RASTER_FORMAT_BGRA32, fixed_extents)))
{
hb_raster_image_destroy (img);
return nullptr;
}
img->clear ();
return img;
}
void release_surface (hb_raster_image_t *img)
{
surface_cache.push (img);
}
hb_raster_clip_t acquire_clip (unsigned w, unsigned h)
{
hb_raster_clip_t clip;
if (clip_cache.length)
clip = clip_cache.pop ();
clip.width = w;
clip.height = h;
clip.stride = (w + 3u) & ~3u;
clip.is_rect = false;
return clip;
}
void release_clip (hb_raster_clip_t &&clip)
{
if (clip.alpha.arrayZ)
clip_cache.push (std::move (clip));
}
void release_all_clips ()
{
while (clip_stack.length)
release_clip (clip_stack.pop ());
}
hb_raster_image_t *current_surface ()
{
return surface_stack.length ? surface_stack.tail () : nullptr;
}
hb_raster_clip_t &current_clip ()
{
return clip_stack.tail ();
}
hb_transform_t<> &current_transform ()
{
return transform_stack.tail ();
}
void apply_scale_factor (hb_transform_t<> &t) const
{
t.xx /= x_scale_factor;
t.xy /= x_scale_factor;
t.x0 /= x_scale_factor;
t.yx /= y_scale_factor;
t.yy /= y_scale_factor;
t.y0 /= y_scale_factor;
}
hb_transform_t<> current_effective_transform ()
{
hb_transform_t<> t = current_transform ();
apply_scale_factor (t);
return t;
}
};
#endif /* HB_RASTER_PAINT_HH */
+2147
View File
File diff suppressed because it is too large. Load diff
+54
View File
@@ -0,0 +1,54 @@
/*
* 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_HH
#define HB_RASTER_SVG_HH
#include "hb.hh"
struct hb_raster_paint_t;
/* Callback type: emits SVG path draw commands to the given draw funcs */
typedef void (*hb_raster_svg_path_func_t) (hb_draw_funcs_t *dfuncs,
void *draw_data,
void *user_data);
/* Push clip from arbitrary path (reuses push_clip_glyph logic) */
HB_INTERNAL void
hb_raster_paint_push_clip_path (hb_raster_paint_t *c,
hb_raster_svg_path_func_t func,
void *user_data);
/* Render SVG document for a specific glyph */
HB_INTERNAL hb_bool_t
hb_raster_svg_render (hb_raster_paint_t *paint,
hb_blob_t *blob,
hb_codepoint_t glyph,
hb_font_t *font,
hb_color_t foreground);
#endif /* HB_RASTER_SVG_HH */
+3
View File
@@ -706,6 +706,9 @@ hb_raster_sources = [
'hb-raster-utils.hh',
'hb-raster-draw.cc',
'hb-raster-paint.cc',
'hb-raster-paint.hh',
'hb-raster-svg.cc',
'hb-raster-svg.hh',
'hb-static.cc',
]
+15 -1
View File
@@ -80,7 +80,8 @@ struct raster_output_t : output_options_t<true>
hb_face_t *face = hb_font_get_face (font);
has_color = hb_ot_color_has_paint (face) ||
hb_ot_color_has_layers (face);
hb_ot_color_has_layers (face) ||
hb_ot_color_has_svg (face);
/* Parse foreground / background colors */
{
@@ -248,6 +249,19 @@ struct raster_output_t : output_options_t<true>
if (!hb_font_get_glyph_extents (font, g.gid, &gext))
continue;
/* SVG glyphs may report zero extents; fall back to advance + font extents. */
if (!gext.width && !gext.height)
{
hb_font_extents_t fext;
hb_font_get_extents_for_direction (font, direction, &fext);
hb_position_t adv_x, adv_y;
hb_font_get_glyph_advance_for_direction (font, g.gid, direction, &adv_x, &adv_y);
gext.x_bearing = 0;
gext.y_bearing = fext.ascender;
gext.width = adv_x ? adv_x : (hb_position_t) step;
gext.height = -(hb_position_t) step;
}
float gx = (g.x + off_x) * sx;
float gy = (g.y + off_y) * sy;