summaryrefslogtreecommitdiff
path: root/pkg/macos/text/font.zig
blob: ea37891f5f39ddb6eec34ad92ba1388f4bed005f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig");
const graphics = @import("../graphics.zig");
const text = @import("../text.zig");
const c = @import("c.zig").c;

pub const Font = opaque {
    pub fn createWithFontDescriptor(desc: *const text.FontDescriptor, size: f32) Allocator.Error!*Font {
        return @as(
            ?*Font,
            @ptrFromInt(@intFromPtr(c.CTFontCreateWithFontDescriptor(
                @ptrCast(desc),
                size,
                null,
            ))),
        ) orelse Allocator.Error.OutOfMemory;
    }

    pub fn createForString(
        self: *Font,
        str: *foundation.String,
        range: foundation.Range,
    ) ?*Font {
        return @ptrCast(@constCast(c.CTFontCreateForString(
            @ptrCast(self),
            @ptrCast(str),
            @bitCast(range),
        )));
    }

    pub fn copyWithAttributes(
        self: *Font,
        size: f32,
        matrix: ?*const graphics.AffineTransform,
        attrs: ?*text.FontDescriptor,
    ) Allocator.Error!*Font {
        return @as(
            ?*Font,
            @ptrFromInt(@intFromPtr(c.CTFontCreateCopyWithAttributes(
                @ptrCast(self),
                size,
                @ptrCast(matrix),
                @ptrCast(attrs),
            ))),
        ) orelse Allocator.Error.OutOfMemory;
    }

    pub fn release(self: *Font) void {
        c.CFRelease(self);
    }

    pub fn retain(self: *Font) void {
        _ = c.CFRetain(self);
    }

    pub fn copyDescriptor(self: *Font) *text.FontDescriptor {
        return @ptrCast(@constCast(c.CTFontCopyFontDescriptor(@ptrCast(self))));
    }

    pub fn copyFeatures(self: *Font) *foundation.Array {
        return @ptrCast(@constCast(c.CTFontCopyFeatures(@ptrCast(self))));
    }

    pub fn copyDefaultCascadeListForLanguages(self: *Font) *foundation.Array {
        return @ptrCast(@constCast(c.CTFontCopyDefaultCascadeListForLanguages(@ptrCast(self), null)));
    }

    pub fn copyTable(self: *Font, tag: FontTableTag) ?*foundation.Data {
        return @ptrCast(@constCast(c.CTFontCopyTable(
            @ptrCast(self),
            @intFromEnum(tag),
            c.kCTFontTableOptionNoOptions,
        )));
    }

    pub fn getGlyphCount(self: *Font) usize {
        return @intCast(c.CTFontGetGlyphCount(@ptrCast(self)));
    }

    pub fn getGlyphsForCharacters(self: *Font, chars: []const u16, glyphs: []graphics.Glyph) bool {
        assert(chars.len == glyphs.len);
        return c.CTFontGetGlyphsForCharacters(
            @ptrCast(self),
            chars.ptr,
            glyphs.ptr,
            @intCast(chars.len),
        );
    }

    pub fn createPathForGlyph(self: *Font, glyph: graphics.Glyph) ?*graphics.Path {
        return @ptrCast(@constCast(c.CTFontCreatePathForGlyph(
            @ptrCast(self),
            glyph,
            null,
        )));
    }

    pub fn drawGlyphs(
        self: *Font,
        glyphs: []const graphics.Glyph,
        positions: []const graphics.Point,
        context: anytype, // Must be some context type from graphics
    ) void {
        assert(positions.len == glyphs.len);
        c.CTFontDrawGlyphs(
            @ptrCast(self),
            glyphs.ptr,
            @ptrCast(positions.ptr),
            glyphs.len,
            @ptrCast(context),
        );
    }

    pub fn getBoundingRectsForGlyphs(
        self: *Font,
        orientation: FontOrientation,
        glyphs: []const graphics.Glyph,
        rects: ?[]graphics.Rect,
    ) graphics.Rect {
        if (rects) |s| assert(glyphs.len == s.len);
        return @bitCast(c.CTFontGetBoundingRectsForGlyphs(
            @ptrCast(self),
            @intFromEnum(orientation),
            glyphs.ptr,
            @ptrCast(if (rects) |s| s.ptr else null),
            @intCast(glyphs.len),
        ));
    }

    pub fn getAdvancesForGlyphs(
        self: *Font,
        orientation: FontOrientation,
        glyphs: []const graphics.Glyph,
        advances: ?[]graphics.Size,
    ) f64 {
        if (advances) |s| assert(glyphs.len == s.len);
        return c.CTFontGetAdvancesForGlyphs(
            @ptrCast(self),
            @intFromEnum(orientation),
            glyphs.ptr,
            @ptrCast(if (advances) |s| s.ptr else null),
            @intCast(glyphs.len),
        );
    }

    pub fn copyAttribute(self: *Font, comptime attr: text.FontAttribute) ?attr.Value() {
        return @ptrFromInt(@intFromPtr(c.CTFontCopyAttribute(
            @ptrCast(self),
            @ptrCast(attr.key()),
        )));
    }

    pub fn copyFamilyName(self: *Font) *foundation.String {
        return @ptrFromInt(@intFromPtr(c.CTFontCopyFamilyName(@ptrCast(self))));
    }

    pub fn copyDisplayName(self: *Font) *foundation.String {
        return @ptrFromInt(@intFromPtr(c.CTFontCopyDisplayName(@ptrCast(self))));
    }

    pub fn copyPostScriptName(self: *Font) *foundation.String {
        return @ptrFromInt(@intFromPtr(c.CTFontCopyPostScriptName(@ptrCast(self))));
    }

    pub fn getSymbolicTraits(self: *Font) text.FontSymbolicTraits {
        return @bitCast(c.CTFontGetSymbolicTraits(@ptrCast(self)));
    }

    pub fn getAscent(self: *Font) f64 {
        return c.CTFontGetAscent(@ptrCast(self));
    }

    pub fn getDescent(self: *Font) f64 {
        return c.CTFontGetDescent(@ptrCast(self));
    }

    pub fn getLeading(self: *Font) f64 {
        return c.CTFontGetLeading(@ptrCast(self));
    }

    pub fn getBoundingBox(self: *Font) graphics.Rect {
        return @bitCast(c.CTFontGetBoundingBox(@ptrCast(self)));
    }

    pub fn getUnderlinePosition(self: *Font) f64 {
        return c.CTFontGetUnderlinePosition(@ptrCast(self));
    }

    pub fn getUnderlineThickness(self: *Font) f64 {
        return c.CTFontGetUnderlineThickness(@ptrCast(self));
    }

    pub fn getCapHeight(self: *Font) f64 {
        return c.CTFontGetCapHeight(@ptrCast(self));
    }

    pub fn getXHeight(self: *Font) f64 {
        return c.CTFontGetXHeight(@ptrCast(self));
    }

    pub fn getUnitsPerEm(self: *Font) u32 {
        return c.CTFontGetUnitsPerEm(@ptrCast(self));
    }

    pub fn getSize(self: *Font) f64 {
        return c.CTFontGetSize(@ptrCast(self));
    }
};

pub const FontOrientation = enum(c_uint) {
    default = c.kCTFontOrientationDefault,
    horizontal = c.kCTFontOrientationHorizontal,
    vertical = c.kCTFontOrientationVertical,
};

pub const FontTableTag = enum(u32) {
    svg = c.kCTFontTableSVG,
    os2 = c.kCTFontTableOS2,
    head = c.kCTFontTableHead,
    hhea = c.kCTFontTableHhea,
    post = c.kCTFontTablePost,
    _,

    pub fn init(v: *const [4]u8) FontTableTag {
        const raw: u32 = @bitCast(foundation.FourCharCode.init(v));
        return @enumFromInt(raw);
    }
};

test {
    const testing = std.testing;

    const name = try foundation.String.createWithBytes("Monaco", .utf8, false);
    defer name.release();
    const desc = try text.FontDescriptor.createWithNameAndSize(name, 12);
    defer desc.release();

    const font = try Font.createWithFontDescriptor(desc, 12);
    defer font.release();

    // Traits
    {
        const traits = font.getSymbolicTraits();
        try testing.expect(!traits.color_glyphs);
    }

    var glyphs = [1]graphics.Glyph{0};
    try testing.expect(font.getGlyphsForCharacters(
        &[_]u16{'A'},
        &glyphs,
    ));
    try testing.expect(glyphs[0] > 0);

    // Bounding rect
    {
        var rect = font.getBoundingRectsForGlyphs(.horizontal, &glyphs, null);
        try testing.expect(rect.size.width > 0);

        var singles: [1]graphics.Rect = undefined;
        rect = font.getBoundingRectsForGlyphs(.horizontal, &glyphs, &singles);
        try testing.expect(rect.size.width > 0);
        try testing.expect(singles[0].size.width > 0);
    }

    // Advances
    {
        var advance = font.getAdvancesForGlyphs(.horizontal, &glyphs, null);
        try testing.expect(advance > 0);

        var singles: [1]graphics.Size = undefined;
        advance = font.getAdvancesForGlyphs(.horizontal, &glyphs, &singles);
        try testing.expect(advance > 0);
        try testing.expect(singles[0].width > 0);
    }

    // Draw
    {
        const cs = try graphics.ColorSpace.createDeviceGray();
        defer cs.release();
        const ctx = try graphics.BitmapContext.create(null, 80, 80, 8, 80, cs, 0);
        const context = graphics.BitmapContext.context;
        defer context.release(ctx);

        var pos = [_]graphics.Point{.{ .x = 0, .y = 0 }};
        font.drawGlyphs(
            &glyphs,
            &pos,
            ctx,
        );
    }
}

test "copy" {
    const name = try foundation.String.createWithBytes("Monaco", .utf8, false);
    defer name.release();
    const desc = try text.FontDescriptor.createWithNameAndSize(name, 12);
    defer desc.release();

    const font = try Font.createWithFontDescriptor(desc, 12);
    defer font.release();

    const f2 = try font.copyWithAttributes(14, null, null);
    defer f2.release();
}