pkg/fontconfig: pattern.add

This commit is contained in:
Mitchell Hashimoto
2022-09-16 09:55:45 -07:00
parent c68c487e14
commit 5b215409c6
3 changed files with 37 additions and 4 deletions

View File

@ -74,7 +74,7 @@ pub const Property = enum {
font_has_hint, font_has_hint,
order, order,
fn cval(self: Property) [:0]const u8 { pub fn cval(self: Property) [:0]const u8 {
@setEvalBranchQuota(10_000); @setEvalBranchQuota(10_000);
inline for (@typeInfo(Property).Enum.fields) |field| { inline for (@typeInfo(Property).Enum.fields) |field| {
if (self == @field(Property, field.name)) { if (self == @field(Property, field.name)) {

View File

@ -2,6 +2,7 @@ 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 ObjectSet = @import("main.zig").ObjectSet; const ObjectSet = @import("main.zig").ObjectSet;
const Property = @import("main.zig").Property;
const Result = @import("main.zig").Result; const Result = @import("main.zig").Result;
const Value = @import("main.zig").Value; const Value = @import("main.zig").Value;
const ValueBinding = @import("main.zig").ValueBinding; const ValueBinding = @import("main.zig").ValueBinding;
@ -23,8 +24,17 @@ pub const Pattern = opaque {
c.FcDefaultSubstitute(self.cval()); c.FcDefaultSubstitute(self.cval());
} }
pub fn delete(self: *Pattern, obj: [:0]const u8) bool { pub fn add(self: *Pattern, prop: Property, value: Value, append: bool) bool {
return c.FcPatternDel(self.cval(), obj.ptr) == c.FcTrue; return c.FcPatternAdd(
self.cval(),
prop.cval(),
value.cval(),
if (append) c.FcTrue else c.FcFalse,
) == c.FcTrue;
}
pub fn delete(self: *Pattern, prop: Property) bool {
return c.FcPatternDel(self.cval(), prop.cval()) == c.FcTrue;
} }
pub fn filter(self: *Pattern, os: *const ObjectSet) *Pattern { pub fn filter(self: *Pattern, os: *const ObjectSet) *Pattern {
@ -124,8 +134,12 @@ pub const Pattern = opaque {
}; };
test "create" { test "create" {
const testing = std.testing;
var pat = Pattern.create(); var pat = Pattern.create();
defer pat.destroy(); defer pat.destroy();
try testing.expect(pat.add(.family, .{ .string = "monospace" }, false));
} }
test "name parse" { test "name parse" {

View File

@ -25,7 +25,7 @@ pub const Value = union(Type) {
@"void": void, @"void": void,
integer: u32, integer: u32,
double: f64, double: f64,
string: []const u8, string: [:0]const u8,
@"bool": bool, @"bool": bool,
matrix: *const Matrix, matrix: *const Matrix,
char_set: *const CharSet, char_set: *const CharSet,
@ -48,6 +48,25 @@ pub const Value = union(Type) {
.range => .{ .range = @ptrCast(*const Range, cvalue.u.r) }, .range => .{ .range = @ptrCast(*const Range, cvalue.u.r) },
}; };
} }
pub fn cval(self: Value) c.struct__FcValue {
return .{
.@"type" = @enumToInt(std.meta.activeTag(self)),
.u = switch (self) {
.unknown => undefined,
.@"void" => undefined,
.integer => |v| .{ .i = @intCast(c_int, v) },
.double => |v| .{ .d = v },
.string => |v| .{ .s = v },
.@"bool" => |v| .{ .b = if (v) c.FcTrue else c.FcFalse },
.matrix => |v| .{ .m = @ptrCast(*const c.struct__FcMatrix, v) },
.char_set => |v| .{ .c = @ptrCast(*const c.struct__FcCharSet, v) },
.ft_face => |v| .{ .f = v },
.lang_set => |v| .{ .l = @ptrCast(*const c.struct__FcLangSet, v) },
.range => |v| .{ .r = @ptrCast(*const c.struct__FcRange, v) },
},
};
}
}; };
pub const ValueBinding = enum(c_int) { pub const ValueBinding = enum(c_int) {