1 // Copyright 2014 PDFium Authors. All rights reserved.
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 "../../include/javascript/JavaScript.h"
8 #include "../../include/javascript/IJavaScript.h"
9 #include "../../include/javascript/JS_Define.h"
10 #include "../../include/javascript/JS_Object.h"
11 #include "../../include/javascript/JS_Value.h"
12 #include "../../include/javascript/color.h"
13 #include "../../include/javascript/JS_EventHandler.h"
14 #include "../../include/javascript/JS_Context.h"
15 #include "../../include/javascript/JS_Runtime.h"
16 
GetIsolate(IFXJS_Context * cc)17 static v8::Isolate* GetIsolate(IFXJS_Context* cc)
18 {
19 	CJS_Context* pContext = (CJS_Context *)cc;
20 	ASSERT(pContext != NULL);
21 
22 	CJS_Runtime* pRuntime = pContext->GetJSRuntime();
23 	ASSERT(pRuntime != NULL);
24 
25 	return pRuntime->GetIsolate();
26 }
27 /* -------------------------- color -------------------------- */
28 
29 BEGIN_JS_STATIC_CONST(CJS_Color)
END_JS_STATIC_CONST()30 END_JS_STATIC_CONST()
31 
32 BEGIN_JS_STATIC_PROP(CJS_Color)
33 	JS_STATIC_PROP_ENTRY(black)
34 	JS_STATIC_PROP_ENTRY(blue)
35 	JS_STATIC_PROP_ENTRY(cyan)
36 	JS_STATIC_PROP_ENTRY(dkGray)
37 	JS_STATIC_PROP_ENTRY(gray)
38 	JS_STATIC_PROP_ENTRY(green)
39 	JS_STATIC_PROP_ENTRY(ltGray)
40 	JS_STATIC_PROP_ENTRY(magenta)
41 	JS_STATIC_PROP_ENTRY(red)
42 	JS_STATIC_PROP_ENTRY(transparent)
43 	JS_STATIC_PROP_ENTRY(white)
44 	JS_STATIC_PROP_ENTRY(yellow)
45 END_JS_STATIC_PROP()
46 
47 BEGIN_JS_STATIC_METHOD(CJS_Color)
48 	JS_STATIC_METHOD_ENTRY(convert)
49 	JS_STATIC_METHOD_ENTRY(equal)
50 END_JS_STATIC_METHOD()
51 
52 IMPLEMENT_JS_CLASS(CJS_Color,color)
53 
54 color::color(CJS_Object* pJSObject): CJS_EmbedObj(pJSObject)
55 {
56 	m_crTransparent = CPWL_Color(COLORTYPE_TRANSPARENT);
57 	m_crBlack = CPWL_Color(COLORTYPE_GRAY, 0);
58 	m_crWhite = CPWL_Color(COLORTYPE_GRAY, 1);
59 	m_crRed = CPWL_Color(COLORTYPE_RGB, 1, 0 ,0);
60 	m_crGreen = CPWL_Color(COLORTYPE_RGB, 0, 1 ,0);
61 	m_crBlue = CPWL_Color(COLORTYPE_RGB, 0, 0 ,1);
62 	m_crCyan = CPWL_Color(COLORTYPE_CMYK, 1, 0 ,0, 0);
63 	m_crMagenta = CPWL_Color(COLORTYPE_CMYK, 0, 1 ,0, 0);
64 	m_crYellow = CPWL_Color(COLORTYPE_CMYK, 0, 0 ,1, 0);
65 	m_crDKGray = CPWL_Color(COLORTYPE_GRAY, 0.25);
66 	m_crGray = CPWL_Color(COLORTYPE_GRAY, 0.5);
67 	m_crLTGray = CPWL_Color(COLORTYPE_GRAY, 0.75);
68 }
69 
~color(void)70 color::~color(void)
71 {
72 }
73 
ConvertPWLColorToArray(const CPWL_Color & color,CJS_Array & array)74 void color::ConvertPWLColorToArray(const CPWL_Color& color, CJS_Array& array)
75 {
76 	switch (color.nColorType)
77 	{
78 	case COLORTYPE_TRANSPARENT:
79 		array.SetElement(0, CJS_Value(array.GetIsolate(), "T"));
80 		break;
81 	case COLORTYPE_GRAY:
82 		array.SetElement(0, CJS_Value(array.GetIsolate(),"G"));
83 		array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));
84 		break;
85 	case COLORTYPE_RGB:
86 		array.SetElement(0, CJS_Value(array.GetIsolate(),"RGB"));
87 		array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));
88 		array.SetElement(2, CJS_Value(array.GetIsolate(),color.fColor2));
89 		array.SetElement(3, CJS_Value(array.GetIsolate(),color.fColor3));
90 		break;
91 	case COLORTYPE_CMYK:
92 		array.SetElement(0, CJS_Value(array.GetIsolate(),"CMYK"));
93 		array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));
94 		array.SetElement(2, CJS_Value(array.GetIsolate(),color.fColor2));
95 		array.SetElement(3, CJS_Value(array.GetIsolate(),color.fColor3));
96 		array.SetElement(4, CJS_Value(array.GetIsolate(),color.fColor4));
97 		break;
98 	}
99 }
100 
ConvertArrayToPWLColor(CJS_Array & array,CPWL_Color & color)101 void color::ConvertArrayToPWLColor(CJS_Array& array, CPWL_Color& color)
102 {
103 	int nArrayLen = array.GetLength();
104 	if (nArrayLen < 1) return;
105 
106 	CJS_Value value(array.GetIsolate());
107 	array.GetElement(0, value);
108 	CFX_ByteString sSpace = value.ToCFXByteString();
109 
110 	double d1 = 0;
111 	double d2 = 0;
112 	double d3 = 0;
113 	double d4 = 0;
114 
115 	if (nArrayLen > 1)
116 	{
117 		array.GetElement(1, value);
118 		d1 = value.ToDouble();
119 	}
120 
121 	if (nArrayLen > 2)
122 	{
123 		array.GetElement(2, value);
124 		d2 = value.ToDouble();
125 	}
126 
127 	if (nArrayLen > 3)
128 	{
129 		array.GetElement(3, value);
130 		d3 = value.ToDouble();
131 	}
132 
133 	if (nArrayLen > 4)
134 	{
135 		array.GetElement(4, value);
136 		d4 = value.ToDouble();
137 	}
138 
139 	if (sSpace == "T")
140 	{
141 		color = CPWL_Color(COLORTYPE_TRANSPARENT);
142 	}
143 	else if (sSpace == "G")
144 	{
145 		color = CPWL_Color(COLORTYPE_GRAY, (FX_FLOAT)d1);
146 	}
147 	else if (sSpace == "RGB")
148 	{
149 		color = CPWL_Color(COLORTYPE_RGB, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3);
150 	}
151 	else if (sSpace == "CMYK")
152 	{
153 		color = CPWL_Color(COLORTYPE_CMYK, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3, (FX_FLOAT)d4);
154 	}
155 }
156 
157 #define JS_IMPLEMENT_COLORPROP(prop, var)\
158 FX_BOOL color::prop(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError)\
159 {\
160 	CJS_Context* pContext = (CJS_Context*)cc;\
161 	v8::Isolate* isolate = pContext->GetJSRuntime()->GetIsolate();\
162 	if (vp.IsGetting())\
163 	{\
164 		CJS_Array array(isolate);\
165 		ConvertPWLColorToArray(var, array);\
166 		vp << array;\
167 	}\
168 	else\
169 	{\
170 		CJS_Array array(isolate);\
171 		if (!vp.ConvertToArray(array)) return FALSE;\
172 		ConvertArrayToPWLColor(array, var);\
173 	}\
174 	return TRUE;\
175 }
176 
JS_IMPLEMENT_COLORPROP(transparent,m_crTransparent)177 JS_IMPLEMENT_COLORPROP(transparent, m_crTransparent)
178 JS_IMPLEMENT_COLORPROP(black, m_crBlack)
179 JS_IMPLEMENT_COLORPROP(white, m_crWhite)
180 JS_IMPLEMENT_COLORPROP(red, m_crRed)
181 JS_IMPLEMENT_COLORPROP(green, m_crGreen)
182 JS_IMPLEMENT_COLORPROP(blue, m_crBlue)
183 JS_IMPLEMENT_COLORPROP(cyan, m_crCyan)
184 JS_IMPLEMENT_COLORPROP(magenta, m_crMagenta)
185 JS_IMPLEMENT_COLORPROP(yellow, m_crYellow)
186 JS_IMPLEMENT_COLORPROP(dkGray, m_crDKGray)
187 JS_IMPLEMENT_COLORPROP(gray, m_crGray)
188 JS_IMPLEMENT_COLORPROP(ltGray, m_crLTGray)
189 
190 FX_BOOL color::convert(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError)
191 {
192 	v8::Isolate* isolate = GetIsolate(cc);
193 	int iSize = params.size();
194 	if (iSize < 2) return FALSE;
195 	CJS_Array aSource(isolate);
196 	if (!params[0].ConvertToArray(aSource)) return FALSE;
197 
198 	CPWL_Color crSource;
199 	ConvertArrayToPWLColor(aSource, crSource);
200 
201 	CFX_ByteString sDestSpace = params[1].ToCFXByteString();
202 	int nColorType = COLORTYPE_TRANSPARENT;
203 
204 	if (sDestSpace == "T")
205 	{
206 		nColorType = COLORTYPE_TRANSPARENT;
207 	}
208 	else if (sDestSpace == "G")
209 	{
210 		nColorType = COLORTYPE_GRAY;
211 	}
212 	else if (sDestSpace == "RGB")
213 	{
214 		nColorType = COLORTYPE_RGB;
215 	}
216 	else if (sDestSpace == "CMYK")
217 	{
218 		nColorType = COLORTYPE_CMYK;
219 	}
220 
221 	CJS_Array aDest(isolate);
222 	CPWL_Color crDest = crSource;
223 	crDest.ConvertColorType(nColorType);
224 	ConvertPWLColorToArray(crDest, aDest);
225 	vRet = aDest;
226 
227 	return TRUE;
228 }
229 
equal(IFXJS_Context * cc,const CJS_Parameters & params,CJS_Value & vRet,CFX_WideString & sError)230 FX_BOOL color::equal(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError)
231 {
232 	v8::Isolate* isolate = GetIsolate(cc);
233 	if (params.size() < 2) return FALSE;
234 
235 	CJS_Array array1(isolate), array2(isolate);
236 
237 	if (!params[0].ConvertToArray(array1)) return FALSE;
238 	if (!params[1].ConvertToArray(array2)) return FALSE;
239 
240 	CPWL_Color color1;
241 	CPWL_Color color2;
242 
243 	ConvertArrayToPWLColor(array1, color1);
244 	ConvertArrayToPWLColor(array2, color2);
245 
246 	color1.ConvertColorType(color2.nColorType);
247 
248 	vRet = color1 == color2;
249 	return TRUE;
250 }
251 
252