From 99840553cda3184ec2e32fbb192d104741ceea86 Mon Sep 17 00:00:00 2001 From: "reed@google.com" Date: Fri, 1 Jun 2012 16:19:32 +0000 Subject: [PATCH] use SkFontDescriptor on mac Review URL: https://codereview.appspot.com/6247064 git-svn-id: http://skia.googlecode.com/svn/trunk@4117 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/ports/SkFontHost_mac_coretext.cpp | 34 ++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/ports/SkFontHost_mac_coretext.cpp b/src/ports/SkFontHost_mac_coretext.cpp index c43d1a654b..f49185fb34 100644 --- a/src/ports/SkFontHost_mac_coretext.cpp +++ b/src/ports/SkFontHost_mac_coretext.cpp @@ -21,6 +21,7 @@ #include "SkCGUtils.h" #include "SkDescriptor.h" #include "SkEndian.h" +#include "SkFontDescriptor.h" #include "SkFloatingPoint.h" #include "SkPaint.h" #include "SkString.h" @@ -378,6 +379,11 @@ public: CTFontRef fFontRef; }; +static CTFontRef typeface_to_fontref(const SkTypeface* face) { + const SkTypeface_Mac* macface = reinterpret_cast(face); + return macface->fFontRef; +} + static SkTypeface* NewFromFontRef(CTFontRef fontRef, const char name[]) { SkASSERT(fontRef); bool isMonospace; @@ -1835,18 +1841,30 @@ size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length, #include "SkStream.h" +// we take ownership of the ref +static const char* get_str(CFStringRef ref, SkString* str) { + CFStringToSkString(ref, str); + CFSafeRelease(ref); + return str->c_str(); +} + void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) { - // hack: need a real name or something from CG - uint32_t fontID = face->uniqueID(); - stream->write(&fontID, 4); + CTFontRef ctFont = typeface_to_fontref(face); + SkFontDescriptor desc(face->style()); + SkString tmpStr; + + desc.setFamilyName(get_str(CTFontCopyFamilyName(ctFont), &tmpStr)); + desc.setFullName(get_str(CTFontCopyFullName(ctFont), &tmpStr)); + desc.setPostscriptName(get_str(CTFontCopyPostScriptName(ctFont), &tmpStr)); + + desc.serialize(stream); } SkTypeface* SkFontHost::Deserialize(SkStream* stream) { - // hack: need a real name or something from CG - SkFontID fontID = stream->readU32(); - SkTypeface* face = SkTypefaceCache::FindByID(fontID); - SkSafeRef(face); - return face; + SkFontDescriptor desc(stream); + + return SkFontHost::CreateTypeface(NULL, desc.getFamilyName(), + desc.getStyle()); } ///////////////////////////////////////////////////////////////////////////////