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