From 4c22dd5690cdec725389055bb7c07c300a4b6fe4 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 3 Apr 2018 19:10:51 +0000 Subject: Use Optional to determine if appearance stream as font This CL changes the CPDF_DefaultAppearance code to remove the HasFont method and change GetFont to return an Optional. This forces all the call sites to verify a font was returned correctly and removes the need for the duplicate appearance stream parsing. Bug: chromium:827430 Change-Id: If09e0a7d3f7dd63ad77b97a5a388127e4a02da61 Reviewed-on: https://pdfium-review.googlesource.com/29610 Commit-Queue: Ryan Harrison Reviewed-by: Ryan Harrison --- core/fpdfdoc/cpdf_defaultappearance.cpp | 14 +++----------- core/fpdfdoc/cpdf_defaultappearance.h | 3 +-- core/fpdfdoc/cpdf_formcontrol.cpp | 10 +++++----- core/fpdfdoc/cpdf_formfield.cpp | 6 +++--- core/fpdfdoc/cpvt_generateap.cpp | 10 +++++----- fpdfsdk/formfiller/cba_fontmap.cpp | 12 +++++++----- fpdfsdk/pwl/cpwl_appstream.cpp | 10 +++++++--- 7 files changed, 31 insertions(+), 34 deletions(-) diff --git a/core/fpdfdoc/cpdf_defaultappearance.cpp b/core/fpdfdoc/cpdf_defaultappearance.cpp index add1c26498..161f90e014 100644 --- a/core/fpdfdoc/cpdf_defaultappearance.cpp +++ b/core/fpdfdoc/cpdf_defaultappearance.cpp @@ -53,18 +53,10 @@ bool FindTagParamFromStart(CPDF_SimpleParser* parser, } // namespace -bool CPDF_DefaultAppearance::HasFont() { - if (m_csDA.IsEmpty()) - return false; - - CPDF_SimpleParser syntax(m_csDA.AsStringView()); - return FindTagParamFromStart(&syntax, "Tf", 2); -} - -ByteString CPDF_DefaultAppearance::GetFont(float* fFontSize) { +Optional CPDF_DefaultAppearance::GetFont(float* fFontSize) { *fFontSize = 0.0f; if (m_csDA.IsEmpty()) - return ByteString(); + return {}; ByteString csFontNameTag; CPDF_SimpleParser syntax(m_csDA.AsStringView()); @@ -73,7 +65,7 @@ ByteString CPDF_DefaultAppearance::GetFont(float* fFontSize) { csFontNameTag.Delete(0, 1); *fFontSize = FX_atof(syntax.GetWord()); } - return PDF_NameDecode(csFontNameTag.AsStringView()); + return {PDF_NameDecode(csFontNameTag.AsStringView())}; } bool CPDF_DefaultAppearance::HasColor() { diff --git a/core/fpdfdoc/cpdf_defaultappearance.h b/core/fpdfdoc/cpdf_defaultappearance.h index 79578930e1..ae3c4f5517 100644 --- a/core/fpdfdoc/cpdf_defaultappearance.h +++ b/core/fpdfdoc/cpdf_defaultappearance.h @@ -25,8 +25,7 @@ class CPDF_DefaultAppearance { CPDF_DefaultAppearance(const CPDF_DefaultAppearance& cDA) : m_csDA(cDA.m_csDA) {} - bool HasFont(); - ByteString GetFont(float* fFontSize); + Optional GetFont(float* fFontSize); bool HasColor(); CFX_Color::Type GetColor(float fc[4]); diff --git a/core/fpdfdoc/cpdf_formcontrol.cpp b/core/fpdfdoc/cpdf_formcontrol.cpp index 384c35ea12..a78a24db9d 100644 --- a/core/fpdfdoc/cpdf_formcontrol.cpp +++ b/core/fpdfdoc/cpdf_formcontrol.cpp @@ -277,15 +277,15 @@ CPDF_DefaultAppearance CPDF_FormControl::GetDefaultAppearance() { CPDF_Font* CPDF_FormControl::GetDefaultControlFont() { float fFontSize; CPDF_DefaultAppearance cDA = GetDefaultAppearance(); - ByteString csFontNameTag = cDA.GetFont(&fFontSize); - if (csFontNameTag.IsEmpty()) + Optional csFontNameTag = cDA.GetFont(&fFontSize); + if (!csFontNameTag || csFontNameTag->IsEmpty()) return nullptr; CPDF_Object* pObj = FPDF_GetFieldAttr(m_pWidgetDict.Get(), "DR"); if (CPDF_Dictionary* pDict = ToDictionary(pObj)) { CPDF_Dictionary* pFonts = pDict->GetDictFor("Font"); if (pFonts) { - CPDF_Dictionary* pElement = pFonts->GetDictFor(csFontNameTag); + CPDF_Dictionary* pElement = pFonts->GetDictFor(*csFontNameTag); if (pElement) { CPDF_Font* pFont = m_pField->GetForm()->GetDocument()->LoadFont(pElement); @@ -294,7 +294,7 @@ CPDF_Font* CPDF_FormControl::GetDefaultControlFont() { } } } - if (CPDF_Font* pFormFont = m_pField->GetForm()->GetFormFont(csFontNameTag)) + if (CPDF_Font* pFormFont = m_pField->GetForm()->GetFormFont(*csFontNameTag)) return pFormFont; CPDF_Dictionary* pPageDict = m_pWidgetDict->GetDictFor("P"); @@ -302,7 +302,7 @@ CPDF_Font* CPDF_FormControl::GetDefaultControlFont() { if (CPDF_Dictionary* pDict = ToDictionary(pObj)) { CPDF_Dictionary* pFonts = pDict->GetDictFor("Font"); if (pFonts) { - CPDF_Dictionary* pElement = pFonts->GetDictFor(csFontNameTag); + CPDF_Dictionary* pElement = pFonts->GetDictFor(*csFontNameTag); if (pElement) { CPDF_Font* pFont = m_pField->GetForm()->GetDocument()->LoadFont(pElement); diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp index f3dcac2228..540e1c9ae0 100644 --- a/core/fpdfdoc/cpdf_formfield.cpp +++ b/core/fpdfdoc/cpdf_formfield.cpp @@ -915,11 +915,11 @@ void CPDF_FormField::LoadDA() { return; CPDF_DefaultAppearance appearance(DA); - if (!appearance.HasFont()) + Optional font_name = appearance.GetFont(&m_FontSize); + if (!font_name) return; - ByteString font_name = appearance.GetFont(&m_FontSize); - CPDF_Dictionary* pFontDict = pFont->GetDictFor(font_name); + CPDF_Dictionary* pFontDict = pFont->GetDictFor(*font_name); if (!pFontDict) return; diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index a6a64aaf8d..ef55545f9e 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -923,13 +923,13 @@ void CPVT_GenerateAP::GenerateFormAP(Type type, return; CPDF_DefaultAppearance appearance(DA); - if (!appearance.HasFont()) - return; - ASSERT(appearance.HasFont()); float fFontSize = 0; - ByteString sFontName = - PDF_NameDecode(appearance.GetFont(&fFontSize).AsStringView()); + Optional font = appearance.GetFont(&fFontSize); + if (!font) + return; + + ByteString sFontName = PDF_NameDecode(font->AsStringView()); if (sFontName.IsEmpty()) return; diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp index b7e14b4636..0daa0cd5ac 100644 --- a/fpdfsdk/formfiller/cba_fontmap.cpp +++ b/fpdfsdk/formfiller/cba_fontmap.cpp @@ -217,12 +217,14 @@ CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(ByteString* sAlias) { return nullptr; CPDF_DefaultAppearance appearance(sDA); - ASSERT(appearance.HasFont()); - float font_size; - ByteString sDecodedFontName = - PDF_NameDecode(appearance.GetFont(&font_size).AsStringView()); - *sAlias = sDecodedFontName.Right(sDecodedFontName.GetLength() - 1); + Optional font = appearance.GetFont(&font_size); + if (font) { + ByteString sDecodedFontName = PDF_NameDecode(font->AsStringView()); + *sAlias = sDecodedFontName.Right(sDecodedFontName.GetLength() - 1); + } else { + *sAlias = ByteString(); + } CPDF_Dictionary* pFontDict = nullptr; if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP")) { diff --git a/fpdfsdk/pwl/cpwl_appstream.cpp b/fpdfsdk/pwl/cpwl_appstream.cpp index 0731398ae2..6e65831627 100644 --- a/fpdfsdk/pwl/cpwl_appstream.cpp +++ b/fpdfsdk/pwl/cpwl_appstream.cpp @@ -1184,9 +1184,13 @@ void CPWL_AppStream::SetAsPushButton() { iColorType = da.GetColor(fc); crText = CFX_Color(iColorType, fc[0], fc[1], fc[2], fc[3]); } - float fFontSize = 12.0f; - if (da.HasFont()) - csNameTag = da.GetFont(&fFontSize); + + float fFontSize; + Optional font = da.GetFont(&fFontSize); + if (font) + csNameTag = *font; + else + fFontSize = 12.0f; WideString csWCaption; WideString csNormalCaption; -- cgit v1.2.3