From 9564c28b08e0be4f763dd6243e09d4dc1b186dd1 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Fri, 28 Sep 2018 19:52:58 +0200 Subject: [PATCH] Create and exposed a SkTypeface::RefDefault to the C API --- include/c/sk_typeface.h | 1 + include/core/SkTypeface.h | 2 ++ src/c/sk_typeface.cpp | 4 ++++ src/core/SkTypeface.cpp | 11 +++++++++++ 4 files changed, 18 insertions(+) diff --git a/include/c/sk_typeface.h b/include/c/sk_typeface.h index b8526ab13800..66620bcf23e5 100644 --- a/include/c/sk_typeface.h +++ b/include/c/sk_typeface.h @@ -17,6 +17,7 @@ SK_C_PLUS_PLUS_BEGIN_GUARD // typeface SK_C_API sk_typeface_t* sk_typeface_create_default(void); +SK_C_API sk_typeface_t* sk_typeface_ref_default(void); SK_C_API sk_typeface_t* sk_typeface_create_from_name_with_font_style(const char *familyName, sk_fontstyle_t* style); SK_C_API void sk_typeface_unref(sk_typeface_t*); SK_C_API sk_typeface_t* sk_typeface_create_from_file(const char* path, int index); diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h index 665491c3dbd9..7a6a06d8ec99 100644 --- a/include/core/SkTypeface.h +++ b/include/core/SkTypeface.h @@ -90,6 +90,8 @@ class SK_API SkTypeface : public SkWeakRefCnt { /** Returns the default normal typeface, which is never nullptr. */ static sk_sp MakeDefault(); + static sk_sp RefDefault(); + /** Creates a new reference to the typeface that most closely matches the requested familyName and fontStyle. This method allows extended font face specifiers as in the SkFontStyle type. Will never return null. diff --git a/src/c/sk_typeface.cpp b/src/c/sk_typeface.cpp index 915599ff324a..a245a1b73c68 100644 --- a/src/c/sk_typeface.cpp +++ b/src/c/sk_typeface.cpp @@ -23,6 +23,10 @@ sk_typeface_t* sk_typeface_create_default(void) { return ToTypeface(SkTypeface::MakeDefault().release()); } +sk_typeface_t* sk_typeface_ref_default(void) { + return ToTypeface(SkTypeface::RefDefault().release()); +} + void sk_typeface_unref(sk_typeface_t* tf) { SkSafeUnref(AsTypeface(tf)); } diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp index 92c8c09e338f..2fdff7063a1d 100644 --- a/src/core/SkTypeface.cpp +++ b/src/core/SkTypeface.cpp @@ -111,6 +111,17 @@ sk_sp SkTypeface::MakeDefault() { return sk_ref_sp(GetDefaultTypeface()); } +sk_sp SkTypeface::RefDefault() { + static SkOnce once; + static sk_sp singleton; + + once([]{ + SkTypeface* tf = GetDefaultTypeface(); + singleton = sk_ref_sp(tf); + }); + return singleton; +} + uint32_t SkTypeface::UniqueID(const SkTypeface* face) { if (nullptr == face) { face = GetDefaultTypeface();