feat: render a string with freetype

dev
Louis Pearson 2024-02-16 18:20:20 -07:00
parent 7fdc7a9dfc
commit 051f0b7ffb
1 changed files with 26 additions and 10 deletions

View File

@ -461,22 +461,40 @@ const Canvas = struct {
const top = pos[1]; const top = pos[1];
const right = @min(left + cols, width); const right = @min(left + cols, width);
const bottom = @min(top + rows, height); const bottom = @min(top + rows, height);
const buffer = bitmap.buffer().?; const buffer = bitmap.buffer() orelse return;
if (pos[0] > width or pos[1] > height) return; if (pos[0] > width or pos[1] > height) return;
for (top..bottom, 0..) |y, i| { for (top..bottom, 0..) |y, i| {
const row = fb[y * width .. (y + 1) * width]; const row = fb[y * width .. (y + 1) * width];
for (left..right, 0..) |x, a| { for (left..right, 0..) |x, a| {
const pixel = &row[x]; const pixel = &row[x];
const intensity = buffer[i * cols + a]; const intensity = buffer[i * cols + a];
pixel.* = .{ if (intensity != 0) {
intensity, pixel.* = .{
intensity, intensity,
intensity, intensity,
0xFF, intensity,
}; 0xFF,
};
}
} }
} }
} }
fn print(canvas: Canvas, face: freetype.Face, string: []const u8, pos: [2]u32) !void {
const unicode_view = try std.unicode.Utf8View.init(string);
var unicode_iter = unicode_view.iterator();
var x: i32 = @intCast(pos[0]);
var y: i32 = @intCast(pos[1]);
while (unicode_iter.nextCodepoint()) |codepoint| {
const glyph_index = face.getCharIndex(codepoint) orelse continue;
try face.loadGlyph(glyph_index, .{ .render = true });
const glyph = face.glyph();
const bitmap = glyph.bitmap();
canvas.blitFreetypeBitmap(bitmap, .{ @intCast(x + glyph.bitmapLeft()), @intCast(y - glyph.bitmapTop()) });
x += @intCast(glyph.advance().x >> 6);
y += @intCast(glyph.advance().y >> 6);
}
}
}; };
fn wmbaseHandler(app: *App, header: wayland.Header, body: []const u32) !void { fn wmbaseHandler(app: *App, header: wayland.Header, body: []const u32) !void {
@ -523,10 +541,8 @@ fn surfaceDesktopHandler(app: *App, header: wayland.Header, body: []const u32) !
// render // render
const canvas = try pool.getFramebuffer(.{ width, height }); const canvas = try pool.getFramebuffer(.{ width, height });
try app.font_face.loadChar('H', .{ .render = true });
const bitmap = app.font_face.glyph().bitmap();
canvas.renderGradient(); canvas.renderGradient();
canvas.blitFreetypeBitmap(bitmap, .{ 0, 0 }); try canvas.print(app.font_face, "Hello, World!", .{ 10, 60 });
try canvas.attach(app.surface); try canvas.attach(app.surface);
try app.callbacks.put(canvas.buffer.id, bufferHandler); try app.callbacks.put(canvas.buffer.id, bufferHandler);
}, },