Add enum FontPitchFamily Replace a set of #defines with an enum. This avoids a potential conflict with #defines in public/fpdf_sysfontinfo.h. Add checks to make sure the enum values and the public #defines match. Simplify static_asserts() along the way. Bug: 42270078 Change-Id: I81dd0c6b0e5a58e8b308c1c5af35c2adbf937334 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/126894 Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Thomas Sepez <tsepez@google.com> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/cfx_folderfontinfo_unittest.cpp b/core/fxge/cfx_folderfontinfo_unittest.cpp index 9b67f57..9093c86 100644 --- a/core/fxge/cfx_folderfontinfo_unittest.cpp +++ b/core/fxge/cfx_folderfontinfo_unittest.cpp
@@ -74,61 +74,70 @@ TEST_F(CFXFolderFontInfoTest, TestFindFont) { // Find "Symbol" font void* font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol, - FXFONT_FF_ROMAN, kSymbol, /*bMatchName=*/true); + pdfium::kFontPitchFamilyRoman, kSymbol, + /*bMatchName=*/true); ASSERT_TRUE(font); EXPECT_EQ(GetFaceName(font), kSymbol); // Find "Calibri" font that is not present in the installed fonts EXPECT_FALSE(FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol, - FXFONT_FF_ROMAN, kCalibri, /*bMatchName=*/true)); + pdfium::kFontPitchFamilyRoman, kCalibri, + /*bMatchName=*/true)); // Find the closest matching font to "Bookshelf" font that is present in the // installed fonts font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol, - FXFONT_FF_ROMAN, kBookshelf, /*bMatchName=*/true); + pdfium::kFontPitchFamilyRoman, kBookshelf, + /*bMatchName=*/true); ASSERT_TRUE(font); EXPECT_EQ(GetFaceName(font), kBookshelfSymbol7); // Find "Book" font is expected to fail, because none of the installed fonts // is in the same font family. EXPECT_FALSE(FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol, - FXFONT_FF_ROMAN, kBook, /*bMatchName=*/true)); + pdfium::kFontPitchFamilyRoman, kBook, + /*bMatchName=*/true)); // Find the closest matching font for "Tofu" in the installed fonts, which // has "," following the string "Tofu". font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol, - FXFONT_FF_ROMAN, kTofu, /*bMatchName=*/true); + pdfium::kFontPitchFamilyRoman, kTofu, + /*bMatchName=*/true); ASSERT_TRUE(font); EXPECT_EQ(GetFaceName(font), kTofuBold); // Find the closest matching font for "Lato" in the installed fonts, which // has a space character following the string "Lato". font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kANSI, - FXFONT_FF_ROMAN, kLato, /*bMatchName=*/true); + pdfium::kFontPitchFamilyRoman, kLato, + /*bMatchName=*/true); ASSERT_TRUE(font); EXPECT_EQ(GetFaceName(font), kLatoUltraBold); // Find the closest matching font for "Oxygen" in the installed fonts, // which has "-" following the string "Oxygen". font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kANSI, - FXFONT_FF_ROMAN, kOxygen, /*bMatchName=*/true); + pdfium::kFontPitchFamilyRoman, kOxygen, + /*bMatchName=*/true); ASSERT_TRUE(font); EXPECT_EQ(GetFaceName(font), kOxygenSansSansBold); // Find the closest matching font for "Oxygen-Sans" in the installed fonts, // to test matching a family name with "-". font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kANSI, - FXFONT_FF_ROMAN, kOxygenSans, /*bMatchName=*/true); + pdfium::kFontPitchFamilyRoman, kOxygenSans, + /*bMatchName=*/true); ASSERT_TRUE(font); EXPECT_EQ(GetFaceName(font), kOxygenSansSansBold); // Find "Symbol" font when name matching is false font = FindFont(/*weight=*/0, /*bItalic=*/false, FX_Charset::kSymbol, - FXFONT_FF_ROMAN, kSymbol, /*bMatchName=*/false); + pdfium::kFontPitchFamilyRoman, kSymbol, + /*bMatchName=*/false); ASSERT_TRUE(font); EXPECT_EQ(GetFaceName(font), kBookshelfSymbol7); - font = FindFont(700, false, FX_Charset::kANSI, FXFONT_FF_FIXEDPITCH, + font = FindFont(700, false, FX_Charset::kANSI, pdfium::kFontPitchFamilyFixed, kComicSansMS, true); ASSERT_TRUE(font); EXPECT_EQ(GetFaceName(font), kComicSansMS);
diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp index 83e8c95..cf8c736 100644 --- a/core/fxge/cfx_fontmapper.cpp +++ b/core/fxge/cfx_fontmapper.cpp
@@ -287,7 +287,7 @@ bool CheckSupportThirdPartFont(const ByteString& name, int* pitch_family) { if (name != "MyriadPro") return false; - *pitch_family &= ~FXFONT_FF_ROMAN; + *pitch_family &= ~pdfium::kFontPitchFamilyRoman; return true; } @@ -304,21 +304,26 @@ } int GetPitchFamilyFromBaseFont(int base_font) { - if (base_font < 4) - return FXFONT_FF_FIXEDPITCH; - if (base_font >= 8) - return FXFONT_FF_ROMAN; + if (base_font < 4) { + return pdfium::kFontPitchFamilyFixed; + } + if (base_font >= 8) { + return pdfium::kFontPitchFamilyRoman; + } return 0; } int GetPitchFamilyFromFlags(uint32_t flags) { int pitch_family = 0; - if (FontStyleIsSerif(flags)) - pitch_family |= FXFONT_FF_ROMAN; - if (FontStyleIsScript(flags)) - pitch_family |= FXFONT_FF_SCRIPT; - if (FontStyleIsFixedPitch(flags)) - pitch_family |= FXFONT_FF_FIXEDPITCH; + if (FontStyleIsSerif(flags)) { + pitch_family |= pdfium::kFontPitchFamilyRoman; + } + if (FontStyleIsScript(flags)) { + pitch_family |= pdfium::kFontPitchFamilyScript; + } + if (FontStyleIsFixedPitch(flags)) { + pitch_family |= pdfium::kFontPitchFamilyFixed; + } return pitch_family; }
diff --git a/core/fxge/fx_font.h b/core/fxge/fx_font.h index e892731..bf7a10d 100644 --- a/core/fxge/fx_font.h +++ b/core/fxge/fx_font.h
@@ -17,6 +17,13 @@ namespace pdfium { +// Font pitch and family flags. +enum FontPitchFamily { + kFontPitchFamilyFixed = 1 << 0, + kFontPitchFamilyRoman = 1 << 4, + kFontPitchFamilyScript = 1 << 6, +}; + // Defined in ISO 32000-1:2008 spec, table 123. // Defined in ISO 32000-2:2020 spec, table 121. enum FontStyle { @@ -42,11 +49,6 @@ } // namespace pdfium -/* Font pitch and family flags */ -#define FXFONT_FF_FIXEDPITCH (1 << 0) -#define FXFONT_FF_ROMAN (1 << 4) -#define FXFONT_FF_SCRIPT (4 << 4) - /* Other font flags */ #define FXFONT_USEEXTERNATTR 0x80000 @@ -94,13 +96,13 @@ } inline bool FontFamilyIsFixedPitch(uint32_t family) { - return !!(family & FXFONT_FF_FIXEDPITCH); + return !!(family & pdfium::kFontPitchFamilyFixed); } inline bool FontFamilyIsRoman(uint32_t family) { - return !!(family & FXFONT_FF_ROMAN); + return !!(family & pdfium::kFontPitchFamilyRoman); } inline bool FontFamilyIsScript(int32_t family) { - return !!(family & FXFONT_FF_SCRIPT); + return !!(family & pdfium::kFontPitchFamilyScript); } wchar_t UnicodeFromAdobeName(const char* name);
diff --git a/fpdfsdk/fpdf_sysfontinfo.cpp b/fpdfsdk/fpdf_sysfontinfo.cpp index 15b9845..6aca358 100644 --- a/fpdfsdk/fpdf_sysfontinfo.cpp +++ b/fpdfsdk/fpdf_sysfontinfo.cpp
@@ -29,51 +29,37 @@ #include "xfa/fgas/font/cfgas_gemodule.h" #endif -static_assert(FXFONT_ANSI_CHARSET == static_cast<int>(FX_Charset::kANSI), - "Charset must match"); -static_assert(FXFONT_DEFAULT_CHARSET == static_cast<int>(FX_Charset::kDefault), - "Charset must match"); -static_assert(FXFONT_SYMBOL_CHARSET == static_cast<int>(FX_Charset::kSymbol), - "Charset must match"); +static_assert(FXFONT_ANSI_CHARSET == static_cast<int>(FX_Charset::kANSI)); +static_assert(FXFONT_DEFAULT_CHARSET == static_cast<int>(FX_Charset::kDefault)); +static_assert(FXFONT_SYMBOL_CHARSET == static_cast<int>(FX_Charset::kSymbol)); static_assert(FXFONT_SHIFTJIS_CHARSET == - static_cast<int>(FX_Charset::kShiftJIS), - "Charset must match"); -static_assert(FXFONT_HANGEUL_CHARSET == static_cast<int>(FX_Charset::kHangul), - "Charset must match"); + static_cast<int>(FX_Charset::kShiftJIS)); +static_assert(FXFONT_HANGEUL_CHARSET == static_cast<int>(FX_Charset::kHangul)); static_assert(FXFONT_GB2312_CHARSET == - static_cast<int>(FX_Charset::kChineseSimplified), - "Charset must match"); + static_cast<int>(FX_Charset::kChineseSimplified)); static_assert(FXFONT_CHINESEBIG5_CHARSET == - static_cast<int>(FX_Charset::kChineseTraditional), - "Charset must match"); + static_cast<int>(FX_Charset::kChineseTraditional)); static_assert(FXFONT_GREEK_CHARSET == - static_cast<int>(FX_Charset::kMSWin_Greek), - "Charset must match"); + static_cast<int>(FX_Charset::kMSWin_Greek)); static_assert(FXFONT_VIETNAMESE_CHARSET == - static_cast<int>(FX_Charset::kMSWin_Vietnamese), - "Charset must match"); + static_cast<int>(FX_Charset::kMSWin_Vietnamese)); static_assert(FXFONT_HEBREW_CHARSET == - static_cast<int>(FX_Charset::kMSWin_Hebrew), - "Charset must match"); + static_cast<int>(FX_Charset::kMSWin_Hebrew)); static_assert(FXFONT_ARABIC_CHARSET == - static_cast<int>(FX_Charset::kMSWin_Arabic), - "Charset must match"); + static_cast<int>(FX_Charset::kMSWin_Arabic)); static_assert(FXFONT_CYRILLIC_CHARSET == - static_cast<int>(FX_Charset::kMSWin_Cyrillic), - "Charset must match"); -static_assert(FXFONT_THAI_CHARSET == static_cast<int>(FX_Charset::kThai), - "Charset must match"); + static_cast<int>(FX_Charset::kMSWin_Cyrillic)); +static_assert(FXFONT_THAI_CHARSET == static_cast<int>(FX_Charset::kThai)); static_assert(FXFONT_EASTERNEUROPEAN_CHARSET == - static_cast<int>(FX_Charset::kMSWin_EasternEuropean), - "Charset must match"); + static_cast<int>(FX_Charset::kMSWin_EasternEuropean)); +static_assert(pdfium::kFontPitchFamilyFixed == FXFONT_FF_FIXEDPITCH); +static_assert(pdfium::kFontPitchFamilyRoman == FXFONT_FF_ROMAN); +static_assert(pdfium::kFontPitchFamilyScript == FXFONT_FF_SCRIPT); static_assert(offsetof(CFX_Font::CharsetFontMap, charset) == - offsetof(FPDF_CharsetFontMap, charset), - "CFX_Font::CharsetFontMap must be same as FPDF_CharsetFontMap"); + offsetof(FPDF_CharsetFontMap, charset)); static_assert(offsetof(CFX_Font::CharsetFontMap, fontname) == - offsetof(FPDF_CharsetFontMap, fontname), - "CFX_Font::CharsetFontMap must be same as FPDF_CharsetFontMap"); -static_assert(sizeof(CFX_Font::CharsetFontMap) == sizeof(FPDF_CharsetFontMap), - "CFX_Font::CharsetFontMap must be same as FPDF_CharsetFontMap"); + offsetof(FPDF_CharsetFontMap, fontname)); +static_assert(sizeof(CFX_Font::CharsetFontMap) == sizeof(FPDF_CharsetFontMap)); static_assert(FXFONT_FW_NORMAL == pdfium::kFontWeightNormal); static_assert(FXFONT_FW_BOLD == pdfium::kFontWeightBold);