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 "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
14 namespace {
15
16 const CXFA_Node::PropertyData kStipplePropertyData[] = {
17 {XFA_Element::Color, 1, 0},
18 {XFA_Element::Extras, 1, 0},
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_Template | XFA_XDPPACKET_Form),
34 XFA_ObjectType::Node,
35 XFA_Element::Stipple,
36 kStipplePropertyData,
37 kStippleAttributeData,
38 pdfium::MakeUnique<CJX_Node>(this)) {}
39
40 CXFA_Stipple::~CXFA_Stipple() = default;
41
GetColorIfExists()42 CXFA_Color* CXFA_Stipple::GetColorIfExists() {
43 return GetChild<CXFA_Color>(0, XFA_Element::Color, false);
44 }
45
GetRate()46 int32_t CXFA_Stipple::GetRate() {
47 return JSObject()
48 ->TryInteger(XFA_Attribute::Rate, true)
49 .value_or(GetDefaultRate());
50 }
51
Draw(CXFA_Graphics * pGS,CXFA_GEPath * fillPath,const CFX_RectF & rtFill,const CFX_Matrix & matrix)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 alpha;
64 FX_COLORREF colorref;
65 std::tie(alpha, colorref) = ArgbToAlphaAndColorRef(crColor);
66 FX_ARGB cr = AlphaAndColorRefToArgb(iRate * alpha / 100, colorref);
67
68 pGS->SaveGraphState();
69 pGS->SetFillColor(CXFA_GEColor(cr));
70 pGS->FillPath(fillPath, FXFILL_WINDING, &matrix);
71 pGS->RestoreGraphState();
72 }
73