Compare commits
No commits in common. "0003a52aaff5e40f060516f720b53e2ec361fa6e" and "09c890970e1dce8b0c1cf0bf298b502265c1a6f9" have entirely different histories.
0003a52aaf
...
09c890970e
1 changed files with 10 additions and 12 deletions
|
@ -2,31 +2,29 @@ const FontFace = @This();
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// it seems that Windows only supports font faces with up to 31 characters,
|
// it seems that Windows only supports font faces with up to 31 characters
|
||||||
// but we use a larger buffer here because GetFamilyNames can apparently
|
pub const max = 31;
|
||||||
// return longer strings
|
|
||||||
pub const max = 254;
|
|
||||||
|
|
||||||
buf: [max + 1]u16,
|
buf: [max + 1]u16,
|
||||||
len: usize,
|
len: u5,
|
||||||
|
|
||||||
pub fn initUtf8(utf8: []const u8) error{ TooLong, InvalidUtf8 }!FontFace {
|
pub fn initUtf8(utf8: []const u8) error{ TooLong, InvalidUtf8 }!FontFace {
|
||||||
const utf16_len = std.unicode.calcUtf16LeLen(utf8) catch return error.InvalidUtf8;
|
const utf16_len = std.unicode.calcUtf16LeLen(utf8) catch return error.InvalidUtf8;
|
||||||
if (utf16_len > max)
|
if (utf16_len > max)
|
||||||
return error.TooLong;
|
return error.TooLong;
|
||||||
var self: FontFace = .{ .buf = undefined, .len = utf16_len };
|
var result: FontFace = .{ .buf = undefined, .len = @intCast(utf16_len) };
|
||||||
const actual_len = try std.unicode.utf8ToUtf16Le(&self.buf, utf8);
|
result.buf[utf16_len] = 0;
|
||||||
|
const actual_len = try std.unicode.utf8ToUtf16Le(&result.buf, utf8);
|
||||||
std.debug.assert(actual_len == utf16_len);
|
std.debug.assert(actual_len == utf16_len);
|
||||||
self.buf[actual_len] = 0;
|
return result;
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ptr(self: *const FontFace) [*:0]const u16 {
|
pub fn ptr(self: *const FontFace) [*:0]const u16 {
|
||||||
return self.slice().ptr;
|
std.debug.assert(self.buf[@as(usize, self.len)] == 0);
|
||||||
|
return @ptrCast(&self.buf);
|
||||||
}
|
}
|
||||||
pub fn slice(self: *const FontFace) [:0]const u16 {
|
pub fn slice(self: *const FontFace) [:0]const u16 {
|
||||||
std.debug.assert(self.buf[self.len] == 0);
|
return self.ptr()[0..self.len :0];
|
||||||
return self.buf[0..self.len :0];
|
|
||||||
}
|
}
|
||||||
pub fn eql(self: *const FontFace, other: *const FontFace) bool {
|
pub fn eql(self: *const FontFace, other: *const FontFace) bool {
|
||||||
return std.mem.eql(u16, self.slice(), other.slice());
|
return std.mem.eql(u16, self.slice(), other.slice());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue