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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "xfa/fxfa/parser/cxfa_linear.h"
8 
9 #include "fxjs/xfa/cjx_linear.h"
10 #include "third_party/base/ptr_util.h"
11 #include "xfa/fxfa/parser/cxfa_color.h"
12 #include "xfa/fxgraphics/cxfa_geshading.h"
13 
14 namespace {
15 
16 const CXFA_Node::PropertyData kPropertyData[] = {{XFA_Element::Color, 1, 0},
17                                                  {XFA_Element::Extras, 1, 0},
18                                                  {XFA_Element::Unknown, 0, 0}};
19 const CXFA_Node::AttributeData kAttributeData[] = {
20     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
21     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
22     {XFA_Attribute::Type, XFA_AttributeType::Enum,
23      (void*)XFA_AttributeEnum::ToRight},
24     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
25     {XFA_Attribute::Unknown, XFA_AttributeType::Integer, nullptr}};
26 
27 constexpr wchar_t kName[] = L"linear";
28 
29 }  // namespace
30 
CXFA_Linear(CXFA_Document * doc,XFA_PacketType packet)31 CXFA_Linear::CXFA_Linear(CXFA_Document* doc, XFA_PacketType packet)
32     : CXFA_Node(doc,
33                 packet,
34                 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
35                 XFA_ObjectType::Node,
36                 XFA_Element::Linear,
37                 kPropertyData,
38                 kAttributeData,
39                 kName,
40                 pdfium::MakeUnique<CJX_Linear>(this)) {}
41 
~CXFA_Linear()42 CXFA_Linear::~CXFA_Linear() {}
43 
GetType()44 XFA_AttributeEnum CXFA_Linear::GetType() {
45   return JSObject()
46       ->TryEnum(XFA_Attribute::Type, true)
47       .value_or(XFA_AttributeEnum::ToRight);
48 }
49 
GetColorIfExists()50 CXFA_Color* CXFA_Linear::GetColorIfExists() {
51   return GetChild<CXFA_Color>(0, XFA_Element::Color, false);
52 }
53 
Draw(CXFA_Graphics * pGS,CXFA_GEPath * fillPath,FX_ARGB crStart,const CFX_RectF & rtFill,const CFX_Matrix & matrix)54 void CXFA_Linear::Draw(CXFA_Graphics* pGS,
55                        CXFA_GEPath* fillPath,
56                        FX_ARGB crStart,
57                        const CFX_RectF& rtFill,
58                        const CFX_Matrix& matrix) {
59   CXFA_Color* pColor = GetColorIfExists();
60   FX_ARGB crEnd = pColor ? pColor->GetValue() : CXFA_Color::kBlackColor;
61 
62   CFX_PointF ptStart;
63   CFX_PointF ptEnd;
64   switch (GetType()) {
65     case XFA_AttributeEnum::ToRight:
66       ptStart = CFX_PointF(rtFill.left, rtFill.top);
67       ptEnd = CFX_PointF(rtFill.right(), rtFill.top);
68       break;
69     case XFA_AttributeEnum::ToBottom:
70       ptStart = CFX_PointF(rtFill.left, rtFill.top);
71       ptEnd = CFX_PointF(rtFill.left, rtFill.bottom());
72       break;
73     case XFA_AttributeEnum::ToLeft:
74       ptStart = CFX_PointF(rtFill.right(), rtFill.top);
75       ptEnd = CFX_PointF(rtFill.left, rtFill.top);
76       break;
77     case XFA_AttributeEnum::ToTop:
78       ptStart = CFX_PointF(rtFill.left, rtFill.bottom());
79       ptEnd = CFX_PointF(rtFill.left, rtFill.top);
80       break;
81     default:
82       break;
83   }
84 
85   CXFA_GEShading shading(ptStart, ptEnd, false, false, crStart, crEnd);
86 
87   pGS->SaveGraphState();
88   pGS->SetFillColor(CXFA_GEColor(&shading));
89   pGS->FillPath(fillPath, FXFILL_WINDING, &matrix);
90   pGS->RestoreGraphState();
91 }
92