mach/freetype/examples/single-glyph.zig
Jamie Brandon 15c71f5135 freetype: Avoid dereferencing null bitmap buffer.
For some characters (eg \r) the returned glyph will have .{.rows = 0, .width=0, .pitch = 0, .buffer = null}. In zig null[0..0] causes a panic rather than returning an empty slice.
2022-06-10 12:55:19 -07:00

33 lines
1 KiB
Zig

// zig build run-example-single-glyph -- B
const std = @import("std");
const freetype = @import("freetype");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
const lib = try freetype.Library.init();
defer lib.deinit();
const face = try lib.newFace("upstream/assets/FiraSans-Regular.ttf", 0);
try face.setCharSize(60 * 48, 0, 50, 0);
try face.loadChar(args[1][0], .{ .render = true });
const bitmap = face.glyph().bitmap();
var i: usize = 0;
while (i < bitmap.rows()) : (i += 1) {
var j: usize = 0;
while (j < bitmap.width()) : (j += 1) {
const char: u8 = switch (bitmap.buffer().?[i * bitmap.width() + j]) {
0 => ' ',
1...128 => ';',
else => '#',
};
std.debug.print("{c}", .{char});
}
std.debug.print("\n", .{});
}
}