Change SkTypeface::MakeFromStream to take unique_ptr

Also, add MakeFromData variant, which matches SkFontMgr

Bug: skia:8350
Change-Id: Ia220496bab8ecf0e7eefaf0c8b62d88ea1394a17
Reviewed-on: https://skia-review.googlesource.com/150969
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Mike Reed <reed@google.com>
Auto-Submit: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2018-09-03 21:10:10 -04:00 committed by Skia Commit-Bot
parent c8a9b3ba69
commit 271d1d9001
7 changed files with 40 additions and 20 deletions

View File

@ -33,6 +33,8 @@ typedef uint32_t SkFontID;
/** Machine endian. */
typedef uint32_t SkFontTableTag;
#define SK_SUPPORT_LEGACY_TYPEFACE_MAKEFROMSTREAM
/** \class SkTypeface
The SkTypeface class specifies the typeface and intrinsic style of a font.
@ -127,7 +129,17 @@ public:
not a valid font file, returns nullptr. Ownership of the stream is
transferred, so the caller must not reference it again.
*/
static sk_sp<SkTypeface> MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index = 0);
#ifdef SK_SUPPORT_LEGACY_TYPEFACE_MAKEFROMSTREAM
// DEPRECATED -- call the version that takes unique_ptr
static sk_sp<SkTypeface> MakeFromStream(SkStreamAsset* stream, int index = 0);
#endif
/** Return a new typeface given a SkData. If the data is null, or is not a valid font file,
* returns nullptr.
*/
static sk_sp<SkTypeface> MakeFromData(sk_sp<SkData>, int index = 0);
/** Return a new typeface given font data and configuration. If the data
is not valid font data, returns nullptr.

View File

@ -142,23 +142,36 @@ sk_sp<SkTypeface> SkTypeface::MakeFromName(const char name[],
(fontStyle.weight() == SkFontStyle::kBold_Weight ? SkTypeface::kBold :
SkTypeface::kNormal))));
}
sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
return fm->legacyMakeTypeface(name, fontStyle);
return SkFontMgr::RefDefault()->legacyMakeTypeface(name, fontStyle);
}
#ifdef SK_SUPPORT_LEGACY_TYPEFACE_MAKEFROMSTREAM
// DEPRECATED
sk_sp<SkTypeface> SkTypeface::MakeFromStream(SkStreamAsset* stream, int index) {
sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
return fm->makeFromStream(std::unique_ptr<SkStreamAsset>(stream), index);
return MakeFromStream(std::unique_ptr<SkStreamAsset>(stream), index);
}
#endif
sk_sp<SkTypeface> SkTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, int index) {
if (!stream) {
return nullptr;
}
return SkFontMgr::RefDefault()->makeFromStream(std::move(stream), index);
}
sk_sp<SkTypeface> SkTypeface::MakeFromData(sk_sp<SkData> data, int index) {
if (!data) {
return nullptr;
}
return SkFontMgr::RefDefault()->makeFromData(std::move(data), index);
}
sk_sp<SkTypeface> SkTypeface::MakeFromFontData(std::unique_ptr<SkFontData> data) {
sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
return fm->makeFromFontData(std::move(data));
return SkFontMgr::RefDefault()->makeFromFontData(std::move(data));
}
sk_sp<SkTypeface> SkTypeface::MakeFromFile(const char path[], int index) {
sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
return fm->makeFromFile(path, index);
return SkFontMgr::RefDefault()->makeFromFile(path, index);
}
sk_sp<SkTypeface> SkTypeface::makeClone(const SkFontArguments& args) const {

View File

@ -94,7 +94,7 @@ DEF_TEST(FontHostStream, reporter) {
return;
}
sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(fontData.release()));
sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(std::move(fontData)));
SkFontDescriptor desc;
bool isLocalStream = false;

View File

@ -363,8 +363,7 @@ static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const cha
static void TestPictureTypefaceSerialization(skiatest::Reporter* reporter) {
{
// Load typeface from file to test CreateFromFile with index.
auto data = GetResourceAsData("fonts/test.ttc");
auto typeface = SkTypeface::MakeFromStream(new SkMemoryStream(std::move(data)), 1);
auto typeface = MakeResourceAsTypeface("fonts/test.ttc", 1);
if (!typeface) {
INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
} else {

View File

@ -51,7 +51,7 @@ static void TypefaceStyle_test(skiatest::Reporter* reporter,
using WidthType = SkOTTableOS2_V0::WidthClass::Value;
os2Table->usWidthClass.value = static_cast<WidthType>(SkEndian_SwapBE16(width));
sk_sp<SkTypeface> newTypeface(SkTypeface::MakeFromStream(new SkMemoryStream(sk_ref_sp(data))));
sk_sp<SkTypeface> newTypeface(SkTypeface::MakeFromData(sk_ref_sp(data)));
if (!newTypeface) {
// Not all SkFontMgr can MakeFromStream().
return;
@ -318,7 +318,7 @@ static void check_serialize_behaviors(sk_sp<SkTypeface> tf, bool isLocalData,
DEF_TEST(Typeface_serialize, reporter) {
check_serialize_behaviors(SkTypeface::MakeDefault(), false, reporter);
check_serialize_behaviors(SkTypeface::MakeFromStream(
GetResourceAsStream("fonts/Distortable.ttf").release()),
GetResourceAsStream("fonts/Distortable.ttf")),
true, reporter);
}

View File

@ -55,10 +55,6 @@ sk_sp<SkData> GetResourceAsData(const char* resource) {
return nullptr;
}
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
if (!stream) {
return nullptr;
}
return SkTypeface::MakeFromStream(stream.release());
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex) {
return SkTypeface::MakeFromStream(GetResourceAsStream(resource), ttcIndex);
}

View File

@ -34,6 +34,6 @@ inline sk_sp<SkImage> GetResourceAsImage(const char* resource) {
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource);
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource);
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex = 0);
#endif // Resources_DEFINED