djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame^] | 1 | /* 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" |
| 19 | #include <expat.h> |
| 20 | #include "SkTDArray.h" |
| 21 | |
| 22 | #define SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" |
| 23 | #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" |
| 24 | #define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml" |
| 25 | |
| 26 | |
| 27 | // These defines are used to determine the kind of tag that we're currently |
| 28 | // populating with data. We only care about the sibling tags nameset and fileset |
| 29 | // for now. |
| 30 | #define NO_TAG 0 |
| 31 | #define NAMESET_TAG 1 |
| 32 | #define FILESET_TAG 2 |
| 33 | |
| 34 | /** |
| 35 | * The FamilyData structure is passed around by the parser so that each handler |
| 36 | * can read these variables that are relevant to the current parsing. |
| 37 | */ |
| 38 | struct FamilyData { |
| 39 | FamilyData(XML_Parser *parserRef, SkTDArray<FontFamily*> &familiesRef) : |
| 40 | parser(parserRef), families(familiesRef), currentTag(NO_TAG) {}; |
| 41 | |
| 42 | 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 |
| 44 | FontFamily *currentFamily; // The current family being created |
| 45 | int currentTag; // A flag to indicate whether we're in nameset/fileset tags |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Handler for arbitrary text. This is used to parse the text inside each name |
| 50 | * or file tag. The resulting strings are put into the fNames or fFileNames arrays. |
| 51 | */ |
| 52 | void textHandler(void *data, const char *s, int len) { |
| 53 | FamilyData *familyData = (FamilyData*) data; |
| 54 | // Make sure we're in the right state to store this name information |
| 55 | if (familyData->currentFamily && |
| 56 | (familyData->currentTag == NAMESET_TAG || familyData->currentTag == FILESET_TAG)) { |
| 57 | // Malloc new buffer to store the string |
| 58 | char *buff; |
| 59 | buff = (char*) malloc((len + 1) * sizeof(char)); |
| 60 | strncpy(buff, s, len); |
| 61 | buff[len] = '\0'; |
| 62 | switch (familyData->currentTag) { |
| 63 | case NAMESET_TAG: |
| 64 | *(familyData->currentFamily->fNames.append()) = buff; |
| 65 | break; |
| 66 | case FILESET_TAG: |
| 67 | *(familyData->currentFamily->fFileNames.append()) = buff; |
| 68 | break; |
| 69 | default: |
| 70 | // Noop - don't care about any text that's not in the Fonts or Names list |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Handler for the start of a tag. The only tags we expect are family, nameset, |
| 78 | * fileset, name, and file. |
| 79 | */ |
| 80 | void startElementHandler(void *data, const char *tag, const char **atts) { |
| 81 | FamilyData *familyData = (FamilyData*) data; |
| 82 | int len = strlen(tag); |
| 83 | if (strncmp(tag, "family", len)== 0) { |
| 84 | familyData->currentFamily = new FontFamily(); |
| 85 | familyData->currentFamily->order = -1; |
| 86 | // The Family tag has an optional "order" attribute with an integer value >= 0 |
| 87 | // If this attribute does not exist, the default value is -1 |
| 88 | for (int i = 0; atts[i] != NULL; i += 2) { |
| 89 | const char* attribute = atts[i]; |
| 90 | const char* valueString = atts[i+1]; |
| 91 | int value; |
| 92 | int len = sscanf(valueString, "%d", &value); |
| 93 | if (len > 0) { |
| 94 | familyData->currentFamily->order = value; |
| 95 | } |
| 96 | } |
| 97 | } else if (len == 7 && strncmp(tag, "nameset", len)== 0) { |
| 98 | familyData->currentTag = NAMESET_TAG; |
| 99 | } else if (len == 7 && strncmp(tag, "fileset", len) == 0) { |
| 100 | familyData->currentTag = FILESET_TAG; |
| 101 | } else if ((strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) || |
| 102 | (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG)) { |
| 103 | // If it's a Name, parse the text inside |
| 104 | XML_SetCharacterDataHandler(*familyData->parser, textHandler); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Handler for the end of tags. We only care about family, nameset, fileset, |
| 110 | * name, and file. |
| 111 | */ |
| 112 | void endElementHandler(void *data, const char *tag) { |
| 113 | FamilyData *familyData = (FamilyData*) data; |
| 114 | int len = strlen(tag); |
| 115 | if (strncmp(tag, "family", len)== 0) { |
| 116 | // Done parsing a Family - store the created currentFamily in the families array |
| 117 | *familyData->families.append() = familyData->currentFamily; |
| 118 | familyData->currentFamily = NULL; |
| 119 | } else if (len == 7 && strncmp(tag, "nameset", len)== 0) { |
| 120 | familyData->currentTag = NO_TAG; |
| 121 | } else if (len == 7 && strncmp(tag, "fileset", len)== 0) { |
| 122 | familyData->currentTag = NO_TAG; |
| 123 | } else if ((strncmp(tag, "name", len) == 0 && familyData->currentTag == NAMESET_TAG) || |
| 124 | (strncmp(tag, "file", len) == 0 && familyData->currentTag == FILESET_TAG)) { |
| 125 | // Disable the arbitrary text handler installed to load Name data |
| 126 | XML_SetCharacterDataHandler(*familyData->parser, NULL); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * This function parses the given filename and stores the results in the given |
| 132 | * families array. |
| 133 | */ |
| 134 | void parseConfigFile(const char *filename, SkTDArray<FontFamily*> &families) { |
| 135 | XML_Parser parser = XML_ParserCreate(NULL); |
| 136 | FamilyData *familyData = new FamilyData(&parser, families); |
| 137 | XML_SetUserData(parser, familyData); |
| 138 | XML_SetElementHandler(parser, startElementHandler, endElementHandler); |
| 139 | FILE *file = fopen(filename, "r"); |
| 140 | // Some of the files we attempt to parse (in particular, /vendor/etc/fallback_fonts.xml) |
| 141 | // are optional - failure here is okay because one of these optional files may not exist. |
| 142 | if (file == NULL) { |
| 143 | return; |
| 144 | } |
| 145 | char buffer[512]; |
| 146 | bool done = false; |
| 147 | while (!done) { |
| 148 | fgets(buffer, sizeof(buffer), file); |
| 149 | int len = strlen(buffer); |
| 150 | if (feof(file) != 0) { |
| 151 | done = true; |
| 152 | } |
| 153 | XML_Parse(parser, buffer, len, done); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Loads data on font families from various expected configuration files. The |
| 159 | * resulting data is returned in the given fontFamilies array. |
| 160 | */ |
| 161 | void getFontFamilies(SkTDArray<FontFamily*> &fontFamilies) { |
| 162 | |
| 163 | SkTDArray<FontFamily*> fallbackFonts; |
| 164 | SkTDArray<FontFamily*> vendorFonts; |
| 165 | parseConfigFile(SYSTEM_FONTS_FILE, fontFamilies); |
| 166 | parseConfigFile(FALLBACK_FONTS_FILE, fallbackFonts); |
| 167 | parseConfigFile(VENDOR_FONTS_FILE, vendorFonts); |
| 168 | |
| 169 | // This loop inserts the vendor fallback fonts in the correct order in the |
| 170 | // overall fallbacks list. |
| 171 | int currentOrder = -1; |
| 172 | for (int i = 0; i < vendorFonts.count(); ++i) { |
| 173 | FontFamily* family = vendorFonts[i]; |
| 174 | int order = family->order; |
| 175 | if (order < 0) { |
| 176 | if (currentOrder < 0) { |
| 177 | // Default case - just add it to the end of the fallback list |
| 178 | *fallbackFonts.append() = family; |
| 179 | } else { |
| 180 | // no order specified on this font, but we're incrementing the order |
| 181 | // based on an earlier order insertion request |
| 182 | *fallbackFonts.insert(currentOrder++) = family; |
| 183 | } |
| 184 | } else { |
| 185 | // Add the font into the fallback list in the specified order. Set |
| 186 | // currentOrder for correct placement of other fonts in the vendor list. |
| 187 | *fallbackFonts.insert(order) = family; |
| 188 | currentOrder = order + 1; |
| 189 | } |
| 190 | } |
| 191 | // Append all fallback fonts to system fonts |
| 192 | for (int i = 0; i < fallbackFonts.count(); ++i) { |
| 193 | *fontFamilies.append() = fallbackFonts[i]; |
| 194 | } |
| 195 | } |