summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorJane Liu <janeliulwq@google.com>2017-06-29 19:18:23 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-06-29 23:38:19 +0000
commitbaa7ff4390b8b36c2d58c1b1f8d9775c76da656a (patch)
treeb734d22e51f6eab9753adf4c9af3c9dbf369c8c9 /core
parentcaf6d61fbb38083f213a9d3441e12c5082d3ad3a (diff)
downloadpdfium-baa7ff4390b8b36c2d58c1b1f8d9775c76da656a.tar.xz
Basic APIs and tests for extracting and setting annotation paths
1. Added APIs for retrieving existing annotation paths and setting annotation paths. * Added an embedder test testing all the new functions. Bug=pdfium:737 Change-Id: Ic451bcd3be488261baf2182549c4238b887b219e Reviewed-on: https://pdfium-review.googlesource.com/6676 Commit-Queue: Jane Liu <janeliulwq@google.com> Reviewed-by: Lei Zhang <thestig@chromium.org> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core')
-rw-r--r--core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp3
-rw-r--r--core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp22
-rw-r--r--core/fpdfdoc/cpvt_generateap.cpp124
-rw-r--r--core/fpdfdoc/cpvt_generateap.h14
4 files changed, 93 insertions, 70 deletions
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 369c2a81ff..18bcbf909d 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -209,6 +209,9 @@ void CPDF_PageContentGenerator::ProcessImage(std::ostringstream* buf,
void CPDF_PageContentGenerator::ProcessPath(std::ostringstream* buf,
CPDF_PathObject* pPathObj) {
ProcessGraphics(buf, pPathObj);
+
+ *buf << pPathObj->m_Matrix << " cm ";
+
auto& pPoints = pPathObj->m_Path.GetPoints();
if (pPathObj->m_Path.IsRect()) {
CFX_PointF diff = pPoints[2].m_Point - pPoints[0].m_Point;
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
index ef186666f1..62b10c9149 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
@@ -58,7 +58,7 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessRect) {
CPDF_PageContentGenerator generator(pTestPage.get());
std::ostringstream buf;
TestProcessPath(&generator, &buf, pPathObj.get());
- EXPECT_EQ("q 10 5 3 25 re B* Q\n", CFX_ByteString(buf));
+ EXPECT_EQ("q 1 0 0 1 0 0 cm 10 5 3 25 re B* Q\n", CFX_ByteString(buf));
pPathObj = pdfium::MakeUnique<CPDF_PathObject>();
pPathObj->m_Path.AppendPoint(CFX_PointF(0, 0), FXPT_TYPE::MoveTo, false);
@@ -71,7 +71,7 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessRect) {
buf.str("");
TestProcessPath(&generator, &buf, pPathObj.get());
- EXPECT_EQ("q 0 0 5.2 3.78 re n Q\n", CFX_ByteString(buf));
+ EXPECT_EQ("q 1 0 0 1 0 0 cm 0 0 5.2 3.78 re n Q\n", CFX_ByteString(buf));
}
TEST_F(CPDF_PageContentGeneratorTest, ProcessPath) {
@@ -103,8 +103,8 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessPath) {
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",
+ "q 1 0 0 1 0 0 cm 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",
CFX_ByteString(buf));
}
@@ -137,10 +137,11 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessGraphics) {
// Color RGB values used are integers divided by 255.
EXPECT_EQ("q 0.501961 0.701961 0.34902 rg 1 0.901961 0 RG /",
pathString.Left(48));
- EXPECT_EQ(" gs 1 2 m 3 4 l 5 6 l h B Q\n", pathString.Right(28));
- ASSERT_TRUE(pathString.GetLength() > 76);
+ EXPECT_EQ(" gs 1 0 0 1 0 0 cm 1 2 m 3 4 l 5 6 l h B Q\n",
+ pathString.Right(43));
+ ASSERT_TRUE(pathString.GetLength() > 91);
CPDF_Dictionary* externalGS = TestGetResource(
- &generator, "ExtGState", pathString.Mid(48, pathString.GetLength() - 76));
+ &generator, "ExtGState", pathString.Mid(48, pathString.GetLength() - 91));
ASSERT_TRUE(externalGS);
EXPECT_EQ(0.5f, externalGS->GetNumberFor("ca"));
EXPECT_EQ(0.8f, externalGS->GetNumberFor("CA"));
@@ -152,7 +153,8 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessGraphics) {
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));
+ EXPECT_EQ(" gs 1 0 0 1 0 0 cm 1 2 m 3 4 l 5 6 l h B Q\n",
+ pathString2.Right(43));
// Compare with the previous (should use same dictionary for gs)
EXPECT_EQ(pathString.GetLength() + 7, pathString2.GetLength());
@@ -302,8 +304,8 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessFormWithPath) {
pDoc->CreateNewDoc();
auto pDict = pdfium::MakeUnique<CPDF_Dictionary>();
const char content[] =
- "q 3.102 4.67 m 5.45 0.29 l 4.24 3.15 4.65 2.98 3.456 0.24 c 3.102 4.67 "
- "l h f Q\n";
+ "q 1 0 0 1 0 0 cm 3.102 4.67 m 5.45 0.29 l 4.24 3.15 4.65 2.98 3.456 "
+ "0.24 c 3.102 4.67 l h f Q\n";
size_t buf_len = FX_ArraySize(content);
std::unique_ptr<uint8_t, FxFreeDeleter> buf(FX_Alloc(uint8_t, buf_len));
memcpy(buf.get(), content, buf_len);
diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp
index ff7f4b68ee..eb874b3352 100644
--- a/core/fpdfdoc/cpvt_generateap.cpp
+++ b/core/fpdfdoc/cpvt_generateap.cpp
@@ -545,27 +545,6 @@ CFX_ByteString GetPopupContentsString(CPDF_Document* pDoc,
return CFX_ByteString(sAppStream);
}
-std::unique_ptr<CPDF_Dictionary> GenerateExtGStateDict(
- const CPDF_Dictionary& pAnnotDict,
- const CFX_ByteString& sExtGSDictName,
- const CFX_ByteString& sBlendMode) {
- auto pGSDict =
- pdfium::MakeUnique<CPDF_Dictionary>(pAnnotDict.GetByteStringPool());
- pGSDict->SetNewFor<CPDF_String>("Type", "ExtGState", false);
-
- float fOpacity =
- pAnnotDict.KeyExist("CA") ? pAnnotDict.GetNumberFor("CA") : 1;
- pGSDict->SetNewFor<CPDF_Number>("CA", fOpacity);
- pGSDict->SetNewFor<CPDF_Number>("ca", fOpacity);
- pGSDict->SetNewFor<CPDF_Boolean>("AIS", false);
- pGSDict->SetNewFor<CPDF_String>("BM", sBlendMode, false);
-
- auto pExtGStateDict =
- pdfium::MakeUnique<CPDF_Dictionary>(pAnnotDict.GetByteStringPool());
- pExtGStateDict->SetFor(sExtGSDictName, std::move(pGSDict));
- return pExtGStateDict;
-}
-
std::unique_ptr<CPDF_Dictionary> GenerateResourceFontDict(
CPDF_Document* pDoc,
const CFX_ByteString& sFontDictName) {
@@ -582,45 +561,6 @@ std::unique_ptr<CPDF_Dictionary> GenerateResourceFontDict(
return pResourceFontDict;
}
-std::unique_ptr<CPDF_Dictionary> GenerateResourceDict(
- CPDF_Document* pDoc,
- std::unique_ptr<CPDF_Dictionary> pExtGStateDict,
- std::unique_ptr<CPDF_Dictionary> pResourceFontDict) {
- auto pResourceDict =
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
- if (pExtGStateDict)
- pResourceDict->SetFor("ExtGState", std::move(pExtGStateDict));
- if (pResourceFontDict)
- pResourceDict->SetFor("Font", std::move(pResourceFontDict));
- return pResourceDict;
-}
-
-void GenerateAndSetAPDict(CPDF_Document* pDoc,
- CPDF_Dictionary* pAnnotDict,
- std::ostringstream* psAppStream,
- std::unique_ptr<CPDF_Dictionary> pResourceDict,
- bool bIsTextMarkupAnnotation) {
- CPDF_Stream* pNormalStream = pDoc->NewIndirect<CPDF_Stream>();
- pNormalStream->SetData(psAppStream);
-
- CPDF_Dictionary* pAPDict = pAnnotDict->GetDictFor("AP");
- if (!pAPDict)
- pAPDict = pAnnotDict->SetNewFor<CPDF_Dictionary>("AP");
-
- pAPDict->SetNewFor<CPDF_Reference>("N", pDoc, pNormalStream->GetObjNum());
-
- CPDF_Dictionary* pStreamDict = pNormalStream->GetDict();
- pStreamDict->SetNewFor<CPDF_Number>("FormType", 1);
- pStreamDict->SetNewFor<CPDF_String>("Subtype", "Form", false);
- pStreamDict->SetMatrixFor("Matrix", CFX_Matrix());
-
- CFX_FloatRect rect = bIsTextMarkupAnnotation
- ? CPDF_Annot::RectFromQuadPoints(pAnnotDict)
- : pAnnotDict->GetRectFor("Rect");
- pStreamDict->SetRectFor("BBox", rect);
- pStreamDict->SetFor("Resources", std::move(pResourceDict));
-}
-
CFX_ByteString GetPaintOperatorString(bool bIsStrokeRect, bool bIsFillRect) {
if (bIsStrokeRect)
return bIsFillRect ? "b" : "s";
@@ -1322,6 +1262,70 @@ CFX_ByteString CPVT_GenerateAP::GenerateColorAP(const CPVT_Color& color,
}
// Static.
+std::unique_ptr<CPDF_Dictionary> CPVT_GenerateAP::GenerateExtGStateDict(
+ const CPDF_Dictionary& pAnnotDict,
+ const CFX_ByteString& sExtGSDictName,
+ const CFX_ByteString& sBlendMode) {
+ auto pGSDict =
+ pdfium::MakeUnique<CPDF_Dictionary>(pAnnotDict.GetByteStringPool());
+ pGSDict->SetNewFor<CPDF_String>("Type", "ExtGState", false);
+
+ float fOpacity =
+ pAnnotDict.KeyExist("CA") ? pAnnotDict.GetNumberFor("CA") : 1;
+ pGSDict->SetNewFor<CPDF_Number>("CA", fOpacity);
+ pGSDict->SetNewFor<CPDF_Number>("ca", fOpacity);
+ pGSDict->SetNewFor<CPDF_Boolean>("AIS", false);
+ pGSDict->SetNewFor<CPDF_String>("BM", sBlendMode, false);
+
+ auto pExtGStateDict =
+ pdfium::MakeUnique<CPDF_Dictionary>(pAnnotDict.GetByteStringPool());
+ pExtGStateDict->SetFor(sExtGSDictName, std::move(pGSDict));
+ return pExtGStateDict;
+}
+
+// Static.
+std::unique_ptr<CPDF_Dictionary> CPVT_GenerateAP::GenerateResourceDict(
+ CPDF_Document* pDoc,
+ std::unique_ptr<CPDF_Dictionary> pExtGStateDict,
+ std::unique_ptr<CPDF_Dictionary> pResourceFontDict) {
+ auto pResourceDict =
+ pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
+ if (pExtGStateDict)
+ pResourceDict->SetFor("ExtGState", std::move(pExtGStateDict));
+ if (pResourceFontDict)
+ pResourceDict->SetFor("Font", std::move(pResourceFontDict));
+ return pResourceDict;
+}
+
+// Static.
+void CPVT_GenerateAP::GenerateAndSetAPDict(
+ CPDF_Document* pDoc,
+ CPDF_Dictionary* pAnnotDict,
+ std::ostringstream* psAppStream,
+ std::unique_ptr<CPDF_Dictionary> pResourceDict,
+ bool bIsTextMarkupAnnotation) {
+ CPDF_Stream* pNormalStream = pDoc->NewIndirect<CPDF_Stream>();
+ pNormalStream->SetData(psAppStream);
+
+ CPDF_Dictionary* pAPDict = pAnnotDict->GetDictFor("AP");
+ if (!pAPDict)
+ pAPDict = pAnnotDict->SetNewFor<CPDF_Dictionary>("AP");
+
+ pAPDict->SetNewFor<CPDF_Reference>("N", pDoc, pNormalStream->GetObjNum());
+
+ CPDF_Dictionary* pStreamDict = pNormalStream->GetDict();
+ pStreamDict->SetNewFor<CPDF_Number>("FormType", 1);
+ pStreamDict->SetNewFor<CPDF_String>("Subtype", "Form", false);
+ pStreamDict->SetMatrixFor("Matrix", CFX_Matrix());
+
+ CFX_FloatRect rect = bIsTextMarkupAnnotation
+ ? CPDF_Annot::RectFromQuadPoints(pAnnotDict)
+ : pAnnotDict->GetRectFor("Rect");
+ pStreamDict->SetRectFor("BBox", rect);
+ pStreamDict->SetFor("Resources", std::move(pResourceDict));
+}
+
+// Static.
CFX_ByteString CPVT_GenerateAP::GetPDFWordString(IPVT_FontMap* pFontMap,
int32_t nFontIndex,
uint16_t Word,
diff --git a/core/fpdfdoc/cpvt_generateap.h b/core/fpdfdoc/cpvt_generateap.h
index c558636255..cee96cda50 100644
--- a/core/fpdfdoc/cpvt_generateap.h
+++ b/core/fpdfdoc/cpvt_generateap.h
@@ -60,6 +60,20 @@ class CPVT_GenerateAP {
const CPVT_Dash& dash);
static CFX_ByteString GenerateColorAP(const CPVT_Color& color,
PaintOperation nOperation);
+ static std::unique_ptr<CPDF_Dictionary> GenerateExtGStateDict(
+ const CPDF_Dictionary& pAnnotDict,
+ const CFX_ByteString& sExtGSDictName,
+ const CFX_ByteString& sBlendMode);
+ static std::unique_ptr<CPDF_Dictionary> GenerateResourceDict(
+ CPDF_Document* pDoc,
+ std::unique_ptr<CPDF_Dictionary> pExtGStateDict,
+ std::unique_ptr<CPDF_Dictionary> pResourceFontDict);
+ static void GenerateAndSetAPDict(
+ CPDF_Document* pDoc,
+ CPDF_Dictionary* pAnnotDict,
+ std::ostringstream* psAppStream,
+ std::unique_ptr<CPDF_Dictionary> pResourceDict,
+ bool bIsTextMarkupAnnotation);
static CFX_ByteString GetPDFWordString(IPVT_FontMap* pFontMap,
int32_t nFontIndex,