From 0500ebafec1879a7c5f76fe40ee30bdab17ac3db Mon Sep 17 00:00:00 2001 From: Ben Wagner Date: Mon, 31 Jul 2017 15:41:14 -0400 Subject: Remove union from SkFontStyle. This is prompted by a bug in vc++ reported at https://developercommunity.visualstudio.com/content/problem/86829/constexpr-initializing-struct-in-union-only-zero-i.html Visual C++ will not properly constexpr initialize a struct inside a union. Removing this construct also allows us to avoid writing and reading from different members of a union. Change-Id: I76e069ecce77ef21b7ad3821bf9c29e1b0843618 Reviewed-on: https://skia-review.googlesource.com/28300 Commit-Queue: Mike Klein Reviewed-by: Brian Salomon Reviewed-by: Mike Klein --- include/core/SkFontStyle.h | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) (limited to 'include/core') diff --git a/include/core/SkFontStyle.h b/include/core/SkFontStyle.h index 306895d78f..a5e3034e1d 100644 --- a/include/core/SkFontStyle.h +++ b/include/core/SkFontStyle.h @@ -44,23 +44,23 @@ public: kOblique_Slant, }; - SkFontStyle(); + constexpr SkFontStyle(int weight, int width, Slant slant) : fValue( + (SkTPin(weight, kInvisible_Weight, kExtraBlack_Weight)) + + (SkTPin(width, kUltraCondensed_Width, kUltraExpanded_Width) << 16) + + (SkTPin(slant, kUpright_Slant, kOblique_Slant) << 24) + ) { } - constexpr SkFontStyle(int weight, int width, Slant slant) : fUnion {{ - static_cast(SkTPin(weight, kInvisible_Weight, kExtraBlack_Weight)), - static_cast(SkTPin(width, kUltraCondensed_Width, kUltraExpanded_Width)), - static_cast(SkTPin(slant, kUpright_Slant, kOblique_Slant)) - }} { } + constexpr SkFontStyle() : SkFontStyle{kNormal_Weight, kNormal_Width, kUpright_Slant} { } static SkFontStyle FromOldStyle(unsigned oldStyle); bool operator==(const SkFontStyle& rhs) const { - return fUnion.fU32 == rhs.fUnion.fU32; + return fValue == rhs.fValue; } - int weight() const { return fUnion.fR.fWeight; } - int width() const { return fUnion.fR.fWidth; } - Slant slant() const { return (Slant)fUnion.fR.fSlant; } + int weight() const { return fValue & 0xFFFF; } + int width() const { return (fValue >> 16) & 0xFF; } + Slant slant() const { return (Slant)((fValue >> 24) & 0xFF); } static constexpr SkFontStyle Normal() { return SkFontStyle(kNormal_Weight, kNormal_Width, kUpright_Slant); @@ -76,14 +76,7 @@ public: } private: - union { - struct { - uint16_t fWeight; // 100 .. 900 - uint8_t fWidth; // 1 .. 9 - uint8_t fSlant; // 0 .. 2 - } fR; - uint32_t fU32; - } fUnion; + uint32_t fValue; }; #endif -- cgit v1.2.3