From 8f38e4fff7e80e676c1c7747634d6d4648688afc Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Fri, 9 Jun 2017 15:37:45 -0400 Subject: Converting CFX_ByteTextBuf to ostringstream in cpdf_pagecontentgenerator. Bug: pdfium:731 Change-Id: If606359bd9264a6f440871ab824ab54a4efae901 Reviewed-on: https://pdfium-review.googlesource.com/6450 Commit-Queue: dsinclair Reviewed-by: dsinclair --- core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp | 27 +++++++++--------- core/fpdfapi/edit/cpdf_pagecontentgenerator.h | 11 ++++---- .../edit/cpdf_pagecontentgenerator_unittest.cpp | 32 +++++++++++----------- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index 2f00c7db09..e06c28d7ae 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -28,7 +28,7 @@ namespace { -CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& ar, const CFX_Matrix& matrix) { +std::ostream& operator<<(std::ostream& ar, const CFX_Matrix& matrix) { ar << matrix.a << " " << matrix.b << " " << matrix.c << " " << matrix.d << " " << matrix.e << " " << matrix.f; return ar; @@ -61,7 +61,7 @@ CPDF_PageContentGenerator::~CPDF_PageContentGenerator() {} void CPDF_PageContentGenerator::GenerateContent() { CPDF_Document* pDoc = m_pDocument.Get(); - CFX_ByteTextBuf buf; + std::ostringstream buf; if (!ProcessPageObjects(&buf)) return; @@ -69,7 +69,8 @@ void CPDF_PageContentGenerator::GenerateContent() { CPDF_Object* pContent = pPageDict ? pPageDict->GetObjectFor("Contents") : nullptr; CPDF_Stream* pStream = pDoc->NewIndirect(); - pStream->SetData(buf.GetBuffer(), buf.GetLength()); + pStream->SetData(reinterpret_cast(buf.str().c_str()), + buf.tellp()); if (pContent) { CPDF_Array* pArray = ToArray(pContent); if (pArray) { @@ -133,7 +134,7 @@ CFX_ByteString CPDF_PageContentGenerator::RealizeResource( return name; } -bool CPDF_PageContentGenerator::ProcessPageObjects(CFX_ByteTextBuf* buf) { +bool CPDF_PageContentGenerator::ProcessPageObjects(std::ostringstream* buf) { bool bDirty = false; for (auto& pPageObj : m_pageObjects) { if (!pPageObj->IsDirty()) @@ -150,7 +151,7 @@ bool CPDF_PageContentGenerator::ProcessPageObjects(CFX_ByteTextBuf* buf) { return bDirty; } -void CPDF_PageContentGenerator::ProcessImage(CFX_ByteTextBuf* buf, +void CPDF_PageContentGenerator::ProcessImage(std::ostringstream* buf, CPDF_ImageObject* pImageObj) { if ((pImageObj->matrix().a == 0 && pImageObj->matrix().b == 0) || (pImageObj->matrix().c == 0 && pImageObj->matrix().d == 0)) { @@ -175,7 +176,7 @@ void CPDF_PageContentGenerator::ProcessImage(CFX_ByteTextBuf* buf, if (bWasInline) pImageObj->SetImage(m_pDocument->GetPageData()->GetImage(dwObjNum)); - *buf << "/" << PDF_NameEncode(name) << " Do Q\n"; + *buf << "/" << PDF_NameEncode(name).c_str() << " Do Q\n"; } // Processing path with operators from Tables 4.9 and 4.10 of PDF spec 1.7: @@ -189,7 +190,7 @@ void CPDF_PageContentGenerator::ProcessImage(CFX_ByteTextBuf* buf, // Path painting operators: "S", "n", "B", "f", "B*", "f*", depending on // the filling mode and whether we want stroking the path or not. // "Q" restores the graphics state imposed by the ProcessGraphics method. -void CPDF_PageContentGenerator::ProcessPath(CFX_ByteTextBuf* buf, +void CPDF_PageContentGenerator::ProcessPath(std::ostringstream* buf, CPDF_PathObject* pPathObj) { ProcessGraphics(buf, pPathObj); auto& pPoints = pPathObj->m_Path.GetPoints(); @@ -242,7 +243,7 @@ void CPDF_PageContentGenerator::ProcessPath(CFX_ByteTextBuf* buf, // "w" sets the stroke line width. // "ca" sets the fill alpha, "CA" sets the stroke alpha. // "q" saves the graphics state, so that the settings can later be reversed -void CPDF_PageContentGenerator::ProcessGraphics(CFX_ByteTextBuf* buf, +void CPDF_PageContentGenerator::ProcessGraphics(std::ostringstream* buf, CPDF_PageObject* pPageObj) { *buf << "q "; float fillColor[3]; @@ -297,14 +298,14 @@ void CPDF_PageContentGenerator::ProcessGraphics(CFX_ByteTextBuf* buf, name = RealizeResource(dwObjNum, "ExtGState"); m_pPage->m_GraphicsMap[graphD] = name; } - *buf << "/" << PDF_NameEncode(name) << " gs "; + *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. // Tj sets the actual text, <####...> is used when specifying charcodes. -void CPDF_PageContentGenerator::ProcessText(CFX_ByteTextBuf* buf, +void CPDF_PageContentGenerator::ProcessText(std::ostringstream* buf, CPDF_TextObject* pTextObj) { *buf << "BT " << pTextObj->GetTextMatrix() << " Tm "; CPDF_Font* pFont = pTextObj->GetFont(); @@ -338,14 +339,14 @@ void CPDF_PageContentGenerator::ProcessText(CFX_ByteTextBuf* buf, dictName = RealizeResource(dwObjNum, "Font"); m_pPage->m_FontsMap[fontD] = dictName; } - *buf << "/" << PDF_NameEncode(dictName) << " " << pTextObj->GetFontSize() - << " Tf "; + *buf << "/" << PDF_NameEncode(dictName).c_str() << " " + << pTextObj->GetFontSize() << " Tf "; CFX_ByteString text; for (uint32_t charcode : pTextObj->m_CharCodes) { if (charcode != CPDF_Font::kInvalidCharCode) pFont->AppendChar(&text, charcode); } ProcessGraphics(buf, pTextObj); - *buf << PDF_EncodeString(text, true) << " Tj ET"; + *buf << PDF_EncodeString(text, true).c_str() << " Tj ET"; *buf << " Q\n"; } diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.h b/core/fpdfapi/edit/cpdf_pagecontentgenerator.h index 302375d536..19a2faa6bf 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.h +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.h @@ -7,6 +7,7 @@ #ifndef CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTGENERATOR_H_ #define CORE_FPDFAPI_EDIT_CPDF_PAGECONTENTGENERATOR_H_ +#include #include #include "core/fxcrt/cfx_unowned_ptr.h" @@ -30,11 +31,11 @@ class CPDF_PageContentGenerator { private: friend class CPDF_PageContentGeneratorTest; - bool ProcessPageObjects(CFX_ByteTextBuf* buf); - void ProcessPath(CFX_ByteTextBuf* buf, CPDF_PathObject* pPathObj); - void ProcessImage(CFX_ByteTextBuf* buf, CPDF_ImageObject* pImageObj); - void ProcessGraphics(CFX_ByteTextBuf* buf, CPDF_PageObject* pPageObj); - void ProcessText(CFX_ByteTextBuf* buf, CPDF_TextObject* pTextObj); + bool ProcessPageObjects(std::ostringstream* buf); + void ProcessPath(std::ostringstream* buf, CPDF_PathObject* pPathObj); + void ProcessImage(std::ostringstream* buf, CPDF_ImageObject* pImageObj); + void ProcessGraphics(std::ostringstream* buf, CPDF_PageObject* pPageObj); + void ProcessText(std::ostringstream* buf, CPDF_TextObject* pTextObj); CFX_ByteString RealizeResource(uint32_t dwResourceObjNum, const CFX_ByteString& bsType); diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp index 8368e8de7c..91389eef5f 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp @@ -26,7 +26,7 @@ class CPDF_PageContentGeneratorTest : public testing::Test { } void TestProcessPath(CPDF_PageContentGenerator* pGen, - CFX_ByteTextBuf* buf, + std::ostringstream* buf, CPDF_PathObject* pPathObj) { pGen->ProcessPath(buf, pPathObj); } @@ -38,7 +38,7 @@ class CPDF_PageContentGeneratorTest : public testing::Test { } void TestProcessText(CPDF_PageContentGenerator* pGen, - CFX_ByteTextBuf* buf, + std::ostringstream* buf, CPDF_TextObject* pTextObj) { pGen->ProcessText(buf, pTextObj); } @@ -52,9 +52,9 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessRect) { auto pTestPage = pdfium::MakeUnique(nullptr, nullptr, false); CPDF_PageContentGenerator generator(pTestPage.get()); - CFX_ByteTextBuf buf; + std::ostringstream buf; TestProcessPath(&generator, &buf, pPathObj.get()); - EXPECT_EQ("q 10 5 3 25 re B* Q\n", buf.MakeString()); + EXPECT_EQ("q 10 5 3 25 re B* Q\n", CFX_ByteString(buf)); pPathObj = pdfium::MakeUnique(); pPathObj->m_Path.AppendPoint(CFX_PointF(0, 0), FXPT_TYPE::MoveTo, false); @@ -64,10 +64,10 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessRect) { pPathObj->m_Path.AppendPoint(CFX_PointF(0, 3.78f), FXPT_TYPE::LineTo, true); pPathObj->m_FillType = 0; pPathObj->m_bStroke = false; - buf.Clear(); + buf.str(""); TestProcessPath(&generator, &buf, pPathObj.get()); - EXPECT_EQ("q 0 0 5.2 3.78 re n Q\n", buf.MakeString()); + EXPECT_EQ("q 0 0 5.2 3.78 re n Q\n", CFX_ByteString(buf)); } TEST_F(CPDF_PageContentGeneratorTest, ProcessPath) { @@ -96,12 +96,12 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessPath) { auto pTestPage = pdfium::MakeUnique(nullptr, nullptr, false); CPDF_PageContentGenerator generator(pTestPage.get()); - CFX_ByteTextBuf buf; + std::ostringstream buf; TestProcessPath(&generator, &buf, pPathObj.get()); EXPECT_EQ( "q 3.102 4.67 m 5.45 0.29 l 4.24 3.15 4.65 2.98 3.456 0.24 c 10.6 11.15 " "l 11 12.5 l 11.46 12.67 11.84 12.96 12 13.64 c h f Q\n", - buf.MakeString()); + CFX_ByteString(buf)); } TEST_F(CPDF_PageContentGeneratorTest, ProcessGraphics) { @@ -126,9 +126,9 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessGraphics) { CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(0); auto pTestPage = pdfium::MakeUnique(pDoc.get(), pPageDict, false); CPDF_PageContentGenerator generator(pTestPage.get()); - CFX_ByteTextBuf buf; + std::ostringstream buf; TestProcessPath(&generator, &buf, pPathObj.get()); - CFX_ByteString pathString = buf.MakeString(); + CFX_ByteString pathString(buf); // Color RGB values used are integers divided by 255. EXPECT_EQ("q 0.501961 0.701961 0.34902 rg 1 0.901961 0 RG /", @@ -143,9 +143,9 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessGraphics) { // Same path, now with a stroke. pPathObj->m_GraphState.SetLineWidth(10.5f); - buf.Clear(); + buf.str(""); TestProcessPath(&generator, &buf, pPathObj.get()); - CFX_ByteString pathString2 = buf.MakeString(); + CFX_ByteString pathString2(buf); EXPECT_EQ("q 0.501961 0.701961 0.34902 rg 1 0.901961 0 RG 10.5 w /", pathString2.Left(55)); EXPECT_EQ(" gs 1 2 m 3 4 l 5 6 l h B Q\n", pathString2.Right(28)); @@ -177,9 +177,9 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessStandardText) { pTextObj->m_GeneralState.SetStrokeAlpha(0.8f); pTextObj->Transform(CFX_Matrix(1, 0, 0, 1, 100, 100)); pTextObj->SetText("Hello World"); - CFX_ByteTextBuf buf; + std::ostringstream buf; TestProcessText(&generator, &buf, pTextObj.get()); - CFX_ByteString textString = buf.MakeString(); + CFX_ByteString textString(buf); int firstResourceAt = textString.Find('/') + 1; int secondResourceAt = textString.ReverseFind('/') + 1; CFX_ByteString firstString = textString.Left(firstResourceAt); @@ -221,7 +221,7 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessText) { auto pTestPage = pdfium::MakeUnique(pDoc.get(), pPageDict, false); CPDF_PageContentGenerator generator(pTestPage.get()); - CFX_ByteTextBuf buf; + std::ostringstream buf; { // Set the text object font and text auto pTextObj = pdfium::MakeUnique(); @@ -245,7 +245,7 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessText) { TestProcessText(&generator, &buf, pTextObj.get()); } - CFX_ByteString textString = buf.MakeString(); + CFX_ByteString textString(buf); int firstResourceAt = textString.Find('/') + 1; CFX_ByteString firstString = textString.Left(firstResourceAt); CFX_ByteString lastString = -- cgit v1.2.3