Add onGetFamilyName to SkTypeface.
This speeds up and documents this particular feature of SkTypeface
and also frees up SkFontDescriptor to be used only in serialization.
R=reed@google.com
Author: bungeman@google.com
Review URL: https://codereview.chromium.org/574873002
diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp
index fcb2b8e..74dd5d9 100644
--- a/src/core/SkTypeface.cpp
+++ b/src/core/SkTypeface.cpp
@@ -67,6 +67,9 @@
public:
virtual bool next(SkTypeface::LocalizedString*) SK_OVERRIDE { return false; }
};
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
+ familyName->reset();
+ }
virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE {
return SkNEW(EmptyLocalizedStrings);
};
@@ -273,10 +276,8 @@
}
void SkTypeface::getFamilyName(SkString* name) const {
- bool isLocal = false;
- SkFontDescriptor desc(this->style());
- this->onGetFontDescriptor(&desc, &isLocal);
- name->set(desc.getFamilyName());
+ SkASSERT(name);
+ this->onGetFamilyName(name);
}
SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics(
diff --git a/src/fonts/SkGScalerContext.cpp b/src/fonts/SkGScalerContext.cpp
index e543fdb..34a788a 100644
--- a/src/fonts/SkGScalerContext.cpp
+++ b/src/fonts/SkGScalerContext.cpp
@@ -206,6 +206,10 @@
return fProxy->getUnitsPerEm();
}
+void SkGTypeface::onGetFamilyName(SkString* familyName) const {
+ fProxy->getFamilyName(familyName);
+}
+
SkTypeface::LocalizedStrings* SkGTypeface::onCreateFamilyNameIterator() const {
return fProxy->createFamilyNameIterator();
}
diff --git a/src/fonts/SkGScalerContext.h b/src/fonts/SkGScalerContext.h
index faca50e..8ad2817 100644
--- a/src/fonts/SkGScalerContext.h
+++ b/src/fonts/SkGScalerContext.h
@@ -34,6 +34,7 @@
virtual int onCountGlyphs() const SK_OVERRIDE;
virtual int onGetUPEM() const SK_OVERRIDE;
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE;
virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE;
virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE;
diff --git a/src/fonts/SkTestScalerContext.cpp b/src/fonts/SkTestScalerContext.cpp
index 854c980..ee379f0 100644
--- a/src/fonts/SkTestScalerContext.cpp
+++ b/src/fonts/SkTestScalerContext.cpp
@@ -175,6 +175,10 @@
return glyphCount;
}
+void SkTestTypeface::onGetFamilyName(SkString* familyName) const {
+ *familyName = fTestFont->fName;
+}
+
SkTypeface::LocalizedStrings* SkTestTypeface::onCreateFamilyNameIterator() const {
SkString familyName(fTestFont->fName);
SkString language("und"); //undetermined
diff --git a/src/fonts/SkTestScalerContext.h b/src/fonts/SkTestScalerContext.h
index aed64e7..42f6049 100644
--- a/src/fonts/SkTestScalerContext.h
+++ b/src/fonts/SkTestScalerContext.h
@@ -92,6 +92,7 @@
return 1;
}
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE;
virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE;
virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE {
diff --git a/src/ports/SkFontConfigTypeface.h b/src/ports/SkFontConfigTypeface.h
index b6f8797..f62d99d 100644
--- a/src/ports/SkFontConfigTypeface.h
+++ b/src/ports/SkFontConfigTypeface.h
@@ -65,6 +65,7 @@
SkSafeRef(localStream);
}
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE;
virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE;
virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE;
diff --git a/src/ports/SkFontHost_fontconfig.cpp b/src/ports/SkFontHost_fontconfig.cpp
index ace7058..57252d0 100644
--- a/src/ports/SkFontHost_fontconfig.cpp
+++ b/src/ports/SkFontHost_fontconfig.cpp
@@ -162,6 +162,10 @@
return stream;
}
+void FontConfigTypeface::onGetFamilyName(SkString* familyName) const {
+ *familyName = this->getFamilyName();
+}
+
void FontConfigTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
bool* isLocalStream) const {
desc->setFamilyName(this->getFamilyName());
diff --git a/src/ports/SkFontHost_linux.cpp b/src/ports/SkFontHost_linux.cpp
index 72a6399..2904cac 100644
--- a/src/ports/SkFontHost_linux.cpp
+++ b/src/ports/SkFontHost_linux.cpp
@@ -40,6 +40,10 @@
virtual const char* getUniqueString() const = 0;
protected:
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
+ *familyName = fFamilyName;
+ }
+
virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const SK_OVERRIDE {
desc->setFamilyName(fFamilyName.c_str());
desc->setFontFileName(this->getUniqueString());
diff --git a/src/ports/SkFontHost_mac.cpp b/src/ports/SkFontHost_mac.cpp
index 3c73493..5386ac3 100755
--- a/src/ports/SkFontHost_mac.cpp
+++ b/src/ports/SkFontHost_mac.cpp
@@ -452,6 +452,7 @@
virtual int onGetUPEM() const SK_OVERRIDE;
virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE;
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE;
virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE;
virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE;
virtual size_t onGetTableData(SkFontTableTag, size_t offset,
@@ -1900,6 +1901,10 @@
return str->c_str();
}
+void SkTypeface_Mac::onGetFamilyName(SkString* familyName) const {
+ get_str(CTFontCopyFamilyName(fFontRef), familyName);
+}
+
void SkTypeface_Mac::onGetFontDescriptor(SkFontDescriptor* desc,
bool* isLocalStream) const {
SkString tmpStr;
diff --git a/src/ports/SkFontHost_win.cpp b/src/ports/SkFontHost_win.cpp
index 6f8669e..1290c00 100755
--- a/src/ports/SkFontHost_win.cpp
+++ b/src/ports/SkFontHost_win.cpp
@@ -276,6 +276,7 @@
uint16_t glyphs[], int glyphCount) const SK_OVERRIDE;
virtual int onCountGlyphs() const SK_OVERRIDE;
virtual int onGetUPEM() const SK_OVERRIDE;
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE;
virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE;
virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE;
virtual size_t onGetTableData(SkFontTableTag, size_t offset,
@@ -1761,16 +1762,14 @@
#endif
}
-void LogFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
- bool* isLocalStream) const {
+void LogFontTypeface::onGetFamilyName(SkString* familyName) const {
// Get the actual name of the typeface. The logfont may not know this.
HFONT font = CreateFontIndirect(&fLogFont);
HDC deviceContext = ::CreateCompatibleDC(NULL);
HFONT savefont = (HFONT)SelectObject(deviceContext, font);
- SkString familyName;
- dcfontname_to_skstring(deviceContext, fLogFont, &familyName);
+ dcfontname_to_skstring(deviceContext, fLogFont, familyName);
if (deviceContext) {
::SelectObject(deviceContext, savefont);
@@ -1779,7 +1778,12 @@
if (font) {
::DeleteObject(font);
}
+}
+void LogFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
+ bool* isLocalStream) const {
+ SkString familyName;
+ this->onGetFamilyName(&familyName);
desc->setFamilyName(familyName.c_str());
*isLocalStream = this->fSerializeAsStream;
}
diff --git a/src/ports/SkFontMgr_android.cpp b/src/ports/SkFontMgr_android.cpp
index bcf544c..2fee735 100644
--- a/src/ports/SkFontMgr_android.cpp
+++ b/src/ports/SkFontMgr_android.cpp
@@ -49,9 +49,11 @@
, fIndex(index)
, fFamilyName(familyName) { }
- const SkString& name() const { return fFamilyName; }
-
protected:
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
+ *familyName = fFamilyName;
+ }
+
int fIndex;
SkString fFamilyName;
diff --git a/src/ports/SkFontMgr_fontconfig.cpp b/src/ports/SkFontMgr_fontconfig.cpp
index b8a3fe5..f4cae23 100644
--- a/src/ports/SkFontMgr_fontconfig.cpp
+++ b/src/ports/SkFontMgr_fontconfig.cpp
@@ -389,6 +389,10 @@
, fIndex(ttcIndex)
{ };
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
+ familyName->reset();
+ }
+
virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const SK_OVERRIDE {
desc->setStyle(this->style());
*serialize = true;
@@ -414,6 +418,10 @@
}
mutable SkAutoFcPattern fPattern;
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
+ *familyName = get_string(fPattern, FC_FAMILY);
+ }
+
virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const SK_OVERRIDE {
FCLocker lock;
desc->setFamilyName(get_string(fPattern, FC_FAMILY));
diff --git a/src/ports/SkTypeface_win_dw.cpp b/src/ports/SkTypeface_win_dw.cpp
index 9c727e7..9c2b41d 100644
--- a/src/ports/SkTypeface_win_dw.cpp
+++ b/src/ports/SkTypeface_win_dw.cpp
@@ -12,6 +12,7 @@
// not use GDI, undefing GetGlyphIndices makes things less confusing.
#undef GetGlyphIndices
+#include "SkDWrite.h"
#include "SkDWriteFontFileStream.h"
#include "SkFontDescriptor.h"
#include "SkFontStream.h"
@@ -24,20 +25,21 @@
#include "SkTypeface_win_dw.h"
#include "SkUtils.h"
+void DWriteFontTypeface::onGetFamilyName(SkString* familyName) const {
+ SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
+ HRV(fDWriteFontFamily->GetFamilyNames(&familyNames));
+
+ sk_get_locale_string(familyNames.get(), NULL/*fMgr->fLocaleName.get()*/, familyName);
+}
+
void DWriteFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
bool* isLocalStream) const {
// Get the family name.
SkTScopedComPtr<IDWriteLocalizedStrings> familyNames;
HRV(fDWriteFontFamily->GetFamilyNames(&familyNames));
- UINT32 familyNamesLen;
- HRV(familyNames->GetStringLength(0, &familyNamesLen));
-
- SkSMallocWCHAR familyName(familyNamesLen+1);
- HRV(familyNames->GetString(0, familyName.get(), familyNamesLen+1));
-
SkString utf8FamilyName;
- HRV(sk_wchar_to_skstring(familyName.get(), familyNamesLen, &utf8FamilyName));
+ sk_get_locale_string(familyNames.get(), NULL/*fMgr->fLocaleName.get()*/, &utf8FamilyName);
desc->setFamilyName(utf8FamilyName.c_str());
*isLocalStream = SkToBool(fDWriteFontFileLoader.get());
diff --git a/src/ports/SkTypeface_win_dw.h b/src/ports/SkTypeface_win_dw.h
index 6506858..531dc51 100644
--- a/src/ports/SkTypeface_win_dw.h
+++ b/src/ports/SkTypeface_win_dw.h
@@ -111,6 +111,7 @@
uint16_t glyphs[], int glyphCount) const SK_OVERRIDE;
virtual int onCountGlyphs() const SK_OVERRIDE;
virtual int onGetUPEM() const SK_OVERRIDE;
+ virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE;
virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE;
virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE;
virtual size_t onGetTableData(SkFontTableTag, size_t offset,