Add enum FontWeight
Replace #defines in core/fxge/fx_font.h with an enum, both to modernize
the code, and to avoid confusion with FXFONT_FW_FOO #defines in
public/fpdf_sysfontinfo.h.
Also add the kExtraLight value and use it in place of the raw integer
value.
Bug: 42270078
Change-Id: I0edcd4eb86127060ce4001bdce8a9f609dfba33f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/126931
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Thomas Sepez <tsepez@google.com>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 5b22355..2ecb0c4 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -810,12 +810,12 @@
}
void CPDF_CIDFont::LoadSubstFont() {
- FX_SAFE_INT32 safeStemV(m_StemV);
- safeStemV *= 5;
- m_Font.LoadSubst(m_BaseFontName, m_FontType == CIDFontType::kTrueType,
- m_Flags, safeStemV.ValueOrDefault(FXFONT_FW_NORMAL),
- m_ItalicAngle, kCharsetCodePages[m_Charset],
- IsVertWriting());
+ FX_SAFE_INT32 safe_stem_v(m_StemV);
+ safe_stem_v *= 5;
+ m_Font.LoadSubst(
+ m_BaseFontName, m_FontType == CIDFontType::kTrueType, m_Flags,
+ safe_stem_v.ValueOrDefault(pdfium::kFontWeightNormal), m_ItalicAngle,
+ kCharsetCodePages[m_Charset], IsVertWriting());
}
// static
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 0ceefeb..5debeb3 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -372,12 +372,12 @@
uint32_t CPDF_Font::FallbackFontFromCharcode(uint32_t charcode) {
if (m_FontFallbacks.empty()) {
m_FontFallbacks.push_back(std::make_unique<CFX_Font>());
- FX_SAFE_INT32 safeWeight = m_StemV;
- safeWeight *= 5;
- m_FontFallbacks[0]->LoadSubst("Arial", IsTrueTypeFont(), m_Flags,
- safeWeight.ValueOrDefault(FXFONT_FW_NORMAL),
- m_ItalicAngle, FX_CodePage::kDefANSI,
- IsVertWriting());
+ FX_SAFE_INT32 safe_weight = m_StemV;
+ safe_weight *= 5;
+ m_FontFallbacks[0]->LoadSubst(
+ "Arial", IsTrueTypeFont(), m_Flags,
+ safe_weight.ValueOrDefault(pdfium::kFontWeightNormal), m_ItalicAngle,
+ FX_CodePage::kDefANSI, IsVertWriting());
}
return 0;
}
diff --git a/core/fpdfapi/font/cpdf_simplefont.cpp b/core/fpdfapi/font/cpdf_simplefont.cpp
index a7e0642..ce99662 100644
--- a/core/fpdfapi/font/cpdf_simplefont.cpp
+++ b/core/fpdfapi/font/cpdf_simplefont.cpp
@@ -284,9 +284,10 @@
}
}
- int weight = GetFontWeight().value_or(FXFONT_FW_NORMAL);
- if (weight < 100 || weight > FXFONT_FW_BOLD_BOLD) {
- weight = FXFONT_FW_NORMAL;
+ int weight = GetFontWeight().value_or(pdfium::kFontWeightNormal);
+ if (weight < pdfium::kFontWeightExtraLight ||
+ weight > pdfium::kFontWeightExtraBold) {
+ weight = pdfium::kFontWeightNormal;
}
m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags, weight,
m_ItalicAngle, FX_CodePage::kDefANSI, /*bVertical=*/false);
diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp
index 274ca34..d1d3103 100644
--- a/core/fxge/cfx_font.cpp
+++ b/core/fxge/cfx_font.cpp
@@ -370,7 +370,8 @@
#if defined(PDF_USE_SKIA)
bool CFX_Font::IsSubstFontBold() const {
CFX_SubstFont* subst_font = GetSubstFont();
- return subst_font && subst_font->GetOriginalWeight() >= FXFONT_FW_BOLD;
+ return subst_font &&
+ subst_font->GetOriginalWeight() >= pdfium::kFontWeightBold;
}
#endif
diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp
index c81271a..fbf7084 100644
--- a/core/fxge/cfx_fontmapper.cpp
+++ b/core/fxge/cfx_fontmapper.cpp
@@ -262,9 +262,9 @@
if (FontStyleIsForceBold(parsed_style)) {
// If we're already bold, then we're double bold, use special weight.
if (FontStyleIsForceBold(*style)) {
- *weight = FXFONT_FW_BOLD_BOLD;
+ *weight = pdfium::kFontWeightExtraBold;
} else {
- *weight = FXFONT_FW_BOLD;
+ *weight = pdfium::kFontWeightBold;
*style |= FXFONT_FORCE_BOLD;
}
@@ -552,7 +552,8 @@
subst_font->m_Family = face_name;
subst_font->m_Charset = charset;
- int face_weight = face->IsBold() ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL;
+ int face_weight =
+ face->IsBold() ? pdfium::kFontWeightBold : pdfium::kFontWeightNormal;
if (weight != face_weight)
subst_font->m_Weight = weight;
if (is_italic && !face->IsItalic()) {
@@ -572,11 +573,12 @@
int italic_angle,
FX_CodePage code_page,
CFX_SubstFont* subst_font) {
- if (weight == 0)
- weight = FXFONT_FW_NORMAL;
+ if (weight == 0) {
+ weight = pdfium::kFontWeightNormal;
+ }
if (!(flags & FXFONT_USEEXTERNATTR)) {
- weight = FXFONT_FW_NORMAL;
+ weight = pdfium::kFontWeightNormal;
italic_angle = 0;
}
const ByteString subst_name = GetSubstName(name, is_truetype);
@@ -640,8 +642,9 @@
}
const int old_weight = weight;
- if (FontStyleIsForceBold(nStyle))
- weight = FXFONT_FW_BOLD;
+ if (FontStyleIsForceBold(nStyle)) {
+ weight = pdfium::kFontWeightBold;
+ }
if (ParseStyles(style, &is_style_available, &weight, &nStyle)) {
family = subst_name;
@@ -673,19 +676,23 @@
is_italic = italic_angle != 0;
weight = old_weight;
}
- if (IsNarrowFontName(subst_name))
+ if (IsNarrowFontName(subst_name)) {
family = kNarrowFamily;
+ }
} else {
subst_font->m_bSubstCJK = true;
- if (nStyle)
- subst_font->m_WeightCJK = nStyle ? weight : FXFONT_FW_NORMAL;
- if (FontStyleIsItalic(nStyle))
+ if (nStyle) {
+ subst_font->m_WeightCJK = nStyle ? weight : pdfium::kFontWeightNormal;
+ }
+ if (FontStyleIsItalic(nStyle)) {
subst_font->m_bItalicCJK = true;
+ }
}
} else {
italic_angle = 0;
- if (nStyle == FXFONT_NORMAL)
- weight = FXFONT_FW_NORMAL;
+ if (nStyle == FXFONT_NORMAL) {
+ weight = pdfium::kFontWeightNormal;
+ }
}
if (!match.IsEmpty() || base_font < kNumStandardFonts) {
diff --git a/core/fxge/fx_font.h b/core/fxge/fx_font.h
index d2ef744..7d871f2 100644
--- a/core/fxge/fx_font.h
+++ b/core/fxge/fx_font.h
@@ -15,16 +15,23 @@
#include "core/fxcrt/fx_coordinates.h"
#include "core/fxcrt/span.h"
+namespace pdfium {
+
+// Font weight values that are in use.
+enum FontWeight {
+ kFontWeightExtraLight = 100,
+ kFontWeightNormal = 400,
+ kFontWeightBold = 700,
+ kFontWeightExtraBold = 900,
+};
+
+} // 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)
-/* Typical weight values */
-#define FXFONT_FW_NORMAL 400
-#define FXFONT_FW_BOLD 700
-#define FXFONT_FW_BOLD_BOLD 900
-
/* Font styles as defined in PDF 1.7 Table 5.20 */
#define FXFONT_NORMAL (0)
#define FXFONT_FIXED_PITCH (1 << 0)
diff --git a/fpdfsdk/fpdf_sysfontinfo.cpp b/fpdfsdk/fpdf_sysfontinfo.cpp
index 04c3da0..15b9845 100644
--- a/fpdfsdk/fpdf_sysfontinfo.cpp
+++ b/fpdfsdk/fpdf_sysfontinfo.cpp
@@ -75,6 +75,9 @@
static_assert(sizeof(CFX_Font::CharsetFontMap) == sizeof(FPDF_CharsetFontMap),
"CFX_Font::CharsetFontMap must be same as FPDF_CharsetFontMap");
+static_assert(FXFONT_FW_NORMAL == pdfium::kFontWeightNormal);
+static_assert(FXFONT_FW_BOLD == pdfium::kFontWeightBold);
+
class CFX_ExternalFontInfo final : public SystemFontInfoIface {
public:
explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {}
diff --git a/testing/fuzzers/pdf_bidi_fuzzer.cc b/testing/fuzzers/pdf_bidi_fuzzer.cc
index aeae3db..7db15d7 100644
--- a/testing/fuzzers/pdf_bidi_fuzzer.cc
+++ b/testing/fuzzers/pdf_bidi_fuzzer.cc
@@ -20,8 +20,8 @@
return 0;
auto font = std::make_unique<CFX_Font>();
- font->LoadSubst("Arial", true, 0, FXFONT_FW_NORMAL, 0, FX_CodePage::kDefANSI,
- false);
+ font->LoadSubst("Arial", true, 0, pdfium::kFontWeightNormal, 0,
+ FX_CodePage::kDefANSI, false);
assert(font);
CFGAS_RTFBreak rtf_break(CFGAS_Break::LayoutStyle::kExpandTab);
diff --git a/xfa/fgas/font/cfgas_gefont.cpp b/xfa/fgas/font/cfgas_gefont.cpp
index 8ed130e..13f3542 100644
--- a/xfa/fgas/font/cfgas_gefont.cpp
+++ b/xfa/fgas/font/cfgas_gefont.cpp
@@ -77,8 +77,9 @@
if (pszFontFamily)
csFontFamily = WideString(pszFontFamily).ToDefANSI();
- int32_t iWeight =
- FontStyleIsForceBold(dwFontStyles) ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL;
+ int32_t iWeight = FontStyleIsForceBold(dwFontStyles)
+ ? pdfium::kFontWeightBold
+ : pdfium::kFontWeightNormal;
m_pFont = std::make_unique<CFX_Font>();
if (FontStyleIsItalic(dwFontStyles) && FontStyleIsForceBold(dwFontStyles))
csFontFamily += ",BoldItalic";
@@ -139,13 +140,16 @@
uint32_t dwStyles = 0;
auto* pSubstFont = m_pFont->GetSubstFont();
if (pSubstFont) {
- if (pSubstFont->m_Weight == FXFONT_FW_BOLD)
+ if (pSubstFont->m_Weight == pdfium::kFontWeightBold) {
dwStyles |= FXFONT_FORCE_BOLD;
+ }
} else {
- if (m_pFont->IsBold())
+ if (m_pFont->IsBold()) {
dwStyles |= FXFONT_FORCE_BOLD;
- if (m_pFont->IsItalic())
+ }
+ if (m_pFont->IsItalic()) {
dwStyles |= FXFONT_ITALIC;
+ }
}
return dwStyles;
}
diff --git a/xfa/fxfa/cxfa_textparser.cpp b/xfa/fxfa/cxfa_textparser.cpp
index 9abfdca..1c1b8d2 100644
--- a/xfa/fxfa/cxfa_textparser.cpp
+++ b/xfa/fxfa/cxfa_textparser.cpp
@@ -151,7 +151,8 @@
pStyle->SetColor(font->GetColor());
pStyle->SetFontStyle(font->IsItalic() ? CFX_CSSFontStyle::Italic
: CFX_CSSFontStyle::Normal);
- pStyle->SetFontWeight(font->IsBold() ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL);
+ pStyle->SetFontWeight(font->IsBold() ? pdfium::kFontWeightBold
+ : pdfium::kFontWeightNormal);
pStyle->SetNumberVerticalAlign(-font->GetBaselineShift());
fFontSize = font->GetFontSize();
CFX_CSSLength letterSpacing;
@@ -351,14 +352,17 @@
if (pStyle) {
std::optional<WideString> last_family = pStyle->GetLastFontFamily();
- if (last_family.has_value())
+ if (last_family.has_value()) {
wsFamily = last_family.value();
+ }
dwStyle = 0;
- if (pStyle->GetFontWeight() > FXFONT_FW_NORMAL)
+ if (pStyle->GetFontWeight() > pdfium::kFontWeightNormal) {
dwStyle |= FXFONT_FORCE_BOLD;
- if (pStyle->GetFontStyle() == CFX_CSSFontStyle::Italic)
+ }
+ if (pStyle->GetFontStyle() == CFX_CSSFontStyle::Italic) {
dwStyle |= FXFONT_ITALIC;
+ }
}
CXFA_FontMgr* pFontMgr = doc->GetApp()->GetXFAFontMgr();