it is done

This commit is contained in:
Hamcha 2024-05-08 00:46:05 +02:00
parent 07d34128dd
commit e259535320
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
4 changed files with 30 additions and 2 deletions

2
.gitignore vendored
View file

@ -3,3 +3,5 @@ zig-out
*.bdf *.bdf
out out
*.font.json *.font.json
*.exe
*.bin

View file

@ -16,7 +16,7 @@ const Character = struct {
}; };
const CharacterMap = std.AutoHashMap(u16, Character); const CharacterMap = std.AutoHashMap(u16, Character);
const Font = struct { pub const Font = struct {
name: []const u8, name: []const u8,
height: u8, height: u8,
width: u8, width: u8,

17
src/bin.zig Normal file
View file

@ -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);
}
}
}

View file

@ -1,9 +1,11 @@
const std = @import("std"); const std = @import("std");
const bdf = @import("bdf.zig"); const bdf = @import("bdf.zig");
const bin = @import("bin.zig");
const FontConfig = struct { const FontConfig = struct {
inputFile: []const u8, inputFile: []const u8,
outputFile: []const u8, outputFile: []const u8,
glyphs: []const u16,
}; };
pub fn main() !void { pub fn main() !void {
@ -30,11 +32,18 @@ pub fn main() !void {
const fontConfig = try readConfig(allocator, configFile); const fontConfig = try readConfig(allocator, configFile);
defer fontConfig.deinit(); defer fontConfig.deinit();
// Open and parse BDF font
const input = try std.fs.cwd().openFile(fontConfig.value.inputFile, .{}); const input = try std.fs.cwd().openFile(fontConfig.value.inputFile, .{});
defer input.close(); defer input.close();
var font = try bdf.parse(allocator, input.reader()); var font = try bdf.parse(allocator, input.reader());
defer font.deinit(allocator); 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) { fn readConfig(allocator: std.mem.Allocator, input: []u8) !std.json.Parsed(FontConfig) {