From a1dda2c37ff87c54354ce9a048781c492ed888e9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 12 Dec 2022 21:33:02 -0800 Subject: [PATCH] font: faceFromIndex returns a pointer to the face --- src/font/Group.zig | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/font/Group.zig b/src/font/Group.zig index dbe9cd499..ae4bc54bd 100644 --- a/src/font/Group.zig +++ b/src/font/Group.zig @@ -245,11 +245,11 @@ pub fn presentationFromIndex(self: Group, index: FontIndex) !font.Presentation { /// Return the Face represented by a given FontIndex. Note that special /// fonts (i.e. box glyphs) do not have a face. -pub fn faceFromIndex(self: Group, index: FontIndex) !Face { +pub fn faceFromIndex(self: Group, index: FontIndex) !*Face { if (index.special() != null) return error.SpecialHasNoFace; const deferred = &self.faces.get(index.style).items[@intCast(usize, index.idx)]; try deferred.load(self.lib, self.size); - return deferred.face.?; + return &deferred.face.?; } /// Render a glyph by glyph index into the given font atlas and return @@ -556,3 +556,27 @@ test "discover monospace with fontconfig and freetype" { ); } } + +test "faceFromIndex returns pointer" { + const testing = std.testing; + const alloc = testing.allocator; + const testFont = @import("test.zig").fontRegular; + + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); + defer atlas_greyscale.deinit(alloc); + + var lib = try Library.init(); + defer lib.deinit(); + + var group = try init(alloc, lib, .{ .points = 12, .xdpi = 96, .ydpi = 96 }); + defer group.deinit(); + + try group.addFace(alloc, .regular, DeferredFace.initLoaded(try Face.init(lib, testFont, .{ .points = 12, .xdpi = 96, .ydpi = 96 }))); + + { + const idx = group.indexForCodepoint('A', .regular, null).?; + const face1 = try group.faceFromIndex(idx); + const face2 = try group.faceFromIndex(idx); + try testing.expectEqual(@ptrToInt(face1), @ptrToInt(face2)); + } +}