From 2ec7c3abb1fc2639dadced0fa879ca62992c71bb Mon Sep 17 00:00:00 2001 From: Jun Min Cheong Date: Wed, 30 Apr 2025 12:37:08 +1000 Subject: [PATCH 1/2] add synchronous LoadTTF --- Polyfills/Canvas/Source/Canvas.cpp | 16 ++++++++++------ Polyfills/Canvas/Source/Canvas.h | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Polyfills/Canvas/Source/Canvas.cpp b/Polyfills/Canvas/Source/Canvas.cpp index cbac8dbba..b90c604b6 100644 --- a/Polyfills/Canvas/Source/Canvas.cpp +++ b/Polyfills/Canvas/Source/Canvas.cpp @@ -59,20 +59,24 @@ namespace Babylon::Polyfills::Internal // called when removed from document which has no meaning for Native } - // this is currently synchronous to work around font corruption issues - Napi::Value NativeCanvas::LoadTTFAsync(const Napi::CallbackInfo& info) + void NativeCanvas::LoadTTF(const Napi::CallbackInfo& info) { - const auto buffer = info[1].As(); - std::vector fontBuffer(buffer.ByteLength()); - memcpy(fontBuffer.data(), (uint8_t*)buffer.Data(), buffer.ByteLength()); - // don't allow same font to be loaded more than once // why? because Context doesn't update nvgCreateFontMem when old fontBuffer released auto fontName = info[0].As().Utf8Value(); if (fontsInfos.find(fontName) == fontsInfos.end()) { + const auto buffer = info[1].As(); + std::vector fontBuffer(buffer.ByteLength()); + memcpy(fontBuffer.data(), (uint8_t*)buffer.Data(), buffer.ByteLength()); fontsInfos[fontName] = std::move(fontBuffer); } + } + + // @deprecated: LoadTTFAsync is always synchronous, use LoadTTF instead + Napi::Value NativeCanvas::LoadTTFAsync(const Napi::CallbackInfo& info) + { + LoadTTF(info); auto deferred{Napi::Promise::Deferred::New(info.Env())}; deferred.Resolve(info.Env().Undefined()); diff --git a/Polyfills/Canvas/Source/Canvas.h b/Polyfills/Canvas/Source/Canvas.h index b8c29972b..d2f1d35e0 100644 --- a/Polyfills/Canvas/Source/Canvas.h +++ b/Polyfills/Canvas/Source/Canvas.h @@ -82,6 +82,7 @@ namespace Babylon::Polyfills::Internal Napi::Value GetHeight(const Napi::CallbackInfo&); void SetHeight(const Napi::CallbackInfo&, const Napi::Value& value); Napi::Value GetCanvasTexture(const Napi::CallbackInfo& info); + static void LoadTTF(const Napi::CallbackInfo& info); static Napi::Value LoadTTFAsync(const Napi::CallbackInfo& info); static Napi::Value ParseColor(const Napi::CallbackInfo& info); void Remove(const Napi::CallbackInfo& info); From 3e7bc57cee4bea7612593e946f7ef78654cadb59 Mon Sep 17 00:00:00 2001 From: Pheo Date: Thu, 1 May 2025 10:54:46 +1000 Subject: [PATCH 2/2] add loadttf to StaticMethod --- Polyfills/Canvas/Source/Canvas.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Polyfills/Canvas/Source/Canvas.cpp b/Polyfills/Canvas/Source/Canvas.cpp index b90c604b6..74c0d51be 100644 --- a/Polyfills/Canvas/Source/Canvas.cpp +++ b/Polyfills/Canvas/Source/Canvas.cpp @@ -25,6 +25,7 @@ namespace Babylon::Polyfills::Internal env, JS_CONSTRUCTOR_NAME, { + StaticMethod("loadTTF", &NativeCanvas::LoadTTF), StaticMethod("loadTTFAsync", &NativeCanvas::LoadTTFAsync), InstanceAccessor("width", &NativeCanvas::GetWidth, &NativeCanvas::SetWidth), InstanceAccessor("height", &NativeCanvas::GetHeight, &NativeCanvas::SetHeight),