diff --git a/Polyfills/Canvas/Source/Canvas.cpp b/Polyfills/Canvas/Source/Canvas.cpp index cbac8dbba..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), @@ -59,20 +60,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);