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" |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 10 | #include "third_party/base/ptr_util.h" |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 11 | |
| 12 | DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPageObj_CreateNewPath(float x, float y) { |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 13 | auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>(); |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 14 | pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 15 | pPathObj->DefaultStates(); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 16 | return pPathObj.release(); // Caller takes ownership. |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPageObj_CreateNewRect(float x, |
| 20 | float y, |
| 21 | float w, |
| 22 | float h) { |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 23 | auto pPathObj = pdfium::MakeUnique<CPDF_PathObject>(); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 24 | pPathObj->m_Path.AppendRect(x, y, x + w, y + h); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 25 | pPathObj->DefaultStates(); |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 26 | return pPathObj.release(); // Caller takes ownership. |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | DLLEXPORT FPDF_BOOL FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path, |
| 30 | unsigned int R, |
| 31 | unsigned int G, |
| 32 | unsigned int B, |
| 33 | unsigned int A) { |
| 34 | if (!path || R > 255 || G > 255 || B > 255 || A > 255) |
| 35 | return false; |
| 36 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 37 | float rgb[3] = {R / 255.f, G / 255.f, B / 255.f}; |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 38 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 39 | pPathObj->m_GeneralState.SetStrokeAlpha(A / 255.f); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 40 | pPathObj->m_ColorState.SetStrokeColor( |
| 41 | CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3); |
| 42 | return true; |
| 43 | } |
| 44 | |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 45 | DLLEXPORT FPDF_BOOL FPDFPath_SetStrokeWidth(FPDF_PAGEOBJECT path, float width) { |
| 46 | if (!path || width < 0.0f) |
| 47 | return false; |
| 48 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 49 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 50 | pPathObj->m_GraphState.SetLineWidth(width); |
| 51 | return true; |
| 52 | } |
| 53 | |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 54 | DLLEXPORT FPDF_BOOL FPDFPath_SetFillColor(FPDF_PAGEOBJECT path, |
| 55 | unsigned int R, |
| 56 | unsigned int G, |
| 57 | unsigned int B, |
| 58 | unsigned int A) { |
| 59 | if (!path || R > 255 || G > 255 || B > 255 || A > 255) |
| 60 | return false; |
| 61 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 62 | float rgb[3] = {R / 255.f, G / 255.f, B / 255.f}; |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 63 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 64 | pPathObj->m_GeneralState.SetFillAlpha(A / 255.f); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 65 | pPathObj->m_ColorState.SetFillColor( |
| 66 | CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3); |
| 67 | return true; |
| 68 | } |
| 69 | |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 70 | DLLEXPORT FPDF_BOOL FPDFPath_GetFillColor(FPDF_PAGEOBJECT path, |
| 71 | unsigned int* R, |
| 72 | unsigned int* G, |
| 73 | unsigned int* B, |
| 74 | unsigned int* A) { |
| 75 | if (!path || !R || !G || !B || !A) |
| 76 | return false; |
| 77 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 78 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 79 | uint32_t fillRGB = pPathObj->m_ColorState.GetFillRGB(); |
| 80 | *R = FXSYS_GetRValue(fillRGB); |
| 81 | *G = FXSYS_GetGValue(fillRGB); |
| 82 | *B = FXSYS_GetBValue(fillRGB); |
| 83 | *A = static_cast<unsigned int>(pPathObj->m_GeneralState.GetFillAlpha() * |
| 84 | 255.f); |
| 85 | return true; |
| 86 | } |
| 87 | |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 88 | DLLEXPORT FPDF_BOOL FPDFPath_MoveTo(FPDF_PAGEOBJECT path, float x, float y) { |
| 89 | if (!path) |
| 90 | return false; |
| 91 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 92 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 93 | pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
| 97 | DLLEXPORT FPDF_BOOL FPDFPath_LineTo(FPDF_PAGEOBJECT path, float x, float y) { |
| 98 | if (!path) |
| 99 | return false; |
| 100 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 101 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 102 | pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::LineTo, false); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | DLLEXPORT FPDF_BOOL FPDFPath_BezierTo(FPDF_PAGEOBJECT path, |
| 107 | float x1, |
| 108 | float y1, |
| 109 | float x2, |
| 110 | float y2, |
| 111 | float x3, |
| 112 | float y3) { |
| 113 | if (!path) |
| 114 | return false; |
| 115 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 116 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 117 | pPathObj->m_Path.AppendPoint(CFX_PointF(x1, y1), FXPT_TYPE::BezierTo, false); |
| 118 | pPathObj->m_Path.AppendPoint(CFX_PointF(x2, y2), FXPT_TYPE::BezierTo, false); |
| 119 | pPathObj->m_Path.AppendPoint(CFX_PointF(x3, y3), FXPT_TYPE::BezierTo, false); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
| 123 | DLLEXPORT FPDF_BOOL FPDFPath_Close(FPDF_PAGEOBJECT path) { |
| 124 | if (!path) |
| 125 | return false; |
| 126 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 127 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 128 | if (pPathObj->m_Path.GetPoints().empty()) |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 129 | return false; |
| 130 | |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 131 | pPathObj->m_Path.ClosePath(); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
| 135 | DLLEXPORT FPDF_BOOL FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path, |
| 136 | int fillmode, |
| 137 | FPDF_BOOL stroke) { |
| 138 | if (!path) |
| 139 | return false; |
| 140 | |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 141 | auto* pPathObj = static_cast<CPDF_PathObject*>(path); |
Nicolas Pena | 55e026b | 2017-02-07 14:59:23 -0500 | [diff] [blame] | 142 | |
| 143 | if (fillmode == FPDF_FILLMODE_ALTERNATE) |
| 144 | pPathObj->m_FillType = FXFILL_ALTERNATE; |
| 145 | else if (fillmode == FPDF_FILLMODE_WINDING) |
| 146 | pPathObj->m_FillType = FXFILL_WINDING; |
| 147 | else |
| 148 | pPathObj->m_FillType = 0; |
| 149 | pPathObj->m_bStroke = stroke != 0; |
| 150 | return true; |
| 151 | } |