SkFontMgr_New_Custom_Data to take SkData

Have the canvaskit bindings provide SkData since only the bindings know
the correct release procedure for the underlying data.

Bug: skia:11778
Change-Id: I0fac15287b014ea01dfc0d4b87dd2fe7987fe1d3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/388496
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
This commit is contained in:
Ben Wagner 2021-03-23 18:02:32 -04:00 committed by Skia Commit-Bot
parent e8da4d2757
commit 1546053269
2 changed files with 18 additions and 16 deletions

View File

@ -91,7 +91,7 @@
#endif
#ifndef SK_NO_FONTS
sk_sp<SkFontMgr> SkFontMgr_New_Custom_Data(const uint8_t** datas, const size_t* sizes, int n);
sk_sp<SkFontMgr> SkFontMgr_New_Custom_Data(sk_sp<SkData>* datas, int n);
#endif
struct OptionalMatrix : SkMatrix {
@ -1162,7 +1162,12 @@ EMSCRIPTEN_BINDINGS(Skia) {
auto datas = reinterpret_cast<const uint8_t**>(dPtr);
auto sizes = reinterpret_cast<const size_t*>(sPtr);
return SkFontMgr_New_Custom_Data(datas, sizes, numFonts);
std::unique_ptr<sk_sp<SkData>[]> skdatas(new sk_sp<SkData>[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)

View File

@ -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<SkMemoryStream> 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<SkMemoryStream>(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<SkData>* 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<SkMemoryStream>(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<SkData>* 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<SkMemoryStream> stream, int index,
SkFontMgr_Custom::Families* families)
{
auto stream = std::make_unique<SkMemoryStream>(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> SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* h
return sk_make_sp<SkFontMgr_Custom>(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> 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> SkFontMgr_New_Custom_Data(sk_sp<SkData>* datas, int n) {
SkASSERT(datas != nullptr);
SkASSERT(sizes != nullptr);
SkASSERT(n > 0);
return sk_make_sp<SkFontMgr_Custom>(DataFontLoader(datas, sizes, n));
return sk_make_sp<SkFontMgr_Custom>(DataFontLoader(datas, n));
}