Index: src/ports/SkFontConfigParser_android.cpp |
diff --git a/src/ports/SkFontConfigParser_android.cpp b/src/ports/SkFontConfigParser_android.cpp |
index cc2ca8cad92f7fe4fcc7e4e66a0c24833081635a..82079dbda683f9186ac0e35d8acd51084383348c 100644 |
--- a/src/ports/SkFontConfigParser_android.cpp |
+++ b/src/ports/SkFontConfigParser_android.cpp |
@@ -11,8 +11,8 @@ |
#include "SkTypeface.h" |
#include <expat.h> |
+#include <dirent.h> |
#include <stdio.h> |
-#include <sys/system_properties.h> |
#include <limits> |
@@ -20,6 +20,10 @@ |
#define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" |
#define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml" |
+#define LOCALE_FALLBACK_FONTS_DIR "/system/etc" |
+#define LOCALE_FALLBACK_FONTS_PREFIX "fallback_fonts-" |
+#define LOCALE_FALLBACK_FONTS_SUFFIX ".xml" |
+ |
// These defines are used to determine the kind of tag that we're currently |
// populating with data. We only care about the sibling tags nameset and fileset |
// for now. |
@@ -194,39 +198,7 @@ static void endElementHandler(void *data, const char *tag) { |
*/ |
static void parseConfigFile(const char *filename, SkTDArray<FontFamily*> &families) { |
- FILE* file = NULL; |
- |
-#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
- // if we are using a version of Android prior to Android 4.2 (JellyBean MR1 |
- // at API Level 17) then we need to look for files with a different suffix. |
- char sdkVersion[PROP_VALUE_MAX]; |
- __system_property_get("ro.build.version.sdk", sdkVersion); |
- const int sdkVersionInt = atoi(sdkVersion); |
- |
- if (0 != *sdkVersion && sdkVersionInt < 17) { |
- SkString basename; |
- SkString updatedFilename; |
- SkString locale = SkFontConfigParser::GetLocale(); |
- |
- basename.set(filename); |
- // Remove the .xml suffix. We'll add it back in a moment. |
- if (basename.endsWith(".xml")) { |
- basename.resize(basename.size()-4); |
- } |
- // Try first with language and region |
- updatedFilename.printf("%s-%s.xml", basename.c_str(), locale.c_str()); |
- file = fopen(updatedFilename.c_str(), "r"); |
- if (!file) { |
- // If not found, try next with just language |
- updatedFilename.printf("%s-%.2s.xml", basename.c_str(), locale.c_str()); |
- file = fopen(updatedFilename.c_str(), "r"); |
- } |
- } |
-#endif |
- |
- if (NULL == file) { |
- file = fopen(filename, "r"); |
- } |
+ FILE* file = fopen(filename, "r"); |
// Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml) |
// are optional - failure here is okay because one of these optional files may not exist. |
@@ -262,6 +234,50 @@ static void getFallbackFontFamilies(SkTDArray<FontFamily*> &fallbackFonts) { |
parseConfigFile(FALLBACK_FONTS_FILE, fallbackFonts); |
parseConfigFile(VENDOR_FONTS_FILE, vendorFonts); |
+#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
+ // In some versions of Android prior to Android 4.2 (JellyBean MR1 at API |
+ // Level 17) the fallback fonts for certain locales were encoded in their |
+ // own XML files with a suffix that identified the locale. We search the |
+ // system for those files and add all of their entries to the fallback chain |
+ // including the locale in the entry. |
+ |
+ DIR* fontDirectory = opendir(LOCALE_FALLBACK_FONTS_DIR); |
+ if (fontDirectory != NULL){ |
+ struct dirent* dirEntry = readdir(fontDirectory); |
+ while (dirEntry){ |
bungeman-skia
2014/08/01 20:41:25
nit: space after ')'
djsollen
2014/08/04 14:18:26
Done.
|
+ |
+ // The size of both the prefix, suffix, and a minimum valid language code |
+ const int minSize = strlen(LOCALE_FALLBACK_FONTS_PREFIX) + |
djsollen
2014/08/01 20:13:41
it appears that both fallback_fonts-**.xml and ven
|
+ strlen(LOCALE_FALLBACK_FONTS_SUFFIX) + 2; |
+ |
+ SkString fileName(dirEntry->d_name); |
+ if (fileName.startsWith(LOCALE_FALLBACK_FONTS_PREFIX) && fileName.size() >= minSize) { |
bungeman-skia
2014/08/01 20:41:25
Do we care to check here if is does end with the S
djsollen
2014/08/04 14:18:26
Added suffix checks
|
+ |
+ SkString locale(fileName); |
bungeman-skia
2014/08/01 20:41:24
Can this be something like
int localeLen = strlen
djsollen
2014/08/04 14:18:26
Done.
|
+ locale.resize(fileName.size() - strlen(LOCALE_FALLBACK_FONTS_SUFFIX)); |
bungeman-skia
2014/08/01 20:41:25
Well, I was going to say replace "strlen(CONST_STR
|
+ locale.remove(0, strlen(LOCALE_FALLBACK_FONTS_PREFIX)); |
+ |
+ SkString absoluteFilename; |
+ absoluteFilename.printf("%s/%s", LOCALE_FALLBACK_FONTS_DIR, fileName.c_str()); |
+ |
+ SkTDArray<FontFamily*> langSpecificFonts; |
+ parseConfigFile(absoluteFilename.c_str(), langSpecificFonts); |
+ |
+ for (int i = 0; i < langSpecificFonts.count(); ++i) { |
+ FontFamily* family = langSpecificFonts[i]; |
+ for (int j = 0; j < family->fFontFiles.count(); ++j) { |
+ family->fFontFiles[j].fPaintOptions.setLanguage(locale); |
+ } |
+ *fallbackFonts.append() = family; |
+ } |
+ } |
+ |
+ // proceed to the next entry in the directory |
+ dirEntry = readdir(fontDirectory); |
+ } |
+ } |
+#endif |
+ |
// This loop inserts the vendor fallback fonts in the correct order in the |
// overall fallbacks list. |
int currentOrder = -1; |
@@ -317,33 +333,3 @@ void SkFontConfigParser::GetTestFontFamilies(SkTDArray<FontFamily*> &fontFamilie |
*fontFamilies.append() = fallbackFonts[i]; |
} |
} |
- |
-/** |
- * Read the persistent locale. |
- */ |
-SkString SkFontConfigParser::GetLocale() |
-{ |
- char propLang[PROP_VALUE_MAX], propRegn[PROP_VALUE_MAX]; |
- __system_property_get("persist.sys.language", propLang); |
- __system_property_get("persist.sys.country", propRegn); |
- |
- if (*propLang == 0 && *propRegn == 0) { |
- /* Set to ro properties, default is en_US */ |
- __system_property_get("ro.product.locale.language", propLang); |
- __system_property_get("ro.product.locale.region", propRegn); |
- if (*propLang == 0 && *propRegn == 0) { |
- strcpy(propLang, "en"); |
- strcpy(propRegn, "US"); |
- } |
- } |
- |
- SkString locale(6); |
- char* localeCStr = locale.writable_str(); |
- |
- strncpy(localeCStr, propLang, 2); |
- localeCStr[2] = '-'; |
- strncpy(&localeCStr[3], propRegn, 2); |
- localeCStr[5] = '\0'; |
- |
- return locale; |
-} |