1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "public/fpdf_edit.h"
#include <vector>
#include "core/fpdfapi/page/cpdf_path.h"
#include "core/fpdfapi/page/cpdf_pathobject.h"
#include "core/fxcrt/fx_system.h"
#include "fpdfsdk/cpdfsdk_helpers.h"
#include "third_party/base/ptr_util.h"
// These checks are here because core/ and public/ cannot depend on each other.
static_assert(CFX_GraphStateData::LineCapButt == FPDF_LINECAP_BUTT,
"CFX_GraphStateData::LineCapButt value mismatch");
static_assert(CFX_GraphStateData::LineCapRound == FPDF_LINECAP_ROUND,
"CFX_GraphStateData::LineCapRound value mismatch");
static_assert(CFX_GraphStateData::LineCapSquare ==
FPDF_LINECAP_PROJECTING_SQUARE,
"CFX_GraphStateData::LineCapSquare value mismatch");
static_assert(CFX_GraphStateData::LineJoinMiter == FPDF_LINEJOIN_MITER,
"CFX_GraphStateData::LineJoinMiter value mismatch");
static_assert(CFX_GraphStateData::LineJoinRound == FPDF_LINEJOIN_ROUND,
"CFX_GraphStateData::LineJoinRound value mismatch");
static_assert(CFX_GraphStateData::LineJoinBevel == FPDF_LINEJOIN_BEVEL,
"CFX_GraphStateData::LineJoinBevel value mismatch");
static_assert(static_cast<int>(FXPT_TYPE::LineTo) == FPDF_SEGMENT_LINETO,
"FXPT_TYPE::LineTo value mismatch");
static_assert(static_cast<int>(FXPT_TYPE::BezierTo) == FPDF_SEGMENT_BEZIERTO,
"FXPT_TYPE::BezierTo value mismatch");
static_assert(static_cast<int>(FXPT_TYPE::MoveTo) == FPDF_SEGMENT_MOVETO,
"FXPT_TYPE::MoveTo value mismatch");
namespace {
CPDF_PathObject* CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
auto* obj = CPDFPageObjectFromFPDFPageObject(page_object);
return obj ? obj->AsPath() : nullptr;
}
const FX_PATHPOINT* FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment) {
return static_cast<const FX_PATHPOINT*>(segment);
}
unsigned int GetAlphaAsUnsignedInt(float alpha) {
return static_cast<unsigned int>(alpha * 255.f + 0.5f);
}
} // namespace
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewPath(float x,
float y) {
auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>();
pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false);
pPathObj->DefaultStates();
return pPathObj.release(); // Caller takes ownership.
}
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewRect(float x,
float y,
float w,
float h) {
auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>();
pPathObj->m_Path.AppendRect(x, y, x + w, y + h);
pPathObj->DefaultStates();
return pPathObj.release(); // Caller takes ownership.
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path,
unsigned int R,
unsigned int G,
unsigned int B,
unsigned int A) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj || R > 255 || G > 255 || B > 255 || A > 255)
return false;
float rgb[3] = {R / 255.f, G / 255.f, B / 255.f};
pPathObj->m_GeneralState.SetStrokeAlpha(A / 255.f);
pPathObj->m_ColorState.SetStrokeColor(
CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3);
pPathObj->SetDirty(true);
return true;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPath_GetStrokeColor(FPDF_PAGEOBJECT path,
unsigned int* R,
unsigned int* G,
unsigned int* B,
unsigned int* A) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj || !R || !G || !B || !A)
return false;
FX_COLORREF strokeColor = pPathObj->m_ColorState.GetStrokeColorRef();
*R = FXSYS_GetRValue(strokeColor);
*G = FXSYS_GetGValue(strokeColor);
*B = FXSYS_GetBValue(strokeColor);
*A = GetAlphaAsUnsignedInt(pPathObj->m_GeneralState.GetStrokeAlpha());
return true;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPath_SetStrokeWidth(FPDF_PAGEOBJECT path, float width) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj || width < 0.0f)
return false;
pPathObj->m_GraphState.SetLineWidth(width);
pPathObj->SetDirty(true);
return true;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetFillColor(FPDF_PAGEOBJECT path,
unsigned int R,
unsigned int G,
unsigned int B,
unsigned int A) {
return FPDFPageObj_SetFillColor(path, R, G, B, A);
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_GetFillColor(FPDF_PAGEOBJECT path,
unsigned int* R,
unsigned int* G,
unsigned int* B,
unsigned int* A) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj || !R || !G || !B || !A)
return false;
FX_COLORREF fillColor = pPathObj->m_ColorState.GetFillColorRef();
*R = FXSYS_GetRValue(fillColor);
*G = FXSYS_GetGValue(fillColor);
*B = FXSYS_GetBValue(fillColor);
*A = GetAlphaAsUnsignedInt(pPathObj->m_GeneralState.GetFillAlpha());
return true;
}
FPDF_EXPORT int FPDF_CALLCONV FPDFPath_CountSegments(FPDF_PAGEOBJECT path) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj)
return -1;
return pdfium::CollectionSize<int>(pPathObj->m_Path.GetPoints());
}
FPDF_EXPORT FPDF_PATHSEGMENT FPDF_CALLCONV
FPDFPath_GetPathSegment(FPDF_PAGEOBJECT path, int index) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj)
return nullptr;
const std::vector<FX_PATHPOINT>& points = pPathObj->m_Path.GetPoints();
return pdfium::IndexInBounds(points, index) ? &points[index] : nullptr;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_MoveTo(FPDF_PAGEOBJECT path,
float x,
float y) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj)
return false;
pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false);
pPathObj->SetDirty(true);
return true;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_LineTo(FPDF_PAGEOBJECT path,
float x,
float y) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj)
return false;
pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::LineTo, false);
pPathObj->SetDirty(true);
return true;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_BezierTo(FPDF_PAGEOBJECT path,
float x1,
float y1,
float x2,
float y2,
float x3,
float y3) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj)
return false;
pPathObj->m_Path.AppendPoint(CFX_PointF(x1, y1), FXPT_TYPE::BezierTo, false);
pPathObj->m_Path.AppendPoint(CFX_PointF(x2, y2), FXPT_TYPE::BezierTo, false);
pPathObj->m_Path.AppendPoint(CFX_PointF(x3, y3), FXPT_TYPE::BezierTo, false);
pPathObj->SetDirty(true);
return true;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_Close(FPDF_PAGEOBJECT path) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj)
return false;
if (pPathObj->m_Path.GetPoints().empty())
return false;
pPathObj->m_Path.ClosePath();
pPathObj->SetDirty(true);
return true;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path,
int fillmode,
FPDF_BOOL stroke) {
auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj)
return false;
if (fillmode == FPDF_FILLMODE_ALTERNATE)
pPathObj->m_FillType = FXFILL_ALTERNATE;
else if (fillmode == FPDF_FILLMODE_WINDING)
pPathObj->m_FillType = FXFILL_WINDING;
else
pPathObj->m_FillType = 0;
pPathObj->m_bStroke = stroke != 0;
pPathObj->SetDirty(true);
return true;
}
FPDF_EXPORT void FPDF_CALLCONV FPDFPath_SetLineJoin(FPDF_PAGEOBJECT path,
int line_join) {
if (!path)
return;
if (line_join <
static_cast<int>(CFX_GraphStateData::LineJoin::LineJoinMiter) ||
line_join >
static_cast<int>(CFX_GraphStateData::LineJoin::LineJoinBevel)) {
return;
}
auto* pPathObj = CPDFPageObjectFromFPDFPageObject(path);
CFX_GraphStateData::LineJoin lineJoin =
static_cast<CFX_GraphStateData::LineJoin>(line_join);
pPathObj->m_GraphState.SetLineJoin(lineJoin);
pPathObj->SetDirty(true);
}
FPDF_EXPORT void FPDF_CALLCONV FPDFPath_SetLineCap(FPDF_PAGEOBJECT path,
int line_cap) {
if (!path)
return;
if (line_cap < static_cast<int>(CFX_GraphStateData::LineCap::LineCapButt) ||
line_cap > static_cast<int>(CFX_GraphStateData::LineCap::LineCapSquare)) {
return;
}
auto* pPathObj = CPDFPageObjectFromFPDFPageObject(path);
CFX_GraphStateData::LineCap lineCap =
static_cast<CFX_GraphStateData::LineCap>(line_cap);
pPathObj->m_GraphState.SetLineCap(lineCap);
pPathObj->SetDirty(true);
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPathSegment_GetPoint(FPDF_PATHSEGMENT segment, float* x, float* y) {
auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
if (!pPathPoint || !x || !y)
return false;
*x = pPathPoint->m_Point.x;
*y = pPathPoint->m_Point.y;
return true;
}
FPDF_EXPORT int FPDF_CALLCONV
FPDFPathSegment_GetType(FPDF_PATHSEGMENT segment) {
auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
return pPathPoint ? static_cast<int>(pPathPoint->m_Type)
: FPDF_SEGMENT_UNKNOWN;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPathSegment_GetClose(FPDF_PATHSEGMENT segment) {
auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
return pPathPoint ? pPathPoint->m_CloseFigure : false;
}
|