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_stipple.h"
8 
9 #include "fxjs/xfa/cjx_stipple.h"
10 #include "third_party/base/ptr_util.h"
11 #include "xfa/fxfa/parser/cxfa_color.h"
12 
13 namespace {
14 
15 const CXFA_Node::PropertyData kPropertyData[] = {{XFA_Element::Color, 1, 0},
16                                                  {XFA_Element::Extras, 1, 0},
17                                                  {XFA_Element::Unknown, 0, 0}};
18 const CXFA_Node::AttributeData kAttributeData[] = {
19     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
20     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
21     {XFA_Attribute::Rate, XFA_AttributeType::Integer, (void*)50},
22     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
23     {XFA_Attribute::Unknown, XFA_AttributeType::Integer, nullptr}};
24 
25 constexpr wchar_t kName[] = L"stipple";
26 
27 }  // namespace
28 
29 CXFA_Stipple::CXFA_Stipple(CXFA_Document* doc, XFA_PacketType packet)
30     : CXFA_Node(doc,
31                 packet,
32                 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
33                 XFA_ObjectType::Node,
34                 XFA_Element::Stipple,
35                 kPropertyData,
36                 kAttributeData,
37                 kName,
38                 pdfium::MakeUnique<CJX_Stipple>(this)) {}
39 
40 CXFA_Stipple::~CXFA_Stipple() {}
41 
42 CXFA_Color* CXFA_Stipple::GetColorIfExists() {
43   return GetChild<CXFA_Color>(0, XFA_Element::Color, false);
44 }
45 
46 int32_t CXFA_Stipple::GetRate() {
47   return JSObject()
48       ->TryInteger(XFA_Attribute::Rate, true)
49       .value_or(GetDefaultRate());
50 }
51 
52 void CXFA_Stipple::Draw(CXFA_Graphics* pGS,
53                         CXFA_GEPath* fillPath,
54                         const CFX_RectF& rtFill,
55                         const CFX_Matrix& matrix) {
56   int32_t iRate = GetRate();
57   if (iRate == 0)
58     iRate = 100;
59 
60   CXFA_Color* pColor = GetColorIfExists();
61   FX_ARGB crColor = pColor ? pColor->GetValue() : CXFA_Color::kBlackColor;
62 
63   int32_t a;
64   FX_COLORREF rgb;
65   std::tie(a, rgb) = ArgbToColorRef(crColor);
66   FX_ARGB cr = ArgbEncode(iRate * a / 100, rgb);
67 
68   pGS->SaveGraphState();
69   pGS->SetFillColor(CXFA_GEColor(cr));
70   pGS->FillPath(fillPath, FXFILL_WINDING, &matrix);
71   pGS->RestoreGraphState();
72 }
73