A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(native): a platform only names the font face a glyph needs, and the rasterizer maps each face's file once, so a new glyph no longer reads a whole system font
yhh8b9d8eb08595
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1be797fdb..aa8a64a08 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,6 +22,7 @@ published separately; it ships inside the editor.### Fixed+- **A new glyph on a native build no longer stalls the frame reading a whole system font** (#106). Every native host (Android, iOS, macOS, Windows, Linux) read the matched font file whole on every call, which is every glyph rasterized, and the rasterizer then dropped those bytes for a face it had already parsed. A system CJK font is tens of MB: on a Snapdragon 8 Elite phone a new CJK glyph took 9.7 ms (the first 63 ms), all of it the read. A platform now only answers which file and which face (Platform::matchFont), and the shared rasterizer maps each face's file the first time it sees it, so the OS pages in only the tables and glyphs it touches. The same glyphs now cost 0.02 ms on the phone; on Windows a new CJK glyph, rasterized and turned into a distance field, costs 0.9 ms.- **Android frames no longer drop to 30 fps when they come close to the frame budget** (#105). The Android host drew each frame from a Choreographer callback and asked for the next one after the frame, whose present had already blocked until the display took it: two clocks, and a frame that ran past one vsync waited for a second. A 15 ms frame on a 60 fps cap ran at 30 fps on a Snapdragon 8 Elite, every interval 33 ms; a shooter in the issue saw 33/50/66 ms. The host now runs its own loop, as the desktop one does: the present paces it, input is served while it waits, and a frame is drawn when one is due even while a fast touch panel keeps the queue full. The same 15 ms frame holds 60 fps (p99 18.5 ms), and a 20 ms one runs at 46 fps instead of halving. A backgrounded app still draws nothing and uses no CPU. The cap App.setTargetFrameRate sets is now held by one pacer every native host shares (the web loop's rule: a cadence, restarted after a stall), so the desktop and iOS hosts, which ignored it, keep it too.- **A native build reports its GPU frame time.** The desktop and Android hosts never asked the GPU for timestamps, so the engine's GPU frame timer and the profiler's GPU track read -1 on every device. They ask for them now where the GPU has them.- **Voxel GI costs less on mobile GPUs.** The gather, which only reads the cache, reads it as a plain array, and the per-frame ageing reads and writes each slot plainly (each invocation owns one) and leaves empty slots alone; both had gone through atomics. On a Snapdragon 8 Elite phone at 2670x1200 a frame with one ray per point went from 134 to 83 ms, and with a 4-voxel range from 32 to 17 ms. Voxel GI is still too heavy for a phone at its defaults (about 0.8 s a frame).diff --git a/native/host/Host.hpp b/native/host/Host.hppindex 23c628c1d..87603782c 100644--- a/native/host/Host.hpp+++ b/native/host/Host.hpp@@ -64,14 +64,16 @@ struct FetchResult {};/**- * A font file the glyph rasterizer can parse. `path` identifies it (the cache- * key — empty means the platform found nothing), `faceIndex` selects a face- * inside a collection (.ttc), and `bytes` is the file, read only the first time- * a path is seen.+ * Which face draws a glyph: the file it lives in (`path`, empty when the platform+ * found nothing) and which face inside it (`faceIndex`, for a collection — .ttc).+ * Only the answer: opening the file is the rasterizer's, once per face for the+ * life of the host (media/glyph_raster.cpp). Every platform used to read the+ * whole file into this on every call, which is every glyph rasterized, and a CJK+ * system font is tens of MB: a frame stalled each time a new glyph appeared+ * (issue #106).*/-struct FontFile {+struct FontMatch {std::string path;- std::vector<esengine::u8> bytes;int faceIndex = 0;/*** What the matched file does NOT provide of the style that was asked for, so@@ -210,14 +212,16 @@ struct Platform {virtual void log(bool error, const char* message) = 0;- /** A font for @p family that covers @p codepoint, with @p style's bold/italic+ /** The face for @p family that covers @p codepoint, with @p style's bold/italic* bits (see GLYPH_BOLD / GLYPH_ITALIC) applied to the match. Empty `path`* when the platform has nothing — the glyph then simply does not draw.*- * This is the ONLY per-OS part of text: which file to open. Android asks its- * font matcher (which handles CJK fallback), iOS asks Core Text; the- * rasterization and the SDF conversion are shared (see media/glyph_raster.hpp). */- virtual FontFile loadFont(const std::string& family, esengine::u32 codepoint, int style) = 0;+ * This is the ONLY per-OS part of text: which face. Android asks its font+ * matcher (which handles CJK fallback), Apple Core Text, Linux fontconfig,+ * Windows DirectWrite; reading the file, the rasterization and the SDF+ * conversion are shared (see media/glyph_raster.hpp). Called per glyph, so it+ * must not touch the file. */+ virtual FontMatch matchFont(const std::string& family, esengine::u32 codepoint, int style) = 0;/*** Coverage the platform drew itself, padded and supersampled as the shareddiff --git a/native/host/media/glyph_raster.cpp b/native/host/media/glyph_raster.cppindex bf4586748..37551ef61 100644--- a/native/host/media/glyph_raster.cpp+++ b/native/host/media/glyph_raster.cpp@@ -8,6 +8,7 @@#include "Host.hpp"#include "Runtime.hpp"+#include "mapped_file.hpp"#include "esengine/text/SdfGenerator.hpp"@@ -28,21 +29,22 @@ namespace {// grid as edge wobble. The SDK's canvas rasterizer supersamples by the same 4.constexpr int SDF_SUPERSAMPLE = 4;-/** A parsed font file, kept for the life of the host: fonts are few, glyphs many. */+/** A parsed font file, kept for the life of the host: fonts are few, glyphs many. Mapped, not read (mapped_file.hpp): the+ * parser touches the tables and the glyphs drawn, which is a small part of a CJK font. */struct Font {- std::vector<uint8_t> bytes;+ MappedFile file;stbtt_fontinfo info{};bool ok = false;};/** Parsed fonts by file AND face — the matcher answers both per glyph, and only- * a face we have not seen costs a read + parse. Keyed by the pair because a+ * a face we have not seen costs a map + parse. Keyed by the pair because a* collection holds several faces at one path: keyed by path alone, whichever* face was parsed first answers for every other one in the same file. */std::unordered_map<std::string, std::unique_ptr<Font>> g_fonts;/** The cache key: a path names a file, a file may hold many faces. */-std::string faceKey(const FontFile& file) {+std::string faceKey(const FontMatch& file) {return file.path + '#' + std::to_string(file.faceIndex);}@@ -55,7 +57,7 @@ struct FontLookup {FontLookup loadFont(Platform& platform, const std::string& family, uint32_t codepoint, int style,bool& syntheticBold, bool& syntheticItalic) {- FontFile file = platform.loadFont(family, codepoint, style);+ const FontMatch file = platform.matchFont(family, codepoint, style);syntheticBold = file.syntheticBold;syntheticItalic = file.syntheticItalic;if (file.path.empty()) return {};@@ -65,10 +67,14 @@ FontLookup loadFont(Platform& platform, const std::string& family, uint32_t codeif (it != g_fonts.end()) return {it->second->ok ? it->second.get() : nullptr, !it->second->ok};auto font = std::make_unique<Font>();- font->bytes = std::move(file.bytes);- const int offset = stbtt_GetFontOffsetForIndex(font->bytes.data(), file.faceIndex);- if (offset >= 0 && stbtt_InitFont(&font->info, font->bytes.data(), offset)) {- font->ok = true;+ if (font->file.open(file.path)) {+ const int offset = stbtt_GetFontOffsetForIndex(font->file.data(), file.faceIndex);+ if (offset >= 0 && stbtt_InitFont(&font->info, font->file.data(), offset)) font->ok = true;+ } else {+ // A file the matcher named and nobody can open: cached all the same, so it is not retried for every glyph.+ ESHOST_LOGE("font: %s could not be opened", file.path.c_str());+ g_fonts.emplace(key, std::move(font));+ return {};}Font* raw = font.get();if (!raw->ok) {diff --git a/native/host/media/glyph_raster.hpp b/native/host/media/glyph_raster.hppindex 0442094e5..d75b39c16 100644--- a/native/host/media/glyph_raster.hpp+++ b/native/host/media/glyph_raster.hpp@@ -11,7 +11,7 @@* identically on both platforms.** Only picking the font file is per-OS (Android's font matcher, Core- * Text on iOS); it sits behind {@link eshost::Platform::loadFont}.+ * Text on iOS); it sits behind {@link eshost::Platform::matchFont}.** @author ESEngine Team* @date 2026diff --git a/native/host/media/mapped_file.hpp b/native/host/media/mapped_file.hppnew file mode 100644index 000000000..731c80f13--- /dev/null+++ b/native/host/media/mapped_file.hpp@@ -0,0 +1,154 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file mapped_file.hpp+ * @brief A read-only file mapped into memory, for the glyph rasterizer's fonts.+ *+ * @details A system CJK font is tens of MB and a game draws a few hundred of its+ * glyphs. Mapped, the OS pages in only what the parser touches and+ * shares those pages with every other process using the same font;+ * read, every byte is copied off storage before the first glyph. Where a+ * mapping is refused the file is read whole instead, so the caller sees+ * the same bytes either way. Move-only: the mapping lives as long as the+ * object does.+ */+#pragma once++#include <cstddef>+#include <cstdint>+#include <cstdio>+#include <string>+#include <utility>+#include <vector>++#if defined(_WIN32)+# ifndef WIN32_LEAN_AND_MEAN+# define WIN32_LEAN_AND_MEAN+# endif+# ifndef NOMINMAX+# define NOMINMAX+# endif+# include <windows.h>+#else+# include <fcntl.h>+# include <sys/mman.h>+# include <sys/stat.h>+# include <unistd.h>+#endif++namespace eshost {++class MappedFile {+public:+ MappedFile() = default;+ MappedFile(const MappedFile&) = delete;+ MappedFile& operator=(const MappedFile&) = delete;+ MappedFile(MappedFile&& other) noexcept { *this = std::move(other); }+ MappedFile& operator=(MappedFile&& other) noexcept {+ if (this != &other) {+ release();+ data_ = std::exchange(other.data_, nullptr);+ size_ = std::exchange(other.size_, 0);+ mapped_ = std::exchange(other.mapped_, false);+ copy_ = std::move(other.copy_);+ }+ return *this;+ }+ ~MappedFile() { release(); }++ /** Maps @p path (UTF-8). False, and empty, when it cannot be opened or is empty. */+ bool open(const std::string& path) {+ release();+ if (path.empty()) return false;+ if (map(path)) return true;+ return readWhole(path);+ }++ const uint8_t* data() const { return data_; }+ size_t size() const { return size_; }+ bool empty() const { return size_ == 0; }+ /** Mapped rather than copied: for a log line, and for the test that keeps it so. */+ bool mapped() const { return mapped_; }++private:+ const uint8_t* data_ = nullptr;+ size_t size_ = 0;+ bool mapped_ = false;+ std::vector<uint8_t> copy_;++#if defined(_WIN32)+ static std::wstring widen(const std::string& s) {+ const int n = MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), nullptr, 0);+ std::wstring w((size_t)(n > 0 ? n : 0), L'\0');+ if (n > 0) MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), w.data(), n);+ return w;+ }+ bool map(const std::string& path) {+ HANDLE file = CreateFileW(widen(path).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,+ FILE_ATTRIBUTE_NORMAL, nullptr);+ if (file == INVALID_HANDLE_VALUE) return false;+ LARGE_INTEGER size{};+ HANDLE mapping = nullptr;+ if (GetFileSizeEx(file, &size) && size.QuadPart > 0) {+ mapping = CreateFileMappingW(file, nullptr, PAGE_READONLY, 0, 0, nullptr);+ }+ CloseHandle(file); // the mapping holds the file open+ if (!mapping) return false;+ void* view = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);+ CloseHandle(mapping); // and the view holds the mapping+ if (!view) return false;+ data_ = static_cast<const uint8_t*>(view);+ size_ = (size_t)size.QuadPart;+ mapped_ = true;+ return true;+ }+ void unmap() { UnmapViewOfFile(data_); }+#else+ bool map(const std::string& path) {+ const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);+ if (fd < 0) return false;+ struct stat st{};+ void* view = MAP_FAILED;+ if (fstat(fd, &st) == 0 && st.st_size > 0) {+ view = mmap(nullptr, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);+ }+ ::close(fd); // the mapping holds the file+ if (view == MAP_FAILED) return false;+ data_ = static_cast<const uint8_t*>(view);+ size_ = (size_t)st.st_size;+ mapped_ = true;+ return true;+ }+ void unmap() { munmap(const_cast<uint8_t*>(data_), size_); }+#endif++ bool readWhole(const std::string& path) {+#if defined(_WIN32)+ FILE* f = _wfopen(widen(path).c_str(), L"rb");+#else+ FILE* f = std::fopen(path.c_str(), "rb");+#endif+ if (!f) return false;+ std::fseek(f, 0, SEEK_END);+ const long size = std::ftell(f);+ std::fseek(f, 0, SEEK_SET);+ if (size > 0) {+ copy_.resize((size_t)size);+ if (std::fread(copy_.data(), 1, copy_.size(), f) != copy_.size()) copy_.clear();+ }+ std::fclose(f);+ data_ = copy_.empty() ? nullptr : copy_.data();+ size_ = copy_.size();+ return size_ > 0;+ }++ void release() {+ if (mapped_ && data_) unmap();+ data_ = nullptr;+ size_ = 0;+ mapped_ = false;+ copy_.clear();+ }+};++} // namespace eshostdiff --git a/native/host/platform/android.cpp b/native/host/platform/android.cppindex 813af5450..ab1d8d519 100644--- a/native/host/platform/android.cpp+++ b/native/host/platform/android.cpp@@ -268,8 +268,8 @@ struct AndroidPlatform final : eshost::Platform {// installed families, applies weight/italic, and — the part worth having —// falls back per codepoint, so CJK text resolves to Noto without the host// hard-coding a single font path.- eshost::FontFile loadFont(const std::string& family, u32 codepoint, int style) override {- eshost::FontFile out;+ eshost::FontMatch matchFont(const std::string& family, u32 codepoint, int style) override {+ eshost::FontMatch out;AFontMatcher* matcher = AFontMatcher_create();if (!matcher) return out;AFontMatcher_setStyle(matcher, (style & eshost::GLYPH_BOLD) ? AFONT_WEIGHT_BOLD : AFONT_WEIGHT_NORMAL,@@ -301,19 +301,8 @@ struct AndroidPlatform final : eshost::Platform {if (const char* path = AFont_getFontFilePath(font)) {out.path = path;out.faceIndex = (int)AFont_getCollectionIndex(font);- if (FILE* f = fopen(path, "rb")) {- fseek(f, 0, SEEK_END);- const long size = ftell(f);- fseek(f, 0, SEEK_SET);- if (size > 0) {- out.bytes.resize((size_t)size);- if (fread(out.bytes.data(), 1, out.bytes.size(), f) != out.bytes.size()) out.bytes.clear();- }- fclose(f);- }}AFont_close(font);- if (out.bytes.empty()) out.path.clear();return out;}diff --git a/native/host/platform/apple_common.hpp b/native/host/platform/apple_common.hppindex 3e1936c63..7ead7dcd1 100644--- a/native/host/platform/apple_common.hpp+++ b/native/host/platform/apple_common.hpp@@ -27,12 +27,12 @@ namespace eshost {/*** A font file for @p family covering @p codepoint, with @p style's bold/italic- * bits (GLYPH_BOLD / GLYPH_ITALIC) applied — implements Platform::loadFont.+ * bits (GLYPH_BOLD / GLYPH_ITALIC) applied — implements Platform::matchFont.** Core Text resolves the family and CTFontCreateForString picks a face that* covers the codepoint: the CJK fallback, without a hard-coded path.*/-FontFile appleLoadFont(const std::string& family, esengine::u32 codepoint, int style);+FontMatch appleMatchFont(const std::string& family, esengine::u32 codepoint, int style);/*** Coverage for @p codepoint drawn by Core Text — implements Platform::drawGlyph.diff --git a/native/host/platform/apple_common.mm b/native/host/platform/apple_common.mmindex 75974060e..411c156ef 100644--- a/native/host/platform/apple_common.mm+++ b/native/host/platform/apple_common.mm@@ -84,8 +84,8 @@ CTFontRef matchFace(const std::string& family, u32 codepoint, int style, CGFloatreturn font;}-FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {- FontFile out;+FontMatch appleMatchFont(const std::string& family, u32 codepoint, int style) {+ FontMatch out;CTFontRef font = matchFace(family, codepoint, style, 16.0,&out.syntheticBold, &out.syntheticItalic);if (!font) return out;@@ -119,13 +119,7 @@ FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {CFRelease(font);NSString* path = [(__bridge NSURL*)url path];CFRelease(url);- if (!path) return out;-- NSData* data = [NSData dataWithContentsOfFile:path];- if (!data) return out;- out.path = std::string([path UTF8String]);- out.bytes.resize(data.length);- memcpy(out.bytes.data(), data.bytes, data.length);+ if (path) out.path = std::string([path UTF8String]);return out;}diff --git a/native/host/platform/desktop.cpp b/native/host/platform/desktop.cppindex 21ad6717b..02618f07b 100644--- a/native/host/platform/desktop.cpp+++ b/native/host/platform/desktop.cpp@@ -301,8 +301,8 @@ struct DesktopPlatform final : eshost::Platform {}#if defined(__APPLE__)- eshost::FontFile loadFont(const std::string& family, u32 codepoint, int style) override {- return eshost::appleLoadFont(family, codepoint, style);+ eshost::FontMatch matchFont(const std::string& family, u32 codepoint, int style) override {+ return eshost::appleMatchFont(family, codepoint, style);}// Core Text draws what stb_truetype cannot read — see Platform::drawGlyph.@@ -313,14 +313,14 @@ struct DesktopPlatform final : eshost::Platform {void startFetch(const eshost::FetchRequest& req) override { eshost::appleStartFetch(req); }#elif defined(_WIN32)- eshost::FontFile loadFont(const std::string& family, u32 codepoint, int style) override {- return eshost::windowsLoadFont(family, codepoint, style);+ eshost::FontMatch matchFont(const std::string& family, u32 codepoint, int style) override {+ return eshost::windowsMatchFont(family, codepoint, style);}void startFetch(const eshost::FetchRequest& req) override { eshost::windowsStartFetch(req); }#else- eshost::FontFile loadFont(const std::string& family, u32 codepoint, int style) override {- return eshost::linuxLoadFont(family, codepoint, style);+ eshost::FontMatch matchFont(const std::string& family, u32 codepoint, int style) override {+ return eshost::linuxMatchFont(family, codepoint, style);}void startFetch(const eshost::FetchRequest& req) override { eshost::linuxStartFetch(req); }diff --git a/native/host/platform/ios.mm b/native/host/platform/ios.mmindex d9dfb3ff4..db2ddc523 100644--- a/native/host/platform/ios.mm+++ b/native/host/platform/ios.mm@@ -158,8 +158,8 @@ struct IOSPlatform final : eshost::Platform {NSLog(@"[Estella]%s %s", error ? " ERROR:" : "", message);}- eshost::FontFile loadFont(const std::string& family, u32 codepoint, int style) override {- return eshost::appleLoadFont(family, codepoint, style);+ eshost::FontMatch matchFont(const std::string& family, u32 codepoint, int style) override {+ return eshost::appleMatchFont(family, codepoint, style);}// Core Text draws what stb_truetype cannot read — see Platform::drawGlyph.diff --git a/native/host/platform/linux_common.cpp b/native/host/platform/linux_common.cppindex 6c56c2050..6f0a0ad3b 100644--- a/native/host/platform/linux_common.cpp+++ b/native/host/platform/linux_common.cpp@@ -37,21 +37,6 @@ FcConfig* fonts() {return config;}-std::vector<u8> readWholeFile(const char* path) {- std::vector<u8> out;- std::FILE* file = std::fopen(path, "rb");- if (!file) return out;- std::fseek(file, 0, SEEK_END);- const long size = std::ftell(file);- std::fseek(file, 0, SEEK_SET);- if (size > 0) {- out.resize(static_cast<size_t>(size));- if (std::fread(out.data(), 1, out.size(), file) != out.size()) out.clear();- }- std::fclose(file);- return out;-}-/** The default family, matching the role Segoe UI plays on Windows and Helvetica* on Apple: whatever this machine calls its sans-serif, which fontconfig knows* and no distribution spells the same way. */@@ -59,8 +44,8 @@ constexpr const char* kDefaultFamily = "sans-serif";} // namespace-FontFile linuxLoadFont(const std::string& family, u32 codepoint, int style) {- FontFile out;+FontMatch linuxMatchFont(const std::string& family, u32 codepoint, int style) {+ FontMatch out;FcConfig* config = fonts();if (!config) return out;@@ -90,11 +75,9 @@ FontFile linuxLoadFont(const std::string& family, u32 codepoint, int style) {int index = 0;if (FcPatternGetString(matched, FC_FILE, 0, &file) == FcResultMatch && file) {out.path = reinterpret_cast<const char*>(file);- out.bytes = readWholeFile(out.path.c_str());- if (out.bytes.empty()) out.path.clear();}// A collection (.ttc) holds several faces, so the index travels with the- // bytes or the rasterizer reads the wrong one.+ // path or the rasterizer reads the wrong one.if (FcPatternGetInteger(matched, FC_INDEX, 0, &index) == FcResultMatch) out.faceIndex = index;// What the matched file does NOT provide of what was asked for. fontconfigdiff --git a/native/host/platform/linux_common.hpp b/native/host/platform/linux_common.hppindex 804b82d61..8fe27ff0d 100644--- a/native/host/platform/linux_common.hpp+++ b/native/host/platform/linux_common.hpp@@ -28,11 +28,11 @@namespace eshost {/** A font file for @p family covering @p codepoint, with @p style's bold/italic- * bits applied — implements Platform::loadFont.+ * bits applied — implements Platform::matchFont.** fontconfig falls back across the installed fonts when given the codepoint as a* required charset: the CJK path, with no per-distribution path hard-coded. */-FontFile linuxLoadFont(const std::string& family, esengine::u32 codepoint, int style);+FontMatch linuxMatchFont(const std::string& family, esengine::u32 codepoint, int style);/** Run @p req on a libcurl worker thread and hand the reply to deliverFetch —* see Platform::startFetch. */diff --git a/native/host/platform/windows_common.cpp b/native/host/platform/windows_common.cppindex e7f46747d..7bbeb2231 100644--- a/native/host/platform/windows_common.cpp+++ b/native/host/platform/windows_common.cpp@@ -66,8 +66,9 @@ IDWriteFactory* factory() {}/** The file behind a font face, and which face inside it. A collection (.ttc)- * holds several, so the index has to travel with the bytes. */-bool readFaceFile(IDWriteFontFace* face, FontFile& out) {+ * holds several, so the index has to travel with the path. Only a face on disk+ * has one: a font from another loader (a memory font) answers false. */+bool faceFilePath(IDWriteFontFace* face, FontMatch& out) {UINT32 fileCount = 0;if (FAILED(face->GetFiles(&fileCount, nullptr)) || fileCount == 0) return false;std::vector<IDWriteFontFile*> files(fileCount, nullptr);@@ -87,23 +88,9 @@ bool readFaceFile(IDWriteFontFace* face, FontFile& out) {std::wstring path(length + 1, L'\0');if (SUCCEEDED(local->GetFilePathFromKey(key, keySize, path.data(), length + 1))) {path.resize(length);- if (HANDLE handle = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ,- nullptr, OPEN_EXISTING,- FILE_ATTRIBUTE_NORMAL, nullptr);- handle != INVALID_HANDLE_VALUE) {- LARGE_INTEGER size{};- if (GetFileSizeEx(handle, &size) && size.QuadPart > 0) {- out.bytes.resize((size_t)size.QuadPart);- DWORD read = 0;- ok = ReadFile(handle, out.bytes.data(), (DWORD)out.bytes.size(), &read, nullptr)- && read == out.bytes.size();- }- CloseHandle(handle);- }- if (ok) {- out.path = narrow(path);- out.faceIndex = (int)face->GetIndex();- }+ out.path = narrow(path);+ out.faceIndex = (int)face->GetIndex();+ ok = !out.path.empty();}}local->Release();@@ -116,8 +103,8 @@ bool readFaceFile(IDWriteFontFace* face, FontFile& out) {} // namespace-FontFile windowsLoadFont(const std::string& family, u32 codepoint, int style) {- FontFile out;+FontMatch windowsMatchFont(const std::string& family, u32 codepoint, int style) {+ FontMatch out;IDWriteFactory* dw = factory();if (!dw) return out;@@ -181,7 +168,7 @@ FontFile windowsLoadFont(const std::string& family, u32 codepoint, int style) {IDWriteFontFace* face = nullptr;if (SUCCEEDED(font->CreateFontFace(&face)) && face) {- if (!readFaceFile(face, out)) out.path.clear();+ if (!faceFilePath(face, out)) out.path.clear();face->Release();}font->Release();diff --git a/native/host/platform/windows_common.hpp b/native/host/platform/windows_common.hppindex 471c8faa2..47ff21ae7 100644--- a/native/host/platform/windows_common.hpp+++ b/native/host/platform/windows_common.hpp@@ -20,12 +20,12 @@namespace eshost {-/** A font file for @p family covering @p codepoint, with @p style's bold/italic- * bits applied — implements Platform::loadFont.+/** The face for @p family covering @p codepoint, with @p style's bold/italic+ * bits applied — implements Platform::matchFont.** DirectWrite resolves the family and finds a face covering the codepoint: the* CJK fallback, without a hard-coded path. */-FontFile windowsLoadFont(const std::string& family, esengine::u32 codepoint, int style);+FontMatch windowsMatchFont(const std::string& family, esengine::u32 codepoint, int style);/** Run @p req on a WinHTTP worker thread and hand the reply to deliverFetch —* see Platform::startFetch. TLS is the OS's, which is why this is platform code. */diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txtindex 9c3a64cbc..6cf3041b5 100644--- a/tests/CMakeLists.txt+++ b/tests/CMakeLists.txt@@ -114,6 +114,14 @@ add_executable(test_frame_pacertarget_include_directories(test_frame_pacer PRIVATE ${CMAKE_SOURCE_DIR}/native/host)add_test(NAME test_frame_pacer COMMAND test_frame_pacer)+# The read-only mapping the native glyph rasterizer opens fonts through+# (native/host/media/mapped_file.hpp): header-only, so no host is needed.+add_executable(test_mapped_file+ text/test_mapped_file.cpp+)+target_include_directories(test_mapped_file PRIVATE ${CMAKE_SOURCE_DIR}/native/host)+add_test(NAME test_mapped_file COMMAND test_mapped_file)+# Tilemap system testsadd_executable(test_tilemaptilemap/test_tilemap.cppdiff --git a/tests/text/test_mapped_file.cpp b/tests/text/test_mapped_file.cppnew file mode 100644index 000000000..b4487a388--- /dev/null+++ b/tests/text/test_mapped_file.cpp@@ -0,0 +1,69 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file The read-only file mapping the native glyph rasterizer opens fonts+ * through (native/host/media/mapped_file.hpp): the bytes it gives are the+ * file's, it maps rather than copies, and it moves without double-freeing.+ */+#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN+#include "doctest.h"++#include "media/mapped_file.hpp"++#include <cstdio>+#include <cstring>+#include <string>++using eshost::MappedFile;++namespace {+std::string writeTemp(const char* name, const std::string& content) {+ const std::string path = std::string(name) + ".bin";+ FILE* f = std::fopen(path.c_str(), "wb");+ REQUIRE(f);+ std::fwrite(content.data(), 1, content.size(), f);+ std::fclose(f);+ return path;+}+} // namespace++TEST_CASE("a mapped file gives the file's bytes, mapped") {+ std::string content(1 << 20, '\0');+ for (size_t i = 0; i < content.size(); ++i) content[i] = (char)(i * 31 + 7);+ const std::string path = writeTemp("test_mapped_file_a", content);+ {+ MappedFile file;+ REQUIRE(file.open(path));+ CHECK(file.mapped());+ REQUIRE(file.size() == content.size());+ CHECK(std::memcmp(file.data(), content.data(), content.size()) == 0);+ }+ std::remove(path.c_str()); // the mapping is gone, so the file can be+}++TEST_CASE("a missing or empty file opens as nothing") {+ MappedFile file;+ CHECK_FALSE(file.open(""));+ CHECK_FALSE(file.open("test_mapped_file_does_not_exist.bin"));+ CHECK(file.empty());+ const std::string path = writeTemp("test_mapped_file_empty", "");+ CHECK_FALSE(file.open(path));+ CHECK(file.data() == nullptr);+ std::remove(path.c_str());+}++TEST_CASE("a mapping moves, and only the last owner unmaps it") {+ const std::string path = writeTemp("test_mapped_file_b", "glyphs");+ {+ MappedFile a;+ REQUIRE(a.open(path));+ MappedFile b = std::move(a);+ CHECK(a.empty());+ REQUIRE(b.size() == 6);+ CHECK(std::memcmp(b.data(), "glyphs", 6) == 0);+ MappedFile c;+ c = std::move(b);+ CHECK(std::memcmp(c.data(), "glyphs", 6) == 0);+ }+ std::remove(path.c_str());+}diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 5ea0ce967..39e8036ef 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -3832,4 +3832,6 @@ export const NOTED = {{"note":"Voxel GI costs less on mobile GPUs."},"fix(native): every native host draws from one pacer, its present's own block and the game's cap, so an Android frame near its budget holds 60 fps instead of dropping to 30":{"note":"Android frames no longer drop to 30 fps when they come close to the frame budget."},+ "fix(native): a platform only names the font face a glyph needs, and the rasterizer maps each face's file once, so a new glyph no longer reads a whole system font":+ {"note":"A new glyph on a native build no longer stalls the frame reading a whole system font."},};