From 9ba8fbc02e86a6b7cb5c4142d9481ae3238c1ec4 Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Wed, 28 Jun 2017 15:31:56 -0400 Subject: Set default graphics before generating page contents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this CL, the content generator sets some default graphics states before processing the page objects. In particular, a default ExtGState is now set before processing, and the last CTM is now stored right after parsing finishes: the only command to change matrix is ctm, and it concatenates, so inverting requires knowing the current value. Bug: pdfium:779 Change-Id: I35b1c07550ce91839fb0e20fbf717e3e80c9b9d6 Reviewed-on: https://pdfium-review.googlesource.com/7070 Commit-Queue: Nicolás Peña Reviewed-by: dsinclair --- core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp') diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index eeb6f81721..10b3933742 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -65,9 +65,24 @@ void CPDF_PageContentGenerator::GenerateContent() { CPDF_Document* pDoc = m_pDocument.Get(); std::ostringstream buf; + + // Set the default graphic state values + buf << "q\n"; + if (!m_pObjHolder->GetLastCTM().IsIdentity()) { + CFX_Matrix reverse; + reverse.SetReverse(m_pObjHolder->GetLastCTM()); + buf << reverse << " cm\n"; + } + ProcessDefaultGraphics(&buf); + + // Process the page objects if (!ProcessPageObjects(&buf)) return; + // Return graphics to original state + buf << "Q\n"; + + // Add buffer to a stream in page's 'Contents' CPDF_Dictionary* pPageDict = m_pObjHolder->m_pFormDict.Get(); CPDF_Object* pContent = pPageDict ? pPageDict->GetObjectFor("Contents") : nullptr; @@ -142,6 +157,7 @@ bool CPDF_PageContentGenerator::ProcessPageObjects(std::ostringstream* buf) { for (auto& pPageObj : m_pageObjects) { if (m_pObjHolder->IsPage() && !pPageObj->IsDirty()) continue; + bDirty = true; if (CPDF_ImageObject* pImageObject = pPageObj->AsImage()) ProcessImage(buf, pImageObject); @@ -304,6 +320,32 @@ void CPDF_PageContentGenerator::ProcessGraphics(std::ostringstream* buf, *buf << "/" << PDF_NameEncode(name) << " gs "; } +void CPDF_PageContentGenerator::ProcessDefaultGraphics( + std::ostringstream* buf) { + *buf << "0 0 0 RG 0 0 0 rg 1 w " + << static_cast(CFX_GraphStateData::LineCapButt) << " J " + << static_cast(CFX_GraphStateData::LineJoinMiter) << " j\n"; + GraphicsData defaultGraphics; + defaultGraphics.fillAlpha = 1.0f; + defaultGraphics.strokeAlpha = 1.0f; + defaultGraphics.blendType = FXDIB_BLEND_NORMAL; + auto it = m_pObjHolder->m_GraphicsMap.find(defaultGraphics); + CFX_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)); + uint32_t dwObjNum = pDict->GetObjNum(); + name = RealizeResource(dwObjNum, "ExtGState"); + m_pObjHolder->m_GraphicsMap[defaultGraphics] = name; + } + *buf << "/" << PDF_NameEncode(name).c_str() << " gs "; +} + // This method adds text to the buffer, BT begins the text object, ET ends it. // Tm sets the text matrix (allows positioning and transforming text). // Tf sets the font name (from Font in Resources) and font size. -- cgit v1.2.3