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

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

Issue 1027373002: Font variations. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase Created 5 years, 7 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
« no previous file with comments | « src/ports/SkFontConfigParser_android.h ('k') | src/ports/SkFontHost_FreeType.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "SkFontMgr_android.h" 9 #include "SkFontMgr_android.h"
10 #include "SkStream.h" 10 #include "SkStream.h"
11 #include "SkTDArray.h" 11 #include "SkTDArray.h"
12 #include "SkTSearch.h" 12 #include "SkTSearch.h"
13 #include "SkTypeface.h" 13 #include "SkTypeface.h"
14 14
15 #include <expat.h> 15 #include <expat.h>
16 #include <dirent.h> 16 #include <dirent.h>
17 #include <stdio.h>
18 17
19 #include <limits>
20 #include <stdlib.h> 18 #include <stdlib.h>
21 19
22 #define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml" 20 #define LMP_SYSTEM_FONTS_FILE "/system/etc/fonts.xml"
23 #define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml" 21 #define OLD_SYSTEM_FONTS_FILE "/system/etc/system_fonts.xml"
24 #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml" 22 #define FALLBACK_FONTS_FILE "/system/etc/fallback_fonts.xml"
25 #define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml" 23 #define VENDOR_FONTS_FILE "/vendor/etc/fallback_fonts.xml"
26 24
27 #define LOCALE_FALLBACK_FONTS_SYSTEM_DIR "/system/etc" 25 #define LOCALE_FALLBACK_FONTS_SYSTEM_DIR "/system/etc"
28 #define LOCALE_FALLBACK_FONTS_VENDOR_DIR "/vendor/etc" 26 #define LOCALE_FALLBACK_FONTS_VENDOR_DIR "/vendor/etc"
29 #define LOCALE_FALLBACK_FONTS_PREFIX "fallback_fonts-" 27 #define LOCALE_FALLBACK_FONTS_PREFIX "fallback_fonts-"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 int fVersion; // The version of the file parsed. 102 int fVersion; // The version of the file parsed.
105 const SkString& fBasePath; // The current base path. 103 const SkString& fBasePath; // The current base path.
106 const bool fIsFallback; // Indicates the file being parsed is a fallback file 104 const bool fIsFallback; // Indicates the file being parsed is a fallback file
107 const char* fFilename; // The name of the file currently being parsed. 105 const char* fFilename; // The name of the file currently being parsed.
108 106
109 int fDepth; // The current element depth of th e parse. 107 int fDepth; // The current element depth of th e parse.
110 int fSkip; // The depth to stop skipping, 0 i f not skipping. 108 int fSkip; // The depth to stop skipping, 0 i f not skipping.
111 SkTDArray<const TagHandler*> fHandler; // The stack of current tag handle rs. 109 SkTDArray<const TagHandler*> fHandler; // The stack of current tag handle rs.
112 }; 110 };
113 111
114 /** Parses a null terminated string into an integer type, checking for overflow.
115 * http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-nega tive-def
116 *
117 * If the string cannot be parsed into 'value', returns false and does not chan ge 'value'.
118 */
119 template <typename T> static bool parse_non_negative_integer(const char* s, T* v alue) {
120 SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer);
121 const T nMax = std::numeric_limits<T>::max() / 10;
122 const T dMax = std::numeric_limits<T>::max() - (nMax * 10);
123 T n = 0;
124 for (; *s; ++s) {
125 // Check if digit
126 if (*s < '0' || '9' < *s) {
127 return false;
128 }
129 int d = *s - '0';
130 // Check for overflow
131 if (n > nMax || (n == nMax && d > dMax)) {
132 return false;
133 }
134 n = (n * 10) + d;
135 }
136 *value = n;
137 return true;
138 }
139
140 static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { 112 static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) {
141 return n1 == n2 && 0 == memcmp(s1, s2, n1); 113 return n1 == n2 && 0 == memcmp(s1, s2, n1);
142 } 114 }
143 #define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n) 115 #define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n)
144 116
145 #define ATTS_NON_NULL(a, i) (a[i] != NULL && a[i+1] != NULL) 117 #define ATTS_NON_NULL(a, i) (a[i] != NULL && a[i+1] != NULL)
146 118
147 #define SK_FONTCONFIGPARSER_PREFIX "[SkFontConfigParser] " 119 #define SK_FONTCONFIGPARSER_PREFIX "[SkFontConfigParser] "
148 120
149 #define SK_FONTCONFIGPARSER_WARNING(message, ...) SkDebugf( \ 121 #define SK_FONTCONFIGPARSER_WARNING(message, ...) SkDebugf( \
(...skipping 17 matching lines...) Expand all
167 while (is_whitespace(*end)) { --end; } 139 while (is_whitespace(*end)) { --end; }
168 ++end; // make end exclusive 140 ++end; // make end exclusive
169 } 141 }
170 size_t len = end - start; 142 size_t len = end - start;
171 memmove(str, start, len); 143 memmove(str, start, len);
172 s->resize(len); 144 s->resize(len);
173 } 145 }
174 146
175 namespace lmpParser { 147 namespace lmpParser {
176 148
149 static const TagHandler axisHandler = {
150 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
151 FontFileInfo& file = *self->fCurrentFontInfo;
152 FontFileInfo::Axis& axis = file.fAxes.push_back();
153 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
154 const char* name = attributes[i];
155 const char* value = attributes[i+1];
156 size_t nameLen = strlen(name);
157 if (MEMEQ("tag", name, nameLen)) {
158 size_t valueLen = strlen(value);
159 if (valueLen == 4) {
160 SkFourByteTag tag = SkSetFourByteTag(value[0], value[1], val ue[2], value[3]);
161 for (int j = 0; j < file.fAxes.count() - 1; ++j) {
162 if (file.fAxes[j].fTag == tag) {
163 SK_FONTCONFIGPARSER_WARNING("'%c%c%c%c' axis specifi ed more than once",
164 (tag >> 24) & 0xFF,
165 (tag >> 16) & 0xFF,
166 (tag >> 8) & 0xFF,
167 (tag ) & 0xFF);
168 }
169 }
170 axis.fTag = SkSetFourByteTag(value[0], value[1], value[2], v alue[3]);
171 } else {
172 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis tag", v alue);
173 }
174 } else if (MEMEQ("stylevalue", name, nameLen)) {
175 if (!parse_fixed<16>(value, &axis.fValue)) {
176 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid axis styleva lue", value);
177 }
178 }
179 }
180 },
181 /*end*/NULL,
182 /*tag*/NULL,
183 /*chars*/NULL,
184 };
185
177 static const TagHandler fontHandler = { 186 static const TagHandler fontHandler = {
178 /*start*/[](FamilyData* self, const char* tag, const char** attributes) { 187 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
179 // 'weight' (non-negative integer) [default 0] 188 // 'weight' (non-negative integer) [default 0]
180 // 'style' ("normal", "italic") [default "auto"] 189 // 'style' ("normal", "italic") [default "auto"]
181 // 'index' (non-negative integer) [default 0] 190 // 'index' (non-negative integer) [default 0]
182 // The character data should be a filename. 191 // The character data should be a filename.
183 FontFileInfo& file = self->fCurrentFamily->fFonts.push_back(); 192 FontFileInfo& file = self->fCurrentFamily->fFonts.push_back();
184 self->fCurrentFontInfo = &file; 193 self->fCurrentFontInfo = &file;
185 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { 194 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) {
186 const char* name = attributes[i]; 195 const char* name = attributes[i];
(...skipping 13 matching lines...) Expand all
200 } else if (MEMEQ("index", name, nameLen)) { 209 } else if (MEMEQ("index", name, nameLen)) {
201 if (!parse_non_negative_integer(value, &file.fIndex)) { 210 if (!parse_non_negative_integer(value, &file.fIndex)) {
202 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid index", valu e); 211 SK_FONTCONFIGPARSER_WARNING("'%s' is an invalid index", valu e);
203 } 212 }
204 } 213 }
205 } 214 }
206 }, 215 },
207 /*end*/[](FamilyData* self, const char* tag) { 216 /*end*/[](FamilyData* self, const char* tag) {
208 trim_string(&self->fCurrentFontInfo->fFileName); 217 trim_string(&self->fCurrentFontInfo->fFileName);
209 }, 218 },
210 /*tag*/NULL, 219 /*tag*/[](FamilyData* self, const char* tag, const char** attributes) -> con st TagHandler* {
220 size_t len = strlen(tag);
221 if (MEMEQ("axis", tag, len)) {
222 return &axisHandler;
223 }
224 return NULL;
225 },
211 /*chars*/[](void* data, const char* s, int len) { 226 /*chars*/[](void* data, const char* s, int len) {
212 FamilyData* self = static_cast<FamilyData*>(data); 227 FamilyData* self = static_cast<FamilyData*>(data);
213 self->fCurrentFontInfo->fFileName.append(s, len); 228 self->fCurrentFontInfo->fFileName.append(s, len);
214 } 229 }
215 }; 230 };
216 231
217 static const TagHandler familyHandler = { 232 static const TagHandler familyHandler = {
218 /*start*/[](FamilyData* self, const char* tag, const char** attributes) { 233 /*start*/[](FamilyData* self, const char* tag, const char** attributes) {
219 // 'name' (string) [optional] 234 // 'name' (string) [optional]
220 // 'lang' (string) [default ""] 235 // 'lang' (string) [default ""]
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 const char* tag = fTag.c_str(); 788 const char* tag = fTag.c_str();
774 789
775 // strip off the rightmost "-.*" 790 // strip off the rightmost "-.*"
776 const char* parentTagEnd = strrchr(tag, '-'); 791 const char* parentTagEnd = strrchr(tag, '-');
777 if (parentTagEnd == NULL) { 792 if (parentTagEnd == NULL) {
778 return SkLanguage(); 793 return SkLanguage();
779 } 794 }
780 size_t parentTagLen = parentTagEnd - tag; 795 size_t parentTagLen = parentTagEnd - tag;
781 return SkLanguage(tag, parentTagLen); 796 return SkLanguage(tag, parentTagLen);
782 } 797 }
OLDNEW
« no previous file with comments | « src/ports/SkFontConfigParser_android.h ('k') | src/ports/SkFontHost_FreeType.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698