blob: ef5a742a18ab90eb22108be000d3d2641f1fef36 [file] [log] [blame]
reed@google.comdd335ae2012-12-13 19:24:05 +00001/*
2 * Copyright 2011 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
djsollen@google.com58629292011-11-03 13:08:29 +00007
8#include "FontHostConfiguration_android.h"
djsollen@google.come63793a2012-03-21 15:39:03 +00009#include "SkString.h"
djsollen@google.com58629292011-11-03 13:08:29 +000010#include "SkTDArray.h"
djsollen@google.come63793a2012-03-21 15:39:03 +000011#include <expat.h>
djsollen@google.comfc9054d2012-05-10 16:13:38 +000012#include <sys/system_properties.h>
djsollen@google.com58629292011-11-03 13:08:29 +000013
14#define SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml"
15#define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml"
16#define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml"
17
18
19// These defines are used to determine the kind of tag that we're currently
20// populating with data. We only care about the sibling tags nameset and fileset
21// for now.
22#define NO_TAG 0
23#define NAMESET_TAG 1
24#define FILESET_TAG 2
25
26/**
27 * The FamilyData structure is passed around by the parser so that each handler
28 * can read these variables that are relevant to the current parsing.
29 */
30struct FamilyData {
djsollen@google.combd084732013-01-29 15:39:35 +000031 FamilyData(XML_Parser *parserRef, SkTDArray<FontFamily*> &familiesRef, const AndroidLocale &localeRef) :
32 parser(parserRef), families(familiesRef), currentTag(NO_TAG),
33 locale(localeRef), currentFamilyLangMatch(false), familyLangMatchCount(0) {}
djsollen@google.com58629292011-11-03 13:08:29 +000034
35 XML_Parser *parser; // The expat parser doing the work
36 SkTDArray<FontFamily*> &families; // The array that each family is put into as it is parsed
37 FontFamily *currentFamily; // The current family being created
38 int currentTag; // A flag to indicate whether we're in nameset/fileset tags
djsollen@google.combd084732013-01-29 15:39:35 +000039 const AndroidLocale &locale; // The locale to which we compare the "lang" attribute of File.
40 bool currentFamilyLangMatch; // If currentFamily's File has a "lang" attribute and matches locale.
41 int familyLangMatchCount; // Number of families containing File which has a "lang" attribute and matches locale.
djsollen@google.com58629292011-11-03 13:08:29 +000042};
43
44/**
45 * Handler for arbitrary text. This is used to parse the text inside each name
46 * or file tag. The resulting strings are put into the fNames or fFileNames arrays.
47 */
48void textHandler(void *data, const char *s, int len) {
49 FamilyData *familyData = (FamilyData*) data;
50 // Make sure we're in the right state to store this name information
51 if (familyData->currentFamily &&
52 (familyData->currentTag == NAMESET_TAG || familyData->currentTag == FILESET_TAG)) {
53 // Malloc new buffer to store the string
54 char *buff;
55 buff = (char*) malloc((len + 1) * sizeof(char));
56 strncpy(buff, s, len);
57 buff[len] = '\0';
58 switch (familyData->currentTag) {
59 case NAMESET_TAG:
60 *(familyData->currentFamily->fNames.append()) = buff;
61 break;
62 case FILESET_TAG:
63 *(familyData->currentFamily->fFileNames.append()) = buff;
64 break;
65 default:
66 // Noop - don't care about any text that's not in the Fonts or Names list
67 break;
68 }
69 }
70}
71
72/**
73 * Handler for the start of a tag. The only tags we expect are family, nameset,
74 * fileset, name, and file.
75 */
76void startElementHandler(void *data, const char *tag, const char **atts) {
77 FamilyData *familyData = (FamilyData*) data;
78 int len = strlen(tag);
79 if (strncmp(tag, "family", len)== 0) {
80 familyData->currentFamily = new FontFamily();
81 familyData->currentFamily->order = -1;
82 // The Family tag has an optional "order" attribute with an integer value >= 0
83 // If this attribute does not exist, the default value is -1
84 for (int i = 0; atts[i] != NULL; i += 2) {
djsollen@google.com58629292011-11-03 13:08:29 +000085 const char* valueString = atts[i+1];
86 int value;
87 int len = sscanf(valueString, "%d", &value);
88 if (len > 0) {
89 familyData->currentFamily->order = value;
90 }
91 }
92 } else if (len == 7 && strncmp(tag, "nameset", len)== 0) {
93 familyData->currentTag = NAMESET_TAG;
94 } else if (len == 7 && strncmp(tag, "fileset", len) == 0) {
95 familyData->currentTag = FILESET_TAG;
djsollen@google.combd084732013-01-29 15:39:35 +000096 } else if (strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) {
djsollen@google.com58629292011-11-03 13:08:29 +000097 XML_SetCharacterDataHandler(*familyData->parser, textHandler);
djsollen@google.combd084732013-01-29 15:39:35 +000098 } else if (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG) {
99 // From JB MR1, the File tag has a "lang" attribute to specify a language specific font file
100 // and the family entry has higher priority than the others without "lang" attribute.
101 bool includeTheEntry = true;
102 for (int i = 0; atts[i] != NULL; i += 2) {
103 const char* attribute = atts[i];
104 const char* value = atts[i+1];
105 if (strncmp(attribute, "lang", 4) == 0) {
106 if (strcmp(value, familyData->locale.language) == 0) {
107 // Found matching "lang" attribute. The current Family will have higher priority in the family list.
108 familyData->currentFamilyLangMatch = true;
109 } else {
110 // Don't include the entry if "lang" is specified but not matching.
111 includeTheEntry = false;
112 }
113 }
114 }
115 if (includeTheEntry) {
116 XML_SetCharacterDataHandler(*familyData->parser, textHandler);
117 }
djsollen@google.com58629292011-11-03 13:08:29 +0000118 }
119}
120
121/**
122 * Handler for the end of tags. We only care about family, nameset, fileset,
123 * name, and file.
124 */
125void endElementHandler(void *data, const char *tag) {
126 FamilyData *familyData = (FamilyData*) data;
127 int len = strlen(tag);
128 if (strncmp(tag, "family", len)== 0) {
129 // Done parsing a Family - store the created currentFamily in the families array
djsollen@google.combd084732013-01-29 15:39:35 +0000130 if (familyData->currentFamilyLangMatch) {
131 *familyData->families.insert(familyData->familyLangMatchCount++) = familyData->currentFamily;
132 familyData->currentFamilyLangMatch = false;
133 } else {
134 *familyData->families.append() = familyData->currentFamily;
135 }
djsollen@google.com58629292011-11-03 13:08:29 +0000136 familyData->currentFamily = NULL;
137 } else if (len == 7 && strncmp(tag, "nameset", len)== 0) {
138 familyData->currentTag = NO_TAG;
139 } else if (len == 7 && strncmp(tag, "fileset", len)== 0) {
140 familyData->currentTag = NO_TAG;
141 } else if ((strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) ||
142 (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG)) {
143 // Disable the arbitrary text handler installed to load Name data
144 XML_SetCharacterDataHandler(*familyData->parser, NULL);
145 }
146}
147
djsollen@google.come63793a2012-03-21 15:39:03 +0000148/**
149 * Read the persistent locale.
150 */
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000151void getLocale(AndroidLocale &locale)
djsollen@google.come63793a2012-03-21 15:39:03 +0000152{
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000153 char propLang[PROP_VALUE_MAX], propRegn[PROP_VALUE_MAX];
154 __system_property_get("persist.sys.language", propLang);
155 __system_property_get("persist.sys.country", propRegn);
djsollen@google.come63793a2012-03-21 15:39:03 +0000156
djsollen@google.come63793a2012-03-21 15:39:03 +0000157 if (*propLang == 0 && *propRegn == 0) {
158 /* Set to ro properties, default is en_US */
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000159 __system_property_get("ro.product.locale.language", propLang);
160 __system_property_get("ro.product.locale.region", propRegn);
161 if (*propLang == 0 && *propRegn == 0) {
162 strcpy(propLang, "en");
163 strcpy(propRegn, "US");
164 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000165 }
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000166 strncpy(locale.language, propLang, 2);
167 locale.language[2] = '\0';
168 strncpy(locale.region, propRegn, 2);
169 locale.region[2] = '\0';
djsollen@google.come63793a2012-03-21 15:39:03 +0000170}
djsollen@google.come63793a2012-03-21 15:39:03 +0000171
172/**
173 * Use the current system locale (language and region) to open the best matching
174 * customization. For example, when the language is Japanese, the sequence might be:
175 * /system/etc/fallback_fonts-ja-JP.xml
176 * /system/etc/fallback_fonts-ja.xml
177 * /system/etc/fallback_fonts.xml
178 */
djsollen@google.combd084732013-01-29 15:39:35 +0000179FILE* openLocalizedFile(const char* origname, const AndroidLocale& locale) {
djsollen@google.come63793a2012-03-21 15:39:03 +0000180 FILE* file = 0;
djsollen@google.come63793a2012-03-21 15:39:03 +0000181 SkString basename;
182 SkString filename;
djsollen@google.come63793a2012-03-21 15:39:03 +0000183
184 basename.set(origname);
185 // Remove the .xml suffix. We'll add it back in a moment.
186 if (basename.endsWith(".xml")) {
187 basename.resize(basename.size()-4);
188 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000189 // Try first with language and region
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000190 filename.printf("%s-%s-%s.xml", basename.c_str(), locale.language, locale.region);
djsollen@google.come63793a2012-03-21 15:39:03 +0000191 file = fopen(filename.c_str(), "r");
192 if (!file) {
193 // If not found, try next with just language
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000194 filename.printf("%s-%s.xml", basename.c_str(), locale.language);
djsollen@google.come63793a2012-03-21 15:39:03 +0000195 file = fopen(filename.c_str(), "r");
djsollen@google.come63793a2012-03-21 15:39:03 +0000196
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000197 if (!file) {
198 // If still not found, try just the original name
199 file = fopen(origname, "r");
200 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000201 }
202 return file;
203}
204
djsollen@google.com58629292011-11-03 13:08:29 +0000205/**
206 * This function parses the given filename and stores the results in the given
207 * families array.
208 */
209void parseConfigFile(const char *filename, SkTDArray<FontFamily*> &families) {
djsollen@google.combd084732013-01-29 15:39:35 +0000210 AndroidLocale locale;
211 getLocale(locale);
djsollen@google.com58629292011-11-03 13:08:29 +0000212 XML_Parser parser = XML_ParserCreate(NULL);
djsollen@google.combd084732013-01-29 15:39:35 +0000213 FamilyData familyData(&parser, families, locale);
214 XML_SetUserData(parser, &familyData);
djsollen@google.com58629292011-11-03 13:08:29 +0000215 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
djsollen@google.combd084732013-01-29 15:39:35 +0000216 FILE *file = openLocalizedFile(filename, locale);
djsollen@google.com58629292011-11-03 13:08:29 +0000217 // Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml)
218 // are optional - failure here is okay because one of these optional files may not exist.
219 if (file == NULL) {
220 return;
221 }
222 char buffer[512];
223 bool done = false;
224 while (!done) {
225 fgets(buffer, sizeof(buffer), file);
226 int len = strlen(buffer);
227 if (feof(file) != 0) {
228 done = true;
229 }
230 XML_Parse(parser, buffer, len, done);
231 }
djsollen@google.combd084732013-01-29 15:39:35 +0000232 fclose(file);
233 XML_ParserFree(parser);
djsollen@google.com58629292011-11-03 13:08:29 +0000234}
235
djsollen@google.come63793a2012-03-21 15:39:03 +0000236void getSystemFontFamilies(SkTDArray<FontFamily*> &fontFamilies) {
djsollen@google.com58629292011-11-03 13:08:29 +0000237 parseConfigFile(SYSTEM_FONTS_FILE, fontFamilies);
djsollen@google.come63793a2012-03-21 15:39:03 +0000238}
239
240void getFallbackFontFamilies(SkTDArray<FontFamily*> &fallbackFonts) {
241 SkTDArray<FontFamily*> vendorFonts;
djsollen@google.com58629292011-11-03 13:08:29 +0000242 parseConfigFile(FALLBACK_FONTS_FILE, fallbackFonts);
243 parseConfigFile(VENDOR_FONTS_FILE, vendorFonts);
244
245 // This loop inserts the vendor fallback fonts in the correct order in the
246 // overall fallbacks list.
247 int currentOrder = -1;
248 for (int i = 0; i < vendorFonts.count(); ++i) {
249 FontFamily* family = vendorFonts[i];
250 int order = family->order;
251 if (order < 0) {
252 if (currentOrder < 0) {
253 // Default case - just add it to the end of the fallback list
254 *fallbackFonts.append() = family;
255 } else {
256 // no order specified on this font, but we're incrementing the order
257 // based on an earlier order insertion request
258 *fallbackFonts.insert(currentOrder++) = family;
259 }
260 } else {
261 // Add the font into the fallback list in the specified order. Set
262 // currentOrder for correct placement of other fonts in the vendor list.
263 *fallbackFonts.insert(order) = family;
264 currentOrder = order + 1;
265 }
266 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000267}
268
269/**
270 * Loads data on font families from various expected configuration files. The
271 * resulting data is returned in the given fontFamilies array.
272 */
273void getFontFamilies(SkTDArray<FontFamily*> &fontFamilies) {
274 SkTDArray<FontFamily*> fallbackFonts;
275
276 getSystemFontFamilies(fontFamilies);
277 getFallbackFontFamilies(fallbackFonts);
278
djsollen@google.com58629292011-11-03 13:08:29 +0000279 // Append all fallback fonts to system fonts
280 for (int i = 0; i < fallbackFonts.count(); ++i) {
281 *fontFamilies.append() = fallbackFonts[i];
282 }
283}
djsollen@google.com5df2a992012-06-25 13:58:22 +0000284
285void getTestFontFamilies(SkTDArray<FontFamily*> &fontFamilies,
286 const char* testMainConfigFile,
287 const char* testFallbackConfigFile) {
288 parseConfigFile(testMainConfigFile, fontFamilies);
289
290 SkTDArray<FontFamily*> fallbackFonts;
291 parseConfigFile(testFallbackConfigFile, fallbackFonts);
292
293 // Append all fallback fonts to system fonts
294 for (int i = 0; i < fallbackFonts.count(); ++i) {
295 *fontFamilies.append() = fallbackFonts[i];
296 }
297}