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