pkg/fontconfig: pattern get

This commit is contained in:
Mitchell Hashimoto
2022-09-17 09:04:40 -07:00
parent 86f7d0e04e
commit 9750ac87fd
3 changed files with 34 additions and 0 deletions

View File

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const c = @import("c.zig"); const c = @import("c.zig");
const Error = @import("main.zig").Error;
pub const Weight = enum(c_uint) { pub const Weight = enum(c_uint) {
thin = c.FC_WEIGHT_THIN, thin = c.FC_WEIGHT_THIN,
@ -111,6 +112,16 @@ pub const Result = enum(c_uint) {
type_mismatch = c.FcResultTypeMismatch, type_mismatch = c.FcResultTypeMismatch,
no_id = c.FcResultNoId, no_id = c.FcResultNoId,
out_of_memory = c.FcResultOutOfMemory, out_of_memory = c.FcResultOutOfMemory,
pub fn toError(self: Result) Error!void {
return switch (self) {
.match => {},
.no_match => Error.FontconfigNoMatch,
.type_mismatch => Error.FontconfigTypeMismatch,
.no_id => Error.FontconfigNoId,
.out_of_memory => Error.OutOfMemory,
};
}
}; };
pub const MatchKind = enum(c_uint) { pub const MatchKind = enum(c_uint) {

View File

@ -1,3 +1,7 @@
pub const Error = error{ pub const Error = error{
OutOfMemory,
FontconfigFailed, FontconfigFailed,
FontconfigNoMatch,
FontconfigTypeMismatch,
FontconfigNoId,
}; };

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const assert = std.debug.assert; const assert = std.debug.assert;
const c = @import("c.zig"); const c = @import("c.zig");
const Error = @import("main.zig").Error;
const ObjectSet = @import("main.zig").ObjectSet; const ObjectSet = @import("main.zig").ObjectSet;
const Property = @import("main.zig").Property; const Property = @import("main.zig").Property;
const Result = @import("main.zig").Result; const Result = @import("main.zig").Result;
@ -34,6 +35,18 @@ pub const Pattern = opaque {
) == c.FcTrue; ) == c.FcTrue;
} }
pub fn get(self: *Pattern, prop: Property, id: u32) Error!Value {
var val: c.struct__FcValue = undefined;
try @intToEnum(Result, c.FcPatternGet(
self.cval(),
prop.cval(),
@intCast(c_int, id),
&val,
)).toError();
return Value.init(&val);
}
pub fn delete(self: *Pattern, prop: Property) bool { pub fn delete(self: *Pattern, prop: Property) bool {
return c.FcPatternDel(self.cval(), prop.cval()) == c.FcTrue; return c.FcPatternDel(self.cval(), prop.cval()) == c.FcTrue;
} }
@ -142,6 +155,12 @@ test "create" {
try testing.expect(pat.add(.family, .{ .string = "monospace" }, false)); try testing.expect(pat.add(.family, .{ .string = "monospace" }, false));
try testing.expect(pat.add(.weight, .{ .integer = @enumToInt(Weight.bold) }, false)); try testing.expect(pat.add(.weight, .{ .integer = @enumToInt(Weight.bold) }, false));
{
const val = try pat.get(.family, 0);
try testing.expect(val == .string);
try testing.expectEqualStrings("monospace", val.string);
}
} }
test "name parse" { test "name parse" {