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

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

Issue 2256843003: Use CSS3 style matching on Android. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 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
« no previous file with comments | « no previous file | no next file » | 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 2014 Google Inc. 2 * Copyright 2014 Google Inc.
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 "SkTypes.h" 8 #include "SkTypes.h"
9 9
10 #include "SkData.h" 10 #include "SkData.h"
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 } 210 }
211 211
212 int count() override { 212 int count() override {
213 return fStyles.count(); 213 return fStyles.count();
214 } 214 }
215 void getStyle(int index, SkFontStyle* style, SkString* name) override { 215 void getStyle(int index, SkFontStyle* style, SkString* name) override {
216 if (index < 0 || fStyles.count() <= index) { 216 if (index < 0 || fStyles.count() <= index) {
217 return; 217 return;
218 } 218 }
219 if (style) { 219 if (style) {
220 *style = this->style(index); 220 *style = fStyles[index]->fontStyle();
221 } 221 }
222 if (name) { 222 if (name) {
223 name->reset(); 223 name->reset();
224 } 224 }
225 } 225 }
226 SkTypeface_AndroidSystem* createTypeface(int index) override { 226 SkTypeface_AndroidSystem* createTypeface(int index) override {
227 if (index < 0 || fStyles.count() <= index) { 227 if (index < 0 || fStyles.count() <= index) {
228 return nullptr; 228 return nullptr;
229 } 229 }
230 return SkRef(fStyles[index].get()); 230 return SkRef(fStyles[index].get());
231 } 231 }
232 232
233 /** Find the typeface in this style set that most closely matches the given pattern.
234 * TODO: consider replacing with SkStyleSet_Indirect::matchStyle();
235 * this simpler version using match_score() passes all our tests.
236 */
237 SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override { 233 SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override {
238 if (0 == fStyles.count()) { 234 return static_cast<SkTypeface_AndroidSystem*>(this->matchStyleCSS3(patte rn));
239 return nullptr;
240 }
241 SkTypeface_AndroidSystem* closest = fStyles[0];
242 int minScore = std::numeric_limits<int>::max();
243 for (int i = 0; i < fStyles.count(); ++i) {
244 SkFontStyle style = this->style(i);
245 int score = match_score(pattern, style);
246 if (score < minScore) {
247 closest = fStyles[i];
248 minScore = score;
249 }
250 }
251 return SkRef(closest);
252 } 235 }
253 236
254 private: 237 private:
255 SkFontStyle style(int index) {
256 return fStyles[index]->fontStyle();
257 }
258 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candid ate) {
259 int score = 0;
260 score += SkTAbs((pattern.width() - candidate.width()) * 100);
261 score += SkTAbs((pattern.slant() == candidate.slant()) ? 0 : 1000);
262 score += SkTAbs(pattern.weight() - candidate.weight());
263 return score;
264 }
265
266 SkTArray<SkAutoTUnref<SkTypeface_AndroidSystem>, true> fStyles; 238 SkTArray<SkAutoTUnref<SkTypeface_AndroidSystem>, true> fStyles;
267 239
268 friend struct NameToFamily; 240 friend struct NameToFamily;
269 friend class SkFontMgr_Android; 241 friend class SkFontMgr_Android;
270 242
271 typedef SkFontStyleSet INHERITED; 243 typedef SkFontStyleSet INHERITED;
272 }; 244 };
273 245
274 /** On Android a single family can have many names, but our API assumes unique n ames. 246 /** On Android a single family can have many names, but our API assumes unique n ames.
275 * Map names to the back end so that all names for a given family refer to the same 247 * Map names to the back end so that all names for a given family refer to the same
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ; 551 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ;
580 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" , 552 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" ,
581 gSystemFontUseStrings[custom->fSystemFontUse], 553 gSystemFontUseStrings[custom->fSystemFontUse],
582 custom->fBasePath, 554 custom->fBasePath,
583 custom->fFontsXml, 555 custom->fFontsXml,
584 custom->fFallbackFontsXml)); 556 custom->fFallbackFontsXml));
585 } 557 }
586 558
587 return new SkFontMgr_Android(custom); 559 return new SkFontMgr_Android(custom);
588 } 560 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698