blob: 19d47e6a440a432b2a155df30a0f162ca44fc8ef [file] [log] [blame]
Nicolas Pena55e026b2017-02-07 14:59:23 -05001// 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 "core/fpdfapi/page/cpdf_path.h"
8#include "core/fpdfapi/page/cpdf_pathobject.h"
9#include "core/fxcrt/fx_system.h"
wileyrya864e9fb2017-05-26 11:38:14 -050010#include "fpdfsdk/fsdk_define.h"
Tom Sepezfe91c6c2017-05-16 15:33:20 -070011#include "third_party/base/ptr_util.h"
Nicolas Pena55e026b2017-02-07 14:59:23 -050012
wileyrya8a598332017-06-01 15:33:42 -050013// These checks are here because core/ and public/ cannot depend on each other.
14static_assert(CFX_GraphStateData::LineCapButt == FPDF_LINECAP_BUTT,
15 "CFX_GraphStateData::LineCapButt value mismatch");
16static_assert(CFX_GraphStateData::LineCapRound == FPDF_LINECAP_ROUND,
17 "CFX_GraphStateData::LineCapRound value mismatch");
18static_assert(CFX_GraphStateData::LineCapSquare ==
19 FPDF_LINECAP_PROJECTING_SQUARE,
20 "CFX_GraphStateData::LineCapSquare value mismatch");
21
22static_assert(CFX_GraphStateData::LineJoinMiter == FPDF_LINEJOIN_MITER,
23 "CFX_GraphStateData::LineJoinMiter value mismatch");
24static_assert(CFX_GraphStateData::LineJoinRound == FPDF_LINEJOIN_ROUND,
25 "CFX_GraphStateData::LineJoinRound value mismatch");
26static_assert(CFX_GraphStateData::LineJoinBevel == FPDF_LINEJOIN_BEVEL,
27 "CFX_GraphStateData::LineJoinBevel value mismatch");
28
Dan Sinclair00d2ad12017-08-10 14:13:02 -040029FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewPath(float x,
30 float y) {
Tom Sepezfe91c6c2017-05-16 15:33:20 -070031 auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>();
dan sinclairb147e072017-02-22 19:56:15 -050032 pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false);
Nicolas Pena0fc185e2017-02-08 12:13:20 -050033 pPathObj->DefaultStates();
Tom Sepezfe91c6c2017-05-16 15:33:20 -070034 return pPathObj.release(); // Caller takes ownership.
Nicolas Pena55e026b2017-02-07 14:59:23 -050035}
36
Dan Sinclair00d2ad12017-08-10 14:13:02 -040037FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewRect(float x,
38 float y,
39 float w,
40 float h) {
Tom Sepezfe91c6c2017-05-16 15:33:20 -070041 auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>();
Nicolas Pena55e026b2017-02-07 14:59:23 -050042 pPathObj->m_Path.AppendRect(x, y, x + w, y + h);
Nicolas Pena0fc185e2017-02-08 12:13:20 -050043 pPathObj->DefaultStates();
Tom Sepezfe91c6c2017-05-16 15:33:20 -070044 return pPathObj.release(); // Caller takes ownership.
Nicolas Pena55e026b2017-02-07 14:59:23 -050045}
46
Dan Sinclair00d2ad12017-08-10 14:13:02 -040047FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
48FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path,
49 unsigned int R,
50 unsigned int G,
51 unsigned int B,
52 unsigned int A) {
Nicolas Pena55e026b2017-02-07 14:59:23 -050053 if (!path || R > 255 || G > 255 || B > 255 || A > 255)
54 return false;
55
Tom Sepezfe91c6c2017-05-16 15:33:20 -070056 float rgb[3] = {R / 255.f, G / 255.f, B / 255.f};
Jane Liu3b057432017-06-19 10:44:01 -040057 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
Nicolas Pena55e026b2017-02-07 14:59:23 -050058 pPathObj->m_GeneralState.SetStrokeAlpha(A / 255.f);
Nicolas Pena55e026b2017-02-07 14:59:23 -050059 pPathObj->m_ColorState.SetStrokeColor(
60 CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3);
wileyryae858aa42017-05-31 14:49:05 -050061 pPathObj->SetDirty(true);
Nicolas Pena55e026b2017-02-07 14:59:23 -050062 return true;
63}
64
Dan Sinclair00d2ad12017-08-10 14:13:02 -040065FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
66FPDFPath_GetStrokeColor(FPDF_PAGEOBJECT path,
67 unsigned int* R,
68 unsigned int* G,
69 unsigned int* B,
70 unsigned int* A) {
Jane Liu3b057432017-06-19 10:44:01 -040071 if (!path || !R || !G || !B || !A)
72 return false;
73
74 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
75 uint32_t strokeRGB = pPathObj->m_ColorState.GetStrokeRGB();
76 *R = FXSYS_GetRValue(strokeRGB);
77 *G = FXSYS_GetGValue(strokeRGB);
78 *B = FXSYS_GetBValue(strokeRGB);
79 *A = static_cast<unsigned int>(pPathObj->m_GeneralState.GetStrokeAlpha() *
80 255.f);
81 return true;
82}
83
Dan Sinclair00d2ad12017-08-10 14:13:02 -040084FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
85FPDFPath_SetStrokeWidth(FPDF_PAGEOBJECT path, float width) {
Nicolas Pena2eb1a702017-02-09 18:17:33 -050086 if (!path || width < 0.0f)
87 return false;
88
Jane Liu3b057432017-06-19 10:44:01 -040089 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
Nicolas Pena2eb1a702017-02-09 18:17:33 -050090 pPathObj->m_GraphState.SetLineWidth(width);
wileyryae858aa42017-05-31 14:49:05 -050091 pPathObj->SetDirty(true);
Nicolas Pena2eb1a702017-02-09 18:17:33 -050092 return true;
93}
94
Dan Sinclair00d2ad12017-08-10 14:13:02 -040095FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetFillColor(FPDF_PAGEOBJECT path,
96 unsigned int R,
97 unsigned int G,
98 unsigned int B,
99 unsigned int A) {
wileyrya864e9fb2017-05-26 11:38:14 -0500100 return FPDFPageObj_SetFillColor(path, R, G, B, A);
Nicolas Pena55e026b2017-02-07 14:59:23 -0500101}
102
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400103FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_GetFillColor(FPDF_PAGEOBJECT path,
104 unsigned int* R,
105 unsigned int* G,
106 unsigned int* B,
107 unsigned int* A) {
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200108 if (!path || !R || !G || !B || !A)
109 return false;
110
Jane Liu3b057432017-06-19 10:44:01 -0400111 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200112 uint32_t fillRGB = pPathObj->m_ColorState.GetFillRGB();
113 *R = FXSYS_GetRValue(fillRGB);
114 *G = FXSYS_GetGValue(fillRGB);
115 *B = FXSYS_GetBValue(fillRGB);
116 *A = static_cast<unsigned int>(pPathObj->m_GeneralState.GetFillAlpha() *
117 255.f);
118 return true;
119}
120
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400121FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_MoveTo(FPDF_PAGEOBJECT path,
122 float x,
123 float y) {
Nicolas Pena55e026b2017-02-07 14:59:23 -0500124 if (!path)
125 return false;
126
Jane Liu3b057432017-06-19 10:44:01 -0400127 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
dan sinclairb147e072017-02-22 19:56:15 -0500128 pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false);
wileyryae858aa42017-05-31 14:49:05 -0500129 pPathObj->SetDirty(true);
Nicolas Pena55e026b2017-02-07 14:59:23 -0500130 return true;
131}
132
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400133FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_LineTo(FPDF_PAGEOBJECT path,
134 float x,
135 float y) {
Nicolas Pena55e026b2017-02-07 14:59:23 -0500136 if (!path)
137 return false;
138
Jane Liu3b057432017-06-19 10:44:01 -0400139 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
dan sinclairb147e072017-02-22 19:56:15 -0500140 pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::LineTo, false);
wileyryae858aa42017-05-31 14:49:05 -0500141 pPathObj->SetDirty(true);
Nicolas Pena55e026b2017-02-07 14:59:23 -0500142 return true;
143}
144
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400145FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_BezierTo(FPDF_PAGEOBJECT path,
146 float x1,
147 float y1,
148 float x2,
149 float y2,
150 float x3,
151 float y3) {
Nicolas Pena55e026b2017-02-07 14:59:23 -0500152 if (!path)
153 return false;
154
Jane Liu3b057432017-06-19 10:44:01 -0400155 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
dan sinclairb147e072017-02-22 19:56:15 -0500156 pPathObj->m_Path.AppendPoint(CFX_PointF(x1, y1), FXPT_TYPE::BezierTo, false);
157 pPathObj->m_Path.AppendPoint(CFX_PointF(x2, y2), FXPT_TYPE::BezierTo, false);
158 pPathObj->m_Path.AppendPoint(CFX_PointF(x3, y3), FXPT_TYPE::BezierTo, false);
wileyryae858aa42017-05-31 14:49:05 -0500159 pPathObj->SetDirty(true);
Nicolas Pena55e026b2017-02-07 14:59:23 -0500160 return true;
161}
162
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400163FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_Close(FPDF_PAGEOBJECT path) {
Nicolas Pena55e026b2017-02-07 14:59:23 -0500164 if (!path)
165 return false;
166
Jane Liu3b057432017-06-19 10:44:01 -0400167 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
Dan Sinclaire4602322017-02-15 11:07:32 -0500168 if (pPathObj->m_Path.GetPoints().empty())
Nicolas Pena55e026b2017-02-07 14:59:23 -0500169 return false;
170
Dan Sinclaire4602322017-02-15 11:07:32 -0500171 pPathObj->m_Path.ClosePath();
wileyryae858aa42017-05-31 14:49:05 -0500172 pPathObj->SetDirty(true);
Nicolas Pena55e026b2017-02-07 14:59:23 -0500173 return true;
174}
175
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400176FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path,
177 int fillmode,
178 FPDF_BOOL stroke) {
Nicolas Pena55e026b2017-02-07 14:59:23 -0500179 if (!path)
180 return false;
181
Jane Liu3b057432017-06-19 10:44:01 -0400182 auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
Nicolas Pena55e026b2017-02-07 14:59:23 -0500183
184 if (fillmode == FPDF_FILLMODE_ALTERNATE)
185 pPathObj->m_FillType = FXFILL_ALTERNATE;
186 else if (fillmode == FPDF_FILLMODE_WINDING)
187 pPathObj->m_FillType = FXFILL_WINDING;
188 else
189 pPathObj->m_FillType = 0;
190 pPathObj->m_bStroke = stroke != 0;
wileyryae858aa42017-05-31 14:49:05 -0500191 pPathObj->SetDirty(true);
Nicolas Pena55e026b2017-02-07 14:59:23 -0500192 return true;
193}
wileyrya22a237f2017-05-26 09:26:27 -0500194
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400195FPDF_EXPORT void FPDF_CALLCONV FPDFPath_SetLineJoin(FPDF_PAGEOBJECT path,
196 int line_join) {
wileyrya22a237f2017-05-26 09:26:27 -0500197 if (!path)
198 return;
199 if (line_join <
200 static_cast<int>(CFX_GraphStateData::LineJoin::LineJoinMiter) ||
201 line_join >
202 static_cast<int>(CFX_GraphStateData::LineJoin::LineJoinBevel)) {
203 return;
204 }
Jane Liu1a084022017-06-29 19:47:12 -0400205 auto* pPathObj = CPDFPageObjectFromFPDFPageObject(path);
wileyrya22a237f2017-05-26 09:26:27 -0500206 CFX_GraphStateData::LineJoin lineJoin =
207 static_cast<CFX_GraphStateData::LineJoin>(line_join);
208 pPathObj->m_GraphState.SetLineJoin(lineJoin);
wileyryae858aa42017-05-31 14:49:05 -0500209 pPathObj->SetDirty(true);
wileyrya22a237f2017-05-26 09:26:27 -0500210}
211
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400212FPDF_EXPORT void FPDF_CALLCONV FPDFPath_SetLineCap(FPDF_PAGEOBJECT path,
213 int line_cap) {
wileyrya22a237f2017-05-26 09:26:27 -0500214 if (!path)
215 return;
216 if (line_cap < static_cast<int>(CFX_GraphStateData::LineCap::LineCapButt) ||
217 line_cap > static_cast<int>(CFX_GraphStateData::LineCap::LineCapSquare)) {
218 return;
219 }
Jane Liu1a084022017-06-29 19:47:12 -0400220 auto* pPathObj = CPDFPageObjectFromFPDFPageObject(path);
wileyrya22a237f2017-05-26 09:26:27 -0500221 CFX_GraphStateData::LineCap lineCap =
222 static_cast<CFX_GraphStateData::LineCap>(line_cap);
223 pPathObj->m_GraphState.SetLineCap(lineCap);
wileyryae858aa42017-05-31 14:49:05 -0500224 pPathObj->SetDirty(true);
wileyrya22a237f2017-05-26 09:26:27 -0500225}