diff options
-rw-r--r-- | core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp | 34 | ||||
-rw-r--r-- | 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<CPDF_Dictionary>(); @@ -343,24 +343,30 @@ void CPDF_PageContentGenerator::ProcessDefaultGraphics( *buf << "0 0 0 RG 0 0 0 rg 1 w " << static_cast<int>(CFX_GraphStateData::LineCapButt) << " J " << static_cast<int>(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<CPDF_Dictionary>(); - gsDict->SetNewFor<CPDF_Number>("ca", defaultGraphics.fillAlpha); - gsDict->SetNewFor<CPDF_Number>("CA", defaultGraphics.strokeAlpha); - gsDict->SetNewFor<CPDF_Name>("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<CPDF_Dictionary>(); + gsDict->SetNewFor<CPDF_Number>("ca", defaultGraphics.fillAlpha); + gsDict->SetNewFor<CPDF_Number>("CA", defaultGraphics.strokeAlpha); + gsDict->SetNewFor<CPDF_Name>("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. |