• 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_color.h"
8 
9 #include "core/fxcrt/fx_extension.h"
10 #include "fxjs/xfa/cjx_node.h"
11 #include "xfa/fgas/graphics/cfgas_gecolor.h"
12 #include "xfa/fxfa/parser/cxfa_document.h"
13 
14 namespace {
15 
16 const CXFA_Node::PropertyData kColorPropertyData[] = {
17     {XFA_Element::Extras, 1, {}},
18 };
19 
20 const CXFA_Node::AttributeData kColorAttributeData[] = {
21     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
22     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
23     {XFA_Attribute::CSpace, XFA_AttributeType::CData, (void*)L"SRGB"},
24     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
25     {XFA_Attribute::Value, XFA_AttributeType::CData, nullptr},
26 };
27 
28 }  // namespace
29 
30 // static
StringToFXARGB(WideStringView view)31 FX_ARGB CXFA_Color::StringToFXARGB(WideStringView view) {
32   static constexpr FX_ARGB kDefaultValue = 0xff000000;
33   if (view.IsEmpty())
34     return kDefaultValue;
35 
36   pdfium::span<const wchar_t> str = view.span();
37   size_t cc = 0;
38   while (cc < str.size() && FXSYS_iswspace(str[cc])) {
39     cc++;
40   }
41 
42   if (cc >= str.size()) {
43     return kDefaultValue;
44   }
45 
46   uint8_t r = 0;
47   uint8_t g = 0;
48   uint8_t b = 0;
49   while (cc < str.size()) {
50     if (str[cc] == ',' || !FXSYS_IsDecimalDigit(str[cc]))
51       break;
52 
53     r = r * 10 + str[cc] - '0';
54     cc++;
55   }
56   if (cc < str.size() && str[cc] == ',') {
57     cc++;
58     while (cc < str.size() && FXSYS_iswspace(str[cc])) {
59       cc++;
60     }
61 
62     while (cc < str.size()) {
63       if (str[cc] == ',' || !FXSYS_IsDecimalDigit(str[cc]))
64         break;
65 
66       g = g * 10 + str[cc] - '0';
67       cc++;
68     }
69     if (cc < str.size() && str[cc] == ',') {
70       cc++;
71       while (cc < str.size() && FXSYS_iswspace(str[cc])) {
72         cc++;
73       }
74 
75       while (cc < str.size()) {
76         if (str[cc] == ',' || !FXSYS_IsDecimalDigit(str[cc]))
77           break;
78 
79         b = b * 10 + str[cc] - '0';
80         cc++;
81       }
82     }
83   }
84   return ArgbEncode(0xFF, r, g, b);
85 }
86 
CXFA_Color(CXFA_Document * doc,XFA_PacketType packet)87 CXFA_Color::CXFA_Color(CXFA_Document* doc, XFA_PacketType packet)
88     : CXFA_Node(doc,
89                 packet,
90                 {XFA_XDPPACKET::kTemplate, XFA_XDPPACKET::kForm},
91                 XFA_ObjectType::Node,
92                 XFA_Element::Color,
93                 kColorPropertyData,
94                 kColorAttributeData,
95                 cppgc::MakeGarbageCollected<CJX_Node>(
96                     doc->GetHeap()->GetAllocationHandle(),
97                     this)) {}
98 
99 CXFA_Color::~CXFA_Color() = default;
100 
GetValue() const101 FX_ARGB CXFA_Color::GetValue() const {
102   std::optional<WideString> val =
103       JSObject()->TryCData(XFA_Attribute::Value, false);
104   return val.has_value() ? StringToFXARGB(val->AsStringView()) : 0xFF000000;
105 }
106 
GetValueOrDefault(FX_ARGB defaultValue) const107 FX_ARGB CXFA_Color::GetValueOrDefault(FX_ARGB defaultValue) const {
108   std::optional<WideString> val =
109       JSObject()->TryCData(XFA_Attribute::Value, false);
110   return val.has_value() ? StringToFXARGB(val->AsStringView()) : defaultValue;
111 }
112 
SetValue(FX_ARGB color)113 void CXFA_Color::SetValue(FX_ARGB color) {
114   JSObject()->SetCData(XFA_Attribute::Value,
115                        WideString::FromASCII(
116                            CFGAS_GEColor::ColorToString(color).AsStringView()));
117 }
118