Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 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 "core/fpdfapi/page/cpdf_path.h" |
| 8 | #include "core/fpdfapi/page/cpdf_pathobject.h" |
| 9 | #include "core/fxcrt/fx_system.h" |
wileyrya | 864e9fb | 2017-05-26 11:38:14 -0500 | [diff] [blame] | 10 | #include "fpdfsdk/fsdk_define.h" |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 11 | #include "third_party/base/ptr_util.h" |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 12 | |
wileyrya | 8a59833 | 2017-06-01 15:33:42 -0500 | [diff] [blame] | 13 | // These checks are here because core/ and public/ cannot depend on each other. |
| 14 | static_assert(CFX_GraphStateData::LineCapButt == FPDF_LINECAP_BUTT, |
| 15 | "CFX_GraphStateData::LineCapButt value mismatch"); |
| 16 | static_assert(CFX_GraphStateData::LineCapRound == FPDF_LINECAP_ROUND, |
| 17 | "CFX_GraphStateData::LineCapRound value mismatch"); |
| 18 | static_assert(CFX_GraphStateData::LineCapSquare == |
| 19 | FPDF_LINECAP_PROJECTING_SQUARE, |
| 20 | "CFX_GraphStateData::LineCapSquare value mismatch"); |
| 21 | |
| 22 | static_assert(CFX_GraphStateData::LineJoinMiter == FPDF_LINEJOIN_MITER, |
| 23 | "CFX_GraphStateData::LineJoinMiter value mismatch"); |
| 24 | static_assert(CFX_GraphStateData::LineJoinRound == FPDF_LINEJOIN_ROUND, |
| 25 | "CFX_GraphStateData::LineJoinRound value mismatch"); |
| 26 | static_assert(CFX_GraphStateData::LineJoinBevel == FPDF_LINEJOIN_BEVEL, |
| 27 | "CFX_GraphStateData::LineJoinBevel value mismatch"); |
| 28 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 29 | FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewPath(float x, |
| 30 | float y) { |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 31 | auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>(); |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 32 | pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 33 | pPathObj->DefaultStates(); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 34 | return pPathObj.release(); // Caller takes ownership. |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 37 | FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewRect(float x, |
| 38 | float y, |
| 39 | float w, |
| 40 | float h) { |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 41 | auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>(); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 42 | pPathObj->m_Path.AppendRect(x, y, x + w, y + h); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 43 | pPathObj->DefaultStates(); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 44 | return pPathObj.release(); // Caller takes ownership. |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 47 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 48 | FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path, |
| 49 | unsigned int R, |
| 50 | unsigned int G, |
| 51 | unsigned int B, |
| 52 | unsigned int A) { |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 53 | if (!path || R > 255 || G > 255 || B > 255 || A > 255) |
| 54 | return false; |
| 55 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 56 | float rgb[3] = {R / 255.f, G / 255.f, B / 255.f}; |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 57 | auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 58 | pPathObj->m_GeneralState.SetStrokeAlpha(A / 255.f); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 59 | pPathObj->m_ColorState.SetStrokeColor( |
| 60 | CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 61 | pPathObj->SetDirty(true); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 65 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 66 | FPDFPath_GetStrokeColor(FPDF_PAGEOBJECT path, |
| 67 | unsigned int* R, |
| 68 | unsigned int* G, |
| 69 | unsigned int* B, |
| 70 | unsigned int* A) { |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 71 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 84 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 85 | FPDFPath_SetStrokeWidth(FPDF_PAGEOBJECT path, float width) { |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 86 | if (!path || width < 0.0f) |
| 87 | return false; |
| 88 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 89 | auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 90 | pPathObj->m_GraphState.SetLineWidth(width); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 91 | pPathObj->SetDirty(true); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 95 | FPDF_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) { |
wileyrya | 864e9fb | 2017-05-26 11:38:14 -0500 | [diff] [blame] | 100 | return FPDFPageObj_SetFillColor(path, R, G, B, A); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 103 | FPDF_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 Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 108 | if (!path || !R || !G || !B || !A) |
| 109 | return false; |
| 110 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 111 | auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path); |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 112 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 121 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_MoveTo(FPDF_PAGEOBJECT path, |
| 122 | float x, |
| 123 | float y) { |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 124 | if (!path) |
| 125 | return false; |
| 126 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 127 | auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path); |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 128 | pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 129 | pPathObj->SetDirty(true); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 133 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_LineTo(FPDF_PAGEOBJECT path, |
| 134 | float x, |
| 135 | float y) { |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 136 | if (!path) |
| 137 | return false; |
| 138 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 139 | auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path); |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 140 | pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::LineTo, false); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 141 | pPathObj->SetDirty(true); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 145 | FPDF_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 Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 152 | if (!path) |
| 153 | return false; |
| 154 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 155 | auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path); |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 156 | 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); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 159 | pPathObj->SetDirty(true); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 163 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_Close(FPDF_PAGEOBJECT path) { |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 164 | if (!path) |
| 165 | return false; |
| 166 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 167 | auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path); |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 168 | if (pPathObj->m_Path.GetPoints().empty()) |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 169 | return false; |
| 170 | |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 171 | pPathObj->m_Path.ClosePath(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 172 | pPathObj->SetDirty(true); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 176 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path, |
| 177 | int fillmode, |
| 178 | FPDF_BOOL stroke) { |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 179 | if (!path) |
| 180 | return false; |
| 181 | |
Jane Liu | 3b05743 | 2017-06-19 10:44:01 -0400 | [diff] [blame] | 182 | auto* pPathObj = CPDFPathObjectFromFPDFPageObject(path); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 183 | |
| 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; |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 191 | pPathObj->SetDirty(true); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 192 | return true; |
| 193 | } |
wileyrya | 22a237f | 2017-05-26 09:26:27 -0500 | [diff] [blame] | 194 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 195 | FPDF_EXPORT void FPDF_CALLCONV FPDFPath_SetLineJoin(FPDF_PAGEOBJECT path, |
| 196 | int line_join) { |
wileyrya | 22a237f | 2017-05-26 09:26:27 -0500 | [diff] [blame] | 197 | 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 Liu | 1a08402 | 2017-06-29 19:47:12 -0400 | [diff] [blame] | 205 | auto* pPathObj = CPDFPageObjectFromFPDFPageObject(path); |
wileyrya | 22a237f | 2017-05-26 09:26:27 -0500 | [diff] [blame] | 206 | CFX_GraphStateData::LineJoin lineJoin = |
| 207 | static_cast<CFX_GraphStateData::LineJoin>(line_join); |
| 208 | pPathObj->m_GraphState.SetLineJoin(lineJoin); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 209 | pPathObj->SetDirty(true); |
wileyrya | 22a237f | 2017-05-26 09:26:27 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 212 | FPDF_EXPORT void FPDF_CALLCONV FPDFPath_SetLineCap(FPDF_PAGEOBJECT path, |
| 213 | int line_cap) { |
wileyrya | 22a237f | 2017-05-26 09:26:27 -0500 | [diff] [blame] | 214 | 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 Liu | 1a08402 | 2017-06-29 19:47:12 -0400 | [diff] [blame] | 220 | auto* pPathObj = CPDFPageObjectFromFPDFPageObject(path); |
wileyrya | 22a237f | 2017-05-26 09:26:27 -0500 | [diff] [blame] | 221 | CFX_GraphStateData::LineCap lineCap = |
| 222 | static_cast<CFX_GraphStateData::LineCap>(line_cap); |
| 223 | pPathObj->m_GraphState.SetLineCap(lineCap); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 224 | pPathObj->SetDirty(true); |
wileyrya | 22a237f | 2017-05-26 09:26:27 -0500 | [diff] [blame] | 225 | } |