chore: import upstream snapshot with attribution
Test / build-libghostty-vt (x86_64-windows-gnu) (push) Has been cancelled
Test / build-libghostty-vt-macos (aarch64-ios) (push) Has been cancelled
Test / build-libghostty-vt-macos (aarch64-macos) (push) Has been cancelled
Test / build-libghostty-vt-macos (x86_64-macos) (push) Has been cancelled
Test / build-nix (namespace-profile-ghostty-md-arm64) (push) Has been cancelled
Test / build-dist (push) Has been cancelled
Test / GTK x11=true wayland=true (push) Has been cancelled
Test / Build -Dsimd=false (push) Has been cancelled
Test / Build -Dsimd=true (push) Has been cancelled
Test / Build -Dsentry=false (push) Has been cancelled
Test / Build -Dsentry=true (push) Has been cancelled
Test / zig-fmt (push) Has been cancelled
Test / GitHub Actions Pins (push) Has been cancelled
Test / prettier (push) Has been cancelled
Test / swiftlint (push) Has been cancelled
Test / alejandra (push) Has been cancelled
Test / typos (push) Has been cancelled
Nix / Required Checks: Nix (push) Has been cancelled
Nix / check-zig-cache-hash (push) Has been cancelled
Test / skip (push) Has been cancelled
Test / Required Checks: Test (push) Has been cancelled
Test / build-bench (push) Has been cancelled
Test / list-examples (push) Has been cancelled
Test / Example ${{ matrix.dir }} (Windows) (push) Has been cancelled
Test / build-cmake (push) Has been cancelled
Test / test-lib-vt-pkgconfig (push) Has been cancelled
Test / build-flatpak (push) Has been cancelled
Test / build-snap (push) Has been cancelled
Test / build-libghostty-vt (aarch64-linux) (push) Has been cancelled
Test / build-libghostty-vt (aarch64-macos) (push) Has been cancelled
Test / build-libghostty-vt (wasm32-freestanding) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-linux) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-linux-musl) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-macos) (push) Has been cancelled
Test / build-libghostty-vt-android (aarch64-linux-android) (push) Has been cancelled
Test / build-libghostty-vt-android (arm-linux-androideabi) (push) Has been cancelled
Test / build-libghostty-vt-android (x86_64-linux-android) (push) Has been cancelled
Test / build-libghostty-vt-windows (push) Has been cancelled
Test / build-libghostty-windows-gnu (push) Has been cancelled
Test / build-linux (namespace-profile-ghostty-md) (push) Has been cancelled
Test / build-linux (namespace-profile-ghostty-md-arm64) (push) Has been cancelled
Test / build-linux-libghostty (push) Has been cancelled
Test / build-nix (namespace-profile-ghostty-md) (push) Has been cancelled
Test / build-dist-lib-vt (push) Has been cancelled
Test / trigger-snap (push) Has been cancelled
Test / trigger-flatpak (push) Has been cancelled
Test / build-macos (push) Has been cancelled
Test / build-macos-freetype (push) Has been cancelled
Test / test (push) Has been cancelled
Test / test-lib-vt (push) Has been cancelled
Test / GTK x11=false wayland=false (push) Has been cancelled
Test / GTK x11=true wayland=false (push) Has been cancelled
Test / GTK x11=false wayland=true (push) Has been cancelled
Test / test-macos (push) Has been cancelled
Test / test-windows (push) Has been cancelled
Test / Build -Di18n=false (push) Has been cancelled
Test / Build -Di18n=true (push) Has been cancelled
Test / Build test/fuzz-libghostty (push) Has been cancelled
Test / shellcheck (push) Has been cancelled
Test / translations (push) Has been cancelled
Test / blueprint-compiler (push) Has been cancelled
Test / Test pkg/wuffs (push) Has been cancelled
Test / Test build on Debian 13 (push) Has been cancelled
Test / valgrind (push) Has been cancelled
Test / Example ${{ matrix.dir }} (push) Has been cancelled
Test / build-libghostty-vt (x86_64-windows-gnu) (push) Has been cancelled
Test / build-libghostty-vt-macos (aarch64-ios) (push) Has been cancelled
Test / build-libghostty-vt-macos (aarch64-macos) (push) Has been cancelled
Test / build-libghostty-vt-macos (x86_64-macos) (push) Has been cancelled
Test / build-nix (namespace-profile-ghostty-md-arm64) (push) Has been cancelled
Test / build-dist (push) Has been cancelled
Test / GTK x11=true wayland=true (push) Has been cancelled
Test / Build -Dsimd=false (push) Has been cancelled
Test / Build -Dsimd=true (push) Has been cancelled
Test / Build -Dsentry=false (push) Has been cancelled
Test / Build -Dsentry=true (push) Has been cancelled
Test / zig-fmt (push) Has been cancelled
Test / GitHub Actions Pins (push) Has been cancelled
Test / prettier (push) Has been cancelled
Test / swiftlint (push) Has been cancelled
Test / alejandra (push) Has been cancelled
Test / typos (push) Has been cancelled
Nix / Required Checks: Nix (push) Has been cancelled
Nix / check-zig-cache-hash (push) Has been cancelled
Test / skip (push) Has been cancelled
Test / Required Checks: Test (push) Has been cancelled
Test / build-bench (push) Has been cancelled
Test / list-examples (push) Has been cancelled
Test / Example ${{ matrix.dir }} (Windows) (push) Has been cancelled
Test / build-cmake (push) Has been cancelled
Test / test-lib-vt-pkgconfig (push) Has been cancelled
Test / build-flatpak (push) Has been cancelled
Test / build-snap (push) Has been cancelled
Test / build-libghostty-vt (aarch64-linux) (push) Has been cancelled
Test / build-libghostty-vt (aarch64-macos) (push) Has been cancelled
Test / build-libghostty-vt (wasm32-freestanding) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-linux) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-linux-musl) (push) Has been cancelled
Test / build-libghostty-vt (x86_64-macos) (push) Has been cancelled
Test / build-libghostty-vt-android (aarch64-linux-android) (push) Has been cancelled
Test / build-libghostty-vt-android (arm-linux-androideabi) (push) Has been cancelled
Test / build-libghostty-vt-android (x86_64-linux-android) (push) Has been cancelled
Test / build-libghostty-vt-windows (push) Has been cancelled
Test / build-libghostty-windows-gnu (push) Has been cancelled
Test / build-linux (namespace-profile-ghostty-md) (push) Has been cancelled
Test / build-linux (namespace-profile-ghostty-md-arm64) (push) Has been cancelled
Test / build-linux-libghostty (push) Has been cancelled
Test / build-nix (namespace-profile-ghostty-md) (push) Has been cancelled
Test / build-dist-lib-vt (push) Has been cancelled
Test / trigger-snap (push) Has been cancelled
Test / trigger-flatpak (push) Has been cancelled
Test / build-macos (push) Has been cancelled
Test / build-macos-freetype (push) Has been cancelled
Test / test (push) Has been cancelled
Test / test-lib-vt (push) Has been cancelled
Test / GTK x11=false wayland=false (push) Has been cancelled
Test / GTK x11=true wayland=false (push) Has been cancelled
Test / GTK x11=false wayland=true (push) Has been cancelled
Test / test-macos (push) Has been cancelled
Test / test-windows (push) Has been cancelled
Test / Build -Di18n=false (push) Has been cancelled
Test / Build -Di18n=true (push) Has been cancelled
Test / Build test/fuzz-libghostty (push) Has been cancelled
Test / shellcheck (push) Has been cancelled
Test / translations (push) Has been cancelled
Test / blueprint-compiler (push) Has been cancelled
Test / Test pkg/wuffs (push) Has been cancelled
Test / Test build on Debian 13 (push) Has been cancelled
Test / valgrind (push) Has been cancelled
Test / Example ${{ matrix.dir }} (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig").c;
|
||||
const Error = @import("errors.zig").Error;
|
||||
|
||||
/// Data type holding the memory modes available to client programs.
|
||||
///
|
||||
/// Regarding these various memory-modes:
|
||||
///
|
||||
/// - In no case shall the HarfBuzz client modify memory that is passed to
|
||||
/// HarfBuzz in a blob. If there is any such possibility,
|
||||
/// HB_MEMORY_MODE_DUPLICATE should be used such that HarfBuzz makes a
|
||||
/// copy immediately,
|
||||
///
|
||||
/// - Use HB_MEMORY_MODE_READONLY otherwise, unless you really really really
|
||||
/// know what you are doing,
|
||||
///
|
||||
/// - HB_MEMORY_MODE_WRITABLE is appropriate if you really made a copy of
|
||||
/// data solely for the purpose of passing to HarfBuzz and doing that
|
||||
/// just once (no reuse!),
|
||||
///
|
||||
/// - If the font is mmap()ed, it's okay to use
|
||||
/// HB_MEMORY_READONLY_MAY_MAKE_WRITABLE , however, using that mode
|
||||
/// correctly is very tricky. Use HB_MEMORY_MODE_READONLY instead.
|
||||
pub const MemoryMode = enum(u2) {
|
||||
/// HarfBuzz immediately makes a copy of the data.
|
||||
duplicate = c.HB_MEMORY_MODE_DUPLICATE,
|
||||
|
||||
/// HarfBuzz client will never modify the data, and HarfBuzz will never
|
||||
/// modify the data.
|
||||
readonly = c.HB_MEMORY_MODE_READONLY,
|
||||
|
||||
/// HarfBuzz client made a copy of the data solely for HarfBuzz, so
|
||||
/// HarfBuzz may modify the data.
|
||||
writable = c.HB_MEMORY_MODE_WRITABLE,
|
||||
|
||||
/// See above
|
||||
readonly_may_make_writable = c.HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
|
||||
};
|
||||
|
||||
/// Blobs wrap a chunk of binary data to handle lifecycle management of data
|
||||
/// while it is passed between client and HarfBuzz. Blobs are primarily
|
||||
/// used to create font faces, but also to access font face tables, as well as
|
||||
/// pass around other binary data.
|
||||
pub const Blob = struct {
|
||||
handle: *c.hb_blob_t,
|
||||
|
||||
/// Creates a new "blob" object wrapping data . The mode parameter is used
|
||||
/// to negotiate ownership and lifecycle of data .
|
||||
///
|
||||
/// Note that this function returns a freshly-allocated empty blob even
|
||||
/// if length is zero. This is in contrast to hb_blob_create(), which
|
||||
/// returns the singleton empty blob (as returned by hb_blob_get_empty())
|
||||
/// if length is zero.
|
||||
pub fn create(data: []const u8, mode: MemoryMode) Error!Blob {
|
||||
const handle = c.hb_blob_create_or_fail(
|
||||
data.ptr,
|
||||
@intCast(data.len),
|
||||
@intFromEnum(mode),
|
||||
null,
|
||||
null,
|
||||
) orelse return Error.HarfbuzzFailed;
|
||||
|
||||
return Blob{ .handle = handle };
|
||||
}
|
||||
|
||||
/// Decreases the reference count on blob , and if it reaches zero,
|
||||
/// destroys blob , freeing all memory, possibly calling the
|
||||
/// destroy-callback the blob was created for if it has not been
|
||||
/// called already.
|
||||
pub fn destroy(self: *Blob) void {
|
||||
c.hb_blob_destroy(self.handle);
|
||||
}
|
||||
|
||||
/// Attaches a user-data key/data pair to the specified blob.
|
||||
pub fn setUserData(
|
||||
self: Blob,
|
||||
comptime T: type,
|
||||
key: ?*anyopaque,
|
||||
ptr: ?*T,
|
||||
comptime destroycb: ?*const fn (?*T) callconv(.c) void,
|
||||
replace: bool,
|
||||
) bool {
|
||||
const Callback = struct {
|
||||
pub fn callback(data: ?*anyopaque) callconv(.c) void {
|
||||
@call(.{ .modifier = .always_inline }, destroycb, .{
|
||||
@as(?*T, @ptrCast(@alignCast(data))),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return c.hb_blob_set_user_data(
|
||||
self.handle,
|
||||
@ptrCast(key),
|
||||
ptr,
|
||||
if (destroycb != null) Callback.callback else null,
|
||||
if (replace) 1 else 0,
|
||||
) > 0;
|
||||
}
|
||||
|
||||
/// Fetches the user data associated with the specified key, attached to
|
||||
/// the specified font-functions structure.
|
||||
pub fn getUserData(
|
||||
self: Blob,
|
||||
comptime T: type,
|
||||
key: ?*anyopaque,
|
||||
) ?*T {
|
||||
const opt = c.hb_blob_get_user_data(self.handle, @ptrCast(key));
|
||||
if (opt) |ptr|
|
||||
return @ptrCast(@alignCast(ptr))
|
||||
else
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
test {
|
||||
const testing = std.testing;
|
||||
|
||||
const data = "hello";
|
||||
var blob = try Blob.create(data, .readonly);
|
||||
defer blob.destroy();
|
||||
|
||||
var userdata: u8 = 127;
|
||||
var key: u8 = 0;
|
||||
try testing.expect(blob.setUserData(u8, &key, &userdata, null, false));
|
||||
try testing.expect(blob.getUserData(u8, &key).?.* == 127);
|
||||
}
|
||||
Reference in New Issue
Block a user