blob: c12bb401148c5d55814b2bb1364ae024c02781b1 [file] [log] [blame]
djsollen@google.com58629292011-11-03 13:08:29 +00001/* libs/graphics/ports/FontHostConfiguration_android.cpp
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "FontHostConfiguration_android.h"
djsollen@google.come63793a2012-03-21 15:39:03 +000019#include "SkString.h"
djsollen@google.com58629292011-11-03 13:08:29 +000020#include "SkTDArray.h"
djsollen@google.come63793a2012-03-21 15:39:03 +000021#include <expat.h>
djsollen@google.comfc9054d2012-05-10 16:13:38 +000022#include <sys/system_properties.h>
djsollen@google.com58629292011-11-03 13:08:29 +000023
24#define SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml"
25#define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml"
26#define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml"
27
28
29// These defines are used to determine the kind of tag that we're currently
30// populating with data. We only care about the sibling tags nameset and fileset
31// for now.
32#define NO_TAG 0
33#define NAMESET_TAG 1
34#define FILESET_TAG 2
35
36/**
37 * The FamilyData structure is passed around by the parser so that each handler
38 * can read these variables that are relevant to the current parsing.
39 */
40struct FamilyData {
41 FamilyData(XML_Parser *parserRef, SkTDArray<FontFamily*> &familiesRef) :
42 parser(parserRef), families(familiesRef), currentTag(NO_TAG) {};
43
44 XML_Parser *parser; // The expat parser doing the work
45 SkTDArray<FontFamily*> &families; // The array that each family is put into as it is parsed
46 FontFamily *currentFamily; // The current family being created
47 int currentTag; // A flag to indicate whether we're in nameset/fileset tags
48};
49
50/**
51 * Handler for arbitrary text. This is used to parse the text inside each name
52 * or file tag. The resulting strings are put into the fNames or fFileNames arrays.
53 */
54void textHandler(void *data, const char *s, int len) {
55 FamilyData *familyData = (FamilyData*) data;
56 // Make sure we're in the right state to store this name information
57 if (familyData->currentFamily &&
58 (familyData->currentTag == NAMESET_TAG || familyData->currentTag == FILESET_TAG)) {
59 // Malloc new buffer to store the string
60 char *buff;
61 buff = (char*) malloc((len + 1) * sizeof(char));
62 strncpy(buff, s, len);
63 buff[len] = '\0';
64 switch (familyData->currentTag) {
65 case NAMESET_TAG:
66 *(familyData->currentFamily->fNames.append()) = buff;
67 break;
68 case FILESET_TAG:
69 *(familyData->currentFamily->fFileNames.append()) = buff;
70 break;
71 default:
72 // Noop - don't care about any text that's not in the Fonts or Names list
73 break;
74 }
75 }
76}
77
78/**
79 * Handler for the start of a tag. The only tags we expect are family, nameset,
80 * fileset, name, and file.
81 */
82void startElementHandler(void *data, const char *tag, const char **atts) {
83 FamilyData *familyData = (FamilyData*) data;
84 int len = strlen(tag);
85 if (strncmp(tag, "family", len)== 0) {
86 familyData->currentFamily = new FontFamily();
87 familyData->currentFamily->order = -1;
88 // The Family tag has an optional "order" attribute with an integer value >= 0
89 // If this attribute does not exist, the default value is -1
90 for (int i = 0; atts[i] != NULL; i += 2) {
91 const char* attribute = atts[i];
92 const char* valueString = atts[i+1];
93 int value;
94 int len = sscanf(valueString, "%d", &value);
95 if (len > 0) {
96 familyData->currentFamily->order = value;
97 }
98 }
99 } else if (len == 7 && strncmp(tag, "nameset", len)== 0) {
100 familyData->currentTag = NAMESET_TAG;
101 } else if (len == 7 && strncmp(tag, "fileset", len) == 0) {
102 familyData->currentTag = FILESET_TAG;
103 } else if ((strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) ||
104 (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG)) {
105 // If it's a Name, parse the text inside
106 XML_SetCharacterDataHandler(*familyData->parser, textHandler);
107 }
108}
109
110/**
111 * Handler for the end of tags. We only care about family, nameset, fileset,
112 * name, and file.
113 */
114void endElementHandler(void *data, const char *tag) {
115 FamilyData *familyData = (FamilyData*) data;
116 int len = strlen(tag);
117 if (strncmp(tag, "family", len)== 0) {
118 // Done parsing a Family - store the created currentFamily in the families array
119 *familyData->families.append() = familyData->currentFamily;
120 familyData->currentFamily = NULL;
121 } else if (len == 7 && strncmp(tag, "nameset", len)== 0) {
122 familyData->currentTag = NO_TAG;
123 } else if (len == 7 && strncmp(tag, "fileset", len)== 0) {
124 familyData->currentTag = NO_TAG;
125 } else if ((strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) ||
126 (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG)) {
127 // Disable the arbitrary text handler installed to load Name data
128 XML_SetCharacterDataHandler(*familyData->parser, NULL);
129 }
130}
131
djsollen@google.come63793a2012-03-21 15:39:03 +0000132/**
133 * Read the persistent locale.
134 */
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000135void getLocale(AndroidLocale &locale)
djsollen@google.come63793a2012-03-21 15:39:03 +0000136{
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000137 char propLang[PROP_VALUE_MAX], propRegn[PROP_VALUE_MAX];
138 __system_property_get("persist.sys.language", propLang);
139 __system_property_get("persist.sys.country", propRegn);
djsollen@google.come63793a2012-03-21 15:39:03 +0000140
djsollen@google.come63793a2012-03-21 15:39:03 +0000141 if (*propLang == 0 && *propRegn == 0) {
142 /* Set to ro properties, default is en_US */
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000143 __system_property_get("ro.product.locale.language", propLang);
144 __system_property_get("ro.product.locale.region", propRegn);
145 if (*propLang == 0 && *propRegn == 0) {
146 strcpy(propLang, "en");
147 strcpy(propRegn, "US");
148 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000149 }
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000150 strncpy(locale.language, propLang, 2);
151 locale.language[2] = '\0';
152 strncpy(locale.region, propRegn, 2);
153 locale.region[2] = '\0';
djsollen@google.come63793a2012-03-21 15:39:03 +0000154}
djsollen@google.come63793a2012-03-21 15:39:03 +0000155
156/**
157 * Use the current system locale (language and region) to open the best matching
158 * customization. For example, when the language is Japanese, the sequence might be:
159 * /system/etc/fallback_fonts-ja-JP.xml
160 * /system/etc/fallback_fonts-ja.xml
161 * /system/etc/fallback_fonts.xml
162 */
163FILE* openLocalizedFile(const char* origname) {
164 FILE* file = 0;
djsollen@google.come63793a2012-03-21 15:39:03 +0000165 SkString basename;
166 SkString filename;
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000167 AndroidLocale locale;
djsollen@google.come63793a2012-03-21 15:39:03 +0000168
169 basename.set(origname);
170 // Remove the .xml suffix. We'll add it back in a moment.
171 if (basename.endsWith(".xml")) {
172 basename.resize(basename.size()-4);
173 }
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000174 getLocale(locale);
djsollen@google.come63793a2012-03-21 15:39:03 +0000175 // Try first with language and region
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000176 filename.printf("%s-%s-%s.xml", basename.c_str(), locale.language, locale.region);
djsollen@google.come63793a2012-03-21 15:39:03 +0000177 file = fopen(filename.c_str(), "r");
178 if (!file) {
179 // If not found, try next with just language
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000180 filename.printf("%s-%s.xml", basename.c_str(), locale.language);
djsollen@google.come63793a2012-03-21 15:39:03 +0000181 file = fopen(filename.c_str(), "r");
djsollen@google.come63793a2012-03-21 15:39:03 +0000182
djsollen@google.comfc9054d2012-05-10 16:13:38 +0000183 if (!file) {
184 // If still not found, try just the original name
185 file = fopen(origname, "r");
186 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000187 }
188 return file;
189}
190
djsollen@google.com58629292011-11-03 13:08:29 +0000191/**
192 * This function parses the given filename and stores the results in the given
193 * families array.
194 */
195void parseConfigFile(const char *filename, SkTDArray<FontFamily*> &families) {
196 XML_Parser parser = XML_ParserCreate(NULL);
197 FamilyData *familyData = new FamilyData(&parser, families);
198 XML_SetUserData(parser, familyData);
199 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
djsollen@google.come63793a2012-03-21 15:39:03 +0000200 FILE *file = openLocalizedFile(filename);
djsollen@google.com58629292011-11-03 13:08:29 +0000201 // Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml)
202 // are optional - failure here is okay because one of these optional files may not exist.
203 if (file == NULL) {
204 return;
205 }
206 char buffer[512];
207 bool done = false;
208 while (!done) {
209 fgets(buffer, sizeof(buffer), file);
210 int len = strlen(buffer);
211 if (feof(file) != 0) {
212 done = true;
213 }
214 XML_Parse(parser, buffer, len, done);
215 }
216}
217
djsollen@google.come63793a2012-03-21 15:39:03 +0000218void getSystemFontFamilies(SkTDArray<FontFamily*> &fontFamilies) {
djsollen@google.com58629292011-11-03 13:08:29 +0000219 parseConfigFile(SYSTEM_FONTS_FILE, fontFamilies);
djsollen@google.come63793a2012-03-21 15:39:03 +0000220}
221
222void getFallbackFontFamilies(SkTDArray<FontFamily*> &fallbackFonts) {
223 SkTDArray<FontFamily*> vendorFonts;
djsollen@google.com58629292011-11-03 13:08:29 +0000224 parseConfigFile(FALLBACK_FONTS_FILE, fallbackFonts);
225 parseConfigFile(VENDOR_FONTS_FILE, vendorFonts);
226
227 // This loop inserts the vendor fallback fonts in the correct order in the
228 // overall fallbacks list.
229 int currentOrder = -1;
230 for (int i = 0; i < vendorFonts.count(); ++i) {
231 FontFamily* family = vendorFonts[i];
232 int order = family->order;
233 if (order < 0) {
234 if (currentOrder < 0) {
235 // Default case - just add it to the end of the fallback list
236 *fallbackFonts.append() = family;
237 } else {
238 // no order specified on this font, but we're incrementing the order
239 // based on an earlier order insertion request
240 *fallbackFonts.insert(currentOrder++) = family;
241 }
242 } else {
243 // Add the font into the fallback list in the specified order. Set
244 // currentOrder for correct placement of other fonts in the vendor list.
245 *fallbackFonts.insert(order) = family;
246 currentOrder = order + 1;
247 }
248 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000249}
250
251/**
252 * Loads data on font families from various expected configuration files. The
253 * resulting data is returned in the given fontFamilies array.
254 */
255void getFontFamilies(SkTDArray<FontFamily*> &fontFamilies) {
256 SkTDArray<FontFamily*> fallbackFonts;
257
258 getSystemFontFamilies(fontFamilies);
259 getFallbackFontFamilies(fallbackFonts);
260
djsollen@google.com58629292011-11-03 13:08:29 +0000261 // Append all fallback fonts to system fonts
262 for (int i = 0; i < fallbackFonts.count(); ++i) {
263 *fontFamilies.append() = fallbackFonts[i];
264 }
265}