summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Pena <npm@chromium.org>2017-02-09 18:17:33 -0500
committerChromium commit bot <commit-bot@chromium.org>2017-02-10 04:08:27 +0000
commit2eb1a705c243b2d5ab912268dd453f5ade075b6f (patch)
tree7cbf6aabde7ecf3e1327535a9f6066856fc6626b
parente818bcba7e036d7e1dc8f282a88022b814dbfb40 (diff)
downloadpdfium-2eb1a705c243b2d5ab912268dd453f5ade075b6f.tar.xz
Add line width option for stroking pathschromium/3011chromium/3010chromium/3009
BUG=pdfium:661 Change-Id: Ie1dc82a1323a35ebbd63a5b7b8f8c95f9a5325fe Reviewed-on: https://pdfium-review.googlesource.com/2613 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: Nicolás Peña <npm@chromium.org>
-rw-r--r--core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp4
-rw-r--r--core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp13
-rw-r--r--fpdfsdk/fpdfedit_embeddertest.cpp51
-rw-r--r--fpdfsdk/fpdfeditpath.cpp9
-rw-r--r--public/fpdf_edit.h8
5 files changed, 85 insertions, 0 deletions
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index a5bd741cc0..9de97ee718 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -192,6 +192,7 @@ void CPDF_PageContentGenerator::ProcessPath(CFX_ByteTextBuf* buf,
// values cannot be obtained. The method also adds an external graphics
// dictionary, as described in Section 4.3.4.
// "rg" sets the fill color, "RG" sets the stroke color (using DefaultRGB)
+// "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,
@@ -207,6 +208,9 @@ void CPDF_PageContentGenerator::ProcessGraphics(CFX_ByteTextBuf* buf,
*buf << strokeColor[0] << " " << strokeColor[1] << " " << strokeColor[2]
<< " RG ";
}
+ FX_FLOAT lineWidth = pPageObj->m_GraphState.GetLineWidth();
+ if (lineWidth != 1.0f)
+ *buf << lineWidth << " w ";
GraphicsData graphD;
graphD.fillAlpha = pPageObj->m_GeneralState.GetFillAlpha();
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
index 3267f5218c..4846b1bd3c 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
@@ -167,4 +167,17 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessGraphics) {
ASSERT_TRUE(externalGS);
EXPECT_EQ(0.5f, externalGS->GetNumberFor("ca"));
EXPECT_EQ(0.8f, externalGS->GetNumberFor("CA"));
+
+ // Same path, now with a stroke.
+ pPathObj->m_GraphState.SetLineWidth(10.5f);
+ buf.Clear();
+ TestProcessPath(&generator, &buf, pPathObj.get());
+ CFX_ByteString pathString2 = buf.MakeString();
+ 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));
+ // Compare with the previous (should use same dictionary for gs)
+ EXPECT_EQ(pathString.GetLength() + 7, pathString2.GetLength());
+ EXPECT_EQ(pathString.Mid(48, pathString.GetLength() - 76),
+ pathString2.Mid(55, pathString2.GetLength() - 83));
}
diff --git a/fpdfsdk/fpdfedit_embeddertest.cpp b/fpdfsdk/fpdfedit_embeddertest.cpp
index 4a67c1720b..83d26312fc 100644
--- a/fpdfsdk/fpdfedit_embeddertest.cpp
+++ b/fpdfsdk/fpdfedit_embeddertest.cpp
@@ -267,3 +267,54 @@ TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
FPDFBitmap_Destroy(bitmap);
UnloadPage(page);
}
+
+TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
+ // Start with a blank page
+ FPDF_DOCUMENT doc = FPDF_CreateNewDocument();
+ FPDF_PAGE page = FPDFPage_New(doc, 0, 612, 792);
+
+ // Add a large stroked rectangle (fill color should not affect it).
+ FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
+ EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
+ EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
+ EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
+ EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
+ FPDFPage_InsertObject(page, rect);
+ EXPECT_TRUE(FPDFPage_GenerateContent(page));
+ FPDF_BITMAP page_bitmap = RenderPage(page);
+ CompareBitmap(page_bitmap, 612, 792, "64bd31f862a89e0a9e505a5af6efd506");
+ FPDFBitmap_Destroy(page_bitmap);
+
+ // Add crossed-checkmark
+ FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
+ EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
+ EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
+ EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
+ EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
+ EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
+ EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
+ EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
+ FPDFPage_InsertObject(page, check);
+ EXPECT_TRUE(FPDFPage_GenerateContent(page));
+ page_bitmap = RenderPage(page);
+ CompareBitmap(page_bitmap, 612, 792, "4b6f3b9d25c4e194821217d5016c3724");
+ FPDFBitmap_Destroy(page_bitmap);
+
+ // Add stroked and filled oval-ish path.
+ FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
+ EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
+ EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
+ EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
+ EXPECT_TRUE(FPDFPath_Close(path));
+ EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
+ EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
+ EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
+ EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
+ FPDFPage_InsertObject(page, path);
+ EXPECT_TRUE(FPDFPage_GenerateContent(page));
+ page_bitmap = RenderPage(page);
+ CompareBitmap(page_bitmap, 612, 792, "ff3e6a22326754944cc6e56609acd73b");
+ FPDFBitmap_Destroy(page_bitmap);
+ FPDF_ClosePage(page);
+ FPDF_CloseDocument(doc);
+}
diff --git a/fpdfsdk/fpdfeditpath.cpp b/fpdfsdk/fpdfeditpath.cpp
index 1d3673c2ae..f6e103c047 100644
--- a/fpdfsdk/fpdfeditpath.cpp
+++ b/fpdfsdk/fpdfeditpath.cpp
@@ -41,6 +41,15 @@ DLLEXPORT FPDF_BOOL FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path,
return true;
}
+DLLEXPORT FPDF_BOOL FPDFPath_SetStrokeWidth(FPDF_PAGEOBJECT path, float width) {
+ if (!path || width < 0.0f)
+ return false;
+
+ auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
+ pPathObj->m_GraphState.SetLineWidth(width);
+ return true;
+}
+
DLLEXPORT FPDF_BOOL FPDFPath_SetFillColor(FPDF_PAGEOBJECT path,
unsigned int R,
unsigned int G,
diff --git a/public/fpdf_edit.h b/public/fpdf_edit.h
index d151c5c6ab..a43d181c81 100644
--- a/public/fpdf_edit.h
+++ b/public/fpdf_edit.h
@@ -299,6 +299,14 @@ DLLEXPORT FPDF_BOOL FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path,
unsigned int B,
unsigned int A);
+// Set the stroke width of a path.
+//
+// path - the handle to the path object.
+// width - the width of the stroke.
+//
+// Returns TRUE on success
+DLLEXPORT FPDF_BOOL FPDFPath_SetStrokeWidth(FPDF_PAGEOBJECT path, float width);
+
// Set the fill RGBA of a path. Range of values: 0 - 255.
//
// path - the handle to the path object.