Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cxfa_color.cpp
Go to the documentation of this file.
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
13namespace {
14
15const CXFA_Node::PropertyData kColorPropertyData[] = {
16 {XFA_Element::Extras, 1, {}},
17};
18
19const CXFA_Node::AttributeData kColorAttributeData[] = {
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
30FX_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
83CXFA_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
95CXFA_Color::~CXFA_Color() = default;
96
97FX_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
103FX_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
109void 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}
FX_ARGB GetValueOrDefault(FX_ARGB defaultValue) const
~CXFA_Color() override
FX_ARGB GetValue() const
static FX_ARGB StringToFXARGB(WideStringView view)
void SetValue(FX_ARGB color)
static WideString Format(const wchar_t *pFormat,...)
constexpr FX_ARGB ArgbEncode(uint32_t a, uint32_t r, uint32_t g, uint32_t b)
Definition fx_dib.h:118
XFA_AttributeType
Definition fxfa_basic.h:83
XFA_Attribute
Definition fxfa_basic.h:67
XFA_Element
Definition fxfa_basic.h:75
XFA_PacketType
Definition fxfa_basic.h:44