Skip to content

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.

fix(native): a font cache that answers per face, and an Apple index to key it by

yhh6ec509512cd9
diff --git a/native/host/media/glyph_raster.cpp b/native/host/media/glyph_raster.cpp
index 956d679fd..4cc308807 100644
--- a/native/host/media/glyph_raster.cpp
+++ b/native/host/media/glyph_raster.cpp
@@ -7,12 +7,14 @@
#include "glyph_raster.hpp"
#include "Host.hpp"
+#include "Runtime.hpp"
#include "esengine/text/SdfGenerator.hpp"
#include <algorithm>
#include <cmath>
#include <memory>
+#include <string>
#include <unordered_map>
#define STB_TRUETYPE_IMPLEMENTATION
@@ -33,10 +35,17 @@ struct Font {
bool ok = false;
};
-/** Parsed fonts by file path — the matcher answers a path per glyph, and only a
- * path we have not seen costs a read + parse. */
+/** 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
+ * 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) {
+ return file.path + '#' + std::to_string(file.faceIndex);
+}
+
Font* loadFont(Platform& platform, const std::string& family, uint32_t codepoint, int style,
bool& syntheticBold, bool& syntheticItalic) {
FontFile file = platform.loadFont(family, codepoint, style);
@@ -44,7 +53,8 @@ Font* loadFont(Platform& platform, const std::string& family, uint32_t codepoint
syntheticItalic = file.syntheticItalic;
if (file.path.empty()) return nullptr;
- auto it = g_fonts.find(file.path);
+ const std::string key = faceKey(file);
+ auto it = g_fonts.find(key);
if (it != g_fonts.end()) return it->second->ok ? it->second.get() : nullptr;
auto font = std::make_unique<Font>();
@@ -54,7 +64,11 @@ Font* loadFont(Platform& platform, const std::string& family, uint32_t codepoint
font->ok = true;
}
Font* raw = font.get();
- g_fonts.emplace(file.path, std::move(font));
+ if (!raw->ok) {
+ ESHOST_LOGE("font: %s face %d did not parse — text in this family draws nothing",
+ file.path.c_str(), file.faceIndex);
+ }
+ g_fonts.emplace(key, std::move(font));
return raw->ok ? raw : nullptr;
}
diff --git a/native/host/platform/apple_common.mm b/native/host/platform/apple_common.mm
index a53c60e98..45b5f5b20 100644
--- a/native/host/platform/apple_common.mm
+++ b/native/host/platform/apple_common.mm
@@ -16,6 +16,7 @@
#include "platform/apple_common.hpp"
#include "media/glyph_raster.hpp" // GLYPH_BOLD / GLYPH_ITALIC, for the font match
+#include "Runtime.hpp" // ESHOST_LOGE
namespace eshost {
@@ -67,8 +68,32 @@ FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {
}
CFURLRef url = (CFURLRef)CTFontCopyAttribute(font, kCTFontURLAttribute);
+ if (!url) {
+ ESHOST_LOGE("font: Core Text matched a face for \"%s\" that names no file, so nothing "
+ "can be read for it", family.c_str());
+ CFRelease(font);
+ return out;
+ }
+ // WHICH face in that file. A .ttc holds several — the CJK fallback lands in
+ // one — and the descriptors come back in file order, so the matched face's
+ // position in them is the index stbtt needs. Every other platform's matcher
+ // answers this itself (AFont_getCollectionIndex, FC_INDEX, IDWriteFont::GetIndex);
+ // Core Text answers a font, so its index is looked up here rather than assumed 0.
+ if (CFStringRef want = CTFontCopyPostScriptName(font)) {
+ if (CFArrayRef faces = CTFontManagerCreateFontDescriptorsFromURL(url)) {
+ const CFIndex count = CFArrayGetCount(faces);
+ for (CFIndex i = 0; i < count; ++i) {
+ CTFontDescriptorRef d = (CTFontDescriptorRef)CFArrayGetValueAtIndex(faces, i);
+ CFStringRef have = (CFStringRef)CTFontDescriptorCopyAttribute(d, kCTFontNameAttribute);
+ const bool same = have && CFStringCompare(have, want, 0) == kCFCompareEqualTo;
+ if (have) CFRelease(have);
+ if (same) { out.faceIndex = (int)i; break; }
+ }
+ CFRelease(faces);
+ }
+ CFRelease(want);
+ }
CFRelease(font);
- if (!url) return out;
NSString* path = [(__bridge NSURL*)url path];
CFRelease(url);
if (!path) return out;