[skottie] SkResources::loadTypeface() callback

Some clients already have SkTypeface objects, and forcing them to pass
these as SkData is awkward - especially since Skottie immediately turns
them into SkTypeface again.

Replace the existing loadFont() callback with loadTypeface().

(for compatibility, we try both for now, but the plan is to phase-out
loadFont)

Change-Id: Ib4c2446a96cb6a5f95581c405d0a1b4ecff7ddb2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/296718
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Florin Malita <fmalita@google.com>
This commit is contained in:
Florin Malita 2020-06-16 09:49:41 -04:00 committed by Skia Commit-Bot
parent 1b0b0046cb
commit 1d585c219b
5 changed files with 36 additions and 13 deletions

View File

@ -25,14 +25,15 @@ static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json"
// Dummy web font loader which serves a single local font (checked in under resources/).
class FakeWebFontProvider final : public skresources::ResourceProvider {
public:
FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {}
FakeWebFontProvider()
: fTypeface(SkTypeface::MakeFromData(GetResourceAsData(kWebFontResource))) {}
sk_sp<SkData> loadFont(const char[], const char[]) const override {
return fFontData;
sk_sp<SkTypeface> loadTypeface(const char[], const char[]) const override {
return fTypeface;
}
private:
sk_sp<SkData> fFontData;
sk_sp<SkTypeface> fTypeface;
using INHERITED = skresources::ResourceProvider;
};

View File

@ -25,14 +25,15 @@ static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json"
// Dummy web font loader which serves a single local font (checked in under resources/).
class FakeWebFontProvider final : public skresources::ResourceProvider {
public:
FakeWebFontProvider() : fFontData(GetResourceAsData(kWebFontResource)) {}
FakeWebFontProvider()
: fTypeface(SkTypeface::MakeFromData(GetResourceAsData(kWebFontResource))) {}
sk_sp<SkData> loadFont(const char[], const char[]) const override {
return fFontData;
sk_sp<SkTypeface> loadTypeface(const char[], const char[]) const override {
return fTypeface;
}
private:
sk_sp<SkData> fFontData;
sk_sp<SkTypeface> fTypeface;
using INHERITED = skresources::ResourceProvider;
};

View File

@ -263,8 +263,14 @@ bool AnimationBuilder::resolveNativeTypefaces() {
// 2) system font (family/style)
// 3) system default
finfo->fTypeface =
fmgr->makeFromData(fResourceProvider->loadFont(name.c_str(), finfo->fPath.c_str()));
finfo->fTypeface = fResourceProvider->loadTypeface(name.c_str(), finfo->fPath.c_str());
// legacy API fallback
// TODO: remove after client migration
if (!finfo->fTypeface) {
finfo->fTypeface = fmgr->makeFromData(
fResourceProvider->loadFont(name.c_str(), finfo->fPath.c_str()));
}
if (!finfo->fTypeface) {
finfo->fTypeface.reset(fmgr->matchFamilyStyle(finfo->fFamily.c_str(),

View File

@ -11,6 +11,7 @@
#include "include/core/SkData.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkString.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "include/private/SkMutex.h"
#include "include/private/SkTHash.h"
@ -96,6 +97,8 @@ public:
}
/**
* DEPRECATED: implement loadTypeface() instead.
*
* Load an external font and return as SkData.
*
* @param name font name ("fName" Lottie property)
@ -112,6 +115,17 @@ public:
const char[] /* url */) const {
return nullptr;
}
/**
* Load an external font and return as SkTypeface.
*
* @param name font name
* @param url web font URL
*/
virtual sk_sp<SkTypeface> loadTypeface(const char[] /* name */,
const char[] /* url */) const {
return nullptr;
}
};
class FileResourceProvider final : public ResourceProvider {
@ -137,7 +151,7 @@ protected:
sk_sp<SkData> load(const char[], const char[]) const override;
sk_sp<ImageAsset> loadImageAsset(const char[], const char[], const char[]) const override;
sk_sp<SkData> loadFont(const char[], const char[]) const override;
sk_sp<SkTypeface> loadTypeface(const char[], const char[]) const override;
private:
const sk_sp<ResourceProvider> fProxy;

View File

@ -196,8 +196,9 @@ sk_sp<ImageAsset> ResourceProviderProxyBase::loadImageAsset(const char rpath[],
: nullptr;
}
sk_sp<SkData> ResourceProviderProxyBase::loadFont(const char name[], const char url[]) const {
return fProxy ? fProxy->loadFont(name, url)
sk_sp<SkTypeface> ResourceProviderProxyBase::loadTypeface(const char name[],
const char url[]) const {
return fProxy ? fProxy->loadTypeface(name, url)
: nullptr;
}