Compare commits
2 commits
22e1bcccea
...
07d34128dd
Author | SHA1 | Date | |
---|---|---|---|
07d34128dd | |||
543610c6d8 |
3 changed files with 115 additions and 10 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -2,4 +2,4 @@ zig-cache
|
||||||
zig-out
|
zig-out
|
||||||
*.bdf
|
*.bdf
|
||||||
out
|
out
|
||||||
*.font.zon
|
*.font.json
|
85
src/bdf.zig
85
src/bdf.zig
|
@ -134,6 +134,58 @@ fn parseLine(allocator: std.mem.Allocator, line: []const u8) !ParsedCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "parseLine: Parses FONTBOUNDINGBOX correctly" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
const line = "FONTBOUNDINGBOX 10 20";
|
||||||
|
const parsed = try parseLine(allocator, line);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(ParsedCommand{ .FONTBOUNDINGBOX = .{ .width = 10, .height = 20 } }, parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseLine: Parses ENCODING correctly" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
const line = "ENCODING 123";
|
||||||
|
const parsed = try parseLine(allocator, line);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(ParsedCommand{ .ENCODING = 123 }, parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseLine: Parses STARTCHAR correctly" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
const line = "STARTCHAR ABC";
|
||||||
|
const parsed = try parseLine(allocator, line);
|
||||||
|
|
||||||
|
switch (parsed) {
|
||||||
|
.STARTCHAR => |name| {
|
||||||
|
try std.testing.expectEqualStrings("ABC", name);
|
||||||
|
allocator.free(name);
|
||||||
|
},
|
||||||
|
else => unreachable,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseLine: Parses FONT correctly" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
const line = "FONT ABC";
|
||||||
|
const parsed = try parseLine(allocator, line);
|
||||||
|
|
||||||
|
switch (parsed) {
|
||||||
|
.FONT => |name| {
|
||||||
|
try std.testing.expectEqualStrings("ABC", name);
|
||||||
|
allocator.free(name);
|
||||||
|
},
|
||||||
|
else => unreachable,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseLine: Parses BITMAP correctly" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
const line = "BITMAP";
|
||||||
|
const parsed = try parseLine(allocator, line);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(ParsedCommand{ .BITMAP = {} }, parsed);
|
||||||
|
}
|
||||||
|
|
||||||
fn readBitmap(
|
fn readBitmap(
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
width: u8,
|
width: u8,
|
||||||
|
@ -181,3 +233,36 @@ fn readBitmap(
|
||||||
|
|
||||||
return char;
|
return char;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "readBitmap: Reads bitmap correctly for < 8px wide glyphs" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
|
||||||
|
var reader = std.io.fixedBufferStream("10\n20\n30\nENDCHAR");
|
||||||
|
|
||||||
|
const bitmap = try readBitmap(allocator, 8, 3, reader.reader());
|
||||||
|
try std.testing.expectEqual(@as(usize, 3), bitmap.len);
|
||||||
|
try std.testing.expectEqualSlices(u8, &[_]u8{0x10}, bitmap[0]);
|
||||||
|
try std.testing.expectEqualSlices(u8, &[_]u8{0x20}, bitmap[1]);
|
||||||
|
try std.testing.expectEqualSlices(u8, &[_]u8{0x30}, bitmap[2]);
|
||||||
|
|
||||||
|
for (bitmap) |line| {
|
||||||
|
allocator.free(line);
|
||||||
|
}
|
||||||
|
allocator.free(bitmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "readBitmap: Reads bitmap correctly for > 8px wide glyphs" {
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
|
||||||
|
var reader = std.io.fixedBufferStream("1234\nABDC\nENDCHAR");
|
||||||
|
|
||||||
|
const bitmap = try readBitmap(allocator, 16, 2, reader.reader());
|
||||||
|
try std.testing.expectEqual(@as(usize, 2), bitmap.len);
|
||||||
|
try std.testing.expectEqualSlices(u8, &[_]u8{ 0x12, 0x34 }, bitmap[0]);
|
||||||
|
try std.testing.expectEqualSlices(u8, &[_]u8{ 0xAB, 0xDC }, bitmap[1]);
|
||||||
|
|
||||||
|
for (bitmap) |line| {
|
||||||
|
allocator.free(line);
|
||||||
|
}
|
||||||
|
allocator.free(bitmap);
|
||||||
|
}
|
||||||
|
|
38
src/main.zig
38
src/main.zig
|
@ -1,6 +1,11 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const bdf = @import("bdf.zig");
|
const bdf = @import("bdf.zig");
|
||||||
|
|
||||||
|
const FontConfig = struct {
|
||||||
|
inputFile: []const u8,
|
||||||
|
outputFile: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
// Get allocator
|
// Get allocator
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
@ -11,21 +16,36 @@ pub fn main() !void {
|
||||||
const args = try std.process.argsAlloc(allocator);
|
const args = try std.process.argsAlloc(allocator);
|
||||||
defer std.process.argsFree(allocator, args);
|
defer std.process.argsFree(allocator, args);
|
||||||
|
|
||||||
if (args.len < 4) {
|
if (args.len < 2) {
|
||||||
std.log.err("Usage: {s} <input.bdf> <input.zon> <output-dir>", .{args[0]});
|
std.log.err("Usage: {s} <config.json>", .{args[0]});
|
||||||
std.process.exit(1);
|
std.process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputFile = args[1];
|
const inputConfig = args[1];
|
||||||
const inputConfig = args[2];
|
|
||||||
_ = inputConfig; // autofix
|
|
||||||
const outputPath = args[3];
|
|
||||||
_ = outputPath; // autofix
|
|
||||||
|
|
||||||
// Read font
|
// Read config
|
||||||
const input = try std.fs.cwd().openFile(inputFile, .{});
|
const configFile = try std.fs.cwd().readFileAlloc(allocator, inputConfig, std.math.maxInt(usize));
|
||||||
|
defer allocator.free(configFile);
|
||||||
|
|
||||||
|
const fontConfig = try readConfig(allocator, configFile);
|
||||||
|
defer fontConfig.deinit();
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn readConfig(allocator: std.mem.Allocator, input: []u8) !std.json.Parsed(FontConfig) {
|
||||||
|
const fontConfig = try std.json.parseFromSlice(
|
||||||
|
FontConfig,
|
||||||
|
allocator,
|
||||||
|
input,
|
||||||
|
.{
|
||||||
|
.ignore_unknown_fields = true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return fontConfig;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue