Home | History | Annotate | Download | only in javascript
      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 
     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)
     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, 2)
     49 	JS_STATIC_METHOD_ENTRY(equal, 2)
     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 
     70 color::~color(void)
     71 {
     72 }
     73 
     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 
    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 	CFX_ByteString sSpace;
    108 	array.GetElement(0, value);
    109 	sSpace = value;
    110 
    111 	double d1 = 0;
    112 	double d2 = 0;
    113 	double d3 = 0;
    114 	double d4 = 0;
    115 
    116 	if (nArrayLen > 1)
    117 	{
    118 		array.GetElement(1, value);
    119 		d1 = value;
    120 	}
    121 
    122 	if (nArrayLen > 2)
    123 	{
    124 		array.GetElement(2, value);
    125 		d2 = value;
    126 	}
    127 
    128 	if (nArrayLen > 3)
    129 	{
    130 		array.GetElement(3, value);
    131 		d3 = value;
    132 	}
    133 
    134 	if (nArrayLen > 4)
    135 	{
    136 		array.GetElement(4, value);
    137 		d4 = value;
    138 	}
    139 
    140 	if (sSpace == "T")
    141 	{
    142 		color = CPWL_Color(COLORTYPE_TRANSPARENT);
    143 	}
    144 	else if (sSpace == "G")
    145 	{
    146 		color = CPWL_Color(COLORTYPE_GRAY, (FX_FLOAT)d1);
    147 	}
    148 	else if (sSpace == "RGB")
    149 	{
    150 		color = CPWL_Color(COLORTYPE_RGB, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3);
    151 	}
    152 	else if (sSpace == "CMYK")
    153 	{
    154 		color = CPWL_Color(COLORTYPE_CMYK, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3, (FX_FLOAT)d4);
    155 	}
    156 }
    157 
    158 #define JS_IMPLEMENT_COLORPROP(prop, var)\
    159 FX_BOOL color::prop(OBJ_PROP_PARAMS)\
    160 {\
    161 	CJS_Context* pContext = (CJS_Context*)cc;\
    162 	v8::Isolate* isolate = pContext->GetJSRuntime()->GetIsolate();\
    163 	if (vp.IsGetting())\
    164 	{\
    165 		CJS_Array array(isolate);\
    166 		ConvertPWLColorToArray(var, array);\
    167 		vp << array;\
    168 	}\
    169 	else\
    170 	{\
    171 		CJS_Array array(isolate);\
    172 		if (!vp.ConvertToArray(array)) return FALSE;\
    173 		ConvertArrayToPWLColor(array, var);\
    174 	}\
    175 	return TRUE;\
    176 }
    177 
    178 JS_IMPLEMENT_COLORPROP(transparent, m_crTransparent)
    179 JS_IMPLEMENT_COLORPROP(black, m_crBlack)
    180 JS_IMPLEMENT_COLORPROP(white, m_crWhite)
    181 JS_IMPLEMENT_COLORPROP(red, m_crRed)
    182 JS_IMPLEMENT_COLORPROP(green, m_crGreen)
    183 JS_IMPLEMENT_COLORPROP(blue, m_crBlue)
    184 JS_IMPLEMENT_COLORPROP(cyan, m_crCyan)
    185 JS_IMPLEMENT_COLORPROP(magenta, m_crMagenta)
    186 JS_IMPLEMENT_COLORPROP(yellow, m_crYellow)
    187 JS_IMPLEMENT_COLORPROP(dkGray, m_crDKGray)
    188 JS_IMPLEMENT_COLORPROP(gray, m_crGray)
    189 JS_IMPLEMENT_COLORPROP(ltGray, m_crLTGray)
    190 
    191 FX_BOOL color::convert(OBJ_METHOD_PARAMS)
    192 {
    193 	v8::Isolate* isolate = GetIsolate(cc);
    194 	int iSize = params.size();
    195 	if (iSize < 2) return FALSE;
    196 	CJS_Array aSource(isolate);
    197 	if (!params[0].ConvertToArray(aSource)) return FALSE;
    198 
    199 	CPWL_Color crSource;
    200 	ConvertArrayToPWLColor(aSource, crSource);
    201 
    202 	CFX_ByteString sDestSpace = params[1];
    203 
    204 	int nColorType = COLORTYPE_TRANSPARENT;
    205 
    206 	if (sDestSpace == "T")
    207 	{
    208 		nColorType = COLORTYPE_TRANSPARENT;
    209 	}
    210 	else if (sDestSpace == "G")
    211 	{
    212 		nColorType = COLORTYPE_GRAY;
    213 	}
    214 	else if (sDestSpace == "RGB")
    215 	{
    216 		nColorType = COLORTYPE_RGB;
    217 	}
    218 	else if (sDestSpace == "CMYK")
    219 	{
    220 		nColorType = COLORTYPE_CMYK;
    221 	}
    222 
    223 	CJS_Array aDest(isolate);
    224 	CPWL_Color crDest = crSource;
    225 	crDest.ConvertColorType(nColorType);
    226 	ConvertPWLColorToArray(crDest, aDest);
    227 	vRet = aDest;
    228 
    229 	return TRUE;
    230 }
    231 
    232 FX_BOOL color::equal(OBJ_METHOD_PARAMS)
    233 {
    234 	v8::Isolate* isolate = GetIsolate(cc);
    235 	if (params.size() < 2) return FALSE;
    236 
    237 	CJS_Array array1(isolate), array2(isolate);
    238 
    239 	if (!params[0].ConvertToArray(array1)) return FALSE;
    240 	if (!params[1].ConvertToArray(array2)) return FALSE;
    241 
    242 	CPWL_Color color1;
    243 	CPWL_Color color2;
    244 
    245 	ConvertArrayToPWLColor(array1, color1);
    246 	ConvertArrayToPWLColor(array2, color2);
    247 
    248 	color1.ConvertColorType(color2.nColorType);
    249 
    250 	vRet = color1 == color2;
    251 	return TRUE;
    252 }
    253 
    254