pkg/fontconfig: add weights/slants enum

This commit is contained in:
Mitchell Hashimoto
2022-09-16 10:08:49 -07:00
parent 5b215409c6
commit d69f8397f3
4 changed files with 109 additions and 85 deletions

View File

@ -1,6 +1,110 @@
const std = @import("std");
const c = @import("c.zig");
pub const Weight = enum(c_uint) {
thin = c.FC_WEIGHT_THIN,
extralight = c.FC_WEIGHT_EXTRALIGHT,
light = c.FC_WEIGHT_LIGHT,
demilight = c.FC_WEIGHT_DEMILIGHT,
book = c.FC_WEIGHT_BOOK,
regular = c.FC_WEIGHT_REGULAR,
medium = c.FC_WEIGHT_MEDIUM,
demibold = c.FC_WEIGHT_DEMIBOLD,
bold = c.FC_WEIGHT_BOLD,
extrabold = c.FC_WEIGHT_EXTRABOLD,
black = c.FC_WEIGHT_BLACK,
extrablack = c.FC_WEIGHT_EXTRABLACK,
};
pub const Slant = enum(c_uint) {
roman = c.FC_SLANT_ROMAN,
italic = c.FC_SLANT_ITALIC,
oblique = c.FC_SLANT_OBLIQUE,
};
pub const Property = enum {
family,
style,
slant,
weight,
size,
aspect,
pixel_size,
spacing,
foundry,
antialias,
hinting,
hint_style,
vertical_layout,
autohint,
global_advance,
width,
file,
index,
ft_face,
rasterizer,
outline,
scalable,
color,
variable,
scale,
symbol,
dpi,
rgba,
minspace,
source,
charset,
lang,
fontversion,
fullname,
familylang,
stylelang,
fullnamelang,
capability,
embolden,
embedded_bitmap,
decorative,
lcd_filter,
font_features,
font_variations,
namelang,
prgname,
hash,
postscript_name,
font_has_hint,
order,
pub fn cval(self: Property) [:0]const u8 {
@setEvalBranchQuota(10_000);
inline for (@typeInfo(Property).Enum.fields) |field| {
if (self == @field(Property, field.name)) {
// Build our string in a comptime context so it is a binary
// constant and not stack allocated.
return comptime name: {
// Replace _ with ""
var buf: [field.name.len]u8 = undefined;
const count = std.mem.replace(u8, field.name, "_", "", &buf);
const replaced = buf[0 .. field.name.len - count];
// Build our string
var name: [replaced.len:0]u8 = undefined;
std.mem.copy(u8, &name, replaced);
name[replaced.len] = 0;
break :name &name;
};
}
}
unreachable;
}
test "cval" {
const testing = std.testing;
try testing.expectEqualStrings("family", Property.family.cval());
try testing.expectEqualStrings("pixelsize", Property.pixel_size.cval());
}
};
pub const Result = enum(c_uint) {
match = c.FcResultMatch,
no_match = c.FcResultNoMatch,

View File

@ -1,5 +1,6 @@
const std = @import("std");
const c = @import("c.zig");
const Property = @import("main.zig").Property;
pub const ObjectSet = opaque {
pub fn create() *ObjectSet {
@ -22,89 +23,6 @@ pub const ObjectSet = opaque {
}
};
pub const Property = enum {
family,
style,
slant,
weight,
size,
aspect,
pixel_size,
spacing,
foundry,
antialias,
hinting,
hint_style,
vertical_layout,
autohint,
global_advance,
width,
file,
index,
ft_face,
rasterizer,
outline,
scalable,
color,
variable,
scale,
symbol,
dpi,
rgba,
minspace,
source,
charset,
lang,
fontversion,
fullname,
familylang,
stylelang,
fullnamelang,
capability,
embolden,
embedded_bitmap,
decorative,
lcd_filter,
font_features,
font_variations,
namelang,
prgname,
hash,
postscript_name,
font_has_hint,
order,
pub fn cval(self: Property) [:0]const u8 {
@setEvalBranchQuota(10_000);
inline for (@typeInfo(Property).Enum.fields) |field| {
if (self == @field(Property, field.name)) {
// Build our string in a comptime context so it is a binary
// constant and not stack allocated.
return comptime name: {
// Replace _ with ""
var buf: [field.name.len]u8 = undefined;
const count = std.mem.replace(u8, field.name, "_", "", &buf);
const replaced = buf[0 .. field.name.len - count];
// Build our string
var name: [replaced.len:0]u8 = undefined;
std.mem.copy(u8, &name, replaced);
name[replaced.len] = 0;
break :name &name;
};
}
}
unreachable;
}
test "cval" {
const testing = std.testing;
try testing.expectEqualStrings("family", Property.family.cval());
try testing.expectEqualStrings("pixelsize", Property.pixel_size.cval());
}
};
test "create" {
const testing = std.testing;

View File

@ -6,6 +6,7 @@ const Property = @import("main.zig").Property;
const Result = @import("main.zig").Result;
const Value = @import("main.zig").Value;
const ValueBinding = @import("main.zig").ValueBinding;
const Weight = @import("main.zig").Weight;
pub const Pattern = opaque {
pub fn create() *Pattern {
@ -140,6 +141,7 @@ test "create" {
defer pat.destroy();
try testing.expect(pat.add(.family, .{ .string = "monospace" }, false));
try testing.expect(pat.add(.weight, .{ .integer = @enumToInt(Weight.bold) }, false));
}
test "name parse" {

View File

@ -23,7 +23,7 @@ pub const Type = enum(c_int) {
pub const Value = union(Type) {
unknown: void,
@"void": void,
integer: u32,
integer: i32,
double: f64,
string: [:0]const u8,
@"bool": bool,
@ -38,7 +38,7 @@ pub const Value = union(Type) {
.unknown => .{ .unknown = {} },
.@"void" => .{ .@"void" = {} },
.string => .{ .string = std.mem.sliceTo(cvalue.u.s, 0) },
.integer => .{ .integer = @intCast(u32, cvalue.u.i) },
.integer => .{ .integer = @intCast(i32, cvalue.u.i) },
.double => .{ .double = cvalue.u.d },
.@"bool" => .{ .@"bool" = cvalue.u.b == c.FcTrue },
.matrix => .{ .matrix = @ptrCast(*const Matrix, cvalue.u.m) },