• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
10 
FPDFPageObj_CreateNewPath(float x,float y)11 DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPageObj_CreateNewPath(float x, float y) {
12   CPDF_PathObject* pPathObj = new CPDF_PathObject;
13   pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false);
14   pPathObj->DefaultStates();
15   return pPathObj;
16 }
17 
FPDFPageObj_CreateNewRect(float x,float y,float w,float h)18 DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPageObj_CreateNewRect(float x,
19                                                             float y,
20                                                             float w,
21                                                             float h) {
22   CPDF_PathObject* pPathObj = new CPDF_PathObject;
23   pPathObj->m_Path.AppendRect(x, y, x + w, y + h);
24   pPathObj->DefaultStates();
25   return pPathObj;
26 }
27 
FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path,unsigned int R,unsigned int G,unsigned int B,unsigned int A)28 DLLEXPORT FPDF_BOOL FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path,
29                                             unsigned int R,
30                                             unsigned int G,
31                                             unsigned int B,
32                                             unsigned int A) {
33   if (!path || R > 255 || G > 255 || B > 255 || A > 255)
34     return false;
35 
36   auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
37   pPathObj->m_GeneralState.SetStrokeAlpha(A / 255.f);
38   FX_FLOAT rgb[3] = {R / 255.f, G / 255.f, B / 255.f};
39   pPathObj->m_ColorState.SetStrokeColor(
40       CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3);
41   return true;
42 }
43 
FPDFPath_SetStrokeWidth(FPDF_PAGEOBJECT path,float width)44 DLLEXPORT FPDF_BOOL FPDFPath_SetStrokeWidth(FPDF_PAGEOBJECT path, float width) {
45   if (!path || width < 0.0f)
46     return false;
47 
48   auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
49   pPathObj->m_GraphState.SetLineWidth(width);
50   return true;
51 }
52 
FPDFPath_SetFillColor(FPDF_PAGEOBJECT path,unsigned int R,unsigned int G,unsigned int B,unsigned int A)53 DLLEXPORT FPDF_BOOL FPDFPath_SetFillColor(FPDF_PAGEOBJECT path,
54                                           unsigned int R,
55                                           unsigned int G,
56                                           unsigned int B,
57                                           unsigned int A) {
58   if (!path || R > 255 || G > 255 || B > 255 || A > 255)
59     return false;
60 
61   auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
62   pPathObj->m_GeneralState.SetFillAlpha(A / 255.f);
63   FX_FLOAT rgb[3] = {R / 255.f, G / 255.f, B / 255.f};
64   pPathObj->m_ColorState.SetFillColor(
65       CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3);
66   return true;
67 }
68 
FPDFPath_MoveTo(FPDF_PAGEOBJECT path,float x,float y)69 DLLEXPORT FPDF_BOOL FPDFPath_MoveTo(FPDF_PAGEOBJECT path, float x, float y) {
70   if (!path)
71     return false;
72 
73   auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
74   pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::MoveTo, false);
75   return true;
76 }
77 
FPDFPath_LineTo(FPDF_PAGEOBJECT path,float x,float y)78 DLLEXPORT FPDF_BOOL FPDFPath_LineTo(FPDF_PAGEOBJECT path, float x, float y) {
79   if (!path)
80     return false;
81 
82   auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
83   pPathObj->m_Path.AppendPoint(CFX_PointF(x, y), FXPT_TYPE::LineTo, false);
84   return true;
85 }
86 
FPDFPath_BezierTo(FPDF_PAGEOBJECT path,float x1,float y1,float x2,float y2,float x3,float y3)87 DLLEXPORT FPDF_BOOL FPDFPath_BezierTo(FPDF_PAGEOBJECT path,
88                                       float x1,
89                                       float y1,
90                                       float x2,
91                                       float y2,
92                                       float x3,
93                                       float y3) {
94   if (!path)
95     return false;
96 
97   auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
98   pPathObj->m_Path.AppendPoint(CFX_PointF(x1, y1), FXPT_TYPE::BezierTo, false);
99   pPathObj->m_Path.AppendPoint(CFX_PointF(x2, y2), FXPT_TYPE::BezierTo, false);
100   pPathObj->m_Path.AppendPoint(CFX_PointF(x3, y3), FXPT_TYPE::BezierTo, false);
101   return true;
102 }
103 
FPDFPath_Close(FPDF_PAGEOBJECT path)104 DLLEXPORT FPDF_BOOL FPDFPath_Close(FPDF_PAGEOBJECT path) {
105   if (!path)
106     return false;
107 
108   auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
109   if (pPathObj->m_Path.GetPoints().empty())
110     return false;
111 
112   pPathObj->m_Path.ClosePath();
113   return true;
114 }
115 
FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path,int fillmode,FPDF_BOOL stroke)116 DLLEXPORT FPDF_BOOL FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path,
117                                          int fillmode,
118                                          FPDF_BOOL stroke) {
119   if (!path)
120     return false;
121 
122   auto pPathObj = reinterpret_cast<CPDF_PathObject*>(path);
123 
124   if (fillmode == FPDF_FILLMODE_ALTERNATE)
125     pPathObj->m_FillType = FXFILL_ALTERNATE;
126   else if (fillmode == FPDF_FILLMODE_WINDING)
127     pPathObj->m_FillType = FXFILL_WINDING;
128   else
129     pPathObj->m_FillType = 0;
130   pPathObj->m_bStroke = stroke != 0;
131   return true;
132 }
133