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