From c377e19bd0c7d9f4acdc6e859001a61e311805ba Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 28 Aug 2022 12:11:26 -0700 Subject: [PATCH] pkg/harfbuzz: shape --- pkg/harfbuzz/common.zig | 29 +++++++++++++++++++++++++++++ pkg/harfbuzz/main.zig | 1 + pkg/harfbuzz/shape.zig | 19 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 pkg/harfbuzz/shape.zig diff --git a/pkg/harfbuzz/common.zig b/pkg/harfbuzz/common.zig index 8a3589fbd..84b4d000f 100644 --- a/pkg/harfbuzz/common.zig +++ b/pkg/harfbuzz/common.zig @@ -214,3 +214,32 @@ pub const Language = struct { return .{ .handle = c.hb_language_get_default() }; } }; + +/// The hb_feature_t is the structure that holds information about requested +/// feature application. The feature will be applied with the given value to +/// all glyphs which are in clusters between start (inclusive) and end +/// (exclusive). Setting start to HB_FEATURE_GLOBAL_START and end to +/// HB_FEATURE_GLOBAL_END specifies that the feature always applies to the +/// entire buffer. +pub const Feature = extern struct { + tag: c.hb_tag_t, + value: u32, + start: c_uint, + end: c_uint, + + pub fn fromString(str: []const u8) ?Feature { + var f: Feature = undefined; + return if (c.hb_feature_from_string( + str.ptr, + @intCast(c_int, str.len), + &f, + ) > 1) + f + else + null; + } + + pub fn toString(self: *Feature, buf: []u8) void { + c.hb_feature_to_string(self, buf.ptr, @intCast(c_uint, buf.len)); + } +}; diff --git a/pkg/harfbuzz/main.zig b/pkg/harfbuzz/main.zig index c1b7c2da9..85df31b30 100644 --- a/pkg/harfbuzz/main.zig +++ b/pkg/harfbuzz/main.zig @@ -5,6 +5,7 @@ pub usingnamespace @import("common.zig"); pub usingnamespace @import("errors.zig"); pub usingnamespace @import("face.zig"); pub usingnamespace @import("font.zig"); +pub usingnamespace @import("shape.zig"); pub usingnamespace @import("version.zig"); pub const Freetype = @import("freetype.zig"); diff --git a/pkg/harfbuzz/shape.zig b/pkg/harfbuzz/shape.zig new file mode 100644 index 000000000..260fbc85d --- /dev/null +++ b/pkg/harfbuzz/shape.zig @@ -0,0 +1,19 @@ +const std = @import("std"); +const c = @import("c.zig"); +const Font = @import("font.zig").Font; +const Buffer = @import("buffer.zig").Buffer; +const Feature = @import("common.zig").Feature; + +/// Shapes buffer using font turning its Unicode characters content to +/// positioned glyphs. If features is not NULL, it will be used to control +/// the features applied during shaping. If two features have the same tag +/// but overlapping ranges the value of the feature with the higher index +/// takes precedence. +pub fn shape(font: Font, buf: Buffer, features: ?[]const Feature) void { + c.hb_shape( + font.handle, + buf.handle, + if (features) |f| @ptrCast([*]const c.hb_feature_t, f.ptr) else null, + if (features) |f| @intCast(c_uint, f.len) else 0, + ); +}