Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Polyfills/Canvas/Source/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<Napi::ArrayBuffer>();
std::vector<uint8_t> 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<Napi::String>().Utf8Value();
if (fontsInfos.find(fontName) == fontsInfos.end())
{
const auto buffer = info[1].As<Napi::ArrayBuffer>();
std::vector<uint8_t> 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());
Expand Down
1 change: 1 addition & 0 deletions Polyfills/Canvas/Source/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down