1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "public/fpdf_edit.h"
6 
7 #include <utility>
8 #include <vector>
9 
10 #include "core/fpdfapi/page/cpdf_path.h"
11 #include "core/fpdfapi/page/cpdf_pathobject.h"
12 #include "core/fxcrt/fx_system.h"
13 #include "fpdfsdk/cpdfsdk_helpers.h"
14 #include "third_party/base/ptr_util.h"
15 #include "third_party/base/stl_util.h"
16 
17 // These checks are here because core/ and public/ cannot depend on each other.
18 static_assert(CFX_GraphStateData::LineCapButt == FPDF_LINECAP_BUTT,
19               "CFX_GraphStateData::LineCapButt value mismatch");
20 static_assert(CFX_GraphStateData::LineCapRound == FPDF_LINECAP_ROUND,
21               "CFX_GraphStateData::LineCapRound value mismatch");
22 static_assert(CFX_GraphStateData::LineCapSquare ==
23                   FPDF_LINECAP_PROJECTING_SQUARE,
24               "CFX_GraphStateData::LineCapSquare value mismatch");
25 
26 static_assert(CFX_GraphStateData::LineJoinMiter == FPDF_LINEJOIN_MITER,
27               "CFX_GraphStateData::LineJoinMiter value mismatch");
28 static_assert(CFX_GraphStateData::LineJoinRound == FPDF_LINEJOIN_ROUND,
29               "CFX_GraphStateData::LineJoinRound value mismatch");
30 static_assert(CFX_GraphStateData::LineJoinBevel == FPDF_LINEJOIN_BEVEL,
31               "CFX_GraphStateData::LineJoinBevel value mismatch");
32 
33 static_assert(static_cast<int>(FXPT_TYPE::LineTo) == FPDF_SEGMENT_LINETO,
34               "FXPT_TYPE::LineTo value mismatch");
35 static_assert(static_cast<int>(FXPT_TYPE::BezierTo) == FPDF_SEGMENT_BEZIERTO,
36               "FXPT_TYPE::BezierTo value mismatch");
37 static_assert(static_cast<int>(FXPT_TYPE::MoveTo) == FPDF_SEGMENT_MOVETO,
38               "FXPT_TYPE::MoveTo value mismatch");
39 
40 namespace {
41 
CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object)42 CPDF_PathObject* CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
43   auto* obj = CPDFPageObjectFromFPDFPageObject(page_object);
44   return obj ? obj->AsPath() : nullptr;
45 }
46 
47 }  // namespace
48 
FPDFPageObj_CreateNewPath(float x,float y)49 FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewPath(float x,
50                                                                     float y) {
51   auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>();
52   pPathObj->path().AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false);
53   pPathObj->DefaultStates();
54 
55   // Caller takes ownership.
56   return FPDFPageObjectFromCPDFPageObject(pPathObj.release());
57 }
58 
FPDFPageObj_CreateNewRect(float x,float y,float w,float h)59 FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewRect(float x,
60                                                                     float y,
61                                                                     float w,
62                                                                     float h) {
63   auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>();
64   pPathObj->path().AppendRect(x, y, x + w, y + h);
65   pPathObj->DefaultStates();
66 
67   // Caller takes ownership.
68   return FPDFPageObjectFromCPDFPageObject(pPathObj.release());
69 }
70 
FPDFPath_CountSegments(FPDF_PAGEOBJECT path)71 FPDF_EXPORT int FPDF_CALLCONV FPDFPath_CountSegments(FPDF_PAGEOBJECT path) {
72   auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
73   if (!pPathObj)
74     return -1;
75   return pdfium::CollectionSize<int>(pPathObj->path().GetPoints());
76 }
77 
78 FPDF_EXPORT FPDF_PATHSEGMENT FPDF_CALLCONV
FPDFPath_GetPathSegment(FPDF_PAGEOBJECT path,int index)79 FPDFPath_GetPathSegment(FPDF_PAGEOBJECT path, int index) {
80   auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
81   if (!pPathObj)
82     return nullptr;
83 
84   const std::vector<FX_PATHPOINT>& points = pPathObj->path().GetPoints();
85   if (!pdfium::IndexInBounds(points, index))
86     return nullptr;
87 
88   return FPDFPathSegmentFromFXPathPoint(&points[index]);
89 }
90 
FPDFPath_MoveTo(FPDF_PAGEOBJECT path,float x,float y)91 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_MoveTo(FPDF_PAGEOBJECT path,
92                                                     float x,
93                                                     float y) {
94   auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
95   if (!pPathObj)
96     return false;
97 
98   pPathObj->path().AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false);
99   pPathObj->SetDirty(true);
100   return true;
101 }
102 
FPDFPath_LineTo(FPDF_PAGEOBJECT path,float x,float y)103 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_LineTo(FPDF_PAGEOBJECT path,
104                                                     float x,
105                                                     float y) {
106   auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
107   if (!pPathObj)
108     return false;
109 
110   pPathObj->path().AppendPoint(CFX_PointF(x, y), FXPT_TYPE::LineTo, false);
111   pPathObj->SetDirty(true);
112   return true;
113 }
114 
FPDFPath_BezierTo(FPDF_PAGEOBJECT path,float x1,float y1,float x2,float y2,float x3,float y3)115 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_BezierTo(FPDF_PAGEOBJECT path,
116                                                       float x1,
117                                                       float y1,
118                                                       float x2,
119                                                       float y2,
120                                                       float x3,
121                                                       float y3) {
122   auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
123   if (!pPathObj)
124     return false;
125 
126   CPDF_Path& cpath = pPathObj->path();
127   cpath.AppendPoint(CFX_PointF(x1, y1), FXPT_TYPE::BezierTo, false);
128   cpath.AppendPoint(CFX_PointF(x2, y2), FXPT_TYPE::BezierTo, false);
129   cpath.AppendPoint(CFX_PointF(x3, y3), FXPT_TYPE::BezierTo, false);
130   pPathObj->SetDirty(true);
131   return true;
132 }
133 
FPDFPath_Close(FPDF_PAGEOBJECT path)134 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_Close(FPDF_PAGEOBJECT path) {
135   auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
136   if (!pPathObj)
137     return false;
138 
139   CPDF_Path& cpath = pPathObj->path();
140   if (cpath.GetPoints().empty())
141     return false;
142 
143   cpath.ClosePath();
144   pPathObj->SetDirty(true);
145   return true;
146 }
147 
FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path,int fillmode,FPDF_BOOL stroke)148 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path,
149                                                          int fillmode,
150                                                          FPDF_BOOL stroke) {
151   auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
152   if (!pPathObj)
153     return false;
154 
155   pPathObj->set_stroke(!!stroke);
156   if (fillmode == FPDF_FILLMODE_ALTERNATE)
157     pPathObj->set_alternate_filltype();
158   else if (fillmode == FPDF_FILLMODE_WINDING)
159     pPathObj->set_winding_filltype();
160   else
161     pPathObj->set_no_filltype();
162   pPathObj->SetDirty(true);
163   return true;
164 }
165 
FPDFPath_GetDrawMode(FPDF_PAGEOBJECT path,int * fillmode,FPDF_BOOL * stroke)166 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_GetDrawMode(FPDF_PAGEOBJECT path,
167                                                          int* fillmode,
168                                                          FPDF_BOOL* stroke) {
169   auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
170   if (!pPathObj || !fillmode || !stroke)
171     return false;
172 
173   if (pPathObj->has_alternate_filltype())
174     *fillmode = FPDF_FILLMODE_ALTERNATE;
175   else if (pPathObj->has_winding_filltype())
176     *fillmode = FPDF_FILLMODE_WINDING;
177   else
178     *fillmode = FPDF_FILLMODE_NONE;
179 
180   *stroke = pPathObj->stroke();
181   return true;
182 }
183 
FPDFPath_GetMatrix(FPDF_PAGEOBJECT path,FS_MATRIX * matrix)184 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_GetMatrix(FPDF_PAGEOBJECT path,
185                                                        FS_MATRIX* matrix) {
186   if (!path || !matrix)
187     return false;
188 
189   CPDF_PathObject* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
190   if (!pPathObj)
191     return false;
192 
193   *matrix = FSMatrixFromCFXMatrix(pPathObj->matrix());
194   return true;
195 }
196 
197 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPath_SetMatrix(FPDF_PAGEOBJECT path,const FS_MATRIX * matrix)198 FPDFPath_SetMatrix(FPDF_PAGEOBJECT path, const FS_MATRIX* matrix) {
199   if (!matrix)
200     return false;
201 
202   CPDF_PathObject* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
203   if (!pPathObj)
204     return false;
205 
206   pPathObj->set_matrix(CFXMatrixFromFSMatrix(*matrix));
207   pPathObj->SetDirty(true);
208   return true;
209 }
210 
211 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPathSegment_GetPoint(FPDF_PATHSEGMENT segment,float * x,float * y)212 FPDFPathSegment_GetPoint(FPDF_PATHSEGMENT segment, float* x, float* y) {
213   auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
214   if (!pPathPoint || !x || !y)
215     return false;
216 
217   *x = pPathPoint->m_Point.x;
218   *y = pPathPoint->m_Point.y;
219   return true;
220 }
221 
222 FPDF_EXPORT int FPDF_CALLCONV
FPDFPathSegment_GetType(FPDF_PATHSEGMENT segment)223 FPDFPathSegment_GetType(FPDF_PATHSEGMENT segment) {
224   auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
225   return pPathPoint ? static_cast<int>(pPathPoint->m_Type)
226                     : FPDF_SEGMENT_UNKNOWN;
227 }
228 
229 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPathSegment_GetClose(FPDF_PATHSEGMENT segment)230 FPDFPathSegment_GetClose(FPDF_PATHSEGMENT segment) {
231   auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
232   return pPathPoint && pPathPoint->m_CloseFigure;
233 }
234