Improved error handling when font loading fails.
Without this this patch, an application could trigger a platform crash
by calling:
Typeface typeface = Typeface.createFromFile("");
boolean isBold = typeface.isBold();
The crash occurs since the find_name_and_style function does not
return an error code.
Change-Id: Id1d2a43b47d956c5a52f01e7d4b8cbb12631d59a
diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp
index c7e310e..a4c5195 100644
--- a/src/ports/SkFontHost_FreeType.cpp
+++ b/src/ports/SkFontHost_FreeType.cpp
@@ -940,11 +940,11 @@
/* Export this so that other parts of our FonttHost port can make use of our
ability to extract the name+style from a stream, using FreeType's api.
*/
-SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name) {
+bool find_name_and_style(SkStream* stream, SkString* name, SkTypeface::Style* style) {
FT_Library library;
if (FT_Init_FreeType(&library)) {
name->set(NULL);
- return SkTypeface::kNormal;
+ return false;
}
FT_Open_Args args;
@@ -972,20 +972,21 @@
if (FT_Open_Face(library, &args, 0, &face)) {
FT_Done_FreeType(library);
name->set(NULL);
- return SkTypeface::kNormal;
+ return false;
}
name->set(face->family_name);
- int style = SkTypeface::kNormal;
+ int tempStyle = SkTypeface::kNormal;
if (face->style_flags & FT_STYLE_FLAG_BOLD) {
- style |= SkTypeface::kBold;
+ tempStyle |= SkTypeface::kBold;
}
if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
- style |= SkTypeface::kItalic;
+ tempStyle |= SkTypeface::kItalic;
}
+ *style = (SkTypeface::Style)tempStyle;
FT_Done_Face(face);
FT_Done_FreeType(library);
- return (SkTypeface::Style)style;
+ return true;
}
diff --git a/src/ports/SkFontHost_android.cpp b/src/ports/SkFontHost_android.cpp
index 6aa7a0b..2dd087a 100644
--- a/src/ports/SkFontHost_android.cpp
+++ b/src/ports/SkFontHost_android.cpp
@@ -31,7 +31,7 @@
#define SK_FONT_FILE_PREFIX "/fonts/"
#endif
-SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name);
+bool find_name_and_style(SkStream* stream, SkString* name, SkTypeface::Style* style);
static void GetFullPathForSysFonts(SkString* full, const char name[]) {
full->set(getenv("ANDROID_ROOT"));
@@ -363,14 +363,12 @@
SkMMAPStream stream(fullpath.c_str());
if (stream.getLength() > 0) {
- *style = find_name_and_style(&stream, name);
- return true;
+ return find_name_and_style(&stream, name, style);
}
else {
SkFILEStream stream(fullpath.c_str());
if (stream.getLength() > 0) {
- *style = find_name_and_style(&stream, name);
- return true;
+ return find_name_and_style(&stream, name, style);
}
}
@@ -643,9 +641,13 @@
}
SkString name;
- SkTypeface::Style style = find_name_and_style(stream, &name);
+ SkTypeface::Style style;
- return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream));
+ if (find_name_and_style(stream, &name, &style)) {
+ return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream));
+ } else {
+ return NULL;
+ }
}
SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {