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

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

Issue 427293003: Implement SkFontMgr_Android::onMatchFamilyStyleCharacter. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address comments, move mask to parser. Created 6 years, 4 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
OLDNEW
1 /* 1 /*
2 * Copyright 2011 The Android Open Source Project 2 * Copyright 2011 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkFontConfigParser_android.h" 8 #include "SkFontConfigParser_android.h"
9 #include "SkTDArray.h" 9 #include "SkTDArray.h"
10 #include "SkTSearch.h" 10 #include "SkTSearch.h"
(...skipping 14 matching lines...) Expand all
25 // for now. 25 // for now.
26 #define NO_TAG 0 26 #define NO_TAG 0
27 #define NAMESET_TAG 1 27 #define NAMESET_TAG 1
28 #define FILESET_TAG 2 28 #define FILESET_TAG 2
29 29
30 /** 30 /**
31 * The FamilyData structure is passed around by the parser so that each handler 31 * The FamilyData structure is passed around by the parser so that each handler
32 * can read these variables that are relevant to the current parsing. 32 * can read these variables that are relevant to the current parsing.
33 */ 33 */
34 struct FamilyData { 34 struct FamilyData {
35 FamilyData(XML_Parser *parserRef, SkTDArray<FontFamily*> &familiesRef) : 35 FamilyData(XML_Parser *parserRef,
36 parser(parserRef), 36 SkTDArray<FontFamily*> &familiesRef,
37 families(familiesRef), 37 uint32_t ignoredVariants)
38 currentFamily(NULL), 38 : parser(parserRef)
39 currentFontInfo(NULL), 39 , families(familiesRef)
40 currentTag(NO_TAG) {}; 40 , fIgnoredVariants(ignoredVariants)
41 , currentFamily(NULL)
42 , currentFontInfo(NULL)
43 , currentTag(NO_TAG) {};
41 44
42 XML_Parser *parser; // The expat parser doing the work 45 XML_Parser *parser; // The expat parser doing the work
43 SkTDArray<FontFamily*> &families; // The array that each family is put into as it is parsed 46 SkTDArray<FontFamily*> &families; // The array that each family is put into as it is parsed
47 uint32_t fIgnoredVariants; // FontVariants which will be ignored.
44 FontFamily *currentFamily; // The current family being created 48 FontFamily *currentFamily; // The current family being created
45 FontFileInfo *currentFontInfo; // The current fontInfo being created 49 FontFileInfo *currentFontInfo; // The current fontInfo being created
46 int currentTag; // A flag to indicate whether we're in na meset/fileset tags 50 int currentTag; // A flag to indicate whether we're in na meset/fileset tags
47 }; 51 };
48 52
49 /** 53 /**
50 * Handler for arbitrary text. This is used to parse the text inside each name 54 * Handler for arbitrary text. This is used to parse the text inside each name
51 * or file tag. The resulting strings are put into the fNames or FontFileInfo ar rays. 55 * or file tag. The resulting strings are put into the fNames or FontFileInfo ar rays.
52 */ 56 */
53 static void textHandler(void *data, const char *s, int len) { 57 static void textHandler(void *data, const char *s, int len) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 if (parseNonNegativeInteger(attributeValue, &value)) { 126 if (parseNonNegativeInteger(attributeValue, &value)) {
123 newFileInfo.fIndex = value; 127 newFileInfo.fIndex = value;
124 } else { 128 } else {
125 SkDebugf("---- SystemFonts index=%s (INVALID)", attributeVal ue); 129 SkDebugf("---- SystemFonts index=%s (INVALID)", attributeVal ue);
126 } 130 }
127 } 131 }
128 //each element is a pair of attributeName/attributeValue string pair s 132 //each element is a pair of attributeName/attributeValue string pair s
129 currentAttributeIndex += 2; 133 currentAttributeIndex += 2;
130 } 134 }
131 } 135 }
132 familyData->currentFontInfo = &newFileInfo; 136
137 if (SkToBool(newFileInfo.fPaintOptions.getFontVariant() & familyData->fIgnor edVariants)) {
138 familyData->currentFamily->fFontFiles.pop_back();
139 familyData->currentFontInfo = NULL;
140 } else {
141 familyData->currentFontInfo = &newFileInfo;
142 }
133 XML_SetCharacterDataHandler(*familyData->parser, textHandler); 143 XML_SetCharacterDataHandler(*familyData->parser, textHandler);
134 } 144 }
135 145
136 /** 146 /**
137 * Handler for the start of a tag. The only tags we expect are family, nameset, 147 * Handler for the start of a tag. The only tags we expect are family, nameset,
138 * fileset, name, and file. 148 * fileset, name, and file.
139 */ 149 */
140 static void startElementHandler(void *data, const char *tag, const char **atts) { 150 static void startElementHandler(void *data, const char *tag, const char **atts) {
141 FamilyData *familyData = (FamilyData*) data; 151 FamilyData *familyData = (FamilyData*) data;
142 int len = strlen(tag); 152 int len = strlen(tag);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET _TAG)) { 195 (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET _TAG)) {
186 // Disable the arbitrary text handler installed to load Name data 196 // Disable the arbitrary text handler installed to load Name data
187 XML_SetCharacterDataHandler(*familyData->parser, NULL); 197 XML_SetCharacterDataHandler(*familyData->parser, NULL);
188 } 198 }
189 } 199 }
190 200
191 /** 201 /**
192 * This function parses the given filename and stores the results in the given 202 * This function parses the given filename and stores the results in the given
193 * families array. 203 * families array.
194 */ 204 */
195 static void parseConfigFile(const char *filename, SkTDArray<FontFamily*> &famili es) { 205 static void parseConfigFile(const char *filename,
206 SkTDArray<FontFamily*> &families,
207 uint32_t ignoredVariants)
djsollen 2014/07/31 14:51:37 ignoredVariants = 0;
bungeman-skia 2014/07/31 22:15:53 Actually, I'm trying to follow the "as few default
208 {
196 209
197 FILE* file = NULL; 210 FILE* file = NULL;
198 211
199 #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) 212 #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
200 // if we are using a version of Android prior to Android 4.2 (JellyBean MR1 213 // if we are using a version of Android prior to Android 4.2 (JellyBean MR1
201 // at API Level 17) then we need to look for files with a different suffix. 214 // at API Level 17) then we need to look for files with a different suffix.
202 char sdkVersion[PROP_VALUE_MAX]; 215 char sdkVersion[PROP_VALUE_MAX];
203 __system_property_get("ro.build.version.sdk", sdkVersion); 216 __system_property_get("ro.build.version.sdk", sdkVersion);
204 const int sdkVersionInt = atoi(sdkVersion); 217 const int sdkVersionInt = atoi(sdkVersion);
205 218
(...skipping 22 matching lines...) Expand all
228 file = fopen(filename, "r"); 241 file = fopen(filename, "r");
229 } 242 }
230 243
231 // Some of the files we attempt to parse (in particular, /vendor/etc/fallbac k_fonts.xml) 244 // Some of the files we attempt to parse (in particular, /vendor/etc/fallbac k_fonts.xml)
232 // are optional - failure here is okay because one of these optional files m ay not exist. 245 // are optional - failure here is okay because one of these optional files m ay not exist.
233 if (NULL == file) { 246 if (NULL == file) {
234 return; 247 return;
235 } 248 }
236 249
237 XML_Parser parser = XML_ParserCreate(NULL); 250 XML_Parser parser = XML_ParserCreate(NULL);
238 FamilyData *familyData = new FamilyData(&parser, families); 251 FamilyData *familyData = new FamilyData(&parser, families, ignoredVariants);
239 XML_SetUserData(parser, familyData); 252 XML_SetUserData(parser, familyData);
240 XML_SetElementHandler(parser, startElementHandler, endElementHandler); 253 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
241 254
242 char buffer[512]; 255 char buffer[512];
243 bool done = false; 256 bool done = false;
244 while (!done) { 257 while (!done) {
245 fgets(buffer, sizeof(buffer), file); 258 fgets(buffer, sizeof(buffer), file);
246 int len = strlen(buffer); 259 int len = strlen(buffer);
247 if (feof(file) != 0) { 260 if (feof(file) != 0) {
248 done = true; 261 done = true;
249 } 262 }
250 XML_Parse(parser, buffer, len, done); 263 XML_Parse(parser, buffer, len, done);
251 } 264 }
252 XML_ParserFree(parser); 265 XML_ParserFree(parser);
253 fclose(file); 266 fclose(file);
254 } 267 }
255 268
256 static void getSystemFontFamilies(SkTDArray<FontFamily*> &fontFamilies) { 269 static void getSystemFontFamilies(SkTDArray<FontFamily*> &fontFamilies) {
257 parseConfigFile(SYSTEM_FONTS_FILE, fontFamilies); 270 parseConfigFile(SYSTEM_FONTS_FILE, fontFamilies, 0);
258 } 271 }
259 272
260 static void getFallbackFontFamilies(SkTDArray<FontFamily*> &fallbackFonts) { 273 static void getFallbackFontFamilies(SkTDArray<FontFamily*> &fallbackFonts,
274 uint32_t ignoredVariants)
275 {
261 SkTDArray<FontFamily*> vendorFonts; 276 SkTDArray<FontFamily*> vendorFonts;
262 parseConfigFile(FALLBACK_FONTS_FILE, fallbackFonts); 277 parseConfigFile(FALLBACK_FONTS_FILE, fallbackFonts, ignoredVariants);
263 parseConfigFile(VENDOR_FONTS_FILE, vendorFonts); 278 parseConfigFile(VENDOR_FONTS_FILE, vendorFonts, ignoredVariants);
264 279
265 // This loop inserts the vendor fallback fonts in the correct order in the 280 // This loop inserts the vendor fallback fonts in the correct order in the
266 // overall fallbacks list. 281 // overall fallbacks list.
267 int currentOrder = -1; 282 int currentOrder = -1;
268 for (int i = 0; i < vendorFonts.count(); ++i) { 283 for (int i = 0; i < vendorFonts.count(); ++i) {
269 FontFamily* family = vendorFonts[i]; 284 FontFamily* family = vendorFonts[i];
270 int order = family->order; 285 int order = family->order;
271 if (order < 0) { 286 if (order < 0) {
272 if (currentOrder < 0) { 287 if (currentOrder < 0) {
273 // Default case - just add it to the end of the fallback list 288 // Default case - just add it to the end of the fallback list
274 *fallbackFonts.append() = family; 289 *fallbackFonts.append() = family;
275 } else { 290 } else {
276 // no order specified on this font, but we're incrementing the o rder 291 // no order specified on this font, but we're incrementing the o rder
277 // based on an earlier order insertion request 292 // based on an earlier order insertion request
278 *fallbackFonts.insert(currentOrder++) = family; 293 *fallbackFonts.insert(currentOrder++) = family;
279 } 294 }
280 } else { 295 } else {
281 // Add the font into the fallback list in the specified order. Set 296 // Add the font into the fallback list in the specified order. Set
282 // currentOrder for correct placement of other fonts in the vendor l ist. 297 // currentOrder for correct placement of other fonts in the vendor l ist.
283 *fallbackFonts.insert(order) = family; 298 *fallbackFonts.insert(order) = family;
284 currentOrder = order + 1; 299 currentOrder = order + 1;
285 } 300 }
286 } 301 }
287 } 302 }
288 303
289 /** 304 /**
290 * Loads data on font families from various expected configuration files. The 305 * Loads data on font families from various expected configuration files. The
291 * resulting data is returned in the given fontFamilies array. 306 * resulting data is returned in the given fontFamilies array.
292 */ 307 */
293 void SkFontConfigParser::GetFontFamilies(SkTDArray<FontFamily*> &fontFamilies) { 308 void SkFontConfigParser::GetFontFamilies(SkTDArray<FontFamily*> &fontFamilies,
294 309 uint32_t ignoredFallbackVariants)
310 {
295 getSystemFontFamilies(fontFamilies); 311 getSystemFontFamilies(fontFamilies);
296 312
297 // Append all the fallback fonts to system fonts 313 // Append all the fallback fonts to system fonts
298 SkTDArray<FontFamily*> fallbackFonts; 314 SkTDArray<FontFamily*> fallbackFonts;
299 getFallbackFontFamilies(fallbackFonts); 315 getFallbackFontFamilies(fallbackFonts, ignoredFallbackVariants);
300 for (int i = 0; i < fallbackFonts.count(); ++i) { 316 for (int i = 0; i < fallbackFonts.count(); ++i) {
301 fallbackFonts[i]->fIsFallbackFont = true; 317 fallbackFonts[i]->fIsFallbackFont = true;
302 *fontFamilies.append() = fallbackFonts[i]; 318 *fontFamilies.append() = fallbackFonts[i];
303 } 319 }
304 } 320 }
305 321
306 void SkFontConfigParser::GetTestFontFamilies(SkTDArray<FontFamily*> &fontFamilie s, 322 void SkFontConfigParser::GetTestFontFamilies(SkTDArray<FontFamily*> &fontFamilie s,
307 const char* testMainConfigFile, 323 const char* testMainConfigFile,
308 const char* testFallbackConfigFile) { 324 const char* testFallbackConfigFile) {
309 parseConfigFile(testMainConfigFile, fontFamilies); 325 parseConfigFile(testMainConfigFile, fontFamilies, 0);
310 326
311 SkTDArray<FontFamily*> fallbackFonts; 327 SkTDArray<FontFamily*> fallbackFonts;
312 parseConfigFile(testFallbackConfigFile, fallbackFonts); 328 parseConfigFile(testFallbackConfigFile, fallbackFonts, 0);
djsollen 2014/07/31 14:51:37 will this break Chromium?
bungeman-skia 2014/07/31 22:15:53 Yes, I need to add the param here as well and fix
313 329
314 // Append all fallback fonts to system fonts 330 // Append all fallback fonts to system fonts
315 for (int i = 0; i < fallbackFonts.count(); ++i) { 331 for (int i = 0; i < fallbackFonts.count(); ++i) {
316 fallbackFonts[i]->fIsFallbackFont = true; 332 fallbackFonts[i]->fIsFallbackFont = true;
317 *fontFamilies.append() = fallbackFonts[i]; 333 *fontFamilies.append() = fallbackFonts[i];
318 } 334 }
319 } 335 }
320 336
321 /** 337 /**
322 * Read the persistent locale. 338 * Read the persistent locale.
(...skipping 17 matching lines...) Expand all
340 SkString locale(6); 356 SkString locale(6);
341 char* localeCStr = locale.writable_str(); 357 char* localeCStr = locale.writable_str();
342 358
343 strncpy(localeCStr, propLang, 2); 359 strncpy(localeCStr, propLang, 2);
344 localeCStr[2] = '-'; 360 localeCStr[2] = '-';
345 strncpy(&localeCStr[3], propRegn, 2); 361 strncpy(&localeCStr[3], propRegn, 2);
346 localeCStr[5] = '\0'; 362 localeCStr[5] = '\0';
347 363
348 return locale; 364 return locale;
349 } 365 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698