From f871630fa40e6dbe035d55376a90d33c4470e5f6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 28 Nov 2022 10:35:46 -0800 Subject: [PATCH] move Atlas to font --- src/DevMode.zig | 4 ++-- src/{ => font}/Atlas.zig | 2 +- src/font/Group.zig | 11 +++++------ src/font/GroupCache.zig | 15 +++++++-------- src/font/Shaper.zig | 1 - src/font/face/coretext.zig | 7 +++---- src/font/face/freetype.zig | 13 ++++++------- src/font/face/freetype_convert.zig | 4 ++-- src/font/main.zig | 1 + src/font/sprite/Box.zig | 5 ++--- src/font/sprite/Canvas.zig | 4 ++-- src/font/sprite/Face.zig | 3 +-- src/font/sprite/underline.zig | 7 +++---- src/main.zig | 1 - src/renderer/Metal.zig | 5 ++--- src/renderer/OpenGL.zig | 1 - 16 files changed, 37 insertions(+), 47 deletions(-) rename src/{ => font}/Atlas.zig (99%) diff --git a/src/DevMode.zig b/src/DevMode.zig index dd7f5e50a..4e0fc0317 100644 --- a/src/DevMode.zig +++ b/src/DevMode.zig @@ -7,7 +7,7 @@ const imgui = @import("imgui"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; -const Atlas = @import("Atlas.zig"); +const font = @import("font/main.zig"); const Window = @import("Window.zig"); const renderer = @import("renderer.zig"); const Config = @import("config.zig").Config; @@ -141,7 +141,7 @@ fn helpMarker(desc: [:0]const u8) void { } } -fn atlasInfo(self: *const DevMode, atlas: *Atlas, tex: ?usize) !void { +fn atlasInfo(self: *const DevMode, atlas: *font.Atlas, tex: ?usize) !void { _ = self; imgui.text("Dimensions: %d x %d", atlas.size, atlas.size); diff --git a/src/Atlas.zig b/src/font/Atlas.zig similarity index 99% rename from src/Atlas.zig rename to src/font/Atlas.zig index f31d595cd..1bb8fb5f5 100644 --- a/src/Atlas.zig +++ b/src/font/Atlas.zig @@ -19,7 +19,7 @@ const std = @import("std"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; const testing = std.testing; -const fastmem = @import("fastmem.zig"); +const fastmem = @import("../fastmem.zig"); /// Data is the raw texture data. data: []u8, diff --git a/src/font/Group.zig b/src/font/Group.zig index 8eb10600b..90024bdd8 100644 --- a/src/font/Group.zig +++ b/src/font/Group.zig @@ -15,7 +15,6 @@ const assert = std.debug.assert; const Allocator = std.mem.Allocator; const font = @import("main.zig"); -const Atlas = @import("../Atlas.zig"); const DeferredFace = @import("main.zig").DeferredFace; const Face = @import("main.zig").Face; const Library = @import("main.zig").Library; @@ -267,7 +266,7 @@ pub fn faceFromIndex(self: Group, index: FontIndex) !Face { pub fn renderGlyph( self: Group, alloc: Allocator, - atlas: *Atlas, + atlas: *font.Atlas, index: FontIndex, glyph_index: u32, max_height: ?u16, @@ -293,7 +292,7 @@ test { const testEmoji = @import("test.zig").fontEmoji; const testEmojiText = @import("test.zig").fontEmojiText; - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); var lib = try Library.init(); @@ -354,7 +353,7 @@ test "box glyph" { const testing = std.testing; const alloc = testing.allocator; - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); var lib = try Library.init(); @@ -387,7 +386,7 @@ test "resize" { const alloc = testing.allocator; const testFont = @import("test.zig").fontRegular; - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); var lib = try Library.init(); @@ -452,7 +451,7 @@ test "discover monospace with fontconfig and freetype" { try group.addFace(alloc, .regular, (try it.next()).?); // Should find all visible ASCII - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); var i: u32 = 32; while (i < 127) : (i += 1) { diff --git a/src/font/GroupCache.zig b/src/font/GroupCache.zig index 77b488bc8..c6fd1d2c5 100644 --- a/src/font/GroupCache.zig +++ b/src/font/GroupCache.zig @@ -5,7 +5,6 @@ const std = @import("std"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; -const Atlas = @import("../Atlas.zig"); const font = @import("main.zig"); const Face = @import("main.zig").Face; const DeferredFace = @import("main.zig").DeferredFace; @@ -30,8 +29,8 @@ group: Group, /// The texture atlas to store renders in. The GroupCache has to store these /// because the cached Glyph result is dependent on the Atlas. -atlas_greyscale: Atlas, -atlas_color: Atlas, +atlas_greyscale: font.Atlas, +atlas_color: font.Atlas, const CodepointKey = struct { style: Style, @@ -46,9 +45,9 @@ const GlyphKey = struct { /// The GroupCache takes ownership of Group and will free it. pub fn init(alloc: Allocator, group: Group) !GroupCache { - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); errdefer atlas_greyscale.deinit(alloc); - var atlas_color = try Atlas.init(alloc, 512, .rgba); + var atlas_color = try font.Atlas.init(alloc, 512, .rgba); errdefer atlas_color.deinit(alloc); var result: GroupCache = .{ @@ -132,7 +131,7 @@ pub fn renderGlyph( if (gop.found_existing) return gop.value_ptr.*; // Uncached, render it - const atlas: *Atlas = switch (try self.group.presentationFromIndex(index)) { + const atlas: *font.Atlas = switch (try self.group.presentationFromIndex(index)) { .text => &self.atlas_greyscale, .emoji => &self.atlas_color, }; @@ -169,7 +168,7 @@ test { const testFont = @import("test.zig").fontRegular; // const testEmoji = @import("test.zig").fontEmoji; - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); var lib = try Library.init(); @@ -238,7 +237,7 @@ test "resize" { const testFont = @import("test.zig").fontRegular; // const testEmoji = @import("test.zig").fontEmoji; - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); var lib = try Library.init(); diff --git a/src/font/Shaper.zig b/src/font/Shaper.zig index d6e38576c..5b04e495b 100644 --- a/src/font/Shaper.zig +++ b/src/font/Shaper.zig @@ -6,7 +6,6 @@ const assert = std.debug.assert; const Allocator = std.mem.Allocator; const harfbuzz = @import("harfbuzz"); const trace = @import("tracy").trace; -const Atlas = @import("../Atlas.zig"); const font = @import("main.zig"); const Face = @import("main.zig").Face; const DeferredFace = @import("main.zig").DeferredFace; diff --git a/src/font/face/coretext.zig b/src/font/face/coretext.zig index c95d8d724..5452bcd68 100644 --- a/src/font/face/coretext.zig +++ b/src/font/face/coretext.zig @@ -4,7 +4,6 @@ const Allocator = std.mem.Allocator; const macos = @import("macos"); const harfbuzz = @import("harfbuzz"); const font = @import("../main.zig"); -const Atlas = @import("../../Atlas.zig"); pub const Face = struct { /// Our font face @@ -99,7 +98,7 @@ pub const Face = struct { pub fn renderGlyph( self: Face, alloc: Allocator, - atlas: *Atlas, + atlas: *font.Atlas, glyph_index: u32, max_height: ?u16, ) !font.Glyph { @@ -343,7 +342,7 @@ test { const testing = std.testing; const alloc = testing.allocator; - var atlas = try Atlas.init(alloc, 512, .greyscale); + var atlas = try font.Atlas.init(alloc, 512, .greyscale); defer atlas.deinit(alloc); const name = try macos.foundation.String.createWithBytes("Monaco", .utf8, false); @@ -391,7 +390,7 @@ test "in-memory" { const alloc = testing.allocator; const testFont = @import("../test.zig").fontRegular; - var atlas = try Atlas.init(alloc, 512, .greyscale); + var atlas = try font.Atlas.init(alloc, 512, .greyscale); defer atlas.deinit(alloc); var lib = try font.Library.init(); diff --git a/src/font/face/freetype.zig b/src/font/face/freetype.zig index 39d0949a8..eca1da33c 100644 --- a/src/font/face/freetype.zig +++ b/src/font/face/freetype.zig @@ -12,7 +12,6 @@ const resize = @import("stb_image_resize"); const assert = std.debug.assert; const testing = std.testing; const Allocator = std.mem.Allocator; -const Atlas = @import("../../Atlas.zig"); const font = @import("../main.zig"); const Glyph = font.Glyph; const Library = font.Library; @@ -123,7 +122,7 @@ pub const Face = struct { pub fn renderGlyph( self: Face, alloc: Allocator, - atlas: *Atlas, + atlas: *font.Atlas, glyph_index: u32, max_height: ?u16, ) !Glyph { @@ -152,7 +151,7 @@ pub const Face = struct { // or color depth is as expected on the texture atlas. If format is null // it means there is no native color format for our Atlas and we must try // conversion. - const format: ?Atlas.Format = switch (bitmap_ft.pixel_mode) { + const format: ?font.Atlas.Format = switch (bitmap_ft.pixel_mode) { freetype.c.FT_PIXEL_MODE_MONO => null, freetype.c.FT_PIXEL_MODE_GRAY => .greyscale, freetype.c.FT_PIXEL_MODE_BGRA => .rgba, @@ -476,7 +475,7 @@ test { var lib = try Library.init(); defer lib.deinit(); - var atlas = try Atlas.init(alloc, 512, .greyscale); + var atlas = try font.Atlas.init(alloc, 512, .greyscale); defer atlas.deinit(alloc); var ft_font = try Face.init(lib, testFont, .{ .points = 12, .xdpi = 96, .ydpi = 96 }); @@ -508,7 +507,7 @@ test "color emoji" { var lib = try Library.init(); defer lib.deinit(); - var atlas = try Atlas.init(alloc, 512, .rgba); + var atlas = try font.Atlas.init(alloc, 512, .rgba); defer atlas.deinit(alloc); var ft_font = try Face.init(lib, testFont, .{ .points = 12, .xdpi = 96, .ydpi = 96 }); @@ -532,7 +531,7 @@ test "metrics" { var lib = try Library.init(); defer lib.deinit(); - var atlas = try Atlas.init(alloc, 512, .greyscale); + var atlas = try font.Atlas.init(alloc, 512, .greyscale); defer atlas.deinit(alloc); var ft_font = try Face.init(lib, testFont, .{ .points = 12, .xdpi = 96, .ydpi = 96 }); @@ -568,7 +567,7 @@ test "mono to rgba" { var lib = try Library.init(); defer lib.deinit(); - var atlas = try Atlas.init(alloc, 512, .rgba); + var atlas = try font.Atlas.init(alloc, 512, .rgba); defer atlas.deinit(alloc); var ft_font = try Face.init(lib, testFont, .{ .points = 12 }); diff --git a/src/font/face/freetype_convert.zig b/src/font/face/freetype_convert.zig index fd819d7d8..616446027 100644 --- a/src/font/face/freetype_convert.zig +++ b/src/font/face/freetype_convert.zig @@ -3,7 +3,7 @@ //! to do this (likely using SIMD), but I started simple. const std = @import("std"); const freetype = @import("freetype"); -const Atlas = @import("../../Atlas.zig"); +const font = @import("../main.zig"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; @@ -22,7 +22,7 @@ pub const Func = std.meta.FnPtr(fn (Allocator, Bitmap) Allocator.Error!Bitmap); /// Alias for the freetype FT_Bitmap type to make it easier to type. pub const Bitmap = freetype.c.struct_FT_Bitmap_; -const AtlasArray = std.EnumArray(Atlas.Format, ?Func); +const AtlasArray = std.EnumArray(font.Atlas.Format, ?Func); fn genMap() Map { var result: Map = undefined; diff --git a/src/font/main.zig b/src/font/main.zig index ddcfe551f..b6747faea 100644 --- a/src/font/main.zig +++ b/src/font/main.zig @@ -1,6 +1,7 @@ const std = @import("std"); const build_options = @import("build_options"); +pub const Atlas = @import("Atlas.zig"); pub const discovery = @import("discovery.zig"); pub const face = @import("face.zig"); pub const DeferredFace = @import("DeferredFace.zig"); diff --git a/src/font/sprite/Box.zig b/src/font/sprite/Box.zig index fd182dbc2..a536de5d7 100644 --- a/src/font/sprite/Box.zig +++ b/src/font/sprite/Box.zig @@ -17,7 +17,6 @@ const Allocator = std.mem.Allocator; const pixman = @import("pixman"); const font = @import("../main.zig"); -const Atlas = @import("../../Atlas.zig"); const log = std.log.scoped(.box_font); @@ -58,7 +57,7 @@ const Thickness = enum { pub fn renderGlyph( self: Box, alloc: Allocator, - atlas: *Atlas, + atlas: *font.Atlas, cp: u32, ) !font.Glyph { // Create the canvas we'll use to draw @@ -2708,7 +2707,7 @@ test "all" { var cp: u32 = 0x2500; const end = 0x2570; while (cp <= end) : (cp += 1) { - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); const face: Box = .{ .width = 18, .height = 36, .thickness = 2 }; diff --git a/src/font/sprite/Canvas.zig b/src/font/sprite/Canvas.zig index 208ad3593..490092d92 100644 --- a/src/font/sprite/Canvas.zig +++ b/src/font/sprite/Canvas.zig @@ -6,7 +6,7 @@ const std = @import("std"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; const pixman = @import("pixman"); -const Atlas = @import("../../Atlas.zig"); +const font = @import("../main.zig"); /// The underlying image. image: *pixman.Image, @@ -70,7 +70,7 @@ pub fn deinit(self: *Canvas, alloc: Allocator) void { } /// Write the data in this drawing to the atlas. -pub fn writeAtlas(self: *Canvas, alloc: Allocator, atlas: *Atlas) !Atlas.Region { +pub fn writeAtlas(self: *Canvas, alloc: Allocator, atlas: *font.Atlas) !font.Atlas.Region { assert(atlas.format == .greyscale); const width = @intCast(u32, self.image.getWidth()); diff --git a/src/font/sprite/Face.zig b/src/font/sprite/Face.zig index b8829e4f5..0ce55b288 100644 --- a/src/font/sprite/Face.zig +++ b/src/font/sprite/Face.zig @@ -18,7 +18,6 @@ const assert = std.debug.assert; const Allocator = std.mem.Allocator; const font = @import("../main.zig"); const Sprite = font.sprite.Sprite; -const Atlas = @import("../../Atlas.zig"); const Box = @import("Box.zig"); const underline = @import("underline.zig"); @@ -46,7 +45,7 @@ pub fn hasCodepoint(self: Face, cp: u32, p: ?font.Presentation) bool { pub fn renderGlyph( self: Face, alloc: Allocator, - atlas: *Atlas, + atlas: *font.Atlas, cp: u32, ) !font.Glyph { if (std.debug.runtime_safety) assert(self.hasCodepoint(cp, null)); diff --git a/src/font/sprite/underline.zig b/src/font/sprite/underline.zig index 5e1deddef..94036a727 100644 --- a/src/font/sprite/underline.zig +++ b/src/font/sprite/underline.zig @@ -13,12 +13,11 @@ const assert = std.debug.assert; const Allocator = std.mem.Allocator; const font = @import("../main.zig"); const Sprite = font.sprite.Sprite; -const Atlas = @import("../../Atlas.zig"); /// Draw an underline. pub fn renderGlyph( alloc: Allocator, - atlas: *Atlas, + atlas: *font.Atlas, sprite: Sprite, width: u32, height: u32, @@ -172,7 +171,7 @@ test "single" { const testing = std.testing; const alloc = testing.allocator; - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); _ = try renderGlyph( @@ -190,7 +189,7 @@ test "curly" { const testing = std.testing; const alloc = testing.allocator; - var atlas_greyscale = try Atlas.init(alloc, 512, .greyscale); + var atlas_greyscale = try font.Atlas.init(alloc, 512, .greyscale); defer atlas_greyscale.deinit(alloc); _ = try renderGlyph( diff --git a/src/main.zig b/src/main.zig index 13fd654b1..fd25118f5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -207,7 +207,6 @@ fn glfwErrorCallback(code: glfw.Error, desc: [:0]const u8) void { } test { - _ = @import("Atlas.zig"); _ = @import("Pty.zig"); _ = @import("Command.zig"); _ = @import("TempDir.zig"); diff --git a/src/renderer/Metal.zig b/src/renderer/Metal.zig index aa4375179..dd5cd0ab4 100644 --- a/src/renderer/Metal.zig +++ b/src/renderer/Metal.zig @@ -10,7 +10,6 @@ const glfw = @import("glfw"); const objc = @import("objc"); const macos = @import("macos"); const imgui = @import("imgui"); -const Atlas = @import("../Atlas.zig"); const font = @import("../font/main.zig"); const terminal = @import("../terminal/main.zig"); const renderer = @import("../renderer.zig"); @@ -1017,7 +1016,7 @@ fn syncCells( /// Sync the atlas data to the given texture. This copies the bytes /// associated with the atlas to the given texture. If the atlas no longer /// fits into the texture, the texture will be resized. -fn syncAtlasTexture(device: objc.Object, atlas: *const Atlas, texture: *objc.Object) !void { +fn syncAtlasTexture(device: objc.Object, atlas: *const font.Atlas, texture: *objc.Object) !void { const width = texture.getProperty(c_ulong, "width"); if (atlas.size > width) { // Free our old texture @@ -1284,7 +1283,7 @@ fn initPipelineState(device: objc.Object, library: objc.Object) !objc.Object { } /// Initialize a MTLTexture object for the given atlas. -fn initAtlasTexture(device: objc.Object, atlas: *const Atlas) !objc.Object { +fn initAtlasTexture(device: objc.Object, atlas: *const font.Atlas) !objc.Object { // Determine our pixel format const pixel_format: MTLPixelFormat = switch (atlas.format) { .greyscale => .r8unorm, diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index f01debb2f..3116a325f 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -7,7 +7,6 @@ const glfw = @import("glfw"); const assert = std.debug.assert; const testing = std.testing; const Allocator = std.mem.Allocator; -const Atlas = @import("../Atlas.zig"); const font = @import("../font/main.zig"); const imgui = @import("imgui"); const renderer = @import("../renderer.zig");