• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_radial.h"
8 
9 #include <math.h>
10 
11 #include <utility>
12 
13 #include "fxjs/xfa/cjx_node.h"
14 #include "xfa/fgas/graphics/cfgas_gegraphics.h"
15 #include "xfa/fgas/graphics/cfgas_geshading.h"
16 #include "xfa/fxfa/parser/cxfa_color.h"
17 #include "xfa/fxfa/parser/cxfa_document.h"
18 
19 namespace {
20 
21 const CXFA_Node::PropertyData kRadialPropertyData[] = {
22     {XFA_Element::Color, 1, {}},
23     {XFA_Element::Extras, 1, {}},
24 };
25 
26 const CXFA_Node::AttributeData kRadialAttributeData[] = {
27     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
28     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
29     {XFA_Attribute::Type, XFA_AttributeType::Enum,
30      (void*)XFA_AttributeValue::ToEdge},
31     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
32 };
33 
34 }  // namespace
35 
CXFA_Radial(CXFA_Document * doc,XFA_PacketType packet)36 CXFA_Radial::CXFA_Radial(CXFA_Document* doc, XFA_PacketType packet)
37     : CXFA_Node(doc,
38                 packet,
39                 {XFA_XDPPACKET::kTemplate, XFA_XDPPACKET::kForm},
40                 XFA_ObjectType::Node,
41                 XFA_Element::Radial,
42                 kRadialPropertyData,
43                 kRadialAttributeData,
44                 cppgc::MakeGarbageCollected<CJX_Node>(
45                     doc->GetHeap()->GetAllocationHandle(),
46                     this)) {}
47 
48 CXFA_Radial::~CXFA_Radial() = default;
49 
IsToEdge()50 bool CXFA_Radial::IsToEdge() {
51   auto value = JSObject()->TryEnum(XFA_Attribute::Type, true);
52   return !value.has_value() || value.value() == XFA_AttributeValue::ToEdge;
53 }
54 
GetColorIfExists()55 CXFA_Color* CXFA_Radial::GetColorIfExists() {
56   return GetChild<CXFA_Color>(0, XFA_Element::Color, false);
57 }
58 
Draw(CFGAS_GEGraphics * pGS,const CFGAS_GEPath & fillPath,FX_ARGB crStart,const CFX_RectF & rtFill,const CFX_Matrix & matrix)59 void CXFA_Radial::Draw(CFGAS_GEGraphics* pGS,
60                        const CFGAS_GEPath& fillPath,
61                        FX_ARGB crStart,
62                        const CFX_RectF& rtFill,
63                        const CFX_Matrix& matrix) {
64   CXFA_Color* pColor = GetColorIfExists();
65   FX_ARGB crEnd = pColor ? pColor->GetValue() : CXFA_Color::kBlackColor;
66   if (!IsToEdge())
67     std::swap(crStart, crEnd);
68 
69   float end_radius = FXSYS_sqrt2(rtFill.Width(), rtFill.Height()) / 2;
70   CFGAS_GEShading shading(rtFill.Center(), rtFill.Center(), 0, end_radius, true,
71                           true, crStart, crEnd);
72 
73   CFGAS_GEGraphics::StateRestorer restorer(pGS);
74   pGS->SetFillColor(CFGAS_GEColor(&shading));
75   pGS->FillPath(fillPath, CFX_FillRenderOptions::FillType::kWinding, matrix);
76 }
77