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