freetype: initial import @ 4e2b158

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Ali Chraghi 2022-05-22 23:50:21 -07:00 committed by Stephen Gutekanst
parent 0d2675507d
commit b50dade2fd
20 changed files with 2252 additions and 0 deletions

48
freetype/src/utils.zig Normal file
View file

@ -0,0 +1,48 @@
const std = @import("std");
const meta = std.meta;
const mem = std.mem;
const testing = std.testing;
pub fn structToBitFields(comptime IntType: type, comptime EnumDataType: type, flags: anytype) IntType {
var value: IntType = 0;
inline for (comptime meta.fieldNames(EnumDataType)) |field_name| {
if (@field(flags, field_name)) {
value |= @enumToInt(@field(EnumDataType, field_name));
}
}
return value;
}
pub fn bitFieldsToStruct(comptime StructType: type, comptime EnumDataType: type, flags: anytype) StructType {
var value = mem.zeroes(StructType);
inline for (comptime meta.fieldNames(EnumDataType)) |field_name| {
if (flags & (@enumToInt(@field(EnumDataType, field_name))) != 0) {
@field(value, field_name) = true;
}
}
return value;
}
const TestEnum = enum(u16) {
filed_1 = (1 << 1),
filed_2 = (1 << 2),
filed_3 = (1 << 3),
};
const TestStruct = packed struct {
filed_1: bool = false,
filed_2: bool = false,
filed_3: bool = false,
};
test "struct fields to bit fields" {
try testing.expectEqual(@as(u16, (1 << 1) | (1 << 3)), structToBitFields(u16, TestEnum, TestStruct{
.filed_1 = true,
.filed_3 = true,
}));
try testing.expectEqual(@as(u16, 0), structToBitFields(u16, TestEnum, TestStruct{}));
}
test "bit fields to struct" {
try testing.expectEqual(TestStruct{ .filed_1 = true, .filed_2 = true, .filed_3 = false }, bitFieldsToStruct(TestStruct, TestEnum, (1 << 1) | (1 << 2)));
}