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_radial.h"
8 
9 #include <utility>
10 
11 #include "core/fxge/render_defines.h"
12 #include "fxjs/xfa/cjx_node.h"
13 #include "third_party/base/ptr_util.h"
14 #include "xfa/fxfa/parser/cxfa_color.h"
15 #include "xfa/fxgraphics/cxfa_geshading.h"
16 
17 namespace {
18 
19 const CXFA_Node::PropertyData kRadialPropertyData[] = {
20     {XFA_Element::Color, 1, 0},
21     {XFA_Element::Extras, 1, 0},
22 };
23 
24 const CXFA_Node::AttributeData kRadialAttributeData[] = {
25     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
26     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
27     {XFA_Attribute::Type, XFA_AttributeType::Enum,
28      (void*)XFA_AttributeValue::ToEdge},
29     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
30 };
31 
32 }  // namespace
33 
CXFA_Radial(CXFA_Document * doc,XFA_PacketType packet)34 CXFA_Radial::CXFA_Radial(CXFA_Document* doc, XFA_PacketType packet)
35     : CXFA_Node(doc,
36                 packet,
37                 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
38                 XFA_ObjectType::Node,
39                 XFA_Element::Radial,
40                 kRadialPropertyData,
41                 kRadialAttributeData,
42                 pdfium::MakeUnique<CJX_Node>(this)) {}
43 
44 CXFA_Radial::~CXFA_Radial() = default;
45 
IsToEdge()46 bool CXFA_Radial::IsToEdge() {
47   auto value = JSObject()->TryEnum(XFA_Attribute::Type, true);
48   return !value.has_value() || value.value() == XFA_AttributeValue::ToEdge;
49 }
50 
GetColorIfExists()51 CXFA_Color* CXFA_Radial::GetColorIfExists() {
52   return GetChild<CXFA_Color>(0, XFA_Element::Color, false);
53 }
54 
Draw(CXFA_Graphics * pGS,CXFA_GEPath * fillPath,FX_ARGB crStart,const CFX_RectF & rtFill,const CFX_Matrix & matrix)55 void CXFA_Radial::Draw(CXFA_Graphics* pGS,
56                        CXFA_GEPath* fillPath,
57                        FX_ARGB crStart,
58                        const CFX_RectF& rtFill,
59                        const CFX_Matrix& matrix) {
60   CXFA_Color* pColor = GetColorIfExists();
61   FX_ARGB crEnd = pColor ? pColor->GetValue() : CXFA_Color::kBlackColor;
62   if (!IsToEdge())
63     std::swap(crStart, crEnd);
64 
65   float endRadius = sqrt(rtFill.Width() * rtFill.Width() +
66                          rtFill.Height() * rtFill.Height()) /
67                     2;
68   CXFA_GEShading shading(rtFill.Center(), rtFill.Center(), 0, endRadius, true,
69                          true, crStart, crEnd);
70 
71   pGS->SaveGraphState();
72   pGS->SetFillColor(CXFA_GEColor(&shading));
73   pGS->FillPath(fillPath, FXFILL_WINDING, &matrix);
74   pGS->RestoreGraphState();
75 }
76