From c2b34d2adc6c987d882b4173265697917df74f54 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Wed, 13 Jun 2018 17:36:56 +0000 Subject: Refactor GetOrCreateDefaultGraphics out of ProcessDefaultGraphics. GetOrCreateDefaultGraphics() has a side effect, and it needs to be called regardless of whether a stream is generated of not. Failing to do so causes default graphics to not exist in some cases. Bug: pdfium:1051 Change-Id: I039cb6b755cc15c8ca667c92e581d085f3f2a124 Reviewed-on: https://pdfium-review.googlesource.com/35010 Commit-Queue: Henrique Nakashima Reviewed-by: dsinclair --- core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp | 34 +++++++++++++++---------- core/fpdfapi/edit/cpdf_pagecontentgenerator.h | 3 ++- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index e3a8674afe..19994fa57f 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -130,7 +130,7 @@ void CPDF_PageContentGenerator::UpdateContentStreams( ByteString CPDF_PageContentGenerator::RealizeResource( const CPDF_Object* pResource, - const ByteString& bsType) { + const ByteString& bsType) const { ASSERT(pResource); if (!m_pObjHolder->m_pResources) { m_pObjHolder->m_pResources = m_pDocument->NewIndirect(); @@ -343,24 +343,30 @@ void CPDF_PageContentGenerator::ProcessDefaultGraphics( *buf << "0 0 0 RG 0 0 0 rg 1 w " << static_cast(CFX_GraphStateData::LineCapButt) << " J " << static_cast(CFX_GraphStateData::LineJoinMiter) << " j\n"; + ByteString name = GetOrCreateDefaultGraphics(); + *buf << "/" << PDF_NameEncode(name).c_str() << " gs "; +} + +ByteString CPDF_PageContentGenerator::GetOrCreateDefaultGraphics() const { GraphicsData defaultGraphics; defaultGraphics.fillAlpha = 1.0f; defaultGraphics.strokeAlpha = 1.0f; defaultGraphics.blendType = FXDIB_BLEND_NORMAL; auto it = m_pObjHolder->m_GraphicsMap.find(defaultGraphics); - ByteString name; - if (it != m_pObjHolder->m_GraphicsMap.end()) { - name = it->second; - } else { - auto gsDict = pdfium::MakeUnique(); - gsDict->SetNewFor("ca", defaultGraphics.fillAlpha); - gsDict->SetNewFor("CA", defaultGraphics.strokeAlpha); - gsDict->SetNewFor("BM", "Normal"); - CPDF_Object* pDict = m_pDocument->AddIndirectObject(std::move(gsDict)); - name = RealizeResource(pDict, "ExtGState"); - m_pObjHolder->m_GraphicsMap[defaultGraphics] = name; - } - *buf << "/" << PDF_NameEncode(name).c_str() << " gs "; + + // If default graphics already exists, return it. + if (it != m_pObjHolder->m_GraphicsMap.end()) + return it->second; + + // Otherwise, create them. + auto gsDict = pdfium::MakeUnique(); + gsDict->SetNewFor("ca", defaultGraphics.fillAlpha); + gsDict->SetNewFor("CA", defaultGraphics.strokeAlpha); + gsDict->SetNewFor("BM", "Normal"); + CPDF_Object* pDict = m_pDocument->AddIndirectObject(std::move(gsDict)); + ByteString name = RealizeResource(pDict, "ExtGState"); + m_pObjHolder->m_GraphicsMap[defaultGraphics] = name; + return name; } // This method adds text to the buffer, BT begins the text object, ET ends it. diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.h b/core/fpdfapi/edit/cpdf_pagecontentgenerator.h index 677d29267b..04adf1c1d2 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.h +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.h @@ -40,8 +40,9 @@ class CPDF_PageContentGenerator { void ProcessGraphics(std::ostringstream* buf, CPDF_PageObject* pPageObj); void ProcessDefaultGraphics(std::ostringstream* buf); void ProcessText(std::ostringstream* buf, CPDF_TextObject* pTextObj); + ByteString GetOrCreateDefaultGraphics() const; ByteString RealizeResource(const CPDF_Object* pResource, - const ByteString& bsType); + const ByteString& bsType) const; // Returns a map from content stream index to new stream data. Unmodified // streams are not touched. -- cgit v1.2.3