Add line width option for stroking paths

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>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index a5bd741..9de97ee 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -192,6 +192,7 @@
 // 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 @@
     *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 3267f52..4846b1b 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
@@ -167,4 +167,17 @@
   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 4a67c17..83d2631 100644
--- a/fpdfsdk/fpdfedit_embeddertest.cpp
+++ b/fpdfsdk/fpdfedit_embeddertest.cpp
@@ -267,3 +267,54 @@
   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 1d3673c..f6e103c 100644
--- a/fpdfsdk/fpdfeditpath.cpp
+++ b/fpdfsdk/fpdfeditpath.cpp
@@ -41,6 +41,15 @@
   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 d151c5c..a43d181 100644
--- a/public/fpdf_edit.h
+++ b/public/fpdf_edit.h
@@ -299,6 +299,14 @@
                                             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.