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 "core/fxge/render_defines.h"
10 #include "fxjs/xfa/cjx_node.h"
11 #include "third_party/base/ptr_util.h"
12 #include "xfa/fxfa/parser/cxfa_color.h"
13 #include "xfa/fxgraphics/cxfa_geshading.h"
14 
15 namespace {
16 
17 const CXFA_Node::PropertyData kLinearPropertyData[] = {
18     {XFA_Element::Color, 1, 0},
19     {XFA_Element::Extras, 1, 0},
20 };
21 
22 const CXFA_Node::AttributeData kLinearAttributeData[] = {
23     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
24     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
25     {XFA_Attribute::Type, XFA_AttributeType::Enum,
26      (void*)XFA_AttributeValue::ToRight},
27     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
28 };
29 
30 }  // namespace
31 
CXFA_Linear(CXFA_Document * doc,XFA_PacketType packet)32 CXFA_Linear::CXFA_Linear(CXFA_Document* doc, XFA_PacketType packet)
33     : CXFA_Node(doc,
34                 packet,
35                 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
36                 XFA_ObjectType::Node,
37                 XFA_Element::Linear,
38                 kLinearPropertyData,
39                 kLinearAttributeData,
40                 pdfium::MakeUnique<CJX_Node>(this)) {}
41 
42 CXFA_Linear::~CXFA_Linear() = default;
43 
GetType()44 XFA_AttributeValue CXFA_Linear::GetType() {
45   return JSObject()
46       ->TryEnum(XFA_Attribute::Type, true)
47       .value_or(XFA_AttributeValue::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_AttributeValue::ToRight:
66       ptStart = CFX_PointF(rtFill.left, rtFill.top);
67       ptEnd = CFX_PointF(rtFill.right(), rtFill.top);
68       break;
69     case XFA_AttributeValue::ToBottom:
70       ptStart = CFX_PointF(rtFill.left, rtFill.top);
71       ptEnd = CFX_PointF(rtFill.left, rtFill.bottom());
72       break;
73     case XFA_AttributeValue::ToLeft:
74       ptStart = CFX_PointF(rtFill.right(), rtFill.top);
75       ptEnd = CFX_PointF(rtFill.left, rtFill.top);
76       break;
77     case XFA_AttributeValue::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