Revert of Extend SkFontMgr_Custom to cover ttc, otf, pfb. (patchset #2 id:20001 of https://codereview.chromium.org/672723002/)
Reason for revert:
Reverting as this appears to be causing issues with color fonts on Android.
Original issue's description:
> Extend SkFontMgr_Custom to cover ttc, otf, pfb.
>
> Adds ability to test ScanFont with Type1 (pfb) fonts
> and fonts with multiple faces (ttc).
>
> Committed: https://skia.googlesource.com/skia/+/ee0c2e4fd429424beaa35f29e7f656997ba3f115
TBR=reed@google.com,halcanary@google.com
NOTREECHECKS=true
NOTRY=true
Review URL: https://codereview.chromium.org/685803003
diff --git a/src/fonts/SkFontMgr_fontconfig.cpp b/src/fonts/SkFontMgr_fontconfig.cpp
index 4f99a6f..c56e065 100644
--- a/src/fonts/SkFontMgr_fontconfig.cpp
+++ b/src/fonts/SkFontMgr_fontconfig.cpp
@@ -214,7 +214,7 @@
class SkFontMgr_fontconfig : public SkFontMgr {
SkAutoTUnref<SkFontConfigInterface> fFCI;
SkDataTable* fFamilyNames;
- SkTypeface_FreeType::Scanner fScanner;
+
public:
SkFontMgr_fontconfig(SkFontConfigInterface* fci)
@@ -302,7 +302,7 @@
// TODO should the caller give us the style or should we get it from freetype?
SkFontStyle style;
bool isFixedWidth = false;
- if (!fScanner.scanFont(stream, 0, NULL, &style, &isFixedWidth)) {
+ if (!SkTypeface_FreeType::ScanFont(stream, 0, NULL, &style, &isFixedWidth)) {
return NULL;
}
diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp
index 58d2999..ba0ce14 100644
--- a/src/ports/SkFontHost_FreeType.cpp
+++ b/src/ports/SkFontHost_FreeType.cpp
@@ -1669,26 +1669,20 @@
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
-
-SkTypeface_FreeType::Scanner::Scanner() {
- if (FT_Init_FreeType(&fLibrary)) {
- fLibrary = NULL;
- }
-}
-SkTypeface_FreeType::Scanner::~Scanner() {
- FT_Done_FreeType(fLibrary);
-}
-
-FT_Face SkTypeface_FreeType::Scanner::openFace(SkStream* stream, int ttcIndex) const {
- if (fLibrary == NULL) {
- return NULL;
+#include "SkTSearch.h"
+/*static*/ bool SkTypeface_FreeType::ScanFont(
+ SkStream* stream, int ttcIndex, SkString* name, SkFontStyle* style, bool* isFixedPitch)
+{
+ FT_Library library;
+ if (FT_Init_FreeType(&library)) {
+ return false;
}
- FT_Open_Args args;
+ FT_Open_Args args;
memset(&args, 0, sizeof(args));
const void* memoryBase = stream->getMemoryBase();
- FT_StreamRec streamRec;
+ FT_StreamRec streamRec;
if (memoryBase) {
args.flags = FT_OPEN_MEMORY;
@@ -1706,30 +1700,8 @@
}
FT_Face face;
- if (FT_Open_Face(fLibrary, &args, ttcIndex, &face)) {
- return NULL;
- }
- return face;
-}
-
-bool SkTypeface_FreeType::Scanner::recognizedFont(SkStream* stream, int* numFaces) const {
- FT_Face face = this->openFace(stream, -1);
- if (NULL == face) {
- return false;
- }
-
- *numFaces = face->num_faces;
-
- FT_Done_Face(face);
- return true;
-}
-
-#include "SkTSearch.h"
-bool SkTypeface_FreeType::Scanner::scanFont(
- SkStream* stream, int ttcIndex, SkString* name, SkFontStyle* style, bool* isFixedPitch) const
-{
- FT_Face face = this->openFace(stream, ttcIndex);
- if (NULL == face) {
+ if (FT_Open_Face(library, &args, ttcIndex, &face)) {
+ FT_Done_FreeType(library);
return false;
}
@@ -1759,19 +1731,14 @@
{ "book", (SkFontStyle::kNormal_Weight + SkFontStyle::kLight_Weight)/2 },
{ "demi", SkFontStyle::kSemiBold_Weight },
{ "demibold", SkFontStyle::kSemiBold_Weight },
- { "extra", SkFontStyle::kExtraBold_Weight },
{ "extrabold", SkFontStyle::kExtraBold_Weight },
{ "extralight", SkFontStyle::kExtraLight_Weight },
- { "hairline", SkFontStyle::kThin_Weight },
{ "heavy", SkFontStyle::kBlack_Weight },
{ "light", SkFontStyle::kLight_Weight },
{ "medium", SkFontStyle::kMedium_Weight },
{ "normal", SkFontStyle::kNormal_Weight },
- { "plain", SkFontStyle::kNormal_Weight },
{ "regular", SkFontStyle::kNormal_Weight },
- { "roman", SkFontStyle::kNormal_Weight },
{ "semibold", SkFontStyle::kSemiBold_Weight },
- { "standard", SkFontStyle::kNormal_Weight },
{ "thin", SkFontStyle::kThin_Weight },
{ "ultra", SkFontStyle::kExtraBold_Weight },
{ "ultrablack", 1000 },
@@ -1784,7 +1751,7 @@
if (index >= 0) {
weight = commonWeights[index].weight;
} else {
- SkDEBUGF(("Do not know weight for: %s (%s) \n", face->family_name, psFontInfo.weight));
+ SkDEBUGF(("Do not know weight for: %s\n", psFontInfo.weight));
}
}
@@ -1799,5 +1766,6 @@
}
FT_Done_Face(face);
+ FT_Done_FreeType(library);
return true;
}
diff --git a/src/ports/SkFontHost_FreeType_common.h b/src/ports/SkFontHost_FreeType_common.h
index f275d44..79e6d65 100644
--- a/src/ports/SkFontHost_FreeType_common.h
+++ b/src/ports/SkFontHost_FreeType_common.h
@@ -48,17 +48,8 @@
/** For SkFontMgrs to make use of our ability to extract
* name and style from a stream, using FreeType's API.
*/
- class Scanner {
- public:
- Scanner();
- ~Scanner();
- bool recognizedFont(SkStream* stream, int* numFonts) const;
- bool scanFont(SkStream* stream, int ttcIndex,
- SkString* name, SkFontStyle* style, bool* isFixedPitch) const;
- private:
- FT_Face openFace(SkStream* stream, int ttcIndex) const;
- FT_Library fLibrary;
- };
+ static bool ScanFont(SkStream* stream, int ttcIndex,
+ SkString* name, SkFontStyle* style, bool* isFixedPitch);
protected:
SkTypeface_FreeType(const SkFontStyle& style, SkFontID uniqueID, bool isFixedPitch)
diff --git a/src/ports/SkFontHost_linux.cpp b/src/ports/SkFontHost_linux.cpp
index 0b99e65..90141f9 100644
--- a/src/ports/SkFontHost_linux.cpp
+++ b/src/ports/SkFontHost_linux.cpp
@@ -22,7 +22,7 @@
#include <limits>
#ifndef SK_FONT_FILE_PREFIX
-# define SK_FONT_FILE_PREFIX "/usr/share/fonts/"
+# define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/"
#endif
///////////////////////////////////////////////////////////////////////////////
@@ -275,7 +275,7 @@
bool isFixedPitch;
SkFontStyle style;
SkString name;
- if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
+ if (SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
stream, ttcIndex));
} else {
@@ -314,48 +314,46 @@
private:
- void load_directory_fonts(const SkString& directory, const char* suffix) {
- SkOSFile::Iter iter(directory.c_str(), suffix);
+ static bool get_name_and_style(const char path[], SkString* name,
+ SkFontStyle* style, bool* isFixedPitch) {
+ SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
+ if (stream.get()) {
+ return SkTypeface_FreeType::ScanFont(stream, 0, name, style, isFixedPitch);
+ } else {
+ SkDebugf("---- failed to open <%s> as a font\n", path);
+ return false;
+ }
+ }
+
+ void load_directory_fonts(const SkString& directory) {
+ SkOSFile::Iter iter(directory.c_str(), ".ttf");
SkString name;
while (iter.next(&name, false)) {
- SkString filename(SkOSPath::Join(directory.c_str(), name.c_str()));
- SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
- if (!stream.get()) {
- SkDebugf("---- failed to open <%s>\n", filename.c_str());
+ SkString filename(
+ SkOSPath::Join(directory.c_str(), name.c_str()));
+
+ bool isFixedPitch;
+ SkString realname;
+ SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
+ if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedPitch)) {
+ SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
continue;
}
- int numFaces;
- if (!fScanner.recognizedFont(stream, &numFaces)) {
- SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
- continue;
+ SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
+ style,
+ isFixedPitch,
+ true, // system-font (cannot delete)
+ realname,
+ filename.c_str(), 0));
+
+ SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str());
+ if (NULL == addTo) {
+ addTo = new SkFontStyleSet_Custom(realname);
+ fFamilies.push_back().reset(addTo);
}
-
- for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
- bool isFixedPitch;
- SkString realname;
- SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
- if (!fScanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) {
- SkDebugf("---- failed to open <%s> <%d> as a font\n",
- filename.c_str(), faceIndex);
- continue;
- }
-
- SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
- style,
- isFixedPitch,
- true, // system-font (cannot delete)
- realname,
- filename.c_str(), 0));
-
- SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str());
- if (NULL == addTo) {
- addTo = new SkFontStyleSet_Custom(realname);
- fFamilies.push_back().reset(addTo);
- }
- addTo->appendTypeface(tf);
- }
+ addTo->appendTypeface(tf);
}
SkOSFile::Iter dirIter(directory.c_str());
@@ -364,16 +362,13 @@
continue;
}
SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
- load_directory_fonts(dirname, suffix);
+ load_directory_fonts(dirname);
}
}
void load_system_fonts(const char* dir) {
SkString baseDirectory(dir);
- load_directory_fonts(baseDirectory, ".ttf");
- load_directory_fonts(baseDirectory, ".ttc");
- load_directory_fonts(baseDirectory, ".otf");
- load_directory_fonts(baseDirectory, ".pfb");
+ load_directory_fonts(baseDirectory);
if (fFamilies.empty()) {
SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
@@ -411,7 +406,6 @@
SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies;
SkFontStyleSet_Custom* gDefaultFamily;
SkTypeface* gDefaultNormal;
- SkTypeface_FreeType::Scanner fScanner;
};
SkFontMgr* SkFontMgr::Factory() {
diff --git a/src/ports/SkFontMgr_android.cpp b/src/ports/SkFontMgr_android.cpp
index 4677a1f..c527551 100644
--- a/src/ports/SkFontMgr_android.cpp
+++ b/src/ports/SkFontMgr_android.cpp
@@ -138,9 +138,7 @@
class SkFontStyleSet_Android : public SkFontStyleSet {
public:
- explicit SkFontStyleSet_Android(const FontFamily& family, const char* basePath,
- const SkTypeface_FreeType::Scanner& scanner)
- {
+ explicit SkFontStyleSet_Android(const FontFamily& family, const char* basePath) {
const SkString* cannonicalFamilyName = NULL;
if (family.fNames.count() > 0) {
cannonicalFamilyName = &family.fNames[0];
@@ -162,7 +160,8 @@
SkString familyName;
SkFontStyle style;
bool isFixedWidth;
- if (!scanner.scanFont(stream.get(), ttcIndex, &familyName, &style, &isFixedWidth)) {
+ if (!SkTypeface_FreeType::ScanFont(stream.get(), ttcIndex,
+ &familyName, &style, &isFixedWidth)) {
DEBUG_FONT(("---- SystemFonts[%d] file=%s (INVALID)", i, pathName.c_str()));
continue;
}
@@ -436,7 +435,7 @@
bool isFixedPitch;
SkFontStyle style;
SkString name;
- if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
+ if (!SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
return NULL;
}
return SkNEW_ARGS(SkTypeface_AndroidStream, (stream, ttcIndex,
@@ -461,8 +460,6 @@
private:
- SkTypeface_FreeType::Scanner fScanner;
-
SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets;
SkFontStyleSet* fDefaultFamily;
SkTypeface* fDefaultTypeface;
@@ -484,8 +481,7 @@
}
}
- SkFontStyleSet_Android* newSet =
- SkNEW_ARGS(SkFontStyleSet_Android, (family, basePath, fScanner));
+ SkFontStyleSet_Android* newSet = SkNEW_ARGS(SkFontStyleSet_Android, (family, basePath));
if (0 == newSet->count()) {
SkDELETE(newSet);
continue;
diff --git a/src/ports/SkFontMgr_fontconfig.cpp b/src/ports/SkFontMgr_fontconfig.cpp
index 815d9f9..d7570d9 100644
--- a/src/ports/SkFontMgr_fontconfig.cpp
+++ b/src/ports/SkFontMgr_fontconfig.cpp
@@ -452,7 +452,6 @@
class SkFontMgr_fontconfig : public SkFontMgr {
mutable SkAutoFcConfig fFC;
SkAutoTUnref<SkDataTable> fFamilyNames;
- SkTypeface_FreeType::Scanner fScanner;
class StyleSet : public SkFontStyleSet {
public:
@@ -828,7 +827,7 @@
SkFontStyle style;
bool isFixedWidth = false;
- if (!fScanner.scanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) {
+ if (!SkTypeface_FreeType::ScanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) {
return NULL;
}