Skip to content

Commit 20dd1e7

Browse files
authored
fix: use bitmap stride
1 parent cfd9fc8 commit 20dd1e7

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

src/gfx/font/native/Font.zig

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,36 +78,46 @@ pub fn render(f: *Font, allocator: std.mem.Allocator, glyph_index: u32, opt: Ren
7878
const buffer = glyph_bitmap.buffer();
7979
const width = glyph_bitmap.width();
8080
const height = glyph_bitmap.rows();
81+
const pitch = @as(u32, @intCast(glyph_bitmap.pitch())); // TODO handle negative could be negative
8182
const margin = 1;
8283

84+
const dst_width = width + (margin * 2);
85+
const dst_height = height + (margin * 2);
86+
8387
if (buffer == null) return RenderedGlyph{
8488
.bitmap = null,
85-
.width = width + (margin * 2),
86-
.height = height + (margin * 2),
89+
.width = dst_width,
90+
.height = dst_height,
8791
};
8892

93+
const num_pixels = dst_width * dst_height;
94+
8995
// Add 1 pixel padding to texture to avoid bleeding over other textures. This is part of the
9096
// render() API contract.
9197
f.bitmap.clearRetainingCapacity();
92-
const num_pixels = (width + (margin * 2)) * (height + (margin * 2));
9398
// TODO: handle OOM here
9499
f.bitmap.ensureTotalCapacity(allocator, num_pixels) catch return error.RenderError;
95100
f.bitmap.resize(allocator, num_pixels) catch return error.RenderError;
96-
for (f.bitmap.items, 0..) |*data, i| {
97-
const x = i % (width + (margin * 2));
98-
const y = i / (width + (margin * 2));
99-
if (x < margin or x > (width + margin) or y < margin or y > (height + margin)) {
100-
data.* = RGBA32{ .r = 0, .g = 0, .b = 0, .a = 0 };
101-
} else {
102-
const alpha = buffer.?[((y - margin) * width + (x - margin)) % buffer.?.len];
103-
data.* = RGBA32{ .r = 0, .g = 0, .b = 0, .a = alpha };
101+
102+
@memset(f.bitmap.items, RGBA32{ .r = 0, .g = 0, .b = 0, .a = 0 });
103+
104+
for (0..height) |y| {
105+
const src_row = buffer.?[y * pitch ..][0..width];
106+
107+
const dst_y = y + margin;
108+
const dst_row_start = (dst_y * dst_width) + margin;
109+
const dst_row = f.bitmap.items[dst_row_start..][0..width];
110+
111+
for (0..width) |x| {
112+
const alpha = src_row[x];
113+
dst_row[x] = RGBA32{ .r = 255, .g = 255, .b = 255, .a = alpha };
104114
}
105115
}
106116

107117
return RenderedGlyph{
108118
.bitmap = f.bitmap.items,
109-
.width = width + (margin * 2),
110-
.height = height + (margin * 2),
119+
.width = dst_width,
120+
.height = dst_height,
111121
};
112122
}
113123

0 commit comments

Comments
 (0)