diff --git a/modules/canvaskit/canvaskit_bindings.cpp b/modules/canvaskit/canvaskit_bindings.cpp index 0b7e269eec..6cf484dc5c 100644 --- a/modules/canvaskit/canvaskit_bindings.cpp +++ b/modules/canvaskit/canvaskit_bindings.cpp @@ -91,7 +91,7 @@ #endif #ifndef SK_NO_FONTS -sk_sp SkFontMgr_New_Custom_Data(const uint8_t** datas, const size_t* sizes, int n); +sk_sp SkFontMgr_New_Custom_Data(sk_sp* datas, int n); #endif struct OptionalMatrix : SkMatrix { @@ -1162,7 +1162,12 @@ EMSCRIPTEN_BINDINGS(Skia) { auto datas = reinterpret_cast(dPtr); auto sizes = reinterpret_cast(sPtr); - return SkFontMgr_New_Custom_Data(datas, sizes, numFonts); + std::unique_ptr[]> skdatas(new sk_sp[numFonts]); + for (int i = 0; i < numFonts; ++i) { + skdatas[i] = SkData::MakeFromMalloc(datas[i], sizes[i]); + } + + return SkFontMgr_New_Custom_Data(skdatas.get(), numFonts); }), allow_raw_pointers()) .class_function("RefDefault", &SkFontMgr::RefDefault) .function("countFamilies", &SkFontMgr::countFamilies) diff --git a/src/ports/SkFontMgr_custom_embedded.cpp b/src/ports/SkFontMgr_custom_embedded.cpp index a9b0f810d3..45af7d00aa 100644 --- a/src/ports/SkFontMgr_custom_embedded.cpp +++ b/src/ports/SkFontMgr_custom_embedded.cpp @@ -13,7 +13,7 @@ struct SkEmbeddedResource { const uint8_t* data; size_t size; }; struct SkEmbeddedResourceHeader { const SkEmbeddedResource* entries; int count; }; static void load_font_from_data(const SkTypeface_FreeType::Scanner& scanner, - const uint8_t* data, size_t size, int index, + std::unique_ptr stream, int index, SkFontMgr_Custom::Families* families); class EmbeddedSystemFontLoader : public SkFontMgr_Custom::SystemFontLoader { @@ -25,7 +25,8 @@ public: { for (int i = 0; i < fHeader->count; ++i) { const SkEmbeddedResource& fontEntry = fHeader->entries[i]; - load_font_from_data(scanner, fontEntry.data, fontEntry.size, i, families); + auto stream = std::make_unique(fontEntry.data, fontEntry.size, false); + load_font_from_data(scanner, std::move(stream), i, families); } if (families->empty()) { @@ -40,13 +41,14 @@ public: class DataFontLoader : public SkFontMgr_Custom::SystemFontLoader { public: - DataFontLoader(const uint8_t** datas, const size_t* sizes, int n) : fDatas(datas), fSizes(sizes), fNum(n) { } + DataFontLoader(sk_sp* datas, int n) : fDatas(datas), fNum(n) { } void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner, SkFontMgr_Custom::Families* families) const override { for (int i = 0; i < fNum; ++i) { - load_font_from_data(scanner, fDatas[i], fSizes[i], i, families); + auto stream = std::make_unique(fDatas[i]); + load_font_from_data(scanner, std::move(stream), i, families); } if (families->empty()) { @@ -56,8 +58,7 @@ public: } } - const uint8_t** fDatas; - const size_t* fSizes; + const sk_sp* fDatas; const int fNum; }; @@ -73,11 +74,9 @@ static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families, } static void load_font_from_data(const SkTypeface_FreeType::Scanner& scanner, - const uint8_t* data, size_t size, int index, + std::unique_ptr stream, int index, SkFontMgr_Custom::Families* families) { - auto stream = std::make_unique(data, size, false); - int numFaces; if (!scanner.recognizedFont(stream.get(), &numFaces)) { SkDebugf("---- failed to open <%d> as a font\n", index); @@ -111,11 +110,9 @@ sk_sp SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* h return sk_make_sp(EmbeddedSystemFontLoader(header)); } -// SkFontMgr_New_Custom_Data expects to be called with the data for n font files. datas and sizes -// are parallel arrays of bytes and byte lengths. -sk_sp SkFontMgr_New_Custom_Data(const uint8_t** datas, const size_t* sizes, int n) { +// SkFontMgr_New_Custom_Data expects to be called with the data for n font files. +sk_sp SkFontMgr_New_Custom_Data(sk_sp* datas, int n) { SkASSERT(datas != nullptr); - SkASSERT(sizes != nullptr); SkASSERT(n > 0); - return sk_make_sp(DataFontLoader(datas, sizes, n)); + return sk_make_sp(DataFontLoader(datas, n)); }