font: render glyf directly into output bitmap

Glyf rasterization previously let z2d allocate its alpha surface, then
duplicated the completed pixels into caller-owned storage. Back the z2d
surface with the final bitmap instead, eliminating one allocation and a
full bitmap copy for each non-empty glyph.
This commit is contained in:
Jon Parise
2026-08-20 08:12:34 -04:00
parent 9ae02a326f
commit 6f02d9aad6

View File

@@ -99,14 +99,16 @@ pub fn rasterize(
// bitmap just like an empty outline.
if (bounds.width() == 0 or bounds.height() == 0) return Bitmap.initEmpty(alloc, width, height);
// Build the surface we'll draw on. This is a simple alpha8 drawing.
var sfc: z2d.Surface = try .init(
// Build the surface directly over the bitmap we'll return.
const data = try alloc.alloc(u8, @as(usize, width) * @as(usize, height));
errdefer alloc.free(data);
var sfc: z2d.Surface = .initBuffer(
.image_surface_alpha8,
alloc,
null,
std.mem.bytesAsSlice(z2d.pixel.Alpha8, data),
@intCast(width),
@intCast(height),
);
defer sfc.deinit(alloc);
var path: z2d.Path = .empty;
defer path.deinit(alloc);
@@ -133,7 +135,7 @@ pub fn rasterize(
return .{
.width = width,
.height = height,
.data = try alloc.dupe(u8, std.mem.sliceAsBytes(sfc.image_surface_alpha8.buf)),
.data = data,
};
}