From 49838acdadc60971c6da1626301b4a0a02adeec2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 22 Feb 2026 01:24:44 -0300 Subject: [PATCH] raster: add hb_raster_paint_t for color glyph rendering Implement a software paint backend (hb_raster_paint_t) that renders COLRv0/v1 color glyphs into BGRA32 pixel buffers with no external dependencies. Implements all 14 hb_paint_funcs_t callbacks including solid fills, linear/radial/sweep gradients, glyph clipping, rectangle clipping, group compositing, and BGRA image blitting. Update hb-raster utility to detect color fonts and use the paint path, outputting PPM (RGB) instead of PGM (greyscale). Co-Authored-By: Claude Opus 4.6 --- src/hb-raster-paint.cc | 1285 ++++++++++++++++++++++++++++++++++++++++ src/hb-raster-paint.hh | 146 +++++ src/hb-raster.h | 58 ++ src/meson.build | 2 + util/raster-output.hh | 201 ++++++- 5 files changed, 1667 insertions(+), 25 deletions(-) create mode 100644 src/hb-raster-paint.cc create mode 100644 src/hb-raster-paint.hh diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc new file mode 100644 index 000000000..6ad0036ed --- /dev/null +++ b/src/hb-raster-paint.cc @@ -0,0 +1,1285 @@ +/* + * 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-paint.hh" +#include "hb-machinery.hh" + +#include + + +/* + * Pixel helpers + */ + +static inline uint8_t +div255 (unsigned a) +{ + return (uint8_t) ((a + 128 + ((a + 128) >> 8)) >> 8); +} + +/* SRC_OVER: premultiplied src over premultiplied dst. */ +static inline uint32_t +src_over (uint32_t src, uint32_t dst) +{ + uint8_t sa = (uint8_t) (src >> 24); + if (sa == 255) return src; + if (sa == 0) return dst; + unsigned inv_sa = 255 - sa; + uint8_t rb = div255 ((dst & 0xFF) * inv_sa) + (uint8_t) (src & 0xFF); + uint8_t rg = div255 (((dst >> 8) & 0xFF) * inv_sa) + (uint8_t) ((src >> 8) & 0xFF); + uint8_t rr = div255 (((dst >> 16) & 0xFF) * inv_sa) + (uint8_t) ((src >> 16) & 0xFF); + uint8_t ra = div255 (((dst >> 24) & 0xFF) * inv_sa) + sa; + return (uint32_t) rb | ((uint32_t) rg << 8) | ((uint32_t) rr << 16) | ((uint32_t) ra << 24); +} + +/* Scale a premultiplied pixel by an alpha [0,255]. */ +static inline uint32_t +alpha_mul (uint32_t px, unsigned a) +{ + if (a == 255) return px; + if (a == 0) return 0; + uint8_t rb = div255 ((px & 0xFF) * a); + uint8_t rg = div255 (((px >> 8) & 0xFF) * a); + uint8_t rr = div255 (((px >> 16) & 0xFF) * a); + uint8_t ra = div255 (((px >> 24) & 0xFF) * a); + return (uint32_t) rb | ((uint32_t) rg << 8) | ((uint32_t) rr << 16) | ((uint32_t) ra << 24); +} + +/* Convert unpremultiplied hb_color_t (BGRA order) to premultiplied BGRA32 pixel. */ +static inline uint32_t +color_to_premul_pixel (hb_color_t color) +{ + uint8_t a = hb_color_get_alpha (color); + uint8_t r = div255 (hb_color_get_red (color) * a); + uint8_t g = div255 (hb_color_get_green (color) * a); + uint8_t b = div255 (hb_color_get_blue (color) * a); + return (uint32_t) b | ((uint32_t) g << 8) | ((uint32_t) r << 16) | ((uint32_t) a << 24); +} + + +/* + * Image compositing (operates on hb_raster_image_t) + */ + +/* Composite src image onto dst image using SRC_OVER. + * Both images must have the same extents and BGRA32 format. */ +static void +composite_src_over (hb_raster_image_t *dst, + const hb_raster_image_t *src) +{ + unsigned w = dst->extents.width; + unsigned h = dst->extents.height; + unsigned stride = dst->extents.stride; + + for (unsigned y = 0; y < h; y++) + { + uint32_t *dp = reinterpret_cast (dst->buffer.arrayZ + y * stride); + const uint32_t *sp = reinterpret_cast (src->buffer.arrayZ + y * stride); + for (unsigned x = 0; x < w; x++) + dp[x] = src_over (sp[x], dp[x]); + } +} + +/* TODO: implement all 28 blend modes from hb_paint_composite_mode_t. + * For now, fall back to SRC_OVER for everything. */ +static void +composite_images (hb_raster_image_t *dst, + const hb_raster_image_t *src, + hb_paint_composite_mode_t mode) +{ + /* TODO: implement all 28 blend modes. For now only a few key ones. */ + if (mode == HB_PAINT_COMPOSITE_MODE_CLEAR) + { + unsigned stride = dst->extents.stride; + unsigned h = dst->extents.height; + /* Clear dst where src has alpha */ + for (unsigned y = 0; y < h; y++) + { + uint32_t *dp = reinterpret_cast (dst->buffer.arrayZ + y * stride); + const uint32_t *sp = reinterpret_cast (src->buffer.arrayZ + y * stride); + for (unsigned x = 0; x < dst->extents.width; x++) + if (sp[x] >> 24) + dp[x] = 0; + } + } + else if (mode == HB_PAINT_COMPOSITE_MODE_SRC) + { + unsigned stride = dst->extents.stride; + unsigned h = dst->extents.height; + memcpy (dst->buffer.arrayZ, src->buffer.arrayZ, stride * h); + } + else if (mode == HB_PAINT_COMPOSITE_MODE_DEST) + { + /* No-op: destination unchanged */ + } + else + { + /* Default: SRC_OVER (covers SRC_OVER and all unimplemented modes) */ + composite_src_over (dst, src); + } +} + + +/* + * Paint callbacks + */ + +/* Lazy initialization: set up root surface, initial clip and transform. + * Called from every paint callback that needs state. + * hb_font_paint_glyph() does NOT wrap with push/pop_transform, + * so the first callback could be push_clip_glyph or paint_color. */ +static void +ensure_initialized (hb_raster_paint_t *c) +{ + if (c->surface_stack.length) return; + + /* Root surface */ + hb_raster_image_t *root = c->acquire_surface (); + if (unlikely (!root)) return; + c->surface_stack.push (root); + + /* Initial transform */ + c->transform_stack.push (c->base_transform); + + /* Initial clip: full coverage rectangle */ + hb_raster_clip_t clip; + clip.init_full (c->fixed_extents.width, c->fixed_extents.height); + c->clip_stack.push (clip); +} + +static void +hb_raster_paint_push_transform (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + float xx, float yx, + float xy, float yy, + float dx, float dy, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + ensure_initialized (c); + + hb_transform_t<> t = c->current_transform (); + t.multiply ({xx, yx, xy, yy, dx, dy}); + c->transform_stack.push (t); +} + +static void +hb_raster_paint_pop_transform (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + c->transform_stack.pop (); +} + +static hb_bool_t +hb_raster_paint_color_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data HB_UNUSED, + hb_codepoint_t glyph HB_UNUSED, + hb_font_t *font HB_UNUSED, + void *user_data HB_UNUSED) +{ + return false; +} + +static void +hb_raster_paint_push_clip_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + hb_codepoint_t glyph, + hb_font_t *font, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_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; + new_clip.width = w; + new_clip.height = h; + new_clip.stride = (w + 3u) & ~3u; + new_clip.is_rect = false; + + /* Rasterize glyph outline as A8 alpha mask using internal rasterizer */ + hb_raster_draw_t *rdr = c->clip_rdr; + const hb_transform_t<> &t = c->current_transform (); + hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0); + hb_raster_draw_set_format (rdr, HB_RASTER_FORMAT_A8); + hb_raster_extents_t clip_ext = { + surf->extents.x_origin, surf->extents.y_origin, + w, h, 0 + }; + hb_raster_draw_set_extents (rdr, &clip_ext); + + hb_font_draw_glyph (font, glyph, hb_raster_draw_get_funcs (), rdr); + hb_raster_image_t *mask_img = hb_raster_draw_render (rdr); + if (unlikely (!mask_img)) + { + /* If mask rendering fails, push a fully transparent clip */ + 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; + c->clip_stack.push (new_clip); + return; + } + + /* Allocate alpha buffer and intersect with previous clip */ + 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; + c->clip_stack.push (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 (); + + for (unsigned y = 0; y < h; y++) + for (unsigned x = 0; x < w; x++) + { + uint8_t glyph_alpha = (x < mask_ext.width && y < mask_ext.height) + ? mask_buf[y * mask_ext.stride + x] : 0; + uint8_t old_alpha = old_clip.get_alpha (x, y); + new_clip.alpha[y * new_clip.stride + x] = div255 (glyph_alpha * old_alpha); + } + + hb_raster_draw_recycle_image (rdr, mask_img); + c->clip_stack.push (new_clip); +} + +static void +hb_raster_paint_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + float xmin, float ymin, + float xmax, float ymax, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_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; + + const hb_transform_t<> &t = c->current_transform (); + + /* Transform rectangle corners to pixel space */ + hb_extents_t<> rect_ext {xmin, ymin, xmax, ymax}; + t.transform_extents (rect_ext); + + /* Convert to pixel coordinates relative to surface origin */ + int px0 = (int) floorf (rect_ext.xmin) - surf->extents.x_origin; + int py0 = (int) floorf (rect_ext.ymin) - surf->extents.y_origin; + int px1 = (int) ceilf (rect_ext.xmax) - surf->extents.x_origin; + int py1 = (int) ceilf (rect_ext.ymax) - surf->extents.y_origin; + + /* Clamp to surface bounds */ + px0 = hb_max (px0, 0); + py0 = hb_max (py0, 0); + px1 = hb_min (px1, (int) w); + py1 = hb_min (py1, (int) h); + + const hb_raster_clip_t &old_clip = c->current_clip (); + + hb_raster_clip_t new_clip; + new_clip.width = w; + new_clip.height = h; + new_clip.stride = (w + 3u) & ~3u; + + if (old_clip.is_rect) + { + /* Fast path: rect-on-rect intersection */ + new_clip.is_rect = true; + new_clip.rect_x0 = hb_max (px0, old_clip.rect_x0); + new_clip.rect_y0 = hb_max (py0, old_clip.rect_y0); + new_clip.rect_x1 = hb_min (px1, old_clip.rect_x1); + new_clip.rect_y1 = hb_min (py1, old_clip.rect_y1); + } + else + { + /* General case: intersect rectangle with existing alpha mask */ + new_clip.is_rect = false; + if (unlikely (!new_clip.alpha.resize (new_clip.stride * h))) + { + 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; + c->clip_stack.push (new_clip); + return; + } + memset (new_clip.alpha.arrayZ, 0, new_clip.stride * h); + for (int y = py0; y < py1; y++) + for (int x = px0; x < px1; x++) + new_clip.alpha[y * new_clip.stride + x] = old_clip.get_alpha (x, y); + } + + c->clip_stack.push (new_clip); +} + +static void +hb_raster_paint_pop_clip (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + c->clip_stack.pop (); +} + +static void +hb_raster_paint_push_group (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + ensure_initialized (c); + + hb_raster_image_t *new_surf = c->acquire_surface (); + if (unlikely (!new_surf)) return; + c->surface_stack.push (new_surf); +} + +static void +hb_raster_paint_pop_group (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + hb_paint_composite_mode_t mode, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + if (c->surface_stack.length < 2) return; + + hb_raster_image_t *src = c->surface_stack.pop (); + hb_raster_image_t *dst = c->current_surface (); + + if (dst && src) + composite_images (dst, src, mode); + + c->release_surface (src); +} + +static void +hb_raster_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + hb_bool_t is_foreground, + hb_color_t color, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + ensure_initialized (c); + + hb_raster_image_t *surf = c->current_surface (); + if (unlikely (!surf)) return; + + if (is_foreground) + { + /* Use foreground color, modulating alpha */ + color = HB_COLOR (hb_color_get_blue (c->foreground), + hb_color_get_green (c->foreground), + hb_color_get_red (c->foreground), + div255 (hb_color_get_alpha (c->foreground) * + hb_color_get_alpha (color))); + } + + uint32_t premul = color_to_premul_pixel (color); + const hb_raster_clip_t &clip = c->current_clip (); + + unsigned w = surf->extents.width; + unsigned h = surf->extents.height; + unsigned stride = surf->extents.stride; + + for (unsigned y = 0; y < h; y++) + { + uint32_t *row = reinterpret_cast (surf->buffer.arrayZ + y * stride); + for (unsigned x = 0; x < w; x++) + { + uint8_t clip_alpha = clip.get_alpha (x, y); + if (clip_alpha == 0) continue; + uint32_t src = alpha_mul (premul, clip_alpha); + row[x] = src_over (src, row[x]); + } + } +} + +static hb_bool_t +hb_raster_paint_image (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + hb_blob_t *blob, + unsigned width, + unsigned height, + hb_tag_t format, + float slant HB_UNUSED, + hb_glyph_extents_t *extents, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + ensure_initialized (c); + + /* Only handle raw BGRA32 for now */ + if (format != HB_TAG ('B','G','R','A')) + return false; + + hb_raster_image_t *surf = c->current_surface (); + if (unlikely (!surf)) return false; + if (!extents) return false; + + unsigned data_len; + const uint8_t *data = (const uint8_t *) hb_blob_get_data (blob, &data_len); + if (!data || data_len < width * height * 4) return false; + + const hb_raster_clip_t &clip = c->current_clip (); + const hb_transform_t<> &t = c->current_transform (); + + /* Compute inverse transform for sampling */ + float det = t.xx * t.yy - t.xy * t.yx; + if (fabsf (det) < 1e-10f) return false; + float inv_det = 1.f / det; + float inv_xx = t.yy * inv_det; + float inv_xy = -t.xy * inv_det; + float inv_yx = -t.yx * inv_det; + float inv_yy = t.xx * inv_det; + float inv_x0 = (t.xy * t.y0 - t.yy * t.x0) * inv_det; + float inv_y0 = (t.yx * t.x0 - t.xx * t.y0) * inv_det; + + unsigned surf_w = surf->extents.width; + unsigned surf_h = surf->extents.height; + unsigned surf_stride = surf->extents.stride; + int ox = surf->extents.x_origin; + int oy = surf->extents.y_origin; + + /* Image source rectangle in glyph space */ + float img_x = extents->x_bearing; + float img_y = extents->y_bearing + extents->height; /* bottom-left in glyph space */ + float img_sx = (float) extents->width / width; + float img_sy = (float) -extents->height / height; /* flip Y */ + + for (unsigned py = 0; py < surf_h; py++) + { + uint32_t *row = reinterpret_cast (surf->buffer.arrayZ + py * surf_stride); + for (unsigned px = 0; px < surf_w; px++) + { + uint8_t clip_alpha = clip.get_alpha (px, py); + if (clip_alpha == 0) continue; + + /* Map pixel to glyph space */ + float gx = inv_xx * (px + ox) + inv_xy * (py + oy) + inv_x0; + float gy = inv_yx * (px + ox) + inv_yy * (py + oy) + inv_y0; + + /* Map glyph space to image texel */ + int ix = (int) floorf ((gx - img_x) / img_sx); + int iy = (int) floorf ((gy - img_y) / img_sy); + + if (ix < 0 || ix >= (int) width || iy < 0 || iy >= (int) height) + continue; + + uint32_t src_px = reinterpret_cast (data)[iy * width + ix]; + src_px = alpha_mul (src_px, clip_alpha); + row[px] = src_over (src_px, row[px]); + } + } + + return true; +} + + +/* + * Gradient helpers + */ + +#define PREALLOCATED_COLOR_STOPS 16 + +static int +cmp_color_stop (const void *p1, const void *p2) +{ + const hb_color_stop_t *c1 = (const hb_color_stop_t *) p1; + const hb_color_stop_t *c2 = (const hb_color_stop_t *) p2; + if (c1->offset < c2->offset) return -1; + if (c1->offset > c2->offset) return 1; + return 0; +} + +static bool +get_color_stops (hb_raster_paint_t *c, + hb_color_line_t *color_line, + unsigned *count, + hb_color_stop_t **stops) +{ + unsigned len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); + if (len > *count) + { + *stops = (hb_color_stop_t *) hb_malloc (len * sizeof (hb_color_stop_t)); + if (unlikely (!*stops)) + return false; + } + hb_color_line_get_color_stops (color_line, 0, &len, *stops); + for (unsigned i = 0; i < len; i++) + if ((*stops)[i].is_foreground) + (*stops)[i].color = HB_COLOR (hb_color_get_blue (c->foreground), + hb_color_get_green (c->foreground), + hb_color_get_red (c->foreground), + div255 (hb_color_get_alpha (c->foreground) * + hb_color_get_alpha ((*stops)[i].color))); + + *count = len; + return true; +} + +static void +normalize_color_line (hb_color_stop_t *stops, + unsigned len, + float *omin, float *omax) +{ + hb_qsort (stops, len, sizeof (hb_color_stop_t), cmp_color_stop); + + float mn = stops[0].offset, mx = stops[0].offset; + for (unsigned i = 1; i < len; i++) + { + mn = hb_min (mn, stops[i].offset); + mx = hb_max (mx, stops[i].offset); + } + if (mn != mx) + for (unsigned i = 0; i < len; i++) + stops[i].offset = (stops[i].offset - mn) / (mx - mn); + + *omin = mn; + *omax = mx; +} + +/* Evaluate color at normalized position t, interpolating in premultiplied space. */ +static uint32_t +evaluate_color_line (const hb_color_stop_t *stops, unsigned len, float t, + hb_paint_extend_t extend) +{ + /* Apply extend mode */ + if (extend == HB_PAINT_EXTEND_PAD) + { + t = hb_clamp (t, 0.f, 1.f); + } + else if (extend == HB_PAINT_EXTEND_REPEAT) + { + t = t - floorf (t); + } + else /* REFLECT */ + { + t = t - floorf (t); + int period = (int) floorf (t); + float frac = t - floorf (t); + t = (period & 1) ? 1.f - frac : frac; + } + + /* Find bounding stops */ + if (t <= stops[0].offset) + return color_to_premul_pixel (stops[0].color); + if (t >= stops[len - 1].offset) + return color_to_premul_pixel (stops[len - 1].color); + + unsigned i; + for (i = 0; i < len - 1; i++) + if (t < stops[i + 1].offset) + break; + + float range = stops[i + 1].offset - stops[i].offset; + float k = range > 0.f ? (t - stops[i].offset) / range : 0.f; + + /* Interpolate in premultiplied [0,255] space */ + hb_color_t c0 = stops[i].color; + hb_color_t c1 = stops[i + 1].color; + + float a0 = hb_color_get_alpha (c0) / 255.f; + float r0 = hb_color_get_red (c0) / 255.f * a0; + float g0 = hb_color_get_green (c0) / 255.f * a0; + float b0 = hb_color_get_blue (c0) / 255.f * a0; + + float a1 = hb_color_get_alpha (c1) / 255.f; + float r1 = hb_color_get_red (c1) / 255.f * a1; + float g1 = hb_color_get_green (c1) / 255.f * a1; + float b1 = hb_color_get_blue (c1) / 255.f * a1; + + float a = a0 + k * (a1 - a0); + float r = r0 + k * (r1 - r0); + float g = g0 + k * (g1 - g0); + float b = b0 + k * (b1 - b0); + + uint8_t pa = (uint8_t) (a * 255.f + 0.5f); + uint8_t pr = (uint8_t) (r * 255.f + 0.5f); + uint8_t pg = (uint8_t) (g * 255.f + 0.5f); + uint8_t pb = (uint8_t) (b * 255.f + 0.5f); + + return (uint32_t) pb | ((uint32_t) pg << 8) | ((uint32_t) pr << 16) | ((uint32_t) pa << 24); +} + +static void +reduce_anchors (float x0, float y0, + float x1, float y1, + float x2, float y2, + float *xx0, float *yy0, + float *xx1, float *yy1) +{ + float q2x = x2 - x0, q2y = y2 - y0; + float q1x = x1 - x0, q1y = y1 - y0; + float s = q2x * q2x + q2y * q2y; + if (s < 0.000001f) + { + *xx0 = x0; *yy0 = y0; + *xx1 = x1; *yy1 = y1; + return; + } + float k = (q2x * q1x + q2y * q1y) / s; + *xx0 = x0; + *yy0 = y0; + *xx1 = x1 - k * q2x; + *yy1 = y1 - k * q2y; +} + + +/* + * Gradient paint callbacks + */ + +static void +hb_raster_paint_linear_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + hb_color_line_t *color_line, + float x0, float y0, + float x1, float y1, + float x2, float y2, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + ensure_initialized (c); + + hb_raster_image_t *surf = c->current_surface (); + if (unlikely (!surf)) return; + + unsigned len = PREALLOCATED_COLOR_STOPS; + hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS]; + hb_color_stop_t *stops = stops_; + + if (unlikely (!get_color_stops (c, color_line, &len, &stops))) + return; + float mn, mx; + normalize_color_line (stops, len, &mn, &mx); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + + /* Reduce 3-point anchor to 2-point gradient axis */ + float lx0, ly0, lx1, ly1; + reduce_anchors (x0, y0, x1, y1, x2, y2, &lx0, &ly0, &lx1, &ly1); + + /* Apply normalization to endpoints */ + float gx0 = lx0 + mn * (lx1 - lx0); + float gy0 = ly0 + mn * (ly1 - ly0); + float gx1 = lx0 + mx * (lx1 - lx0); + float gy1 = ly0 + mx * (ly1 - ly0); + + /* Inverse transform: pixel → glyph space */ + const hb_transform_t<> &t = c->current_transform (); + float det = t.xx * t.yy - t.xy * t.yx; + if (fabsf (det) < 1e-10f) goto done; + + { + float inv_det = 1.f / det; + float inv_xx = t.yy * inv_det; + float inv_xy = -t.xy * inv_det; + float inv_yx = -t.yx * inv_det; + float inv_yy = t.xx * inv_det; + float inv_x0 = (t.xy * t.y0 - t.yy * t.x0) * inv_det; + float inv_y0 = (t.yx * t.x0 - t.xx * t.y0) * inv_det; + + /* Gradient direction vector and denominator for projection */ + float dx = gx1 - gx0, dy = gy1 - gy0; + float denom = dx * dx + dy * dy; + if (denom < 1e-10f) goto done; + float inv_denom = 1.f / denom; + + const hb_raster_clip_t &clip = c->current_clip (); + unsigned w = surf->extents.width; + unsigned h = surf->extents.height; + unsigned stride = surf->extents.stride; + int ox = surf->extents.x_origin; + int oy = surf->extents.y_origin; + + for (unsigned py = 0; py < h; py++) + { + uint32_t *row = reinterpret_cast (surf->buffer.arrayZ + py * stride); + for (unsigned px = 0; px < w; px++) + { + uint8_t clip_alpha = clip.get_alpha (px, py); + if (clip_alpha == 0) continue; + + /* Pixel center → glyph space */ + float gx = inv_xx * (px + ox + 0.5f) + inv_xy * (py + oy + 0.5f) + inv_x0; + float gy = inv_yx * (px + ox + 0.5f) + inv_yy * (py + oy + 0.5f) + inv_y0; + + /* Project onto gradient axis: t = dot(p - p0, dir) / dot(dir, dir) */ + float proj_t = ((gx - gx0) * dx + (gy - gy0) * dy) * inv_denom; + + uint32_t src = evaluate_color_line (stops, len, proj_t, extend); + src = alpha_mul (src, clip_alpha); + row[px] = src_over (src, row[px]); + } + } + } + +done: + if (stops != stops_) + hb_free (stops); +} + +static void +hb_raster_paint_radial_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + hb_color_line_t *color_line, + float x0, float y0, float r0, + float x1, float y1, float r1, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + ensure_initialized (c); + + hb_raster_image_t *surf = c->current_surface (); + if (unlikely (!surf)) return; + + unsigned len = PREALLOCATED_COLOR_STOPS; + hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS]; + hb_color_stop_t *stops = stops_; + + if (unlikely (!get_color_stops (c, color_line, &len, &stops))) + return; + float mn, mx; + normalize_color_line (stops, len, &mn, &mx); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + + /* Apply normalization to circle parameters */ + float cx0 = x0 + mn * (x1 - x0); + float cy0 = y0 + mn * (y1 - y0); + float cr0 = r0 + mn * (r1 - r0); + float cx1 = x0 + mx * (x1 - x0); + float cy1 = y0 + mx * (y1 - y0); + float cr1 = r0 + mx * (r1 - r0); + + /* Inverse transform */ + const hb_transform_t<> &t = c->current_transform (); + float det = t.xx * t.yy - t.xy * t.yx; + if (fabsf (det) < 1e-10f) goto done; + + { + float inv_det = 1.f / det; + float inv_xx = t.yy * inv_det; + float inv_xy = -t.xy * inv_det; + float inv_yx = -t.yx * inv_det; + float inv_yy = t.xx * inv_det; + float inv_x0 = (t.xy * t.y0 - t.yy * t.x0) * inv_det; + float inv_y0 = (t.yx * t.x0 - t.xx * t.y0) * inv_det; + + /* Precompute quadratic coefficients for radial gradient: + * |p - c0 - t*(c1-c0)|^2 = (r0 + t*(r1-r0))^2 + * + * Expanding gives At^2 + Bt + C = 0 where: + * cdx = c1.x - c0.x, cdy = c1.y - c0.y, dr = r1 - r0 + * A = cdx^2 + cdy^2 - dr^2 + * B = -2*(px-c0.x)*cdx - 2*(py-c0.y)*cdy - 2*r0*dr + * C = (px-c0.x)^2 + (py-c0.y)^2 - r0^2 + */ + float cdx = cx1 - cx0, cdy = cy1 - cy0; + float dr = cr1 - cr0; + float A = cdx * cdx + cdy * cdy - dr * dr; + + const hb_raster_clip_t &clip = c->current_clip (); + unsigned w = surf->extents.width; + unsigned h = surf->extents.height; + unsigned stride = surf->extents.stride; + int ox = surf->extents.x_origin; + int oy = surf->extents.y_origin; + + for (unsigned py = 0; py < h; py++) + { + uint32_t *row = reinterpret_cast (surf->buffer.arrayZ + py * stride); + for (unsigned px = 0; px < w; px++) + { + uint8_t clip_alpha = clip.get_alpha (px, py); + if (clip_alpha == 0) continue; + + float gx = inv_xx * (px + ox + 0.5f) + inv_xy * (py + oy + 0.5f) + inv_x0; + float gy = inv_yx * (px + ox + 0.5f) + inv_yy * (py + oy + 0.5f) + inv_y0; + + float dpx = gx - cx0, dpy = gy - cy0; + float B = -2.f * (dpx * cdx + dpy * cdy + cr0 * dr); + float C = dpx * dpx + dpy * dpy - cr0 * cr0; + + float grad_t; + if (fabsf (A) > 1e-10f) + { + float disc = B * B - 4.f * A * C; + if (disc < 0.f) continue; + float sq = sqrtf (disc); + /* Pick the larger root (t closer to 1 = outer circle) */ + float t1 = (-B + sq) / (2.f * A); + float t2 = (-B - sq) / (2.f * A); + /* Choose the root that gives a positive radius */ + if (cr0 + t1 * dr >= 0.f) + grad_t = t1; + else + grad_t = t2; + } + else + { + /* Linear case: Bt + C = 0 */ + if (fabsf (B) < 1e-10f) continue; + grad_t = -C / B; + } + + uint32_t src = evaluate_color_line (stops, len, grad_t, extend); + src = alpha_mul (src, clip_alpha); + row[px] = src_over (src, row[px]); + } + } + } + +done: + if (stops != stops_) + hb_free (stops); +} + +static void +hb_raster_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + hb_color_line_t *color_line, + float cx, float cy, + float start_angle, + float end_angle, + void *user_data HB_UNUSED) +{ + hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data; + + ensure_initialized (c); + + hb_raster_image_t *surf = c->current_surface (); + if (unlikely (!surf)) return; + + unsigned len = PREALLOCATED_COLOR_STOPS; + hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS]; + hb_color_stop_t *stops = stops_; + + if (unlikely (!get_color_stops (c, color_line, &len, &stops))) + return; + float mn, mx; + normalize_color_line (stops, len, &mn, &mx); + + hb_paint_extend_t extend = hb_color_line_get_extend (color_line); + + /* Apply normalization to angle range */ + float a0 = start_angle + mn * (end_angle - start_angle); + float a1 = start_angle + mx * (end_angle - start_angle); + float angle_range = a1 - a0; + + /* Inverse transform */ + const hb_transform_t<> &t = c->current_transform (); + float det = t.xx * t.yy - t.xy * t.yx; + if (fabsf (det) < 1e-10f || fabsf (angle_range) < 1e-10f) goto done; + + { + float inv_det = 1.f / det; + float inv_xx = t.yy * inv_det; + float inv_xy = -t.xy * inv_det; + float inv_yx = -t.yx * inv_det; + float inv_yy = t.xx * inv_det; + float inv_x0 = (t.xy * t.y0 - t.yy * t.x0) * inv_det; + float inv_y0 = (t.yx * t.x0 - t.xx * t.y0) * inv_det; + + float inv_angle_range = 1.f / angle_range; + + const hb_raster_clip_t &clip = c->current_clip (); + unsigned w = surf->extents.width; + unsigned h = surf->extents.height; + unsigned stride = surf->extents.stride; + int ox = surf->extents.x_origin; + int oy = surf->extents.y_origin; + + for (unsigned py = 0; py < h; py++) + { + uint32_t *row = reinterpret_cast (surf->buffer.arrayZ + py * stride); + for (unsigned px = 0; px < w; px++) + { + uint8_t clip_alpha = clip.get_alpha (px, py); + if (clip_alpha == 0) continue; + + float gx = inv_xx * (px + ox + 0.5f) + inv_xy * (py + oy + 0.5f) + inv_x0; + float gy = inv_yx * (px + ox + 0.5f) + inv_yy * (py + oy + 0.5f) + inv_y0; + + float angle = atan2f (gy - cy, gx - cx); + + /* Map angle to normalized gradient parameter */ + float grad_t = (angle - a0) * inv_angle_range; + + uint32_t src = evaluate_color_line (stops, len, grad_t, extend); + src = alpha_mul (src, clip_alpha); + row[px] = src_over (src, row[px]); + } + } + } + +done: + if (stops != stops_) + hb_free (stops); +} + +static hb_bool_t +hb_raster_paint_custom_palette_color (hb_paint_funcs_t *funcs HB_UNUSED, + void *paint_data HB_UNUSED, + unsigned int color_index HB_UNUSED, + hb_color_t *color HB_UNUSED, + void *user_data HB_UNUSED) +{ + return false; +} + + +/* + * Lazy-loader singleton for paint funcs + */ + +static inline void free_static_raster_paint_funcs (); + +static struct hb_raster_paint_funcs_lazy_loader_t : hb_paint_funcs_lazy_loader_t +{ + static hb_paint_funcs_t *create () + { + hb_paint_funcs_t *funcs = hb_paint_funcs_create (); + + hb_paint_funcs_set_push_transform_func (funcs, hb_raster_paint_push_transform, nullptr, nullptr); + hb_paint_funcs_set_pop_transform_func (funcs, hb_raster_paint_pop_transform, nullptr, nullptr); + hb_paint_funcs_set_color_glyph_func (funcs, hb_raster_paint_color_glyph, nullptr, nullptr); + hb_paint_funcs_set_push_clip_glyph_func (funcs, hb_raster_paint_push_clip_glyph, nullptr, nullptr); + hb_paint_funcs_set_push_clip_rectangle_func (funcs, hb_raster_paint_push_clip_rectangle, nullptr, nullptr); + hb_paint_funcs_set_pop_clip_func (funcs, hb_raster_paint_pop_clip, nullptr, nullptr); + hb_paint_funcs_set_push_group_func (funcs, hb_raster_paint_push_group, nullptr, nullptr); + hb_paint_funcs_set_pop_group_func (funcs, hb_raster_paint_pop_group, nullptr, nullptr); + hb_paint_funcs_set_color_func (funcs, hb_raster_paint_color, nullptr, nullptr); + hb_paint_funcs_set_image_func (funcs, hb_raster_paint_image, nullptr, nullptr); + hb_paint_funcs_set_linear_gradient_func (funcs, hb_raster_paint_linear_gradient, nullptr, nullptr); + hb_paint_funcs_set_radial_gradient_func (funcs, hb_raster_paint_radial_gradient, nullptr, nullptr); + hb_paint_funcs_set_sweep_gradient_func (funcs, hb_raster_paint_sweep_gradient, nullptr, nullptr); + hb_paint_funcs_set_custom_palette_color_func (funcs, hb_raster_paint_custom_palette_color, nullptr, nullptr); + + hb_paint_funcs_make_immutable (funcs); + + hb_atexit (free_static_raster_paint_funcs); + + return funcs; + } +} static_raster_paint_funcs; + +static inline void +free_static_raster_paint_funcs () +{ + static_raster_paint_funcs.free_instance (); +} + + +/* + * Public API + */ + +/** + * hb_raster_paint_create: + * + * Creates a new color-glyph paint context. + * + * Return value: (transfer full): + * A newly allocated #hb_raster_paint_t. + * + * XSince: REPLACEME + **/ +hb_raster_paint_t * +hb_raster_paint_create (void) +{ + hb_raster_paint_t *paint = hb_object_create (); + if (unlikely (!paint)) return nullptr; + + paint->clip_rdr = hb_raster_draw_create (); + + return paint; +} + +/** + * hb_raster_paint_reference: (skip) + * @paint: a paint context + * + * Increases the reference count on @paint by one. + * + * Return value: (transfer full): + * The referenced #hb_raster_paint_t. + * + * XSince: REPLACEME + **/ +hb_raster_paint_t * +hb_raster_paint_reference (hb_raster_paint_t *paint) +{ + return hb_object_reference (paint); +} + +/** + * hb_raster_paint_destroy: (skip) + * @paint: a paint context + * + * Decreases the reference count on @paint by one. When the + * reference count reaches zero, the paint context is freed. + * + * XSince: REPLACEME + **/ +void +hb_raster_paint_destroy (hb_raster_paint_t *paint) +{ + if (!hb_object_destroy (paint)) return; + hb_raster_draw_destroy (paint->clip_rdr); + for (auto *s : paint->surface_stack) + hb_raster_image_destroy (s); + for (auto *s : paint->surface_cache) + hb_raster_image_destroy (s); + hb_raster_image_destroy (paint->recycled_image); + hb_free (paint); +} + +/** + * hb_raster_paint_set_user_data: (skip) + * @paint: a paint context + * @key: the user-data key + * @data: a pointer to the user data + * @destroy: (nullable): a callback to call when @data is not needed anymore + * @replace: whether to replace an existing data with the same key + * + * Attaches a user-data key/data pair to the specified paint context. + * + * Return value: `true` if success, `false` otherwise + * + * XSince: REPLACEME + **/ +hb_bool_t +hb_raster_paint_set_user_data (hb_raster_paint_t *paint, + hb_user_data_key_t *key, + void *data, + hb_destroy_func_t destroy, + hb_bool_t replace) +{ + return hb_object_set_user_data (paint, key, data, destroy, replace); +} + +/** + * hb_raster_paint_get_user_data: (skip) + * @paint: a paint context + * @key: the user-data key + * + * Fetches the user-data associated with the specified key, + * attached to the specified paint context. + * + * Return value: (transfer none): + * A pointer to the user data + * + * XSince: REPLACEME + **/ +void * +hb_raster_paint_get_user_data (hb_raster_paint_t *paint, + hb_user_data_key_t *key) +{ + return hb_object_get_user_data (paint, key); +} + +/** + * hb_raster_paint_set_transform: + * @paint: a paint context + * @xx: xx component of the transform matrix + * @yx: yx component of the transform matrix + * @xy: xy component of the transform matrix + * @yy: yy component of the transform matrix + * @dx: x translation + * @dy: y translation + * + * Sets the base 2×3 affine transform that maps from glyph-space + * coordinates to pixel-space coordinates. + * + * XSince: REPLACEME + **/ +void +hb_raster_paint_set_transform (hb_raster_paint_t *paint, + float xx, float yx, + float xy, float yy, + float dx, float dy) +{ + if (unlikely (!paint)) return; + paint->base_transform = {xx, yx, xy, yy, dx, dy}; +} + +/** + * hb_raster_paint_set_extents: + * @paint: a paint context + * @extents: the desired output extents + * + * Sets the output image extents (pixel rectangle). + * + * XSince: REPLACEME + **/ +void +hb_raster_paint_set_extents (hb_raster_paint_t *paint, + const hb_raster_extents_t *extents) +{ + if (unlikely (!paint || !extents)) return; + paint->fixed_extents = *extents; + paint->has_fixed_extents = true; + if (paint->fixed_extents.stride == 0) + paint->fixed_extents.stride = paint->fixed_extents.width * 4; +} + +/** + * hb_raster_paint_get_funcs: + * + * Fetches the singleton #hb_paint_funcs_t that renders color glyphs + * into an #hb_raster_paint_t. Pass the #hb_raster_paint_t as the + * @paint_data argument when calling hb_font_paint_glyph(). + * + * Return value: (transfer none): + * The rasterizer paint functions + * + * XSince: REPLACEME + **/ +hb_paint_funcs_t * +hb_raster_paint_get_funcs (void) +{ + return static_raster_paint_funcs.get_unconst (); +} + +/** + * hb_raster_paint_render: + * @paint: a paint context + * + * Extracts the rendered image after hb_font_paint_glyph() has + * completed. The paint context's surface stack is consumed and + * the result returned as a new #hb_raster_image_t. + * + * Call hb_font_paint_glyph() before calling this function. + * The paint context's stacks are initialized before paint and + * cleaned up here. + * + * Return value: (transfer full): + * A newly allocated #hb_raster_image_t, or `NULL` on failure + * + * XSince: REPLACEME + **/ +hb_raster_image_t * +hb_raster_paint_render (hb_raster_paint_t *paint) +{ + if (unlikely (!paint)) return nullptr; + + hb_raster_image_t *result = nullptr; + + if (paint->surface_stack.length) + { + result = paint->surface_stack[0]; + /* Release any remaining group surfaces (shouldn't happen with + * well-formed paint calls, but be safe). */ + for (unsigned i = 1; i < paint->surface_stack.length; i++) + paint->release_surface (paint->surface_stack[i]); + paint->surface_stack.resize (0); + } + + /* Clean up stacks */ + paint->transform_stack.resize (0); + paint->clip_stack.resize (0); + + return result; +} + +/** + * hb_raster_paint_reset: + * @paint: a paint context + * + * Resets the paint context to its initial state, clearing all + * configuration and cached surfaces. + * + * XSince: REPLACEME + **/ +void +hb_raster_paint_reset (hb_raster_paint_t *paint) +{ + if (unlikely (!paint)) return; + paint->base_transform = {1, 0, 0, 1, 0, 0}; + paint->fixed_extents = {}; + paint->has_fixed_extents = false; + paint->foreground = HB_COLOR (0, 0, 0, 255); + paint->transform_stack.resize (0); + paint->clip_stack.resize (0); + for (auto *s : paint->surface_stack) + hb_raster_image_destroy (s); + paint->surface_stack.resize (0); + for (auto *s : paint->surface_cache) + hb_raster_image_destroy (s); + paint->surface_cache.resize (0); + hb_raster_image_destroy (paint->recycled_image); + paint->recycled_image = nullptr; +} + +/** + * hb_raster_paint_recycle_image: + * @paint: a paint context + * @image: a raster image to recycle + * + * Recycles @image for reuse by subsequent render calls. + * The caller transfers ownership of @image to @paint. + * + * XSince: REPLACEME + **/ +void +hb_raster_paint_recycle_image (hb_raster_paint_t *paint, + hb_raster_image_t *image) +{ + if (unlikely (!paint || !image)) return; + hb_raster_image_destroy (paint->recycled_image); + paint->recycled_image = image; +} diff --git a/src/hb-raster-paint.hh b/src/hb-raster-paint.hh new file mode 100644 index 000000000..f10c67e9f --- /dev/null +++ b/src/hb-raster-paint.hh @@ -0,0 +1,146 @@ +/* + * 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.hh" +#include "hb-geometry.hh" + + +/* hb_raster_clip_t — alpha mask for clipping */ +struct hb_raster_clip_t +{ + hb_vector_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; + + 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; + } + + 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}; + hb_raster_extents_t fixed_extents = {}; + bool has_fixed_extents = false; + hb_color_t foreground = HB_COLOR (0, 0, 0, 255); + + /* Stacks */ + hb_vector_t> transform_stack; + hb_vector_t clip_stack; + hb_vector_t surface_stack; + + /* Cached surface pool (freelist for reuse across push/pop group) */ + hb_vector_t surface_cache; + + /* Internal rasterizer for clip-to-glyph */ + hb_raster_draw_t *clip_rdr = nullptr; + + /* Recycled output image */ + hb_raster_image_t *recycled_image = nullptr; + + /* Helpers */ + + hb_raster_image_t *acquire_surface () + { + hb_raster_image_t *img; + if (surface_cache.length) + img = surface_cache.pop (); + else + { + img = hb_object_create (); + if (unlikely (!img)) return nullptr; + } + + img->format = HB_RASTER_FORMAT_BGRA32; + img->extents = fixed_extents; + if (img->extents.stride == 0) + img->extents.stride = img->extents.width * 4; + + size_t buf_size = (size_t) img->extents.stride * img->extents.height; + if (unlikely (!img->buffer.resize_dirty (buf_size))) + { + hb_raster_image_destroy (img); + return nullptr; + } + memset (img->buffer.arrayZ, 0, buf_size); + return img; + } + + void release_surface (hb_raster_image_t *img) + { + surface_cache.push (img); + } + + hb_raster_image_t *current_surface () + { + return surface_stack.length ? surface_stack.tail () : nullptr; + } + + hb_raster_clip_t ¤t_clip () + { + return clip_stack.tail (); + } + + hb_transform_t<> ¤t_transform () + { + return transform_stack.tail (); + } +}; + + +#endif /* HB_RASTER_PAINT_HH */ diff --git a/src/hb-raster.h b/src/hb-raster.h index ecb00d386..3687c0435 100644 --- a/src/hb-raster.h +++ b/src/hb-raster.h @@ -178,6 +178,64 @@ hb_raster_draw_recycle_image (hb_raster_draw_t *draw, hb_raster_image_t *image); + +/* ── hb_raster_paint_t ───────────────────────────────────────────── */ + +/** + * hb_raster_paint_t: + * + * An opaque color-glyph paint context. Implements #hb_paint_funcs_t + * callbacks that render COLRv0/v1 color glyphs into a BGRA32 + * #hb_raster_image_t. + * + * XSince: REPLACEME + **/ +typedef struct hb_raster_paint_t hb_raster_paint_t; + +HB_EXTERN hb_raster_paint_t * +hb_raster_paint_create (void); + +HB_EXTERN hb_raster_paint_t * +hb_raster_paint_reference (hb_raster_paint_t *paint); + +HB_EXTERN void +hb_raster_paint_destroy (hb_raster_paint_t *paint); + +HB_EXTERN hb_bool_t +hb_raster_paint_set_user_data (hb_raster_paint_t *paint, + hb_user_data_key_t *key, + void *data, + hb_destroy_func_t destroy, + hb_bool_t replace); + +HB_EXTERN void * +hb_raster_paint_get_user_data (hb_raster_paint_t *paint, + hb_user_data_key_t *key); + +HB_EXTERN void +hb_raster_paint_set_transform (hb_raster_paint_t *paint, + float xx, float yx, + float xy, float yy, + float dx, float dy); + +HB_EXTERN void +hb_raster_paint_set_extents (hb_raster_paint_t *paint, + const hb_raster_extents_t *extents); + +HB_EXTERN hb_paint_funcs_t * +hb_raster_paint_get_funcs (void); + +HB_EXTERN hb_raster_image_t * +hb_raster_paint_render (hb_raster_paint_t *paint); + +HB_EXTERN void +hb_raster_paint_reset (hb_raster_paint_t *paint); + +HB_EXTERN void +hb_raster_paint_recycle_image (hb_raster_paint_t *paint, + hb_raster_image_t *image); + + HB_END_DECLS #endif /* HB_RASTER_H */ diff --git a/src/meson.build b/src/meson.build index 91105483b..791372d34 100644 --- a/src/meson.build +++ b/src/meson.build @@ -703,7 +703,9 @@ hb_raster_sources = [ 'hb-raster.cc', 'hb-raster-image.cc', 'hb-raster-draw.cc', + 'hb-raster-paint.cc', 'hb-raster.hh', + 'hb-raster-paint.hh', 'hb-static.cc', ] diff --git a/util/raster-output.hh b/util/raster-output.hh index 42b4f848c..3d9f3de84 100644 --- a/util/raster-output.hh +++ b/util/raster-output.hh @@ -29,6 +29,7 @@ #include "output-options.hh" #include "hb-raster.h" +#include "hb-ot.h" #include #include @@ -39,6 +40,7 @@ struct raster_output_t : output_options_t ~raster_output_t () { hb_raster_draw_destroy (rdr); + hb_raster_paint_destroy (pnt); hb_font_destroy (upem_font); hb_font_destroy (font); } @@ -46,7 +48,7 @@ struct raster_output_t : output_options_t void add_options (option_parser_t *parser) { parser->set_summary ("Rasterize text with given font."); - parser->set_description ("Renders shaped text as a PGM raster image."); + parser->set_description ("Renders shaped text as a raster image (PGM for outlines, PPM for color)."); output_options_t::add_options (parser); } @@ -70,7 +72,14 @@ struct raster_output_t : output_options_t hb_font_set_var_coords_design (upem_font, coords, coords_len); #endif + 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_png (face); + rdr = hb_raster_draw_create (); + if (has_color) + pnt = hb_raster_paint_create (); } void new_line () { lines.emplace_back (); } @@ -137,28 +146,36 @@ struct raster_output_t : output_options_t * upem / axis_scale); if (!(step > 0.f)) step = (float) upem; - for (unsigned li = 0; li < lines.size (); li++) + if (has_color && pnt) { - float off_x = vertical ? -(step * (float) li) : 0.f; - float off_y = vertical ? 0.f : -(step * (float) li); - - for (const auto &g : lines[li].glyphs) + finish_paint (sx, sy, step, vertical); + } + else + { + for (unsigned li = 0; li < lines.size (); li++) { - float pen_x = (g.x + off_x) * sx; - float pen_y = (g.y + off_y) * sy; - hb_raster_draw_set_transform (rdr, sx, 0.f, 0.f, sy, pen_x, pen_y); - hb_font_draw_glyph (upem_font, g.gid, hb_raster_draw_get_funcs (), rdr); + float off_x = vertical ? -(step * (float) li) : 0.f; + float off_y = vertical ? 0.f : -(step * (float) li); + + for (const auto &g : lines[li].glyphs) + { + float pen_x = (g.x + off_x) * sx; + float pen_y = (g.y + off_y) * sy; + hb_raster_draw_set_transform (rdr, sx, 0.f, 0.f, sy, pen_x, pen_y); + hb_font_draw_glyph (upem_font, g.gid, hb_raster_draw_get_funcs (), rdr); + } + } + + hb_raster_image_t *img = hb_raster_draw_render (rdr); + if (img) + { + write_pgm (img); + hb_raster_image_destroy (img); } } - hb_raster_image_t *img = hb_raster_draw_render (rdr); - if (img) - { - write_pgm (img); - hb_raster_image_destroy (img); - } - hb_raster_draw_destroy (rdr); rdr = nullptr; + hb_raster_paint_destroy (pnt); pnt = nullptr; hb_font_destroy (upem_font); upem_font = nullptr; hb_font_destroy (font); font = nullptr; lines.clear (); @@ -179,6 +196,110 @@ struct raster_output_t : output_options_t float to_upem_y (hb_position_t v) const { return y_scale ? (float) v * upem / y_scale : (float) v; } + void finish_paint (float sx, float sy, float step, bool vertical) + { + /* First pass: compute bounding box in pixel space */ + float pmin_x = 1e30f, pmin_y = 1e30f; + float pmax_x = -1e30f, pmax_y = -1e30f; + + for (unsigned li = 0; li < lines.size (); li++) + { + float off_x = vertical ? -(step * (float) li) : 0.f; + float off_y = vertical ? 0.f : -(step * (float) li); + + for (const auto &g : lines[li].glyphs) + { + hb_glyph_extents_t gext; + if (!hb_font_get_glyph_extents (upem_font, g.gid, &gext)) + continue; + + float gx = (g.x + off_x) * sx; + float gy = (g.y + off_y) * sy; + + float x0 = gx + gext.x_bearing * sx; + float y0 = gy + (gext.y_bearing + gext.height) * sy; + float x1 = gx + (gext.x_bearing + gext.width) * sx; + float y1 = gy + gext.y_bearing * sy; + + pmin_x = hb_min (pmin_x, hb_min (x0, x1)); + pmin_y = hb_min (pmin_y, hb_min (y0, y1)); + pmax_x = hb_max (pmax_x, hb_max (x0, x1)); + pmax_y = hb_max (pmax_y, hb_max (y0, y1)); + } + } + + if (pmin_x >= pmax_x || pmin_y >= pmax_y) return; + + int ix0 = (int) floorf (pmin_x) - 1; + int iy0 = (int) floorf (pmin_y) - 1; + int ix1 = (int) ceilf (pmax_x) + 1; + int iy1 = (int) ceilf (pmax_y) + 1; + unsigned w = (unsigned) (ix1 - ix0); + unsigned h = (unsigned) (iy1 - iy0); + unsigned stride = w * 4; + + /* Allocate output BGRA32 buffer */ + std::vector out_buf (stride * h, 0); + + hb_raster_extents_t ext = {ix0, iy0, w, h, stride}; + + /* Second pass: paint each glyph */ + for (unsigned li = 0; li < lines.size (); li++) + { + float off_x = vertical ? -(step * (float) li) : 0.f; + float off_y = vertical ? 0.f : -(step * (float) li); + + for (const auto &g : lines[li].glyphs) + { + float pen_x = (g.x + off_x) * sx; + float pen_y = (g.y + off_y) * sy; + + hb_raster_paint_set_transform (pnt, sx, 0.f, 0.f, sy, pen_x, pen_y); + hb_raster_paint_set_extents (pnt, &ext); + + hb_font_paint_glyph (upem_font, g.gid, + hb_raster_paint_get_funcs (), pnt, + 0, HB_COLOR (0, 0, 0, 255)); + + hb_raster_image_t *img = hb_raster_paint_render (pnt); + if (img) + { + /* Composite onto output buffer (SRC_OVER) */ + const uint8_t *src = hb_raster_image_get_buffer (img); + hb_raster_extents_t img_ext; + hb_raster_image_get_extents (img, &img_ext); + + for (unsigned y = 0; y < h; y++) + for (unsigned x = 0; x < w; x++) + { + uint32_t s; + memcpy (&s, src + y * img_ext.stride + x * 4, 4); + if (!s) continue; + uint8_t sa = (uint8_t) (s >> 24); + uint32_t d; + memcpy (&d, out_buf.data () + y * stride + x * 4, 4); + if (sa == 255) { d = s; } + else + { + unsigned inv_sa = 255 - sa; + uint8_t rb = (uint8_t) (((d & 0xFF) * inv_sa + 128 + (((d & 0xFF) * inv_sa + 128) >> 8)) >> 8) + (uint8_t) (s & 0xFF); + uint8_t rg = (uint8_t) ((((d >> 8) & 0xFF) * inv_sa + 128 + ((((d >> 8) & 0xFF) * inv_sa + 128) >> 8)) >> 8) + (uint8_t) ((s >> 8) & 0xFF); + uint8_t rr = (uint8_t) ((((d >> 16) & 0xFF) * inv_sa + 128 + ((((d >> 16) & 0xFF) * inv_sa + 128) >> 8)) >> 8) + (uint8_t) ((s >> 16) & 0xFF); + uint8_t ra = (uint8_t) ((((d >> 24) & 0xFF) * inv_sa + 128 + ((((d >> 24) & 0xFF) * inv_sa + 128) >> 8)) >> 8) + sa; + d = (uint32_t) rb | ((uint32_t) rg << 8) | ((uint32_t) rr << 16) | ((uint32_t) ra << 24); + } + memcpy (out_buf.data () + y * stride + x * 4, &d, 4); + } + + hb_raster_paint_recycle_image (pnt, img); + } + } + } + + /* Write as PPM (RGB, Y-flipped) */ + write_ppm (out_buf.data (), w, h, stride); + } + /* Write a PGM file; Y-flipped so text reads correctly in viewers. */ void write_pgm (hb_raster_image_t *img) { @@ -192,14 +313,44 @@ struct raster_output_t : output_options_t fwrite (buf + (ext.height - 1 - row) * ext.stride, 1, ext.width, out_fp); } - hb_raster_draw_t *rdr = nullptr; - hb_font_t *font = nullptr; - hb_font_t *upem_font = nullptr; - unsigned upem = 0; - int x_scale = 0; - int y_scale = 0; - unsigned subpixel_bits = 0; - hb_direction_t direction = HB_DIRECTION_INVALID; + /* Write a PPM file from BGRA32 buffer; Y-flipped, composited over white. */ + void write_ppm (const uint8_t *buf, unsigned w, unsigned h, unsigned stride) + { + if (!w || !h) return; + + fprintf (out_fp, "P6\n%u %u\n255\n", w, h); + std::vector row_buf (w * 3); + for (unsigned row = 0; row < h; row++) + { + const uint8_t *src = buf + (h - 1 - row) * stride; + for (unsigned x = 0; x < w; x++) + { + uint32_t px; + memcpy (&px, src + x * 4, 4); + uint8_t b = (uint8_t) (px & 0xFF); + uint8_t g = (uint8_t) ((px >> 8) & 0xFF); + uint8_t r = (uint8_t) ((px >> 16) & 0xFF); + uint8_t a = (uint8_t) (px >> 24); + /* Composite over white background */ + unsigned inv_a = 255 - a; + row_buf[x * 3 + 0] = (uint8_t) (r + ((255 * inv_a + 128 + ((255 * inv_a + 128) >> 8)) >> 8)); + row_buf[x * 3 + 1] = (uint8_t) (g + ((255 * inv_a + 128 + ((255 * inv_a + 128) >> 8)) >> 8)); + row_buf[x * 3 + 2] = (uint8_t) (b + ((255 * inv_a + 128 + ((255 * inv_a + 128) >> 8)) >> 8)); + } + fwrite (row_buf.data (), 1, w * 3, out_fp); + } + } + + hb_raster_draw_t *rdr = nullptr; + hb_raster_paint_t *pnt = nullptr; + hb_font_t *font = nullptr; + hb_font_t *upem_font = nullptr; + unsigned upem = 0; + int x_scale = 0; + int y_scale = 0; + unsigned subpixel_bits = 0; + bool has_color = false; + hb_direction_t direction = HB_DIRECTION_INVALID; std::vector lines; };