diff --git a/engine/src/flutter/sample/MinikinSkia.cpp b/engine/src/flutter/sample/MinikinSkia.cpp index c920ca63e5..ec1e9da78c 100644 --- a/engine/src/flutter/sample/MinikinSkia.cpp +++ b/engine/src/flutter/sample/MinikinSkia.cpp @@ -6,17 +6,13 @@ namespace minikin { -MinikinFontSkia::MinikinFontSkia(SkTypeface *typeface) : +MinikinFontSkia::MinikinFontSkia(sk_sp typeface) : MinikinFont(typeface->uniqueID()), - mTypeface(typeface) { + mTypeface(std::move(typeface)) { } -MinikinFontSkia::~MinikinFontSkia() { - SkSafeUnref(mTypeface); -} - -static void MinikinFontSkia_SetSkiaPaint(SkTypeface* typeface, SkPaint* skPaint, const MinikinPaint& paint) { - skPaint->setTypeface(typeface); +static void MinikinFontSkia_SetSkiaPaint(sk_sp typeface, SkPaint* skPaint, const MinikinPaint& paint) { + skPaint->setTypeface(std::move(typeface)); skPaint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); // TODO: set more paint parameters from Minikin skPaint->setTextSize(paint.size); @@ -65,7 +61,7 @@ const void* MinikinFontSkia::GetTable(uint32_t tag, size_t* size, MinikinDestroy } SkTypeface *MinikinFontSkia::GetSkTypeface() { - return mTypeface; + return mTypeface.get(); } } // namespace minikin diff --git a/engine/src/flutter/sample/MinikinSkia.h b/engine/src/flutter/sample/MinikinSkia.h index 92f5ed2f52..d995f7d279 100644 --- a/engine/src/flutter/sample/MinikinSkia.h +++ b/engine/src/flutter/sample/MinikinSkia.h @@ -17,9 +17,7 @@ namespace minikin { class MinikinFontSkia : public MinikinFont { public: - explicit MinikinFontSkia(SkTypeface *typeface); - - ~MinikinFontSkia(); + explicit MinikinFontSkia(sk_sp typeface); float GetHorizontalAdvance(uint32_t glyph_id, const MinikinPaint &paint) const; @@ -32,7 +30,7 @@ public: SkTypeface *GetSkTypeface(); private: - SkTypeface *mTypeface; + sk_sp mTypeface; }; diff --git a/engine/src/flutter/sample/example_skia.cpp b/engine/src/flutter/sample/example_skia.cpp index bd2d779b9e..5e15a107c2 100644 --- a/engine/src/flutter/sample/example_skia.cpp +++ b/engine/src/flutter/sample/example_skia.cpp @@ -57,8 +57,8 @@ FontCollection *makeFontCollection() { FontFamily *family = new FontFamily(); for (size_t i = 0; i < sizeof(fns)/sizeof(fns[0]); i++) { const char *fn = fns[i]; - SkTypeface *skFace = SkTypeface::CreateFromFile(fn); - MinikinFont *font = new MinikinFontSkia(skFace); + sk_sp skFace = SkTypeface::MakeFromFile(fn); + MinikinFont *font = new MinikinFontSkia(std::move(skFace)); family->addFont(font); } typefaces.push_back(family); @@ -66,8 +66,8 @@ FontCollection *makeFontCollection() { #if 1 family = new FontFamily(); const char *fn = "/system/fonts/DroidSansDevanagari-Regular.ttf"; - SkTypeface *skFace = SkTypeface::CreateFromFile(fn); - MinikinFont *font = new MinikinFontSkia(skFace); + sk_sp skFace = SkTypeface::MakeFromFile(fn); + MinikinFont *font = new MinikinFontSkia(std::move(skFace)); family->addFont(font); typefaces.push_back(family); #endif @@ -93,13 +93,13 @@ void drawToSkia(SkCanvas *canvas, SkPaint *paint, Layout *layout, float x, float pos[i].fX = x + layout->getX(i); pos[i].fY = y + layout->getY(i); if (i > 0 && skFace != lastFace) { - paint->setTypeface(lastFace); + paint->setTypeface(sk_ref_sp(lastFace)); canvas->drawPosText(glyphs + start, (i - start) << 1, pos + start, *paint); start = i; } lastFace = skFace; } - paint->setTypeface(skFace); + paint->setTypeface(sk_ref_sp(skFace)); canvas->drawPosText(glyphs + start, (nGlyphs - start) << 1, pos + start, *paint); delete[] glyphs; delete[] pos; diff --git a/engine/src/flutter/tests/util/MinikinFontForTest.cpp b/engine/src/flutter/tests/util/MinikinFontForTest.cpp index 392d5b2ea5..807b234296 100644 --- a/engine/src/flutter/tests/util/MinikinFontForTest.cpp +++ b/engine/src/flutter/tests/util/MinikinFontForTest.cpp @@ -26,30 +26,23 @@ namespace minikin { // static MinikinFontForTest* MinikinFontForTest::createFromFile(const std::string& font_path) { - SkTypeface* typeface = SkTypeface::CreateFromFile(font_path.c_str()); - MinikinFontForTest* font = new MinikinFontForTest(font_path, typeface); - SkSafeUnref(typeface); + sk_sp typeface = SkTypeface::MakeFromFile(font_path.c_str()); + MinikinFontForTest* font = new MinikinFontForTest(font_path, std::move(typeface)); return font; } // static MinikinFontForTest* MinikinFontForTest::createFromFileWithIndex(const std::string& font_path, int index) { - SkTypeface* typeface = SkTypeface::CreateFromFile(font_path.c_str(), index); - MinikinFontForTest* font = new MinikinFontForTest(font_path, typeface); - SkSafeUnref(typeface); + sk_sp typeface = SkTypeface::MakeFromFile(font_path.c_str(), index); + MinikinFontForTest* font = new MinikinFontForTest(font_path, std::move(typeface)); return font; } -MinikinFontForTest::MinikinFontForTest(const std::string& font_path, SkTypeface* typeface) : +MinikinFontForTest::MinikinFontForTest(const std::string& font_path, sk_sp typeface) : MinikinFont(typeface->uniqueID()), - mTypeface(typeface), + mTypeface(std::move(typeface)), mFontPath(font_path) { - SkSafeRef(mTypeface); -} - -MinikinFontForTest::~MinikinFontForTest() { - SkSafeUnref(mTypeface); } float MinikinFontForTest::GetHorizontalAdvance(uint32_t /* glyph_id */, diff --git a/engine/src/flutter/tests/util/MinikinFontForTest.h b/engine/src/flutter/tests/util/MinikinFontForTest.h index 7b5322ca0b..423792f8d5 100644 --- a/engine/src/flutter/tests/util/MinikinFontForTest.h +++ b/engine/src/flutter/tests/util/MinikinFontForTest.h @@ -18,6 +18,7 @@ #define MINIKIN_TEST_MINIKIN_FONT_FOR_TEST_H #include +#include class SkTypeface; @@ -25,8 +26,7 @@ namespace minikin { class MinikinFontForTest : public MinikinFont { public: - MinikinFontForTest(const std::string& font_path, SkTypeface* typeface); - ~MinikinFontForTest(); + MinikinFontForTest(const std::string& font_path, sk_sp typeface); // Helper function for creating MinikinFontForTest instance from font file. // Calller need to unref returned object. @@ -45,7 +45,7 @@ private: MinikinFontForTest(const MinikinFontForTest&) = delete; MinikinFontForTest& operator=(MinikinFontForTest&) = delete; - SkTypeface *mTypeface; + sk_sp mTypeface; const std::string mFontPath; };