Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fpdf_editpath.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
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 <memory>
8#include <utility>
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 "core/fxcrt/stl_util.h"
14#include "fpdfsdk/cpdfsdk_helpers.h"
15#include "third_party/base/containers/span.h"
16
17// These checks are here because core/ and public/ cannot depend on each other.
18static_assert(static_cast<int>(CFX_GraphStateData::LineCap::kButt) ==
20 "CFX_GraphStateData::LineCap::kButt value mismatch");
21static_assert(static_cast<int>(CFX_GraphStateData::LineCap::kRound) ==
23 "CFX_GraphStateData::LineCap::kRound value mismatch");
24static_assert(static_cast<int>(CFX_GraphStateData::LineCap::kSquare) ==
26 "CFX_GraphStateData::LineCap::kSquare value mismatch");
27
28static_assert(static_cast<int>(CFX_GraphStateData::LineJoin::kMiter) ==
30 "CFX_GraphStateData::LineJoin::kMiter value mismatch");
31static_assert(static_cast<int>(CFX_GraphStateData::LineJoin::kRound) ==
33 "CFX_GraphStateData::LineJoin::kRound value mismatch");
34static_assert(static_cast<int>(CFX_GraphStateData::LineJoin::kBevel) ==
36 "CFX_GraphStateData::LineJoin::kBevel value mismatch");
37
38static_assert(static_cast<int>(CFX_Path::Point::Type::kLine) ==
40 "CFX_Path::Point::Type::kLine value mismatch");
41static_assert(static_cast<int>(CFX_Path::Point::Type::kBezier) ==
43 "CFX_Path::Point::Type::kBezier value mismatch");
44static_assert(static_cast<int>(CFX_Path::Point::Type::kMove) ==
46 "CFX_Path::Point::Type::kMove value mismatch");
47
48namespace {
49
50CPDF_PathObject* CPDFPathObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
51 auto* obj = CPDFPageObjectFromFPDFPageObject(page_object);
52 return obj ? obj->AsPath() : nullptr;
53}
54
55} // namespace
56
58 float y) {
59 auto pPathObj = std::make_unique<CPDF_PathObject>();
60 pPathObj->path().AppendPoint(CFX_PointF(x, y), CFX_Path::Point::Type::kMove);
61 pPathObj->SetDefaultStates();
62
63 // Caller takes ownership.
64 return FPDFPageObjectFromCPDFPageObject(pPathObj.release());
65}
66
68 float y,
69 float w,
70 float h) {
71 auto pPathObj = std::make_unique<CPDF_PathObject>();
72 pPathObj->path().AppendRect(x, y, x + w, y + h);
73 pPathObj->SetDefaultStates();
74
75 // Caller takes ownership.
76 return FPDFPageObjectFromCPDFPageObject(pPathObj.release());
77}
78
80 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
81 if (!pPathObj)
82 return -1;
83 return fxcrt::CollectionSize<int>(pPathObj->path().GetPoints());
84}
85
86FPDF_EXPORT FPDF_PATHSEGMENT FPDF_CALLCONV
87FPDFPath_GetPathSegment(FPDF_PAGEOBJECT path, int index) {
88 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
89 if (!pPathObj)
90 return nullptr;
91
92 pdfium::span<const CFX_Path::Point> points = pPathObj->path().GetPoints();
93 if (!fxcrt::IndexInBounds(points, index))
94 return nullptr;
95
96 return FPDFPathSegmentFromFXPathPoint(&points[index]);
97}
98
99FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_MoveTo(FPDF_PAGEOBJECT path,
100 float x,
101 float y) {
102 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
103 if (!pPathObj)
104 return false;
105
107 pPathObj->SetDirty(true);
108 return true;
109}
110
111FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_LineTo(FPDF_PAGEOBJECT path,
112 float x,
113 float y) {
114 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
115 if (!pPathObj)
116 return false;
117
119 pPathObj->SetDirty(true);
120 return true;
121}
122
123FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_BezierTo(FPDF_PAGEOBJECT path,
124 float x1,
125 float y1,
126 float x2,
127 float y2,
128 float x3,
129 float y3) {
130 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
131 if (!pPathObj)
132 return false;
133
134 CPDF_Path& cpath = pPathObj->path();
135 cpath.AppendPoint(CFX_PointF(x1, y1), CFX_Path::Point::Type::kBezier);
136 cpath.AppendPoint(CFX_PointF(x2, y2), CFX_Path::Point::Type::kBezier);
137 cpath.AppendPoint(CFX_PointF(x3, y3), CFX_Path::Point::Type::kBezier);
138 pPathObj->SetDirty(true);
139 return true;
140}
141
142FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_Close(FPDF_PAGEOBJECT path) {
143 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
144 if (!pPathObj)
145 return false;
146
147 CPDF_Path& cpath = pPathObj->path();
148 if (cpath.GetPoints().empty())
149 return false;
150
151 cpath.ClosePath();
152 pPathObj->SetDirty(true);
153 return true;
154}
155
156FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path,
157 int fillmode,
158 FPDF_BOOL stroke) {
159 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
160 if (!pPathObj)
161 return false;
162
163 pPathObj->set_stroke(!!stroke);
164 if (fillmode == FPDF_FILLMODE_ALTERNATE)
166 else if (fillmode == FPDF_FILLMODE_WINDING)
168 else
169 pPathObj->set_no_filltype();
170 pPathObj->SetDirty(true);
171 return true;
172}
173
174FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_GetDrawMode(FPDF_PAGEOBJECT path,
175 int* fillmode,
176 FPDF_BOOL* stroke) {
177 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
178 if (!pPathObj || !fillmode || !stroke)
179 return false;
180
181 if (pPathObj->has_alternate_filltype())
182 *fillmode = FPDF_FILLMODE_ALTERNATE;
183 else if (pPathObj->has_winding_filltype())
184 *fillmode = FPDF_FILLMODE_WINDING;
185 else
186 *fillmode = FPDF_FILLMODE_NONE;
187
188 *stroke = pPathObj->stroke();
189 return true;
190}
191
193FPDFPathSegment_GetPoint(FPDF_PATHSEGMENT segment, float* x, float* y) {
194 auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
195 if (!pPathPoint || !x || !y)
196 return false;
197
198 *x = pPathPoint->m_Point.x;
199 *y = pPathPoint->m_Point.y;
200 return true;
201}
202
204FPDFPathSegment_GetType(FPDF_PATHSEGMENT segment) {
205 auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
206 return pPathPoint ? static_cast<int>(pPathPoint->m_Type)
208}
209
211FPDFPathSegment_GetClose(FPDF_PATHSEGMENT segment) {
212 auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
213 return pPathPoint && pPathPoint->m_CloseFigure;
214}
bool m_CloseFigure
Definition cfx_path.h:35
void SetDirty(bool value)
virtual CPDF_PathObject * AsPath()
void set_alternate_filltype()
void set_winding_filltype()
bool has_winding_filltype() const
bool stroke() const
CPDF_Path & path()
void set_stroke(bool stroke)
bool has_alternate_filltype() const
void AppendPoint(const CFX_PointF &point, CFX_Path::Point::Type type)
Definition cpdf_path.cpp:57
void ClosePath()
Definition cpdf_path.cpp:19
const CFX_Path::Point * FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment)
CPDF_PageObject * CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object)
FPDF_EXPORT FPDF_PATHSEGMENT FPDF_CALLCONV FPDFPath_GetPathSegment(FPDF_PAGEOBJECT path, int index)
#define FPDF_LINECAP_BUTT
Definition fpdf_edit.h:58
#define FPDF_FILLMODE_ALTERNATE
Definition fpdf_edit.h:52
FPDF_EXPORT int FPDF_CALLCONV FPDFPathSegment_GetType(FPDF_PATHSEGMENT segment)
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewPath(float x, float y)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path, int fillmode, FPDF_BOOL stroke)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPathSegment_GetPoint(FPDF_PATHSEGMENT segment, float *x, float *y)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_Close(FPDF_PAGEOBJECT path)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_GetDrawMode(FPDF_PAGEOBJECT path, int *fillmode, FPDF_BOOL *stroke)
#define FPDF_FILLMODE_NONE
Definition fpdf_edit.h:51
#define FPDF_LINEJOIN_MITER
Definition fpdf_edit.h:62
#define FPDF_LINEJOIN_ROUND
Definition fpdf_edit.h:63
#define FPDF_SEGMENT_MOVETO
Definition fpdf_edit.h:49
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewRect(float x, float y, float w, float h)
#define FPDF_LINECAP_PROJECTING_SQUARE
Definition fpdf_edit.h:60
#define FPDF_LINECAP_ROUND
Definition fpdf_edit.h:59
#define FPDF_SEGMENT_UNKNOWN
Definition fpdf_edit.h:46
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_LineTo(FPDF_PAGEOBJECT path, float x, float y)
#define FPDF_SEGMENT_BEZIERTO
Definition fpdf_edit.h:48
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_BezierTo(FPDF_PAGEOBJECT path, float x1, float y1, float x2, float y2, float x3, float y3)
FPDF_EXPORT int FPDF_CALLCONV FPDFPath_CountSegments(FPDF_PAGEOBJECT path)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_MoveTo(FPDF_PAGEOBJECT path, float x, float y)
#define FPDF_LINEJOIN_BEVEL
Definition fpdf_edit.h:64
#define FPDF_SEGMENT_LINETO
Definition fpdf_edit.h:47
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPathSegment_GetClose(FPDF_PATHSEGMENT segment)
#define FPDF_FILLMODE_WINDING
Definition fpdf_edit.h:53
#define FPDF_CALLCONV
Definition fpdfview.h:227
#define FPDF_EXPORT
Definition fpdfview.h:221