Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/ports/FontHostConfiguration_android.cpp

Issue 18666003: Remove old and unused Android font host code (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ports/FontHostConfiguration_android.h ('k') | src/ports/SkFontHost_android.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
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 */
7
8 #include "FontHostConfiguration_android.h"
9 #include "SkString.h"
10 #include "SkTDArray.h"
11 #include <expat.h>
12 #include <sys/system_properties.h>
13
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 */
30 struct FamilyData {
31 FamilyData(XML_Parser *parserRef, SkTDArray<FontFamily*> &familiesRef, const AndroidLocale &localeRef) :
32 parser(parserRef), families(familiesRef), currentTag(NO_TAG),
33 locale(localeRef), currentFamilyLangMatch(false), familyLangMatchCou nt(0) {}
34
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 na meset/fileset tags
39 const AndroidLocale &locale; // The locale to which we compare the "la ng" attribute of File.
40 bool currentFamilyLangMatch; // If currentFamily's File has a "lang" a ttribute and matches locale.
41 int familyLangMatchCount; // Number of families containing File whi ch has a "lang" attribute and matches locale.
42 };
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 arra ys.
47 */
48 void 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 */
76 void 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 valu e >= 0
83 // If this attribute does not exist, the default value is -1
84 for (int i = 0; atts[i] != NULL; i += 2) {
85 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;
96 } else if (strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMES ET_TAG) {
97 XML_SetCharacterDataHandler(*familyData->parser, textHandler);
98 } else if (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILES ET_TAG) {
99 // From JB MR1, the File tag has a "lang" attribute to specify a languag e specific font file
100 // and the family entry has higher priority than the others without "lan g" 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 ma tching.
111 includeTheEntry = false;
112 }
113 }
114 }
115 if (includeTheEntry) {
116 XML_SetCharacterDataHandler(*familyData->parser, textHandler);
117 }
118 }
119 }
120
121 /**
122 * Handler for the end of tags. We only care about family, nameset, fileset,
123 * name, and file.
124 */
125 void 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 famili es array
130 if (familyData->currentFamilyLangMatch) {
131 *familyData->families.insert(familyData->familyLangMatchCount++) = f amilyData->currentFamily;
132 familyData->currentFamilyLangMatch = false;
133 } else {
134 *familyData->families.append() = familyData->currentFamily;
135 }
136 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 == NAME SET_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
148 /**
149 * Read the persistent locale.
150 */
151 void getLocale(AndroidLocale &locale)
152 {
153 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);
156
157 if (*propLang == 0 && *propRegn == 0) {
158 /* Set to ro properties, default is en_US */
159 __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 }
165 }
166 strncpy(locale.language, propLang, 2);
167 locale.language[2] = '\0';
168 strncpy(locale.region, propRegn, 2);
169 locale.region[2] = '\0';
170 }
171
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 */
179 FILE* openLocalizedFile(const char* origname, const AndroidLocale& locale) {
180 FILE* file = 0;
181 SkString basename;
182 SkString filename;
183
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 }
189 // Try first with language and region
190 filename.printf("%s-%s-%s.xml", basename.c_str(), locale.language, locale.re gion);
191 file = fopen(filename.c_str(), "r");
192 if (!file) {
193 // If not found, try next with just language
194 filename.printf("%s-%s.xml", basename.c_str(), locale.language);
195 file = fopen(filename.c_str(), "r");
196
197 if (!file) {
198 // If still not found, try just the original name
199 file = fopen(origname, "r");
200 }
201 }
202 return file;
203 }
204
205 /**
206 * This function parses the given filename and stores the results in the given
207 * families array.
208 */
209 void parseConfigFile(const char *filename, SkTDArray<FontFamily*> &families) {
210 AndroidLocale locale;
211 getLocale(locale);
212 XML_Parser parser = XML_ParserCreate(NULL);
213 FamilyData familyData(&parser, families, locale);
214 XML_SetUserData(parser, &familyData);
215 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
216 FILE *file = openLocalizedFile(filename, locale);
217 // Some of the files we attempt to parse (in particular, /vendor/etc/fallbac k_fonts.xml)
218 // are optional - failure here is okay because one of these optional files m ay 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 }
232 fclose(file);
233 XML_ParserFree(parser);
234 }
235
236 void getSystemFontFamilies(SkTDArray<FontFamily*> &fontFamilies) {
237 parseConfigFile(SYSTEM_FONTS_FILE, fontFamilies);
238 }
239
240 void getFallbackFontFamilies(SkTDArray<FontFamily*> &fallbackFonts) {
241 SkTDArray<FontFamily*> vendorFonts;
242 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 o rder
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 l ist.
263 *fallbackFonts.insert(order) = family;
264 currentOrder = order + 1;
265 }
266 }
267 }
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 */
273 void getFontFamilies(SkTDArray<FontFamily*> &fontFamilies) {
274 SkTDArray<FontFamily*> fallbackFonts;
275
276 getSystemFontFamilies(fontFamilies);
277 getFallbackFontFamilies(fallbackFonts);
278
279 // Append all fallback fonts to system fonts
280 for (int i = 0; i < fallbackFonts.count(); ++i) {
281 *fontFamilies.append() = fallbackFonts[i];
282 }
283 }
284
285 void 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 }
OLDNEW
« no previous file with comments | « src/ports/FontHostConfiguration_android.h ('k') | src/ports/SkFontHost_android.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698