OLD | NEW |
---|---|
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" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 | 108 |
109 static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { | 109 static bool memeq(const char* s1, const char* s2, size_t n1, size_t n2) { |
110 return n1 == n2 && 0 == memcmp(s1, s2, n1); | 110 return n1 == n2 && 0 == memcmp(s1, s2, n1); |
111 } | 111 } |
112 #define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n) | 112 #define MEMEQ(c, s, n) memeq(c, s, sizeof(c) - 1, n) |
113 | 113 |
114 #define ATTS_NON_NULL(a, i) (a[i] != NULL && a[i+1] != NULL) | 114 #define ATTS_NON_NULL(a, i) (a[i] != NULL && a[i+1] != NULL) |
115 | 115 |
116 namespace lmpParser { | 116 namespace lmpParser { |
117 | 117 |
118 static void family_element_handler(FontFamily* family, const char** attributes) { | 118 static void family_element_handler(FamilyData* self, const char** attributes) { |
119 // A non-fallback <family> tag must have a canonical name attribute. | 119 // A non-fallback <family> tag must have a canonical name attribute. |
120 // A fallback <family> tag has no name, and may have lang and variant | 120 // A fallback <family> tag has no name, and may have lang and variant |
121 // attributes. | 121 // attributes. |
122 family->fIsFallbackFont = true; | 122 FontFamily& family = *self->fCurrentFamily; |
123 family.fIsFallbackFont = true; | |
tomhudson
2015/04/20 18:11:39
Why are you making /family/ a reference instead of
bungeman-skia
2015/04/20 20:14:57
Mostly for consistency with font_element_handler,
| |
123 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { | 124 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
124 const char* name = attributes[i]; | 125 const char* name = attributes[i]; |
125 const char* value = attributes[i+1]; | 126 const char* value = attributes[i+1]; |
126 size_t nameLen = strlen(name); | 127 size_t nameLen = strlen(name); |
127 size_t valueLen = strlen(value); | 128 size_t valueLen = strlen(value); |
128 if (MEMEQ("name", name, nameLen)) { | 129 if (MEMEQ("name", name, nameLen)) { |
129 SkAutoAsciiToLC tolc(value); | 130 SkAutoAsciiToLC tolc(value); |
130 family->fNames.push_back().set(tolc.lc()); | 131 family.fNames.push_back().set(tolc.lc()); |
131 family->fIsFallbackFont = false; | 132 family.fIsFallbackFont = false; |
132 } else if (MEMEQ("lang", name, nameLen)) { | 133 } else if (MEMEQ("lang", name, nameLen)) { |
133 family->fLanguage = SkLanguage(value, valueLen); | 134 family.fLanguage = SkLanguage(value, valueLen); |
134 } else if (MEMEQ("variant", name, nameLen)) { | 135 } else if (MEMEQ("variant", name, nameLen)) { |
135 // Value should be either elegant or compact. | 136 // Value should be either elegant or compact. |
136 if (MEMEQ("elegant", value, valueLen)) { | 137 if (MEMEQ("elegant", value, valueLen)) { |
137 family->fVariant = kElegant_FontVariant; | 138 family.fVariant = kElegant_FontVariant; |
138 } else if (MEMEQ("compact", value, valueLen)) { | 139 } else if (MEMEQ("compact", value, valueLen)) { |
139 family->fVariant = kCompact_FontVariant; | 140 family.fVariant = kCompact_FontVariant; |
140 } | 141 } |
141 } | 142 } |
142 } | 143 } |
143 } | 144 } |
144 | 145 |
145 static void XMLCALL font_file_name_handler(void* data, const char* s, int len) { | 146 static void XMLCALL font_file_name_handler(void* data, const char* s, int len) { |
146 FamilyData* self = static_cast<FamilyData*>(data); | 147 FamilyData* self = static_cast<FamilyData*>(data); |
147 self->fCurrentFontInfo->fFileName.append(s, len); | 148 self->fCurrentFontInfo->fFileName.append(s, len); |
148 } | 149 } |
149 | 150 |
150 static void font_element_handler(FamilyData* self, FontFileInfo* file, const cha r** attributes) { | 151 static void font_element_handler(FamilyData* self, const char** attributes) { |
151 // A <font> should have weight (integer) and style (normal, italic) attribut es. | 152 // A <font> should have weight (integer) and style (normal, italic) attribut es. |
152 // NOTE: we ignore the style. | 153 // NOTE: we ignore the style. |
153 // The element should contain a filename. | 154 // The element should contain a filename. |
155 FontFileInfo& file = *self->fCurrentFontInfo; | |
154 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { | 156 for (size_t i = 0; ATTS_NON_NULL(attributes, i); i += 2) { |
155 const char* name = attributes[i]; | 157 const char* name = attributes[i]; |
156 const char* value = attributes[i+1]; | 158 const char* value = attributes[i+1]; |
157 size_t nameLen = strlen(name); | 159 size_t nameLen = strlen(name); |
158 if (MEMEQ("weight", name, nameLen)) { | 160 if (MEMEQ("weight", name, nameLen)) { |
159 if (!parse_non_negative_integer(value, &file->fWeight)) { | 161 if (!parse_non_negative_integer(value, &file.fWeight)) { |
160 SkDebugf("---- Font weight %s (INVALID)", value); | 162 SkDebugf("---- Font weight %s (INVALID)", value); |
161 } | 163 } |
162 } else if (MEMEQ("style", name, nameLen)) { | 164 } else if (MEMEQ("style", name, nameLen)) { |
163 size_t valueLen = strlen(value); | 165 size_t valueLen = strlen(value); |
164 if (MEMEQ("normal", value, valueLen)) { | 166 if (MEMEQ("normal", value, valueLen)) { |
165 file->fStyle = FontFileInfo::Style::kNormal; | 167 file.fStyle = FontFileInfo::Style::kNormal; |
166 } else if (MEMEQ("italic", value, valueLen)) { | 168 } else if (MEMEQ("italic", value, valueLen)) { |
167 file->fStyle = FontFileInfo::Style::kItalic; | 169 file.fStyle = FontFileInfo::Style::kItalic; |
168 } | 170 } |
169 } else if (MEMEQ("index", name, nameLen)) { | 171 } else if (MEMEQ("index", name, nameLen)) { |
170 if (!parse_non_negative_integer(value, &file->fIndex)) { | 172 if (!parse_non_negative_integer(value, &file.fIndex)) { |
171 SkDebugf("---- Font index %s (INVALID)", value); | 173 SkDebugf("---- Font index %s (INVALID)", value); |
172 } | 174 } |
173 } | 175 } |
174 } | 176 } |
175 XML_SetCharacterDataHandler(self->fParser, font_file_name_handler); | 177 XML_SetCharacterDataHandler(self->fParser, font_file_name_handler); |
176 } | 178 } |
177 | 179 |
178 static FontFamily* find_family(FamilyData* self, const SkString& familyName) { | 180 static FontFamily* find_family(FamilyData* self, const SkString& familyName) { |
179 for (int i = 0; i < self->fFamilies.count(); i++) { | 181 for (int i = 0; i < self->fFamilies.count(); i++) { |
180 FontFamily* candidate = self->fFamilies[i]; | 182 FontFamily* candidate = self->fFamilies[i]; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 } else { | 235 } else { |
234 targetFamily->fNames.push_back().set(aliasName); | 236 targetFamily->fNames.push_back().set(aliasName); |
235 } | 237 } |
236 } | 238 } |
237 | 239 |
238 static void XMLCALL start_element_handler(void* data, const char* tag, const cha r** attributes) { | 240 static void XMLCALL start_element_handler(void* data, const char* tag, const cha r** attributes) { |
239 FamilyData* self = static_cast<FamilyData*>(data); | 241 FamilyData* self = static_cast<FamilyData*>(data); |
240 size_t len = strlen(tag); | 242 size_t len = strlen(tag); |
241 if (MEMEQ("family", tag, len)) { | 243 if (MEMEQ("family", tag, len)) { |
242 self->fCurrentFamily.reset(new FontFamily(self->fBasePath, self->fIsFall back)); | 244 self->fCurrentFamily.reset(new FontFamily(self->fBasePath, self->fIsFall back)); |
243 family_element_handler(self->fCurrentFamily, attributes); | 245 family_element_handler(self, attributes); |
244 } else if (MEMEQ("font", tag, len)) { | 246 } else if (MEMEQ("font", tag, len)) { |
245 FontFileInfo* file = &self->fCurrentFamily->fFonts.push_back(); | 247 self->fCurrentFontInfo = &self->fCurrentFamily->fFonts.push_back(); |
246 self->fCurrentFontInfo = file; | 248 font_element_handler(self, attributes); |
247 font_element_handler(self, file, attributes); | |
248 } else if (MEMEQ("alias", tag, len)) { | 249 } else if (MEMEQ("alias", tag, len)) { |
249 alias_element_handler(self, attributes); | 250 alias_element_handler(self, attributes); |
250 } | 251 } |
251 } | 252 } |
252 | 253 |
253 static void XMLCALL end_element_handler(void* data, const char* tag) { | 254 static void XMLCALL end_element_handler(void* data, const char* tag) { |
254 FamilyData* self = static_cast<FamilyData*>(data); | 255 FamilyData* self = static_cast<FamilyData*>(data); |
255 size_t len = strlen(tag); | 256 size_t len = strlen(tag); |
256 if (MEMEQ("family", tag, len)) { | 257 if (MEMEQ("family", tag, len)) { |
257 *self->fFamilies.append() = self->fCurrentFamily.detach(); | 258 *self->fFamilies.append() = self->fCurrentFamily.detach(); |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
634 const char* tag = fTag.c_str(); | 635 const char* tag = fTag.c_str(); |
635 | 636 |
636 // strip off the rightmost "-.*" | 637 // strip off the rightmost "-.*" |
637 const char* parentTagEnd = strrchr(tag, '-'); | 638 const char* parentTagEnd = strrchr(tag, '-'); |
638 if (parentTagEnd == NULL) { | 639 if (parentTagEnd == NULL) { |
639 return SkLanguage(); | 640 return SkLanguage(); |
640 } | 641 } |
641 size_t parentTagLen = parentTagEnd - tag; | 642 size_t parentTagLen = parentTagEnd - tag; |
642 return SkLanguage(tag, parentTagLen); | 643 return SkLanguage(tag, parentTagLen); |
643 } | 644 } |
OLD | NEW |