mach/freetype/examples/single-glyph.zig
iddev5 336134c996 freetype: change example names to match mach convention
General mach examples use ``long-example-name`` case but freetype was
using ``snake_case`` right now, which is inconsistent and midly
inconvenient to use.

Renamed: glyph_to_svg -> glyph-to-svg, single_glyph -> single-glyph
2022-05-25 23:08:45 -07:00

58 lines
1.5 KiB
Zig

const std = @import("std");
const freetype = @import("freetype");
const WIDTH = 32;
const HEIGHT = 24;
fn drawBitmap(bitmap: freetype.Bitmap, x: usize, y: usize) [HEIGHT][WIDTH]u8 {
var figure = std.mem.zeroes([HEIGHT][WIDTH]u8);
var p: usize = 0;
var q: usize = 0;
const w = bitmap.width();
const x_max = x + w;
const y_max = y + bitmap.rows();
var i: usize = 0;
while (i < x_max - x) : (i += 1) {
var j: usize = 0;
while (j < y_max - y) : (j += 1) {
if (i < WIDTH and j < HEIGHT) {
figure[j][i] |= bitmap.buffer()[q * w + p];
q += 1;
}
}
q = 0;
p += 1;
}
return figure;
}
pub fn main() !void {
const lib = try freetype.Library.init();
defer lib.deinit();
const face = try lib.newFace("upstream/assets/FiraSans-Regular.ttf", 0);
defer face.deinit();
try face.setCharSize(40 * 64, 0, 50, 0);
try face.loadChar('@', .{ .render = true });
const glyph = face.glyph;
const x = @intCast(usize, glyph.bitmapLeft());
const y = HEIGHT - @intCast(usize, glyph.bitmapTop());
var figure = drawBitmap(glyph.bitmap(), x, y);
var i: usize = 0;
while (i < HEIGHT) : (i += 1) {
var j: usize = 0;
while (j < WIDTH) : (j += 1) {
const char: u8 = switch (figure[i][j]) {
0 => ' ',
1...128 => ';',
else => '#',
};
std.debug.print("{c}", .{char});
}
std.debug.print("\n", .{});
}
}