Home | History | Annotate | Download | only in parser
      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 "fxjs/xfa/cjx_pattern.h"
     10 #include "third_party/base/ptr_util.h"
     11 #include "xfa/fxfa/parser/cxfa_color.h"
     12 #include "xfa/fxgraphics/cxfa_gepattern.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::CrossHatch},
     24     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
     25     {XFA_Attribute::Unknown, XFA_AttributeType::Integer, nullptr}};
     26 
     27 constexpr wchar_t kName[] = L"pattern";
     28 
     29 }  // namespace
     30 
     31 CXFA_Pattern::CXFA_Pattern(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::Pattern,
     37                 kPropertyData,
     38                 kAttributeData,
     39                 kName,
     40                 pdfium::MakeUnique<CJX_Pattern>(this)) {}
     41 
     42 CXFA_Pattern::~CXFA_Pattern() {}
     43 
     44 CXFA_Color* CXFA_Pattern::GetColorIfExists() {
     45   return GetChild<CXFA_Color>(0, XFA_Element::Color, false);
     46 }
     47 
     48 XFA_AttributeEnum CXFA_Pattern::GetType() {
     49   return JSObject()->GetEnum(XFA_Attribute::Type);
     50 }
     51 
     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_AttributeEnum::CrossDiagonal:
     63       iHatch = FX_HatchStyle::DiagonalCross;
     64       break;
     65     case XFA_AttributeEnum::DiagonalLeft:
     66       iHatch = FX_HatchStyle::ForwardDiagonal;
     67       break;
     68     case XFA_AttributeEnum::DiagonalRight:
     69       iHatch = FX_HatchStyle::BackwardDiagonal;
     70       break;
     71     case XFA_AttributeEnum::Horizontal:
     72       iHatch = FX_HatchStyle::Horizontal;
     73       break;
     74     case XFA_AttributeEnum::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