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_pattern.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_gepattern.h"
14 
15 namespace {
16 
17 const CXFA_Node::PropertyData kPatternPropertyData[] = {
18     {XFA_Element::Color, 1, 0},
19     {XFA_Element::Extras, 1, 0},
20 };
21 
22 const CXFA_Node::AttributeData kPatternAttributeData[] = {
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::CrossHatch},
27     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
28 };
29 
30 }  // namespace
31 
CXFA_Pattern(CXFA_Document * doc,XFA_PacketType packet)32 CXFA_Pattern::CXFA_Pattern(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::Pattern,
38                 kPatternPropertyData,
39                 kPatternAttributeData,
40                 pdfium::MakeUnique<CJX_Node>(this)) {}
41 
42 CXFA_Pattern::~CXFA_Pattern() = default;
43 
GetColorIfExists()44 CXFA_Color* CXFA_Pattern::GetColorIfExists() {
45   return GetChild<CXFA_Color>(0, XFA_Element::Color, false);
46 }
47 
GetType()48 XFA_AttributeValue CXFA_Pattern::GetType() {
49   return JSObject()->GetEnum(XFA_Attribute::Type);
50 }
51 
Draw(CXFA_Graphics * pGS,CXFA_GEPath * fillPath,FX_ARGB crStart,const CFX_RectF & rtFill,const CFX_Matrix & matrix)52 void CXFA_Pattern::Draw(CXFA_Graphics* pGS,
53                         CXFA_GEPath* fillPath,
54                         FX_ARGB crStart,
55                         const CFX_RectF& rtFill,
56                         const CFX_Matrix& matrix) {
57   CXFA_Color* pColor = GetColorIfExists();
58   FX_ARGB crEnd = pColor ? pColor->GetValue() : CXFA_Color::kBlackColor;
59 
60   FX_HatchStyle iHatch = FX_HatchStyle::Cross;
61   switch (GetType()) {
62     case XFA_AttributeValue::CrossDiagonal:
63       iHatch = FX_HatchStyle::DiagonalCross;
64       break;
65     case XFA_AttributeValue::DiagonalLeft:
66       iHatch = FX_HatchStyle::ForwardDiagonal;
67       break;
68     case XFA_AttributeValue::DiagonalRight:
69       iHatch = FX_HatchStyle::BackwardDiagonal;
70       break;
71     case XFA_AttributeValue::Horizontal:
72       iHatch = FX_HatchStyle::Horizontal;
73       break;
74     case XFA_AttributeValue::Vertical:
75       iHatch = FX_HatchStyle::Vertical;
76       break;
77     default:
78       break;
79   }
80 
81   CXFA_GEPattern pattern(iHatch, crEnd, crStart);
82 
83   pGS->SaveGraphState();
84   pGS->SetFillColor(CXFA_GEColor(&pattern, 0x0));
85   pGS->FillPath(fillPath, FXFILL_WINDING, &matrix);
86   pGS->RestoreGraphState();
87 }
88