1 // Copyright 2017 The PDFium Authors
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_node.h"
10 #include "xfa/fgas/graphics/cfgas_gegraphics.h"
11 #include "xfa/fxfa/parser/cxfa_color.h"
12 #include "xfa/fxfa/parser/cxfa_document.h"
13
14 namespace {
15
16 const CXFA_Node::PropertyData kStipplePropertyData[] = {
17 {XFA_Element::Color, 1, {}},
18 {XFA_Element::Extras, 1, {}},
19 };
20
21 const CXFA_Node::AttributeData kStippleAttributeData[] = {
22 {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
23 {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
24 {XFA_Attribute::Rate, XFA_AttributeType::Integer, (void*)50},
25 {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
26 };
27
28 } // namespace
29
CXFA_Stipple(CXFA_Document * doc,XFA_PacketType packet)30 CXFA_Stipple::CXFA_Stipple(CXFA_Document* doc, XFA_PacketType packet)
31 : CXFA_Node(doc,
32 packet,
33 {XFA_XDPPACKET::kTemplate, XFA_XDPPACKET::kForm},
34 XFA_ObjectType::Node,
35 XFA_Element::Stipple,
36 kStipplePropertyData,
37 kStippleAttributeData,
38 cppgc::MakeGarbageCollected<CJX_Node>(
39 doc->GetHeap()->GetAllocationHandle(),
40 this)) {}
41
42 CXFA_Stipple::~CXFA_Stipple() = default;
43
GetColorIfExists()44 CXFA_Color* CXFA_Stipple::GetColorIfExists() {
45 return GetChild<CXFA_Color>(0, XFA_Element::Color, false);
46 }
47
GetRate()48 int32_t CXFA_Stipple::GetRate() {
49 return JSObject()
50 ->TryInteger(XFA_Attribute::Rate, true)
51 .value_or(GetDefaultRate());
52 }
53
Draw(CFGAS_GEGraphics * pGS,const CFGAS_GEPath & fillPath,const CFX_RectF & rtFill,const CFX_Matrix & matrix)54 void CXFA_Stipple::Draw(CFGAS_GEGraphics* pGS,
55 const CFGAS_GEPath& fillPath,
56 const CFX_RectF& rtFill,
57 const CFX_Matrix& matrix) {
58 int32_t iRate = GetRate();
59 if (iRate == 0)
60 iRate = 100;
61
62 CXFA_Color* pColor = GetColorIfExists();
63 FX_ARGB crColor = pColor ? pColor->GetValue() : CXFA_Color::kBlackColor;
64
65 int32_t alpha;
66 FX_COLORREF colorref;
67 std::tie(alpha, colorref) = ArgbToAlphaAndColorRef(crColor);
68 FX_ARGB cr = AlphaAndColorRefToArgb(iRate * alpha / 100, colorref);
69
70 CFGAS_GEGraphics::StateRestorer restorer(pGS);
71 pGS->SetFillColor(CFGAS_GEColor(cr));
72 pGS->FillPath(fillPath, CFX_FillRenderOptions::FillType::kWinding, matrix);
73 }
74