diff --git a/.gitignore b/.gitignore index 7545796..05882e9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ zig-cache zig-out *.bdf out -*.font.json \ No newline at end of file +*.font.json +*.exe +*.bin \ No newline at end of file diff --git a/src/bdf.zig b/src/bdf.zig index 013d2eb..bed10de 100644 --- a/src/bdf.zig +++ b/src/bdf.zig @@ -16,7 +16,7 @@ const Character = struct { }; const CharacterMap = std.AutoHashMap(u16, Character); -const Font = struct { +pub const Font = struct { name: []const u8, height: u8, width: u8, diff --git a/src/bin.zig b/src/bin.zig new file mode 100644 index 0000000..ac6e8cf --- /dev/null +++ b/src/bin.zig @@ -0,0 +1,17 @@ +const std = @import("std"); +const bdf = @import("bdf.zig"); + +const WriteError = error{UnknownGlyph}; + +pub fn writeGlyphs(output: anytype, font: bdf.Font, glyphs: []const u16) !void { + for (glyphs) |glyph| { + const character = font.characters.get(glyph) orelse { + std.log.err("Unknown glyph: {d}", .{glyph}); + return WriteError.UnknownGlyph; + }; + + for (character.bitmap) |line| { + _ = try output.write(line); + } + } +} diff --git a/src/main.zig b/src/main.zig index e0fe883..9a558eb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,9 +1,11 @@ const std = @import("std"); const bdf = @import("bdf.zig"); +const bin = @import("bin.zig"); const FontConfig = struct { inputFile: []const u8, outputFile: []const u8, + glyphs: []const u16, }; pub fn main() !void { @@ -30,11 +32,18 @@ pub fn main() !void { const fontConfig = try readConfig(allocator, configFile); defer fontConfig.deinit(); + // Open and parse BDF font const input = try std.fs.cwd().openFile(fontConfig.value.inputFile, .{}); defer input.close(); var font = try bdf.parse(allocator, input.reader()); defer font.deinit(allocator); + + // Write output + const output = try std.fs.cwd().createFile(fontConfig.value.outputFile, .{}); + defer output.close(); + + try bin.writeGlyphs(output.writer(), font, fontConfig.value.glyphs); } fn readConfig(allocator: std.mem.Allocator, input: []u8) !std.json.Parsed(FontConfig) {