mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-25 08:51:07 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
5e4c105c6f
commit
49838acdad
5 files changed
+1667
-25
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -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<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;
|
||||
|
||||
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<hb_transform_t<>> transform_stack;
|
||||
hb_vector_t<hb_raster_clip_t> clip_stack;
|
||||
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;
|
||||
|
||||
/* 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<hb_raster_image_t> ();
|
||||
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 */
|
||||
@@ -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 */
|
||||
@@ -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',
|
||||
]
|
||||
|
||||
|
||||
+176
-25
@@ -29,6 +29,7 @@
|
||||
|
||||
#include "output-options.hh"
|
||||
#include "hb-raster.h"
|
||||
#include "hb-ot.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
@@ -39,6 +40,7 @@ struct raster_output_t : output_options_t<true>
|
||||
~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<true>
|
||||
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<true>
|
||||
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<true>
|
||||
* 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<true>
|
||||
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<uint8_t> 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<true>
|
||||
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<uint8_t> 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<line_t> lines;
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user