Consolidate the determination code for font styles core/fxge/android/cfpf_skiafontmgr.cpp and xfa/fgas/font/cfgas_fontmgr.cpp utilise nearly common code for font style determination. This CL consolidates this logic into one file to reduce duplicate code. The function (in cfx_face.cpp) returns a struct FontStyleInfo with uint32_t values for font style and os2_codepage_mask which is equal to code_page_range.value()[0] for finding the charset in some callers. Bug: 452079357 Change-Id: I3de610c9201e68312fce72367eb142b7c37aad22 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/137430 Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/android/cfpf_skiafontmgr.cpp b/core/fxge/android/cfpf_skiafontmgr.cpp index ce21eb9..a326fe2 100644 --- a/core/fxge/android/cfpf_skiafontmgr.cpp +++ b/core/fxge/android/cfpf_skiafontmgr.cpp
@@ -409,36 +409,10 @@ std::unique_ptr<CFPF_SkiaPathFont> CFPF_SkiaFontMgr::ReportFace( RetainPtr<CFX_Face> face, const ByteString& file) { - uint32_t style = 0; - if (face->IsBold()) { - style |= pdfium::kFontStyleForceBold; - } - if (face->IsItalic()) { - style |= pdfium::kFontStyleItalic; - } - if (face->IsFixedWidth()) { - style |= pdfium::kFontStyleFixedPitch; - } - - uint32_t charset = SKIACHARSET_Default; - std::optional<std::array<uint32_t, 2>> code_page_range = - face->GetOs2CodePageRange(); - if (code_page_range.has_value()) { - if (code_page_range.value()[0] & (1 << 31)) { - style |= pdfium::kFontStyleSymbolic; - } - charset |= SkiaGetFaceCharset(code_page_range.value()[0]); - } - - std::optional<std::array<uint8_t, 2>> panose = face->GetOs2Panose(); - if (panose.has_value() && panose.value()[0] == 2) { - uint8_t serif = panose.value()[1]; - if ((serif > 1 && serif < 10) || serif > 13) { - style |= pdfium::kFontStyleSerif; - } - } - - return std::make_unique<CFPF_SkiaPathFont>(file, face->GetFamilyName(), style, - face->GetRec()->face_index, - charset, face->GetGlyphCount()); + CFX_Face::FontStyleInfo fontinfo = face->GetFontStyleInfo(); + uint32_t charset = + SKIACHARSET_Default | SkiaGetFaceCharset(fontinfo.os2_codepage_mask); + return std::make_unique<CFPF_SkiaPathFont>( + file, face->GetFamilyName(), fontinfo.style, face->GetRec()->face_index, + charset, face->GetGlyphCount()); }
diff --git a/core/fxge/cfx_face.cpp b/core/fxge/cfx_face.cpp index 56a4520..3b7fa70 100644 --- a/core/fxge/cfx_face.cpp +++ b/core/fxge/cfx_face.cpp
@@ -865,3 +865,33 @@ } FT_Set_MM_Design_Coordinates(rec, 2, coords); } + +CFX_Face::FontStyleInfo CFX_Face::GetFontStyleInfo() { + uint32_t style = 0; + if (IsBold()) { + style |= pdfium::kFontStyleForceBold; + } + if (IsItalic()) { + style |= pdfium::kFontStyleItalic; + } + if (IsFixedWidth()) { + style |= pdfium::kFontStyleFixedPitch; + } + + uint32_t os2_codepage_mask = 0; + std::optional<std::array<uint32_t, 2>> code_page_range = + GetOs2CodePageRange(); + if (code_page_range.has_value() && (code_page_range.value()[0] & (1 << 31))) { + style |= pdfium::kFontStyleSymbolic; + os2_codepage_mask = code_page_range.value()[0]; + } + + std::optional<std::array<uint8_t, 2>> panose = GetOs2Panose(); + if (panose.has_value() && panose.value()[0] == 2) { + uint8_t serif = panose.value()[1]; + if ((serif > 1 && serif < 10) || serif > 13) { + style |= pdfium::kFontStyleSerif; + } + } + return {style, os2_codepage_mask}; +}
diff --git a/core/fxge/cfx_face.h b/core/fxge/cfx_face.h index fc46f98..4c23eaa 100644 --- a/core/fxge/cfx_face.h +++ b/core/fxge/cfx_face.h
@@ -38,6 +38,12 @@ uint32_t glyph_index; }; + struct FontStyleInfo { + // Style utilizes enum FontStyle values + uint32_t style; + uint32_t os2_codepage_mask; + }; + // Note that this corresponds to the cmap header in fonts, and not the cmap // data in PDFs. struct CharMapId { @@ -76,6 +82,7 @@ bool IsItalic() const; bool IsBold() const; + FontStyleInfo GetFontStyleInfo(); ByteString GetFamilyName() const; ByteString GetStyleName() const; @@ -93,7 +100,6 @@ std::optional<std::array<uint32_t, 4>> GetOs2UnicodeRange(); std::optional<std::array<uint32_t, 2>> GetOs2CodePageRange(); - std::optional<std::array<uint8_t, 2>> GetOs2Panose(); int GetGlyphCount() const; // TODO(crbug.com/pdfium/2037): Can this method be private? @@ -147,6 +153,7 @@ ~CFX_Face() override; void AdjustVariationParams(int glyph_index, int dest_width, int weight); + std::optional<std::array<uint8_t, 2>> GetOs2Panose(); ScopedFXFTFaceRec const rec_; RetainPtr<Retainable> const desc_;
diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp index 74fc7c3..e5b486a 100644 --- a/xfa/fgas/font/cfgas_fontmgr.cpp +++ b/xfa/fgas/font/cfgas_fontmgr.cpp
@@ -487,31 +487,8 @@ } uint32_t GetFlags(const RetainPtr<CFX_Face>& face) { - uint32_t flags = 0; - if (face->IsBold()) { - flags |= pdfium::kFontStyleForceBold; - } - if (face->IsItalic()) { - flags |= pdfium::kFontStyleItalic; - } - if (face->IsFixedWidth()) { - flags |= pdfium::kFontStyleFixedPitch; - } - - std::optional<std::array<uint32_t, 2>> code_page_range = - face->GetOs2CodePageRange(); - if (code_page_range.has_value() && (code_page_range.value()[0] & (1 << 31))) { - flags |= pdfium::kFontStyleSymbolic; - } - - std::optional<std::array<uint8_t, 2>> panose = face->GetOs2Panose(); - if (panose.has_value() && panose.value()[0] == 2) { - uint8_t serif = panose.value()[1]; - if ((serif > 1 && serif < 10) || serif > 13) { - flags |= pdfium::kFontStyleSerif; - } - } - return flags; + CFX_Face::FontStyleInfo fontinfo = face->GetFontStyleInfo(); + return fontinfo.style; } RetainPtr<IFX_SeekableReadStream> CreateFontStream(CFX_FontMapper* font_mapper,