Update typeface serialization style.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2030683002

Committed: https://skia.googlesource.com/skia/+/7ae1c72e593823e6e300e4199558555765bcec17
Review-Url: https://codereview.chromium.org/2030683002
diff --git a/include/core/SkPicture.h b/include/core/SkPicture.h
index 8e3d1c8..75fe66d 100644
--- a/include/core/SkPicture.h
+++ b/include/core/SkPicture.h
@@ -220,7 +220,10 @@
 
     static_assert(MIN_PICTURE_VERSION <= 43,
                   "Remove SkBitmapSourceDeserializer.");
-    
+
+    static_assert(MIN_PICTURE_VERSION <= 45,
+                  "Remove decoding of old SkTypeface::Style from SkFontDescriptor.cpp.");
+
     static bool IsValidPictInfo(const SkPictInfo& info);
     static sk_sp<SkPicture> Forwardport(const SkPictInfo&,
                                         const SkPictureData*,
diff --git a/src/core/SkFontDescriptor.cpp b/src/core/SkFontDescriptor.cpp
index 69fdc15..85629ef 100644
--- a/src/core/SkFontDescriptor.cpp
+++ b/src/core/SkFontDescriptor.cpp
@@ -23,7 +23,7 @@
     kSentinel       = 0xFF,
 };
 
-SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) : fStyle(style) { }
+SkFontDescriptor::SkFontDescriptor() { }
 
 static void read_string(SkStream* stream, SkString* string) {
     const uint32_t length = SkToU32(stream->readPackedUInt());
@@ -59,7 +59,15 @@
 }
 
 bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
-    result->fStyle = (SkTypeface::Style)stream->readPackedUInt();
+    size_t styleBits = stream->readPackedUInt();
+    if (styleBits <= 2) {
+        // Remove this branch when MIN_PICTURE_VERSION > 45
+        result->fStyle = SkFontStyle::FromOldStyle(styleBits);
+    } else {
+        result->fStyle = SkFontStyle((styleBits >> 16) & 0xFFFF,
+                                     (styleBits >> 8 ) & 0xFF,
+                                     static_cast<SkFontStyle::Slant>(styleBits & 0xFF));
+    }
 
     SkAutoSTMalloc<4, SkFixed> axis;
     size_t axisCount = 0;
@@ -109,7 +117,8 @@
 }
 
 void SkFontDescriptor::serialize(SkWStream* stream) {
-    stream->writePackedUInt(fStyle);
+    uint32_t styleBits = (fStyle.weight() << 16) | (fStyle.width() << 8) | (fStyle.slant());
+    stream->writePackedUInt(styleBits);
 
     write_string(stream, fFamilyName, kFontFamilyName);
     write_string(stream, fFullName, kFullName);
diff --git a/src/core/SkFontDescriptor.h b/src/core/SkFontDescriptor.h
index 9b87365..cb8d2f4 100644
--- a/src/core/SkFontDescriptor.h
+++ b/src/core/SkFontDescriptor.h
@@ -50,14 +50,14 @@
 
 class SkFontDescriptor : SkNoncopyable {
 public:
-    SkFontDescriptor(SkTypeface::Style = SkTypeface::kNormal);
+    SkFontDescriptor();
     // Does not affect ownership of SkStream.
     static bool Deserialize(SkStream*, SkFontDescriptor* result);
 
     void serialize(SkWStream*);
 
-    SkTypeface::Style getStyle() { return fStyle; }
-    void setStyle(SkTypeface::Style style) { fStyle = style; }
+    SkFontStyle getStyle() { return fStyle; }
+    void setStyle(SkFontStyle style) { fStyle = style; }
 
     const char* getFamilyName() const { return fFamilyName.c_str(); }
     const char* getFullName() const { return fFullName.c_str(); }
@@ -78,7 +78,7 @@
     SkString fPostscriptName;
     SkAutoTDelete<SkFontData> fFontData;
 
-    SkTypeface::Style fStyle;
+    SkFontStyle fStyle;
 };
 
 #endif // SkFontDescriptor_DEFINED
diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp
index 4b0dd08..24e1a67 100644
--- a/src/core/SkTypeface.cpp
+++ b/src/core/SkTypeface.cpp
@@ -168,7 +168,7 @@
         return;
     }
     bool isLocal = false;
-    SkFontDescriptor desc(this->style());
+    SkFontDescriptor desc;
     this->onGetFontDescriptor(&desc, &isLocal);
 
     // Embed font data if it's a local font.
@@ -196,8 +196,7 @@
         }
     }
 
-    return SkTypeface::MakeFromName(desc.getFamilyName(),
-                                    SkFontStyle::FromOldStyle(desc.getStyle()));
+    return SkTypeface::MakeFromName(desc.getFamilyName(), desc.getStyle());
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/fonts/SkFontMgr_fontconfig.cpp b/src/fonts/SkFontMgr_fontconfig.cpp
index 9376964..09b4d13 100644
--- a/src/fonts/SkFontMgr_fontconfig.cpp
+++ b/src/fonts/SkFontMgr_fontconfig.cpp
@@ -31,6 +31,7 @@
     SkString name;
     this->getFamilyName(&name);
     desc->setFamilyName(name.c_str());
+    desc->setStyle(this->fontStyle());
     *isLocalStream = SkToBool(this->getLocalStream());
 }
 
diff --git a/src/fonts/SkTestScalerContext.cpp b/src/fonts/SkTestScalerContext.cpp
index bab6b89..fcb65a8 100644
--- a/src/fonts/SkTestScalerContext.cpp
+++ b/src/fonts/SkTestScalerContext.cpp
@@ -156,6 +156,7 @@
 
 void SkTestTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
     desc->setFamilyName(fTestFont->fName);
+    desc->setStyle(this->fontStyle());
     *isLocal = false;
 }
 
diff --git a/src/ports/SkFontHost_mac.cpp b/src/ports/SkFontHost_mac.cpp
index 41cb52b..d95ebbb 100644
--- a/src/ports/SkFontHost_mac.cpp
+++ b/src/ports/SkFontHost_mac.cpp
@@ -2087,6 +2087,7 @@
     desc->setFamilyName(get_str(CTFontCopyFamilyName(fFontRef), &tmpStr));
     desc->setFullName(get_str(CTFontCopyFullName(fFontRef), &tmpStr));
     desc->setPostscriptName(get_str(CTFontCopyPostScriptName(fFontRef), &tmpStr));
+    desc->setStyle(this->fontStyle());
     *isLocalStream = fIsLocalStream;
 }
 
diff --git a/src/ports/SkFontHost_win.cpp b/src/ports/SkFontHost_win.cpp
index 226f219..aa6a3ce 100644
--- a/src/ports/SkFontHost_win.cpp
+++ b/src/ports/SkFontHost_win.cpp
@@ -1714,6 +1714,7 @@
     SkString familyName;
     this->onGetFamilyName(&familyName);
     desc->setFamilyName(familyName.c_str());
+    desc->setStyle(this->fontStyle());
     *isLocalStream = this->fSerializeAsStream;
 }
 
diff --git a/src/ports/SkFontMgr_android.cpp b/src/ports/SkFontMgr_android.cpp
index a8d5b12..23c950d 100644
--- a/src/ports/SkFontMgr_android.cpp
+++ b/src/ports/SkFontMgr_android.cpp
@@ -85,6 +85,7 @@
         SkASSERT(desc);
         SkASSERT(serialize);
         desc->setFamilyName(fFamilyName.c_str());
+        desc->setStyle(this->fontStyle());
         *serialize = false;
     }
     SkStreamAsset* onOpenStream(int* ttcIndex) const override {
diff --git a/src/ports/SkFontMgr_custom.cpp b/src/ports/SkFontMgr_custom.cpp
index a05cc9b..6588766 100644
--- a/src/ports/SkFontMgr_custom.cpp
+++ b/src/ports/SkFontMgr_custom.cpp
@@ -43,6 +43,7 @@
 
     void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override {
         desc->setFamilyName(fFamilyName.c_str());
+        desc->setStyle(this->fontStyle());
         *isLocal = !this->isSysFont();
     }
 
diff --git a/src/ports/SkFontMgr_fontconfig.cpp b/src/ports/SkFontMgr_fontconfig.cpp
index d4d8737..b5c31c7 100644
--- a/src/ports/SkFontMgr_fontconfig.cpp
+++ b/src/ports/SkFontMgr_fontconfig.cpp
@@ -449,6 +449,7 @@
         desc->setFamilyName(get_string(fPattern, FC_FAMILY));
         desc->setFullName(get_string(fPattern, FC_FULLNAME));
         desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME));
+        desc->setStyle(this->fontStyle());
         *serialize = false;
     }
 
diff --git a/src/ports/SkTypeface_win_dw.cpp b/src/ports/SkTypeface_win_dw.cpp
index a12438c..11e5727 100644
--- a/src/ports/SkTypeface_win_dw.cpp
+++ b/src/ports/SkTypeface_win_dw.cpp
@@ -45,6 +45,7 @@
     sk_get_locale_string(familyNames.get(), nullptr/*fMgr->fLocaleName.get()*/, &utf8FamilyName);
 
     desc->setFamilyName(utf8FamilyName.c_str());
+    desc->setStyle(this->fontStyle());
     *isLocalStream = SkToBool(fDWriteFontFileLoader.get());
 }
 
diff --git a/src/utils/SkWhitelistTypefaces.cpp b/src/utils/SkWhitelistTypefaces.cpp
index bc454bf..2912fe2 100644
--- a/src/utils/SkWhitelistTypefaces.cpp
+++ b/src/utils/SkWhitelistTypefaces.cpp
@@ -83,12 +83,13 @@
     return SkChecksum::Murmur3(data.begin(), length);
 }
 
-static void serialize_sub(const char* fontName, SkTypeface::Style style, SkWStream* wstream) {
-    SkFontDescriptor desc(style);
+static void serialize_sub(const char* fontName, SkFontStyle style, SkWStream* wstream) {
+    SkFontDescriptor desc;
     SkString subName(SUBNAME_PREFIX);
     subName.append(fontName);
     const char* familyName = subName.c_str();
     desc.setFamilyName(familyName);
+    desc.setStyle(style);
     desc.serialize(wstream);
 #if WHITELIST_DEBUG
     for (int i = 0; i < whitelistCount; ++i) {
@@ -105,14 +106,14 @@
 
 static bool is_local(const SkTypeface* tf) {
     bool isLocal = false;
-    SkFontDescriptor desc(tf->style());
+    SkFontDescriptor desc;
     tf->getFontDescriptor(&desc, &isLocal);
     return isLocal;
 }
 
 static void serialize_full(const SkTypeface* tf, SkWStream* wstream) {
     bool isLocal = false;
-    SkFontDescriptor desc(tf->style());
+    SkFontDescriptor desc;
     tf->getFontDescriptor(&desc, &isLocal);
 
     // Embed font data if it's a local font.
@@ -124,7 +125,7 @@
 
 static void serialize_name_only(const SkTypeface* tf, SkWStream* wstream) {
     bool isLocal = false;
-    SkFontDescriptor desc(tf->style());
+    SkFontDescriptor desc;
     tf->getFontDescriptor(&desc, &isLocal);
     SkASSERT(!isLocal);
 #if WHITELIST_DEBUG
@@ -180,7 +181,7 @@
 #endif
         whitelist[whitelistIndex].fChecksum = checksum;
     }
-    serialize_sub(fontName, tf->style(), wstream);
+    serialize_sub(fontName, tf->fontStyle(), wstream);
 }
 
 sk_sp<SkTypeface> WhitelistDeserializeTypeface(SkStream* stream) {
@@ -200,7 +201,7 @@
     if (!strncmp(SUBNAME_PREFIX, familyName, sizeof(SUBNAME_PREFIX) - 1)) {
         familyName += sizeof(SUBNAME_PREFIX) - 1;
     }
-    return SkTypeface::MakeFromName(familyName, SkFontStyle::FromOldStyle(desc.getStyle()));
+    return SkTypeface::MakeFromName(familyName, desc.getStyle());
 }
 
 bool CheckChecksums() {