From 9116bd3bee7a5597d1ed6ac61b359be628c04b4c Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 7 Feb 2022 15:00:26 -0800 Subject: [PATCH] Convert font width values from variable axes to SkFontStyle widths A recent change added support for using the width variable axis to override the usWidthClass value from the font's OS/2 table. However, the width axis uses a different value scale. This PR converts the axis value to a usWidthClass value that is equivalent to the SkFontStyle Width enum. Change-Id: I6ce78f4dcad3abdcf3e00bd524ffb08a57e51919 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/505356 Reviewed-by: Ben Wagner Commit-Queue: Ben Wagner --- src/core/SkFontDescriptor.cpp | 9 +++++++-- src/core/SkFontDescriptor.h | 2 ++ src/ports/SkFontHost_FreeType.cpp | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/core/SkFontDescriptor.cpp b/src/core/SkFontDescriptor.cpp index c8e94c60eb..b23ce183e0 100644 --- a/src/core/SkFontDescriptor.cpp +++ b/src/core/SkFontDescriptor.cpp @@ -140,8 +140,8 @@ bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) { SkFontStyle::Slant slantEnum = SkFontStyle::kUpright_Slant; if (slant != 0) { slantEnum = SkFontStyle::kOblique_Slant; } if (0 < italic) { slantEnum = SkFontStyle::kItalic_Slant; } - int usWidth = SkScalarRoundToInt(SkScalarInterpFunc(width, &width_for_usWidth[1], usWidths, 9)); - result->fStyle = SkFontStyle(SkScalarRoundToInt(weight), usWidth, slantEnum); + SkFontStyle::Width widthEnum = SkFontStyleWidthForWidthAxisValue(width); + result->fStyle = SkFontStyle(SkScalarRoundToInt(weight), widthEnum, slantEnum); size_t length; if (!stream->readPackedUInt(&length)) { return false; } @@ -191,3 +191,8 @@ void SkFontDescriptor::serialize(SkWStream* stream) const { stream->writePackedUInt(0); } } + +SkFontStyle::Width SkFontDescriptor::SkFontStyleWidthForWidthAxisValue(SkScalar width) { + int usWidth = SkScalarRoundToInt(SkScalarInterpFunc(width, &width_for_usWidth[1], usWidths, 9)); + return static_cast(usWidth); +} diff --git a/src/core/SkFontDescriptor.h b/src/core/SkFontDescriptor.h index 4bc328a9a2..0f52f12c98 100644 --- a/src/core/SkFontDescriptor.h +++ b/src/core/SkFontDescriptor.h @@ -94,6 +94,8 @@ public: return fVariation.reset(coordinateCount); } + static SkFontStyle::Width SkFontStyleWidthForWidthAxisValue(SkScalar width); + private: SkString fFamilyName; SkString fFullName; diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp index ce55fde6a5..0a751d22ec 100644 --- a/src/ports/SkFontHost_FreeType.cpp +++ b/src/ports/SkFontHost_FreeType.cpp @@ -1940,7 +1940,8 @@ bool SkTypeface_FreeType::Scanner::scanFont( } if (wdthIndex) { SkASSERT(*wdthIndex < numAxes); - width = SkScalarRoundToInt(SkFixedToScalar(coords[*wdthIndex])); + SkScalar wdthValue = SkFixedToScalar(coords[*wdthIndex]); + width = SkFontDescriptor::SkFontStyleWidthForWidthAxisValue(wdthValue); } if (slntIndex) { SkASSERT(*slntIndex < numAxes);