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
cjs_color.cpp
Go to the documentation of this file.
1// Copyright 2014 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 "fxjs/cjs_color.h"
8
9#include <algorithm>
10#include <vector>
11
12#include "core/fxge/cfx_color.h"
13#include "fxjs/cjs_event_context.h"
14#include "fxjs/cjs_object.h"
15#include "fxjs/cjs_runtime.h"
16#include "fxjs/fxv8.h"
17#include "fxjs/js_define.h"
18#include "third_party/base/containers/span.h"
19#include "v8/include/v8-container.h"
20
21const JSPropertySpec CJS_Color::PropertySpecs[] = {
22 {"black", get_black_static, set_black_static},
23 {"blue", get_blue_static, set_blue_static},
24 {"cyan", get_cyan_static, set_cyan_static},
25 {"dkGray", get_dark_gray_static, set_dark_gray_static},
26 {"gray", get_gray_static, set_gray_static},
27 {"green", get_green_static, set_green_static},
28 {"ltGray", get_light_gray_static, set_light_gray_static},
29 {"magenta", get_magenta_static, set_magenta_static},
30 {"red", get_red_static, set_red_static},
31 {"transparent", get_transparent_static, set_transparent_static},
32 {"white", get_white_static, set_white_static},
33 {"yellow", get_yellow_static, set_yellow_static}};
34
35const JSMethodSpec CJS_Color::MethodSpecs[] = {{"convert", convert_static},
36 {"equal", equal_static}};
37
38uint32_t CJS_Color::ObjDefnID = 0;
39const char CJS_Color::kName[] = "color";
40
41// static
42uint32_t CJS_Color::GetObjDefnID() {
43 return ObjDefnID;
44}
45
46// static
47void CJS_Color::DefineJSObjects(CFXJS_Engine* pEngine) {
48 ObjDefnID = pEngine->DefineObj(CJS_Color::kName, FXJSOBJTYPE_STATIC,
49 JSConstructor<CJS_Color>, JSDestructor);
50 DefineProps(pEngine, ObjDefnID, PropertySpecs);
51 DefineMethods(pEngine, ObjDefnID, MethodSpecs);
52}
53
54// static
55v8::Local<v8::Array> CJS_Color::ConvertPWLColorToArray(CJS_Runtime* pRuntime,
56 const CFX_Color& color) {
57 v8::Local<v8::Array> array;
58 switch (color.nColorType) {
59 case CFX_Color::Type::kTransparent:
60 array = pRuntime->NewArray();
61 pRuntime->PutArrayElement(array, 0, pRuntime->NewString("T"));
62 break;
63 case CFX_Color::Type::kGray:
64 array = pRuntime->NewArray();
65 pRuntime->PutArrayElement(array, 0, pRuntime->NewString("G"));
66 pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1));
67 break;
68 case CFX_Color::Type::kRGB:
69 array = pRuntime->NewArray();
70 pRuntime->PutArrayElement(array, 0, pRuntime->NewString("RGB"));
71 pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1));
72 pRuntime->PutArrayElement(array, 2, pRuntime->NewNumber(color.fColor2));
73 pRuntime->PutArrayElement(array, 3, pRuntime->NewNumber(color.fColor3));
74 break;
75 case CFX_Color::Type::kCMYK:
76 array = pRuntime->NewArray();
77 pRuntime->PutArrayElement(array, 0, pRuntime->NewString("CMYK"));
78 pRuntime->PutArrayElement(array, 1, pRuntime->NewNumber(color.fColor1));
79 pRuntime->PutArrayElement(array, 2, pRuntime->NewNumber(color.fColor2));
80 pRuntime->PutArrayElement(array, 3, pRuntime->NewNumber(color.fColor3));
81 pRuntime->PutArrayElement(array, 4, pRuntime->NewNumber(color.fColor4));
82 break;
83 }
84 return array;
85}
86
87// static
88CFX_Color CJS_Color::ConvertArrayToPWLColor(CJS_Runtime* pRuntime,
89 v8::Local<v8::Array> array) {
90 size_t nArrayLen = pRuntime->GetArrayLength(array);
91 if (nArrayLen == 0)
92 return CFX_Color();
93
94 WideString sSpace =
95 pRuntime->ToWideString(pRuntime->GetArrayElement(array, 0));
96 if (sSpace.EqualsASCII("T"))
98
99 float d1 = 0;
100 if (nArrayLen > 1) {
101 d1 = static_cast<float>(
102 pRuntime->ToDouble(pRuntime->GetArrayElement(array, 1)));
103 }
104 if (sSpace.EqualsASCII("G"))
106
107 float d2 = 0;
108 float d3 = 0;
109 if (nArrayLen > 2) {
110 d2 = static_cast<float>(
111 pRuntime->ToDouble(pRuntime->GetArrayElement(array, 2)));
112 }
113 if (nArrayLen > 3) {
114 d3 = static_cast<float>(
115 pRuntime->ToDouble(pRuntime->GetArrayElement(array, 3)));
116 }
117 if (sSpace.EqualsASCII("RGB"))
119
120 float d4 = 0;
121 if (nArrayLen > 4) {
122 d4 = static_cast<float>(
123 pRuntime->ToDouble(pRuntime->GetArrayElement(array, 4)));
124 }
125 if (sSpace.EqualsASCII("CMYK"))
126 return CFX_Color(CFX_Color::Type::kCMYK, d1, d2, d3, d4);
127
128 return CFX_Color();
129}
130
131CJS_Color::CJS_Color(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
132 : CJS_Object(pObject, pRuntime),
133 m_crTransparent(CFX_Color::Type::kTransparent),
134 m_crBlack(CFX_Color::Type::kGray, 0),
135 m_crWhite(CFX_Color::Type::kGray, 1),
136 m_crRed(CFX_Color::Type::kRGB, 1, 0, 0),
137 m_crGreen(CFX_Color::Type::kRGB, 0, 1, 0),
138 m_crBlue(CFX_Color::Type::kRGB, 0, 0, 1),
139 m_crCyan(CFX_Color::Type::kCMYK, 1, 0, 0, 0),
140 m_crMagenta(CFX_Color::Type::kCMYK, 0, 1, 0, 0),
141 m_crYellow(CFX_Color::Type::kCMYK, 0, 0, 1, 0),
142 m_crDKGray(CFX_Color::Type::kGray, 0.25),
143 m_crGray(CFX_Color::Type::kGray, 0.5),
144 m_crLTGray(CFX_Color::Type::kGray, 0.75) {}
145
146CJS_Color::~CJS_Color() = default;
147
148CJS_Result CJS_Color::get_transparent(CJS_Runtime* pRuntime) {
149 return GetPropertyHelper(pRuntime, &m_crTransparent);
150}
151
152CJS_Result CJS_Color::set_transparent(CJS_Runtime* pRuntime,
153 v8::Local<v8::Value> vp) {
154 return SetPropertyHelper(pRuntime, vp, &m_crTransparent);
155}
156
157CJS_Result CJS_Color::get_black(CJS_Runtime* pRuntime) {
158 return GetPropertyHelper(pRuntime, &m_crBlack);
159}
160
161CJS_Result CJS_Color::set_black(CJS_Runtime* pRuntime,
162 v8::Local<v8::Value> vp) {
163 return SetPropertyHelper(pRuntime, vp, &m_crBlack);
164}
165
166CJS_Result CJS_Color::get_white(CJS_Runtime* pRuntime) {
167 return GetPropertyHelper(pRuntime, &m_crWhite);
168}
169
170CJS_Result CJS_Color::set_white(CJS_Runtime* pRuntime,
171 v8::Local<v8::Value> vp) {
172 return SetPropertyHelper(pRuntime, vp, &m_crWhite);
173}
174
175CJS_Result CJS_Color::get_red(CJS_Runtime* pRuntime) {
176 return GetPropertyHelper(pRuntime, &m_crRed);
177}
178
179CJS_Result CJS_Color::set_red(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
180 return SetPropertyHelper(pRuntime, vp, &m_crRed);
181}
182
183CJS_Result CJS_Color::get_green(CJS_Runtime* pRuntime) {
184 return GetPropertyHelper(pRuntime, &m_crGreen);
185}
186
187CJS_Result CJS_Color::set_green(CJS_Runtime* pRuntime,
188 v8::Local<v8::Value> vp) {
189 return SetPropertyHelper(pRuntime, vp, &m_crGreen);
190}
191
192CJS_Result CJS_Color::get_blue(CJS_Runtime* pRuntime) {
193 return GetPropertyHelper(pRuntime, &m_crBlue);
194}
195
196CJS_Result CJS_Color::set_blue(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
197 return SetPropertyHelper(pRuntime, vp, &m_crBlue);
198}
199
200CJS_Result CJS_Color::get_cyan(CJS_Runtime* pRuntime) {
201 return GetPropertyHelper(pRuntime, &m_crCyan);
202}
203
204CJS_Result CJS_Color::set_cyan(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
205 return SetPropertyHelper(pRuntime, vp, &m_crCyan);
206}
207
208CJS_Result CJS_Color::get_magenta(CJS_Runtime* pRuntime) {
209 return GetPropertyHelper(pRuntime, &m_crMagenta);
210}
211
212CJS_Result CJS_Color::set_magenta(CJS_Runtime* pRuntime,
213 v8::Local<v8::Value> vp) {
214 return SetPropertyHelper(pRuntime, vp, &m_crMagenta);
215}
216
217CJS_Result CJS_Color::get_yellow(CJS_Runtime* pRuntime) {
218 return GetPropertyHelper(pRuntime, &m_crYellow);
219}
220
221CJS_Result CJS_Color::set_yellow(CJS_Runtime* pRuntime,
222 v8::Local<v8::Value> vp) {
223 return SetPropertyHelper(pRuntime, vp, &m_crYellow);
224}
225
226CJS_Result CJS_Color::get_dark_gray(CJS_Runtime* pRuntime) {
227 return GetPropertyHelper(pRuntime, &m_crDKGray);
228}
229
230CJS_Result CJS_Color::set_dark_gray(CJS_Runtime* pRuntime,
231 v8::Local<v8::Value> vp) {
232 return SetPropertyHelper(pRuntime, vp, &m_crDKGray);
233}
234
235CJS_Result CJS_Color::get_gray(CJS_Runtime* pRuntime) {
236 return GetPropertyHelper(pRuntime, &m_crGray);
237}
238
239CJS_Result CJS_Color::set_gray(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
240 return SetPropertyHelper(pRuntime, vp, &m_crGray);
241}
242
243CJS_Result CJS_Color::get_light_gray(CJS_Runtime* pRuntime) {
244 return GetPropertyHelper(pRuntime, &m_crLTGray);
245}
246
247CJS_Result CJS_Color::set_light_gray(CJS_Runtime* pRuntime,
248 v8::Local<v8::Value> vp) {
249 return SetPropertyHelper(pRuntime, vp, &m_crLTGray);
250}
251
252CJS_Result CJS_Color::GetPropertyHelper(CJS_Runtime* pRuntime, CFX_Color* var) {
253 v8::Local<v8::Value> array = ConvertPWLColorToArray(pRuntime, *var);
254 if (array.IsEmpty())
255 return CJS_Result::Success(pRuntime->NewArray());
256
257 return CJS_Result::Success(array);
258}
259
260CJS_Result CJS_Color::SetPropertyHelper(CJS_Runtime* pRuntime,
261 v8::Local<v8::Value> vp,
262 CFX_Color* var) {
263 if (vp.IsEmpty())
265
266 if (!vp->IsArray())
268
269 *var = ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(vp));
271}
272
273CJS_Result CJS_Color::convert(CJS_Runtime* pRuntime,
274 pdfium::span<v8::Local<v8::Value>> params) {
275 if (params.size() < 2)
277
278 if (!fxv8::IsArray(params[0]))
280
281 WideString sDestSpace = pRuntime->ToWideString(params[1]);
283 if (sDestSpace.EqualsASCII("T"))
285 else if (sDestSpace.EqualsASCII("G"))
286 nColorType = CFX_Color::Type::kGray;
287 else if (sDestSpace.EqualsASCII("RGB"))
288 nColorType = CFX_Color::Type::kRGB;
289 else if (sDestSpace.EqualsASCII("CMYK"))
290 nColorType = CFX_Color::Type::kCMYK;
291
292 CFX_Color color =
293 ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(params[0]));
294 v8::Local<v8::Value> array =
295 ConvertPWLColorToArray(pRuntime, color.ConvertColorType(nColorType));
296 if (array.IsEmpty())
297 return CJS_Result::Success(pRuntime->NewArray());
298
299 return CJS_Result::Success(array);
300}
301
302CJS_Result CJS_Color::equal(CJS_Runtime* pRuntime,
303 pdfium::span<v8::Local<v8::Value>> params) {
304 if (params.size() < 2)
306
307 if (!fxv8::IsArray(params[0]) || !fxv8::IsArray(params[1]))
309
310 CFX_Color color1 =
311 ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(params[0]));
312 CFX_Color color2 =
313 ConvertArrayToPWLColor(pRuntime, pRuntime->ToArray(params[1]));
314
315 // Relies on higher values having more components.
316 CFX_Color::Type best = std::max(color1.nColorType, color2.nColorType);
317 return CJS_Result::Success(pRuntime->NewBoolean(
318 color1.ConvertColorType(best) == color2.ConvertColorType(best)));
319}
bool operator==(const CFX_Color &c1, const CFX_Color &c2)
Definition cfx_color.h:61
@ FXJSOBJTYPE_STATIC
~CJS_Color() override
static uint32_t GetObjDefnID()
Definition cjs_color.cpp:42
CJS_Color(v8::Local< v8::Object > pObject, CJS_Runtime *pRuntime)
static void DefineJSObjects(CFXJS_Engine *pEngine)
Definition cjs_color.cpp:47
static void DefineProps(CFXJS_Engine *pEngine, uint32_t nObjDefnID, pdfium::span< const JSPropertySpec > consts)
static void DefineMethods(CFXJS_Engine *pEngine, uint32_t nObjDefnID, pdfium::span< const JSMethodSpec > consts)
static CJS_Result Success()
Definition cjs_result.h:27
static CJS_Result Failure(JSMessage id)
Definition cjs_result.h:34
bool EqualsASCII(ByteStringView that) const
Definition widestring.h:216
JSMessage
CFX_Color ConvertColorType(Type nConvertColorType) const
Definition cfx_color.cpp:75
Type nColorType
Definition cfx_color.h:54
constexpr CFX_Color(Type type=CFX_Color::Type::kTransparent, float color1=0.0f, float color2=0.0f, float color3=0.0f, float color4=0.0f)
Definition cfx_color.h:27