From 0a718ec3eb6456e378b2209321c67ae3d78c850a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 1 Jul 2023 13:38:10 -0700 Subject: [PATCH] pkg/macos: do not use bitCast through a function workaround Zig issue: https://github.com/ziglang/zig/issues/16290 --- pkg/macos/foundation/attributed_string.zig | 8 ++++++-- pkg/macos/foundation/base.zig | 6 ++---- pkg/macos/foundation/character_set.zig | 2 +- pkg/macos/graphics/affine_transform.zig | 4 ---- pkg/macos/graphics/context.zig | 2 +- pkg/macos/graphics/geometry.zig | 22 ++++++++++------------ pkg/macos/graphics/path.zig | 8 ++++++-- pkg/macos/text/font_descriptor.zig | 6 +----- pkg/macos/text/line.zig | 6 +----- src/font/discovery.zig | 2 +- 10 files changed, 29 insertions(+), 37 deletions(-) diff --git a/pkg/macos/foundation/attributed_string.zig b/pkg/macos/foundation/attributed_string.zig index a8e647ec9..0c7bb13c3 100644 --- a/pkg/macos/foundation/attributed_string.zig +++ b/pkg/macos/foundation/attributed_string.zig @@ -43,7 +43,7 @@ pub const MutableAttributedString = opaque { ) void { c.CFAttributedStringReplaceString( @ptrCast(self), - range.cval(), + @bitCast(range), @ptrCast(replacement), ); } @@ -64,11 +64,15 @@ pub const MutableAttributedString = opaque { c.CFAttributedStringSetAttribute( @ptrCast(self), - range.cval(), + @bitCast(range), @ptrCast(key_arg), value, ); } + + pub fn getLength(self: *MutableAttributedString) usize { + return @intCast(c.CFAttributedStringGetLength(@ptrCast(self))); + } }; test "mutable attributed string" { diff --git a/pkg/macos/foundation/base.zig b/pkg/macos/foundation/base.zig index 0584a1767..bfd436115 100644 --- a/pkg/macos/foundation/base.zig +++ b/pkg/macos/foundation/base.zig @@ -1,3 +1,5 @@ +const std = @import("std"); +const assert = std.debug.assert; const c = @import("c.zig"); pub const ComparisonResult = enum(c_int) { @@ -13,8 +15,4 @@ pub const Range = extern struct { pub fn init(loc: usize, len: usize) Range { return @bitCast(c.CFRangeMake(@intCast(loc), @intCast(len))); } - - pub fn cval(self: Range) c.CFRange { - return @bitCast(self); - } }; diff --git a/pkg/macos/foundation/character_set.zig b/pkg/macos/foundation/character_set.zig index 62438a052..b6df66542 100644 --- a/pkg/macos/foundation/character_set.zig +++ b/pkg/macos/foundation/character_set.zig @@ -19,7 +19,7 @@ pub const CharacterSet = opaque { ) Allocator.Error!*CharacterSet { return @as(?*CharacterSet, @ptrFromInt(@intFromPtr(c.CFCharacterSetCreateWithCharactersInRange( null, - range.cval(), + @bitCast(range), )))) orelse Allocator.Error.OutOfMemory; } diff --git a/pkg/macos/graphics/affine_transform.zig b/pkg/macos/graphics/affine_transform.zig index 9cbde25e8..dd07f125a 100644 --- a/pkg/macos/graphics/affine_transform.zig +++ b/pkg/macos/graphics/affine_transform.zig @@ -13,8 +13,4 @@ pub const AffineTransform = extern struct { pub fn identity() AffineTransform { return @bitCast(c.CGAffineTransformIdentity); } - - pub fn cval(self: AffineTransform) c.struct_CGAffineTransform { - return @bitCast(self); - } }; diff --git a/pkg/macos/graphics/context.zig b/pkg/macos/graphics/context.zig index 15d0fd906..6e1ac5016 100644 --- a/pkg/macos/graphics/context.zig +++ b/pkg/macos/graphics/context.zig @@ -121,7 +121,7 @@ pub fn Context(comptime T: type) type { pub fn setTextMatrix(self: *T, matrix: graphics.AffineTransform) void { c.CGContextSetTextMatrix( @ptrCast(self), - matrix.cval(), + @bitCast(matrix), ); } diff --git a/pkg/macos/graphics/geometry.zig b/pkg/macos/graphics/geometry.zig index 77e67d770..14dee2b42 100644 --- a/pkg/macos/graphics/geometry.zig +++ b/pkg/macos/graphics/geometry.zig @@ -1,12 +1,10 @@ +const std = @import("std"); +const assert = std.debug.assert; const c = @import("c.zig"); pub const Point = extern struct { x: c.CGFloat, y: c.CGFloat, - - pub fn cval(self: Point) c.struct_CGPoint { - return @bitCast(self); - } }; pub const Rect = extern struct { @@ -17,20 +15,20 @@ pub const Rect = extern struct { return @bitCast(c.CGRectMake(x, y, width, height)); } - pub fn cval(self: Rect) c.struct_CGRect { - return @bitCast(self); + pub fn isNull(self: Rect) bool { + return c.CGRectIsNull(@bitCast(self)); } - pub fn isNull(self: Rect) bool { - return c.CGRectIsNull(self.cval()); + pub fn getHeight(self: Rect) c.CGFloat { + return c.CGRectGetHeight(@bitCast(self)); + } + + pub fn getWidth(self: Rect) c.CGFloat { + return c.CGRectGetWidth(@bitCast(self)); } }; pub const Size = extern struct { width: c.CGFloat, height: c.CGFloat, - - pub fn cval(self: Size) c.struct_CGSize { - return @bitCast(self); - } }; diff --git a/pkg/macos/graphics/path.zig b/pkg/macos/graphics/path.zig index 8a7bf7f6c..1dea355ea 100644 --- a/pkg/macos/graphics/path.zig +++ b/pkg/macos/graphics/path.zig @@ -13,7 +13,7 @@ pub const Path = opaque { return @as( ?*Path, @ptrFromInt(@intFromPtr(c.CGPathCreateWithRect( - rect.cval(), + @bitCast(rect), @ptrCast(transform), ))), ) orelse Allocator.Error.OutOfMemory; @@ -44,9 +44,13 @@ pub const MutablePath = opaque { c.CGPathAddRect( @ptrCast(self), @ptrCast(transform), - rect.cval(), + @bitCast(rect), ); } + + pub fn getBoundingBox(self: *MutablePath) graphics.Rect { + return @bitCast(c.CGPathGetBoundingBox(@ptrCast(self))); + } }; test "mutable path" { diff --git a/pkg/macos/text/font_descriptor.zig b/pkg/macos/text/font_descriptor.zig index 6a33cf245..f7f8097a1 100644 --- a/pkg/macos/text/font_descriptor.zig +++ b/pkg/macos/text/font_descriptor.zig @@ -159,7 +159,7 @@ pub const FontTraitKey = enum { } }; -pub const FontSymbolicTraits = packed struct { +pub const FontSymbolicTraits = packed struct(u32) { italic: bool = false, bold: bool = false, _unused1: u3 = 0, @@ -179,10 +179,6 @@ pub const FontSymbolicTraits = packed struct { return @as(FontSymbolicTraits, @bitCast(raw)); } - pub fn cval(self: FontSymbolicTraits) c.CTFontSymbolicTraits { - return @as(c.CTFontSymbolicTraits, @bitCast(self)); - } - test { try std.testing.expectEqual( @bitSizeOf(c.CTFontSymbolicTraits), diff --git a/pkg/macos/text/line.zig b/pkg/macos/text/line.zig index d392643e4..e6f798bed 100644 --- a/pkg/macos/text/line.zig +++ b/pkg/macos/text/line.zig @@ -32,7 +32,7 @@ pub const Line = opaque { ) graphics.Rect { return @bitCast(c.CTLineGetBoundsWithOptions( @ptrCast(self), - opts.cval(), + @bitCast(opts), )); } @@ -60,10 +60,6 @@ pub const LineBoundsOptions = packed struct { language_extents: bool = false, _padding: u58 = 0, - pub fn cval(self: LineBoundsOptions) c.CTLineBoundsOptions { - return @bitCast(self); - } - test { try std.testing.expectEqual( @bitSizeOf(c.CTLineBoundsOptions), diff --git a/src/font/discovery.zig b/src/font/discovery.zig index 6cfc0d888..2ef7080d8 100644 --- a/src/font/discovery.zig +++ b/src/font/discovery.zig @@ -126,7 +126,7 @@ pub const Descriptor = struct { .bold = self.bold, .italic = self.italic, }; - const traits_cval = traits.cval(); + const traits_cval: u32 = @bitCast(traits); if (traits_cval > 0) { // Setting traits is a pain. We have to create a nested dictionary // of the symbolic traits value, and set that in our attributes.