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): Apple draws the glyphs its own fonts carry

yhh814e7dea0243
diff --git a/native/host/Host.hpp b/native/host/Host.hpp
index 55eaa8eb6..86927a0d1 100644
--- a/native/host/Host.hpp
+++ b/native/host/Host.hpp
@@ -188,6 +188,33 @@ struct Platform {
* 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;
+ /**
+ * Coverage the platform drew itself, padded and supersampled as the shared
+ * path builds it — everything after (the engine's SDF, the downsample, the
+ * tile) stays one implementation. See {@link Platform::drawGlyph}.
+ */
+ struct GlyphCoverage {
+ std::vector<esengine::u8> alpha; ///< `width * height`, 255 = full ink.
+ int width = 0; ///< Supersampled px, padding included.
+ int height = 0;
+ esengine::f32 advance = 0.0f; ///< Px at the asked size, not supersampled.
+ esengine::f32 bearingX = 0.0f; ///< Tile's top-left in pen space, y UP, at the asked size.
+ esengine::f32 bearingY = 0.0f;
+ bool blank = false; ///< Whitespace: an advance and no tile.
+ };
+
+ /**
+ * Draw @p codepoint with the platform's OWN rasterizer, for a system whose
+ * font files the shared parser cannot read — Apple ships CFF2 outlines and
+ * stb_truetype has no CFF2. Reached only where the shared path could not, so
+ * the default false keeps stb_truetype and costs a platform nothing.
+ */
+ virtual bool drawGlyph(const std::string& /*family*/, esengine::u32 /*codepoint*/,
+ int /*style*/, esengine::f32 /*pixelSize*/, int /*supersample*/,
+ int /*padding*/, GlyphCoverage& /*out*/) {
+ return false;
+ }
+
/** Perform an HTTP request off the main thread (NSURLSession / a JNI
* HttpURLConnection), then hand the reply back through {@link deliverFetch}
* with the same `req.id`. The OS owns the TLS stack, so this is where native
diff --git a/native/host/media/glyph_raster.cpp b/native/host/media/glyph_raster.cpp
index 4cc308807..bf4586748 100644
--- a/native/host/media/glyph_raster.cpp
+++ b/native/host/media/glyph_raster.cpp
@@ -46,16 +46,23 @@ 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) {
+/** A face this parser cannot read is not the same answer as no face at all: the
+ * first is the platform's to draw, the second is nobody's. */
+struct FontLookup {
+ Font* font = nullptr;
+ bool matchedButUnreadable = false;
+};
+
+FontLookup loadFont(Platform& platform, const std::string& family, uint32_t codepoint, int style,
+ bool& syntheticBold, bool& syntheticItalic) {
FontFile file = platform.loadFont(family, codepoint, style);
syntheticBold = file.syntheticBold;
syntheticItalic = file.syntheticItalic;
- if (file.path.empty()) return nullptr;
+ if (file.path.empty()) return {};
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;
+ if (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);
@@ -65,11 +72,13 @@ Font* loadFont(Platform& platform, const std::string& family, uint32_t codepoint
}
Font* raw = font.get();
if (!raw->ok) {
- ESHOST_LOGE("font: %s face %d did not parse — text in this family draws nothing",
+ // Once per face: the platform is asked to draw it instead, and a line per
+ // glyph of a family it draws fine would be the noisiest kind of correct.
+ ESHOST_LOGI("font: %s face %d is past this parser — the platform draws it",
file.path.c_str(), file.faceIndex);
}
g_fonts.emplace(key, std::move(font));
- return raw->ok ? raw : nullptr;
+ return {raw->ok ? raw : nullptr, !raw->ok};
}
/** Average an alpha grid down by an integer factor (the SDK's downsampleBytes). */
@@ -145,21 +154,61 @@ std::vector<uint8_t> toAtlasRgba(const std::vector<uint8_t>& coverage, int width
} // namespace
+/**
+ * The half both rasterizers share: a padded, supersampled alpha grid becomes the
+ * tile the atlas uploads. The SDF is the ENGINE's, so a field the shared shader
+ * samples is encoded the same whoever drew the coverage.
+ */
+void finishTile(GlyphBitmap& out, std::vector<uint8_t>& alpha, int wss, int hss,
+ int ss, int pad, bool sdf) {
+ std::vector<uint8_t> coverage;
+ if (sdf) {
+ std::vector<uint8_t> field((size_t)wss * hss, 0);
+ esengine::text::sdfFromAlpha(alpha.data(), field.data(), (esengine::u32)wss, (esengine::u32)hss,
+ (esengine::f32)(pad * ss));
+ coverage = downsample(field, wss, hss, ss);
+ } else {
+ coverage = std::move(alpha);
+ }
+ out.width = wss / ss;
+ out.height = hss / ss;
+ out.rgba = toAtlasRgba(coverage, out.width, out.height);
+}
+
GlyphBitmap rasterizeGlyph(Platform& platform, uint32_t codepoint, const std::string& family,
int style, float pixelSize, bool sdf, float padding) {
GlyphBitmap out;
if (pixelSize <= 0.0f) return out;
bool syntheticBold = false, syntheticItalic = false;
- Font* font = loadFont(platform, family, codepoint, style, syntheticBold, syntheticItalic);
+ const FontLookup found = loadFont(platform, family, codepoint, style, syntheticBold, syntheticItalic);
+ Font* font = found.font;
+ const int padPx = (int)std::lround(padding);
+ const int superSample = sdf ? SDF_SUPERSAMPLE : 1;
+
+ // A face this parser cannot read is the platform's to draw. Asked here rather
+ // than instead of the parser, so every font that already rasterizes keeps the
+ // one implementation and only what it cannot read takes the other path.
+ if (!font && found.matchedButUnreadable) {
+ Platform::GlyphCoverage cov;
+ if (platform.drawGlyph(family, codepoint, style, pixelSize, superSample, padPx, cov)) {
+ out.ok = true;
+ out.advance = cov.advance;
+ if (cov.blank || cov.width <= 0 || cov.height <= 0) return out;
+ finishTile(out, cov.alpha, cov.width, cov.height, superSample, padPx, sdf);
+ out.bearingX = cov.bearingX;
+ out.bearingY = cov.bearingY;
+ return out;
+ }
+ }
if (!font) return out;
const int glyph = stbtt_FindGlyphIndex(&font->info, (int)codepoint);
if (glyph == 0) return out; // the matched font does not have it after all
// px per em, the same unit CSS font-size (and so the SDK's pixelSize) means.
- const int pad = (int)std::lround(padding);
- const int ss = sdf ? SDF_SUPERSAMPLE : 1;
+ const int pad = padPx;
+ const int ss = superSample;
const float scale = stbtt_ScaleForMappingEmToPixels(&font->info, pixelSize) * (float)ss;
int advanceRaw = 0, lsbRaw = 0;
@@ -204,21 +253,7 @@ GlyphBitmap rasterizeGlyph(Platform& platform, uint32_t codepoint, const std::st
oblique(alpha, wss, hss, pad * ss - y0);
}
- std::vector<uint8_t> coverage;
- if (sdf) {
- // The engine's own generator, so the field the shared SDF shader samples is
- // encoded exactly as the web's (128 = edge, spread = padding after folding).
- std::vector<uint8_t> field((size_t)wss * hss, 0);
- esengine::text::sdfFromAlpha(alpha.data(), field.data(), (esengine::u32)wss, (esengine::u32)hss,
- (esengine::f32)(pad * ss));
- coverage = downsample(field, wss, hss, ss);
- } else {
- coverage = std::move(alpha);
- }
-
- out.rgba = toAtlasRgba(coverage, w, h);
- out.width = w;
- out.height = h;
+ finishTile(out, alpha, wss, hss, ss, pad, sdf);
// The tile's top-left in pen space, y UP: the SDK places it at
// (penX + bearingX, bearingY). The ink starts `pad` in from both.
out.bearingX = (float)x0 / (float)ss - (float)pad;
diff --git a/native/host/platform/apple_common.hpp b/native/host/platform/apple_common.hpp
index 16d1d8f69..3e1936c63 100644
--- a/native/host/platform/apple_common.hpp
+++ b/native/host/platform/apple_common.hpp
@@ -34,6 +34,17 @@ namespace eshost {
*/
FontFile appleLoadFont(const std::string& family, esengine::u32 codepoint, int style);
+/**
+ * Coverage for @p codepoint drawn by Core Text — implements Platform::drawGlyph.
+ *
+ * Apple's system fonts carry CFF2 variable outlines, which the shared parser has
+ * no reader for, so its CJK faces load and rasterize nothing. Core Graphics draws
+ * the same glyph into the padded, supersampled grid the shared path expects.
+ */
+bool appleDrawGlyph(const std::string& family, esengine::u32 codepoint, int style,
+ esengine::f32 pixelSize, int supersample, int padding,
+ Platform::GlyphCoverage& out);
+
/**
* Run @p req on NSURLSession's background queue and hand the reply to
* deliverFetch — see Platform::startFetch. TLS is the OS's, which is the reason
diff --git a/native/host/platform/apple_common.mm b/native/host/platform/apple_common.mm
index 45b5f5b20..d10e90269 100644
--- a/native/host/platform/apple_common.mm
+++ b/native/host/platform/apple_common.mm
@@ -17,23 +17,46 @@
#include "platform/apple_common.hpp"
#include "media/glyph_raster.hpp" // GLYPH_BOLD / GLYPH_ITALIC, for the font match
#include "Runtime.hpp" // ESHOST_LOGE
+#include <vector>
+#include <cstring>
+#include <cmath>
namespace eshost {
using esengine::u32;
+using esengine::f32;
using esengine::u8;
-FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {
- FontFile out;
+/** @p codepoint as UTF-16; one unit below U+10000, a surrogate pair above it. */
+CFIndex utf16For(u32 codepoint, UniChar (&chars)[2]) {
+ if (codepoint >= 0x10000) {
+ const uint32_t v = codepoint - 0x10000;
+ chars[0] = (UniChar)(0xD800 + (v >> 10));
+ chars[1] = (UniChar)(0xDC00 + (v & 0x3FF));
+ return 2;
+ }
+ chars[0] = (UniChar)codepoint;
+ return 1;
+}
+
+/**
+ * The face Core Text gives for @p family at @p size that also covers @p codepoint
+ * — the family, the style traits it has, and the per-codepoint fallback that is
+ * how CJK resolves without a font name written down anywhere.
+ *
+ * Caller releases. `synthetic*` report the traits the matched face does NOT have.
+ */
+CTFontRef matchFace(const std::string& family, u32 codepoint, int style, CGFloat size,
+ bool* syntheticBold, bool* syntheticItalic) {
CTFontSymbolicTraits traits = 0;
if (style & GLYPH_BOLD) traits |= kCTFontTraitBold;
if (style & GLYPH_ITALIC) traits |= kCTFontTraitItalic;
NSString* name = family.empty() ? @"Helvetica" : [NSString stringWithUTF8String:family.c_str()];
- CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)name, 16.0, NULL);
- if (!font) return out;
+ CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)name, size, NULL);
+ if (!font) return NULL;
if (traits) {
- if (CTFontRef styled = CTFontCreateCopyWithSymbolicTraits(font, 16.0, NULL, traits, traits)) {
+ if (CTFontRef styled = CTFontCreateCopyWithSymbolicTraits(font, size, NULL, traits, traits)) {
CFRelease(font);
font = styled;
}
@@ -41,20 +64,13 @@ FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {
// A family without a real bold or italic face leaves the trait unset; the
// rasterizer then synthesizes it, as a browser does for the same family.
const CTFontSymbolicTraits actual = CTFontGetSymbolicTraits(font);
- out.syntheticBold = (traits & kCTFontTraitBold) && !(actual & kCTFontTraitBold);
- out.syntheticItalic = (traits & kCTFontTraitItalic) && !(actual & kCTFontTraitItalic);
+ if (syntheticBold) *syntheticBold = (traits & kCTFontTraitBold) && !(actual & kCTFontTraitBold);
+ if (syntheticItalic) *syntheticItalic = (traits & kCTFontTraitItalic) && !(actual & kCTFontTraitItalic);
// Does this face have the glyph? If not, let Core Text find one that does.
if (codepoint) {
UniChar chars[2];
- CFIndex length = 0;
- if (codepoint >= 0x10000) {
- const uint32_t v = codepoint - 0x10000;
- chars[length++] = (UniChar)(0xD800 + (v >> 10));
- chars[length++] = (UniChar)(0xDC00 + (v & 0x3FF));
- } else {
- chars[length++] = (UniChar)codepoint;
- }
+ const CFIndex length = utf16For(codepoint, chars);
CGGlyph glyphs[2] = {0, 0};
if (!CTFontGetGlyphsForCharacters(font, chars, glyphs, length)) {
CFStringRef text = CFStringCreateWithCharacters(NULL, chars, length);
@@ -66,6 +82,14 @@ FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {
}
}
}
+ return font;
+}
+
+FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {
+ FontFile out;
+ CTFontRef font = matchFace(family, codepoint, style, 16.0,
+ &out.syntheticBold, &out.syntheticItalic);
+ if (!font) return out;
CFURLRef url = (CFURLRef)CTFontCopyAttribute(font, kCTFontURLAttribute);
if (!url) {
@@ -106,6 +130,81 @@ FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {
return out;
}
+bool appleDrawGlyph(const std::string& family, u32 codepoint, int style,
+ f32 pixelSize, int supersample, int padding,
+ Platform::GlyphCoverage& out) {
+ const int ss = supersample > 0 ? supersample : 1;
+ if (pixelSize <= 0.0f) return false;
+
+ // Drawn at the supersampled size, so the grid handed back tiles the shared
+ // path's exactly and its downsample is the only place resolution is lost.
+ const CGFloat size = (CGFloat)pixelSize * (CGFloat)ss;
+ CTFontRef font = matchFace(family, codepoint, style, size, NULL, NULL);
+ if (!font) return false;
+
+ UniChar chars[2];
+ const CFIndex length = utf16For(codepoint, chars);
+ CGGlyph glyphs[2] = {0, 0};
+ if (!CTFontGetGlyphsForCharacters(font, chars, glyphs, length) || glyphs[0] == 0) {
+ CFRelease(font);
+ return false;
+ }
+ const CGGlyph glyph = glyphs[0];
+
+ CGSize adv = CGSizeZero;
+ CTFontGetAdvancesForGlyphs(font, kCTFontOrientationHorizontal, &glyph, &adv, 1);
+ out.advance = (f32)(adv.width / ss);
+
+ // Ink box in supersampled px, y UP from the baseline — Core Graphics' own
+ // convention, and the one the draw below happens in.
+ const CGRect box = CTFontGetBoundingRectsForGlyphs(font, kCTFontOrientationHorizontal,
+ &glyph, NULL, 1);
+ const int x0 = (int)std::floor(CGRectGetMinX(box));
+ const int y0 = (int)std::floor(CGRectGetMinY(box));
+ const int inkWss = (int)std::ceil(CGRectGetMaxX(box)) - x0;
+ const int inkHss = (int)std::ceil(CGRectGetMaxY(box)) - y0;
+ if (inkWss <= 0 || inkHss <= 0) { // whitespace: an advance and no tile
+ out.blank = true;
+ CFRelease(font);
+ return true;
+ }
+
+ // Padding is in STORED px, so the tile is sized there and multiplied up —
+ // which is also what keeps the grid an exact multiple of the supersample.
+ const int w = (inkWss + ss - 1) / ss + padding * 2;
+ const int h = (inkHss + ss - 1) / ss + padding * 2;
+ const int wss = w * ss;
+ const int hss = h * ss;
+
+ std::vector<esengine::u8> canvas((size_t)wss * hss, 0);
+ CGContextRef ctx = CGBitmapContextCreate(canvas.data(), (size_t)wss, (size_t)hss, 8,
+ (size_t)wss, NULL, kCGImageAlphaOnly);
+ if (!ctx) {
+ CFRelease(font);
+ return false;
+ }
+ CGContextSetShouldAntialias(ctx, true);
+ CGContextSetShouldSmoothFonts(ctx, false); // coverage, not subpixel triplets
+ // Put the ink's low corner `padding` stored px in from the tile's.
+ const CGPoint pen = CGPointMake((CGFloat)(padding * ss - x0), (CGFloat)(padding * ss - y0));
+ CTFontDrawGlyphs(font, &glyph, &pen, 1, ctx);
+ CGContextRelease(ctx);
+ CFRelease(font);
+
+ // Core Graphics counts rows from the bottom and the atlas from the top.
+ out.alpha.resize((size_t)wss * hss);
+ for (int row = 0; row < hss; ++row) {
+ std::memcpy(out.alpha.data() + (size_t)row * wss,
+ canvas.data() + (size_t)(hss - 1 - row) * wss, (size_t)wss);
+ }
+ out.width = wss;
+ out.height = hss;
+ // The tile's top-left in pen space, y UP — the same two the stb path reports.
+ out.bearingX = (f32)x0 / (f32)ss - (f32)padding;
+ out.bearingY = (f32)(y0 + inkHss) / (f32)ss + (f32)padding;
+ return true;
+}
+
// NSURLSession runs the request (TLS via the OS) on a background queue and calls
// its completion there; deliverFetch is thread-safe and the JS callback runs back
// on the main thread in drainFetches.
diff --git a/native/host/platform/desktop.cpp b/native/host/platform/desktop.cpp
index 8c589f4b3..e3d25abe2 100644
--- a/native/host/platform/desktop.cpp
+++ b/native/host/platform/desktop.cpp
@@ -297,6 +297,12 @@ struct DesktopPlatform final : eshost::Platform {
return eshost::appleLoadFont(family, codepoint, style);
}
+ // Core Text draws what stb_truetype cannot read — see Platform::drawGlyph.
+ bool drawGlyph(const std::string& family, u32 codepoint, int style, esengine::f32 pixelSize,
+ int supersample, int padding, GlyphCoverage& out) override {
+ return eshost::appleDrawGlyph(family, codepoint, style, pixelSize, supersample, padding, out);
+ }
+
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 {
diff --git a/native/host/platform/ios.mm b/native/host/platform/ios.mm
index 0fbbbf28b..73b0fa99b 100644
--- a/native/host/platform/ios.mm
+++ b/native/host/platform/ios.mm
@@ -151,6 +151,12 @@ struct IOSPlatform final : eshost::Platform {
return eshost::appleLoadFont(family, codepoint, style);
}
+ // Core Text draws what stb_truetype cannot read — see Platform::drawGlyph.
+ bool drawGlyph(const std::string& family, u32 codepoint, int style, esengine::f32 pixelSize,
+ int supersample, int padding, GlyphCoverage& out) override {
+ return eshost::appleDrawGlyph(family, codepoint, style, pixelSize, supersample, padding, out);
+ }
+
void startFetch(const eshost::FetchRequest& req) override {
eshost::appleStartFetch(req);
}