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

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

Issue 1873923002: Begin switch to SkFontStyle for legacy calls. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Dont update bzl file now. Created 4 years, 8 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/SkFontConfigInterface_direct.h ('k') | src/ports/SkFontConfigTypeface.h » ('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 2009-2015 Google Inc. 2 * Copyright 2009-2015 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 /* migrated from chrome/src/skia/ext/SkFontHost_fontconfig_direct.cpp */ 8 /* migrated from chrome/src/skia/ext/SkFontHost_fontconfig_direct.cpp */
9 9
10 #include "SkBuffer.h" 10 #include "SkBuffer.h"
11 #include "SkDataTable.h" 11 #include "SkDataTable.h"
12 #include "SkFixed.h"
12 #include "SkFontConfigInterface_direct.h" 13 #include "SkFontConfigInterface_direct.h"
13 #include "SkFontStyle.h" 14 #include "SkFontStyle.h"
14 #include "SkMutex.h" 15 #include "SkMutex.h"
15 #include "SkStream.h" 16 #include "SkStream.h"
16 #include "SkString.h" 17 #include "SkString.h"
17 #include "SkTArray.h" 18 #include "SkTArray.h"
18 #include "SkTDArray.h" 19 #include "SkTDArray.h"
19 #include "SkTemplates.h" 20 #include "SkTemplates.h"
20 #include "SkTypeface.h" 21 #include "SkTypeface.h"
21 #include "SkTypes.h" 22 #include "SkTypes.h"
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 // tells you whether a given request is for such a fallback. 298 // tells you whether a given request is for such a fallback.
298 bool IsFallbackFontAllowed(const SkString& family) { 299 bool IsFallbackFontAllowed(const SkString& family) {
299 const char* family_cstr = family.c_str(); 300 const char* family_cstr = family.c_str();
300 return family.isEmpty() || 301 return family.isEmpty() ||
301 strcasecmp(family_cstr, "sans") == 0 || 302 strcasecmp(family_cstr, "sans") == 0 ||
302 strcasecmp(family_cstr, "serif") == 0 || 303 strcasecmp(family_cstr, "serif") == 0 ||
303 strcasecmp(family_cstr, "monospace") == 0; 304 strcasecmp(family_cstr, "monospace") == 0;
304 } 305 }
305 306
306 // Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|. 307 // Retrieves |is_bold|, |is_italic| and |font_family| properties from |font|.
308 #ifdef SK_VERY_LEGACY_CREATE_TYPEFACE
307 SkTypeface::Style GetFontStyle(FcPattern* font) { 309 SkTypeface::Style GetFontStyle(FcPattern* font) {
308 int resulting_bold; 310 int resulting_bold;
309 if (FcPatternGetInteger(font, FC_WEIGHT, 0, &resulting_bold)) 311 if (FcPatternGetInteger(font, FC_WEIGHT, 0, &resulting_bold))
310 resulting_bold = FC_WEIGHT_NORMAL; 312 resulting_bold = FC_WEIGHT_NORMAL;
311 313
312 int resulting_italic; 314 int resulting_italic;
313 if (FcPatternGetInteger(font, FC_SLANT, 0, &resulting_italic)) 315 if (FcPatternGetInteger(font, FC_SLANT, 0, &resulting_italic))
314 resulting_italic = FC_SLANT_ROMAN; 316 resulting_italic = FC_SLANT_ROMAN;
315 317
316 // If we ask for an italic font, fontconfig might take a roman font and set 318 // If we ask for an italic font, fontconfig might take a roman font and set
(...skipping 11 matching lines...) Expand all
328 int styleBits = 0; 330 int styleBits = 0;
329 if (resulting_bold > FC_WEIGHT_MEDIUM && !have_embolden) { 331 if (resulting_bold > FC_WEIGHT_MEDIUM && !have_embolden) {
330 styleBits |= SkTypeface::kBold; 332 styleBits |= SkTypeface::kBold;
331 } 333 }
332 if (resulting_italic > FC_SLANT_ROMAN && !have_matrix) { 334 if (resulting_italic > FC_SLANT_ROMAN && !have_matrix) {
333 styleBits |= SkTypeface::kItalic; 335 styleBits |= SkTypeface::kItalic;
334 } 336 }
335 337
336 return (SkTypeface::Style)styleBits; 338 return (SkTypeface::Style)styleBits;
337 } 339 }
340 #else
341
342 static int get_int(FcPattern* pattern, const char object[], int missing) {
343 int value;
344 if (FcPatternGetInteger(pattern, object, 0, &value) != FcResultMatch) {
345 return missing;
346 }
347 return value;
348 }
349
350 static int map_range(SkFixed value,
351 SkFixed old_min, SkFixed old_max,
352 SkFixed new_min, SkFixed new_max)
353 {
354 SkASSERT(old_min < old_max);
355 SkASSERT(new_min <= new_max);
356 return new_min + SkMulDiv(value - old_min, new_max - new_min, old_max - old_ min);
357 }
358
359 struct MapRanges {
360 SkFixed old_val;
361 SkFixed new_val;
362 };
363
364 static SkFixed map_ranges_fixed(SkFixed val, MapRanges const ranges[], int range sCount) {
365 // -Inf to [0]
366 if (val < ranges[0].old_val) {
367 return ranges[0].new_val;
368 }
369
370 // Linear from [i] to [i+1]
371 for (int i = 0; i < rangesCount - 1; ++i) {
372 if (val < ranges[i+1].old_val) {
373 return map_range(val, ranges[i].old_val, ranges[i+1].old_val,
374 ranges[i].new_val, ranges[i+1].new_val);
375 }
376 }
377
378 // From [n] to +Inf
379 // if (fcweight < Inf)
380 return ranges[rangesCount-1].new_val;
381 }
382
383 static int map_ranges(int val, MapRanges const ranges[], int rangesCount) {
384 return SkFixedRoundToInt(map_ranges_fixed(SkIntToFixed(val), ranges, rangesC ount));
385 }
386
387 template<int n> struct SkTFixed {
388 static_assert(-32768 <= n && n <= 32767, "SkTFixed_n_not_in_range");
389 static const SkFixed value = static_cast<SkFixed>(n << 16);
390 };
391
392 static SkFontStyle skfontstyle_from_fcpattern(FcPattern* pattern) {
393 typedef SkFontStyle SkFS;
394
395 static const MapRanges weightRanges[] = {
396 { SkTFixed<FC_WEIGHT_THIN>::value, SkTFixed<SkFS::kThin_Weight>::v alue },
397 { SkTFixed<FC_WEIGHT_EXTRALIGHT>::value, SkTFixed<SkFS::kExtraLight_Weig ht>::value },
398 { SkTFixed<FC_WEIGHT_LIGHT>::value, SkTFixed<SkFS::kLight_Weight>:: value },
399 { SkTFixed<FC_WEIGHT_REGULAR>::value, SkTFixed<SkFS::kNormal_Weight>: :value },
400 { SkTFixed<FC_WEIGHT_MEDIUM>::value, SkTFixed<SkFS::kMedium_Weight>: :value },
401 { SkTFixed<FC_WEIGHT_DEMIBOLD>::value, SkTFixed<SkFS::kSemiBold_Weight >::value },
402 { SkTFixed<FC_WEIGHT_BOLD>::value, SkTFixed<SkFS::kBold_Weight>::v alue },
403 { SkTFixed<FC_WEIGHT_EXTRABOLD>::value, SkTFixed<SkFS::kExtraBold_Weigh t>::value },
404 { SkTFixed<FC_WEIGHT_BLACK>::value, SkTFixed<SkFS::kBlack_Weight>:: value },
405 { SkTFixed<FC_WEIGHT_EXTRABLACK>::value, SkTFixed<1000>::value },
406 };
407 int weight = map_ranges(get_int(pattern, FC_WEIGHT, FC_WEIGHT_REGULAR),
408 weightRanges, SK_ARRAY_COUNT(weightRanges));
409
410 static const MapRanges widthRanges[] = {
411 { SkTFixed<FC_WIDTH_ULTRACONDENSED>::value, SkTFixed<SkFS::kUltraCondens ed_Width>::value },
412 { SkTFixed<FC_WIDTH_EXTRACONDENSED>::value, SkTFixed<SkFS::kExtraCondens ed_Width>::value },
413 { SkTFixed<FC_WIDTH_CONDENSED>::value, SkTFixed<SkFS::kCondensed_Wi dth>::value },
414 { SkTFixed<FC_WIDTH_SEMICONDENSED>::value, SkTFixed<SkFS::kSemiCondense d_Width>::value },
415 { SkTFixed<FC_WIDTH_NORMAL>::value, SkTFixed<SkFS::kNormal_Width >::value },
416 { SkTFixed<FC_WIDTH_SEMIEXPANDED>::value, SkTFixed<SkFS::kSemiExpanded _Width>::value },
417 { SkTFixed<FC_WIDTH_EXPANDED>::value, SkTFixed<SkFS::kExpanded_Wid th>::value },
418 { SkTFixed<FC_WIDTH_EXTRAEXPANDED>::value, SkTFixed<SkFS::kExtraExpande d_Width>::value },
419 { SkTFixed<FC_WIDTH_ULTRAEXPANDED>::value, SkTFixed<SkFS::kUltaExpanded _Width>::value },
420 };
421 int width = map_ranges(get_int(pattern, FC_WIDTH, FC_WIDTH_NORMAL),
422 widthRanges, SK_ARRAY_COUNT(widthRanges));
423
424 SkFS::Slant slant = get_int(pattern, FC_SLANT, FC_SLANT_ROMAN) > 0
425 ? SkFS::kItalic_Slant
426 : SkFS::kUpright_Slant;
427
428 return SkFontStyle(weight, width, slant);
429 }
430
431 static void fcpattern_from_skfontstyle(SkFontStyle style, FcPattern* pattern) {
432 typedef SkFontStyle SkFS;
433
434 static const MapRanges weightRanges[] = {
435 { SkTFixed<SkFS::kThin_Weight>::value, SkTFixed<FC_WEIGHT_THIN>::v alue },
436 { SkTFixed<SkFS::kExtraLight_Weight>::value, SkTFixed<FC_WEIGHT_EXTRALIG HT>::value },
437 { SkTFixed<SkFS::kLight_Weight>::value, SkTFixed<FC_WEIGHT_LIGHT>:: value },
438 { SkTFixed<SkFS::kNormal_Weight>::value, SkTFixed<FC_WEIGHT_REGULAR> ::value },
439 { SkTFixed<SkFS::kMedium_Weight>::value, SkTFixed<FC_WEIGHT_MEDIUM>: :value },
440 { SkTFixed<SkFS::kSemiBold_Weight>::value, SkTFixed<FC_WEIGHT_DEMIBOLD >::value },
441 { SkTFixed<SkFS::kBold_Weight>::value, SkTFixed<FC_WEIGHT_BOLD>::v alue },
442 { SkTFixed<SkFS::kExtraBold_Weight>::value, SkTFixed<FC_WEIGHT_EXTRABOL D>::value },
443 { SkTFixed<SkFS::kBlack_Weight>::value, SkTFixed<FC_WEIGHT_BLACK>:: value },
444 { SkTFixed<1000>::value, SkTFixed<FC_WEIGHT_EXTRABLA CK>::value },
445 };
446 int weight = map_ranges(style.weight(), weightRanges, SK_ARRAY_COUNT(weightR anges));
447
448 static const MapRanges widthRanges[] = {
449 { SkTFixed<SkFS::kUltraCondensed_Width>::value, SkTFixed<FC_WIDTH_ULTRAC ONDENSED>::value },
450 { SkTFixed<SkFS::kExtraCondensed_Width>::value, SkTFixed<FC_WIDTH_EXTRAC ONDENSED>::value },
451 { SkTFixed<SkFS::kCondensed_Width>::value, SkTFixed<FC_WIDTH_CONDEN SED>::value },
452 { SkTFixed<SkFS::kSemiCondensed_Width>::value, SkTFixed<FC_WIDTH_SEMICO NDENSED>::value },
453 { SkTFixed<SkFS::kNormal_Width>::value, SkTFixed<FC_WIDTH_NORMAL >::value },
454 { SkTFixed<SkFS::kSemiExpanded_Width>::value, SkTFixed<FC_WIDTH_SEMIEX PANDED>::value },
455 { SkTFixed<SkFS::kExpanded_Width>::value, SkTFixed<FC_WIDTH_EXPAND ED>::value },
456 { SkTFixed<SkFS::kExtraExpanded_Width>::value, SkTFixed<FC_WIDTH_EXTRAE XPANDED>::value },
457 { SkTFixed<SkFS::kUltaExpanded_Width>::value, SkTFixed<FC_WIDTH_ULTRAE XPANDED>::value },
458 };
459 int width = map_ranges(style.width(), widthRanges, SK_ARRAY_COUNT(widthRange s));
460
461 FcPatternAddInteger(pattern, FC_WEIGHT, weight);
462 FcPatternAddInteger(pattern, FC_WIDTH, width);
463 FcPatternAddInteger(pattern, FC_SLANT, style.isItalic() ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
464 }
465
466 SkFontStyle GetFontStyle(FcPattern* font) {
467 return skfontstyle_from_fcpattern(font);
468 }
469 #endif
338 470
339 } // anonymous namespace 471 } // anonymous namespace
340 472
341 /////////////////////////////////////////////////////////////////////////////// 473 ///////////////////////////////////////////////////////////////////////////////
342 474
343 #define kMaxFontFamilyLength 2048 475 #define kMaxFontFamilyLength 2048
344 476
345 SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() { 477 SkFontConfigInterfaceDirect::SkFontConfigInterfaceDirect() {
346 SkAutoMutexAcquire ac(mutex_); 478 SkAutoMutexAcquire ac(mutex_);
347 479
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 if (acceptable_substitute) 542 if (acceptable_substitute)
411 break; 543 break;
412 } 544 }
413 if (!acceptable_substitute) 545 if (!acceptable_substitute)
414 return nullptr; 546 return nullptr;
415 } 547 }
416 548
417 return match; 549 return match;
418 } 550 }
419 551
552 #ifdef SK_VERY_LEGACY_CREATE_TYPEFACE
420 bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[], 553 bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[],
421 SkTypeface::Style style, 554 SkTypeface::Style style,
422 FontIdentity* outIdentity, 555 FontIdentity* outIdentity,
423 SkString* outFamilyName, 556 SkString* outFamilyName,
424 SkTypeface::Style* outStyle) { 557 SkTypeface::Style* outStyle) {
558 #else
559 bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[],
560 SkFontStyle style,
561 FontIdentity* outIdentity,
562 SkString* outFamilyName,
563 SkFontStyle* outStyle) {
564 #endif
425 SkString familyStr(familyName ? familyName : ""); 565 SkString familyStr(familyName ? familyName : "");
426 if (familyStr.size() > kMaxFontFamilyLength) { 566 if (familyStr.size() > kMaxFontFamilyLength) {
427 return false; 567 return false;
428 } 568 }
429 569
430 SkAutoMutexAcquire ac(mutex_); 570 SkAutoMutexAcquire ac(mutex_);
431 571
432 FcPattern* pattern = FcPatternCreate(); 572 FcPattern* pattern = FcPatternCreate();
433 573
434 if (familyName) { 574 if (familyName) {
435 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); 575 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
436 } 576 }
577 #ifdef SK_VERY_LEGACY_CREATE_TYPEFACE
437 FcPatternAddInteger(pattern, FC_WEIGHT, 578 FcPatternAddInteger(pattern, FC_WEIGHT,
438 (style & SkTypeface::kBold) ? FC_WEIGHT_BOLD 579 (style & SkTypeface::kBold) ? FC_WEIGHT_BOLD
439 : FC_WEIGHT_NORMAL); 580 : FC_WEIGHT_NORMAL);
440 FcPatternAddInteger(pattern, FC_SLANT, 581 FcPatternAddInteger(pattern, FC_SLANT,
441 (style & SkTypeface::kItalic) ? FC_SLANT_ITALIC 582 (style & SkTypeface::kItalic) ? FC_SLANT_ITALIC
442 : FC_SLANT_ROMAN); 583 : FC_SLANT_ROMAN);
584 #else
585 fcpattern_from_skfontstyle(style, pattern);
586 #endif
587
443 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); 588 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
444 589
445 FcConfigSubstitute(nullptr, pattern, FcMatchPattern); 590 FcConfigSubstitute(nullptr, pattern, FcMatchPattern);
446 FcDefaultSubstitute(pattern); 591 FcDefaultSubstitute(pattern);
447 592
448 // Font matching: 593 // Font matching:
449 // CSS often specifies a fallback list of families: 594 // CSS often specifies a fallback list of families:
450 // font-family: a, b, c, serif; 595 // font-family: a, b, c, serif;
451 // However, fontconfig will always do its best to find *a* font when asked 596 // However, fontconfig will always do its best to find *a* font when asked
452 // for something so we need a way to tell if the match which it has found is 597 // for something so we need a way to tell if the match which it has found is
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 const char* famName = get_name(match, FC_FAMILY); 720 const char* famName = get_name(match, FC_FAMILY);
576 if (famName && !find_name(names, famName)) { 721 if (famName && !find_name(names, famName)) {
577 *names.append() = famName; 722 *names.append() = famName;
578 *sizes.append() = strlen(famName) + 1; 723 *sizes.append() = strlen(famName) + 1;
579 } 724 }
580 } 725 }
581 726
582 return SkDataTable::NewCopyArrays((const void*const*)names.begin(), 727 return SkDataTable::NewCopyArrays((const void*const*)names.begin(),
583 sizes.begin(), names.count()); 728 sizes.begin(), names.count());
584 } 729 }
OLDNEW
« no previous file with comments | « src/ports/SkFontConfigInterface_direct.h ('k') | src/ports/SkFontConfigTypeface.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698