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
refactor(native): Core Text and NSURLSession are Foundation, not iOS
esenginec72567ef7ee9
diff --git a/native/CMakeLists.txt b/native/CMakeLists.txtindex 1e16cedab..c4d07aa67 100644--- a/native/CMakeLists.txt+++ b/native/CMakeLists.txt@@ -248,7 +248,8 @@ if(ESTELLA_QUICKJS_DIR)if(ESTELLA_IOS)# Static: the thin Xcode app (native/ios) links this plus the libraries# below. -ObjC on the app keeps the Obj-C classes in platform/ios.mm alive.- add_library(estella_js_host STATIC ${HOST_SOURCES} host/platform/ios.mm)+ add_library(estella_js_host STATIC ${HOST_SOURCES} host/platform/ios.mm+ host/platform/apple_common.mm)# miniaudio's CoreAudio backend uses AVAudioSession (Obj-C), so its TU must# be Objective-C++ on Apple; it also forces MA_NO_RUNTIME_LINKING there, so# the audio frameworks are linked at build time (the app links the mergeddiff --git a/native/host/platform/apple_common.hpp b/native/host/platform/apple_common.hppnew file mode 100644index 000000000..16d1d8f69--- /dev/null+++ b/native/host/platform/apple_common.hpp@@ -0,0 +1,44 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file apple_common.hpp+ * @brief The parts of the platform seam iOS and macOS answer with the SAME+ * Apple frameworks: font matching (Core Text) and HTTP (NSURLSession).+ * @details Neither has an iOS-only line in it — Core Text and NSURLSession are+ * Foundation, not UIKit — and a second copy is how two platforms end up+ * matching different faces for the same family.+ *+ * What genuinely differs stays in ios.mm and desktop.cpp: the window,+ * the event loop, the writable directories, the device description.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the Apache License, Version 2.0.+ */+#pragma once++#include <string>++#include "Host.hpp"++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.+ *+ * 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);++/**+ * 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+ * this is platform code at all.+ */+void appleStartFetch(const FetchRequest& req);++} // namespace eshostdiff --git a/native/host/platform/apple_common.mm b/native/host/platform/apple_common.mmnew file mode 100644index 000000000..a53c60e98--- /dev/null+++ b/native/host/platform/apple_common.mm@@ -0,0 +1,133 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file apple_common.mm+ * @brief Core Text font matching and NSURLSession fetching, shared by the iOS+ * app and the desktop (macOS) host. See apple_common.hpp for why.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the Apache License, Version 2.0.+ */+#import <CoreText/CoreText.h>+#import <Foundation/Foundation.h>++#include "platform/apple_common.hpp"+#include "media/glyph_raster.hpp" // GLYPH_BOLD / GLYPH_ITALIC, for the font match++namespace eshost {++using esengine::u32;+using esengine::u8;++FontFile appleLoadFont(const std::string& family, u32 codepoint, int style) {+ FontFile out;+ 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;+ if (traits) {+ if (CTFontRef styled = CTFontCreateCopyWithSymbolicTraits(font, 16.0, NULL, traits, traits)) {+ CFRelease(font);+ font = styled;+ }+ }+ // 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);++ // 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;+ }+ CGGlyph glyphs[2] = {0, 0};+ if (!CTFontGetGlyphsForCharacters(font, chars, glyphs, length)) {+ CFStringRef text = CFStringCreateWithCharacters(NULL, chars, length);+ CTFontRef fallback = CTFontCreateForString(font, text, CFRangeMake(0, length));+ CFRelease(text);+ if (fallback) {+ CFRelease(font);+ font = fallback;+ }+ }+ }++ CFURLRef url = (CFURLRef)CTFontCopyAttribute(font, kCTFontURLAttribute);+ CFRelease(font);+ if (!url) return out;+ 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);+ return out;+}++// 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.+void appleStartFetch(const FetchRequest& req) {+ NSURL* url = [NSURL URLWithString:[NSString stringWithUTF8String:req.url.c_str()]];+ if (!url) {+ FetchResult r; r.id = req.id; r.error = "invalid url";+ deliverFetch(std::move(r));+ return;+ }+ NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];+ request.HTTPMethod = [NSString stringWithUTF8String:req.method.c_str()];+ for (const auto& kv : req.headers) {+ [request setValue:[NSString stringWithUTF8String:kv.second.c_str()]+ forHTTPHeaderField:[NSString stringWithUTF8String:kv.first.c_str()]];+ }+ if (!req.body.empty()) {+ request.HTTPBody = [NSData dataWithBytes:req.body.data() length:req.body.size()];+ }+ const int id = req.id;+ const bool wantText = req.wantText;+ NSURLSessionDataTask* task = [NSURLSession.sharedSession dataTaskWithRequest:request+ completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {+ FetchResult r;+ r.id = id;+ r.isText = wantText;+ if (error) {+ r.error = error.localizedDescription.UTF8String ?: "network error";+ deliverFetch(std::move(r));+ return;+ }+ NSHTTPURLResponse* http = [response isKindOfClass:NSHTTPURLResponse.class]+ ? (NSHTTPURLResponse*)response : nil;+ r.status = http ? (int)http.statusCode : 200;+ r.ok = r.status >= 200 && r.status < 300;+ r.statusText = [NSHTTPURLResponse localizedStringForStatusCode:r.status].UTF8String ?: "";+ for (NSString* key in http.allHeaderFields) {+ NSString* val = [http.allHeaderFields[key] description];+ r.headers.emplace_back(key.UTF8String, val.UTF8String ?: "");+ }+ if (data.length) {+ const u8* bytes = (const u8*)data.bytes;+ r.body.assign(bytes, bytes + data.length);+ }+ deliverFetch(std::move(r));+ }];+ [task resume];+}++} // namespace eshostdiff --git a/native/host/platform/ios.mm b/native/host/platform/ios.mmindex d073807fa..0fbbbf28b 100644--- a/native/host/platform/ios.mm+++ b/native/host/platform/ios.mm@@ -22,10 +22,9 @@*/#import <UIKit/UIKit.h>#import <QuartzCore/CAMetalLayer.h>-#import <CoreText/CoreText.h>#include "Host.hpp"-#include "media/glyph_raster.hpp" // GLYPH_BOLD / GLYPH_ITALIC, for the font match+#include "platform/apple_common.hpp" // Core Text + NSURLSession, shared with macOSusing esengine::u32;using esengine::u8;@@ -148,116 +147,12 @@ struct IOSPlatform final : eshost::Platform {NSLog(@"[Estella]%s %s", error ? " ERROR:" : "", message);}- // Core Text names the file: it resolves a family (falling back to the system- // font when the name is unknown), and CTFontCreateForString picks a face that- // actually covers the codepoint — the CJK fallback, without a hard-coded path.- // The URL is a real file under /System/Library/Fonts, readable from the app.eshost::FontFile loadFont(const std::string& family, u32 codepoint, int style) override {- eshost::FontFile out;- CTFontSymbolicTraits traits = 0;- if (style & eshost::GLYPH_BOLD) traits |= kCTFontTraitBold;- if (style & eshost::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;- if (traits) {- if (CTFontRef styled = CTFontCreateCopyWithSymbolicTraits(font, 16.0, NULL, traits, traits)) {- CFRelease(font);- font = styled;- }- }- // 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);-- // 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;- }- CGGlyph glyphs[2] = {0, 0};- if (!CTFontGetGlyphsForCharacters(font, chars, glyphs, length)) {- CFStringRef text = CFStringCreateWithCharacters(NULL, chars, length);- CTFontRef fallback = CTFontCreateForString(font, text, CFRangeMake(0, length));- CFRelease(text);- if (fallback) {- CFRelease(font);- font = fallback;- }- }- }-- CFURLRef url = (CFURLRef)CTFontCopyAttribute(font, kCTFontURLAttribute);- CFRelease(font);- if (!url) return out;- 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);- return out;+ return eshost::appleLoadFont(family, codepoint, style);}- // 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.void startFetch(const eshost::FetchRequest& req) override {- NSURL* url = [NSURL URLWithString:[NSString stringWithUTF8String:req.url.c_str()]];- if (!url) {- eshost::FetchResult r; r.id = req.id; r.error = "invalid url";- eshost::deliverFetch(std::move(r));- return;- }- NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];- request.HTTPMethod = [NSString stringWithUTF8String:req.method.c_str()];- for (const auto& kv : req.headers) {- [request setValue:[NSString stringWithUTF8String:kv.second.c_str()]- forHTTPHeaderField:[NSString stringWithUTF8String:kv.first.c_str()]];- }- if (!req.body.empty()) {- request.HTTPBody = [NSData dataWithBytes:req.body.data() length:req.body.size()];- }- const int id = req.id;- const bool wantText = req.wantText;- NSURLSessionDataTask* task = [NSURLSession.sharedSession dataTaskWithRequest:request- completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {- eshost::FetchResult r;- r.id = id;- r.isText = wantText;- if (error) {- r.error = error.localizedDescription.UTF8String ?: "network error";- eshost::deliverFetch(std::move(r));- return;- }- NSHTTPURLResponse* http = [response isKindOfClass:NSHTTPURLResponse.class]- ? (NSHTTPURLResponse*)response : nil;- r.status = http ? (int)http.statusCode : 200;- r.ok = r.status >= 200 && r.status < 300;- r.statusText = [NSHTTPURLResponse localizedStringForStatusCode:r.status].UTF8String ?: "";- for (NSString* key in http.allHeaderFields) {- NSString* val = [http.allHeaderFields[key] description];- r.headers.emplace_back(key.UTF8String, val.UTF8String ?: "");- }- if (data.length) {- const u8* bytes = (const u8*)data.bytes;- r.body.assign(bytes, bytes + data.length);- }- eshost::deliverFetch(std::move(r));- }];- [task resume];+ eshost::appleStartFetch(req);}};