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 "fpdfsdk/javascript/color.h" 8 9 #include <vector> 10 11 #include "fpdfsdk/javascript/JS_Define.h" 12 #include "fpdfsdk/javascript/JS_EventHandler.h" 13 #include "fpdfsdk/javascript/JS_Object.h" 14 #include "fpdfsdk/javascript/JS_Value.h" 15 #include "fpdfsdk/javascript/cjs_event_context.h" 16 #include "fpdfsdk/javascript/cjs_runtime.h" 17 18 JSConstSpec CJS_Color::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}}; 19 20 JSPropertySpec CJS_Color::PropertySpecs[] = { 21 {"black", get_black_static, set_black_static}, 22 {"blue", get_blue_static, set_blue_static}, 23 {"cyan", get_cyan_static, set_cyan_static}, 24 {"dkGray", get_dkGray_static, set_dkGray_static}, 25 {"gray", get_gray_static, set_gray_static}, 26 {"green", get_green_static, set_green_static}, 27 {"ltGray", get_ltGray_static, set_ltGray_static}, 28 {"magenta", get_magenta_static, set_magenta_static}, 29 {"red", get_red_static, set_red_static}, 30 {"transparent", get_transparent_static, set_transparent_static}, 31 {"white", get_white_static, set_white_static}, 32 {"yellow", get_yellow_static, set_yellow_static}, 33 {0, 0, 0}}; 34 35 JSMethodSpec CJS_Color::MethodSpecs[] = {{"convert", convert_static}, 36 {"equal", equal_static}, 37 {0, 0}}; 38 39 IMPLEMENT_JS_CLASS(CJS_Color, color) 40 41 color::color(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject) { 42 m_crTransparent = CPWL_Color(COLORTYPE_TRANSPARENT); 43 m_crBlack = CPWL_Color(COLORTYPE_GRAY, 0); 44 m_crWhite = CPWL_Color(COLORTYPE_GRAY, 1); 45 m_crRed = CPWL_Color(COLORTYPE_RGB, 1, 0, 0); 46 m_crGreen = CPWL_Color(COLORTYPE_RGB, 0, 1, 0); 47 m_crBlue = CPWL_Color(COLORTYPE_RGB, 0, 0, 1); 48 m_crCyan = CPWL_Color(COLORTYPE_CMYK, 1, 0, 0, 0); 49 m_crMagenta = CPWL_Color(COLORTYPE_CMYK, 0, 1, 0, 0); 50 m_crYellow = CPWL_Color(COLORTYPE_CMYK, 0, 0, 1, 0); 51 m_crDKGray = CPWL_Color(COLORTYPE_GRAY, 0.25); 52 m_crGray = CPWL_Color(COLORTYPE_GRAY, 0.5); 53 m_crLTGray = CPWL_Color(COLORTYPE_GRAY, 0.75); 54 } 55 56 color::~color() {} 57 58 void color::ConvertPWLColorToArray(CJS_Runtime* pRuntime, 59 const CPWL_Color& color, 60 CJS_Array* array) { 61 switch (color.nColorType) { 62 case COLORTYPE_TRANSPARENT: 63 array->SetElement(pRuntime, 0, CJS_Value(pRuntime, "T")); 64 break; 65 case COLORTYPE_GRAY: 66 array->SetElement(pRuntime, 0, CJS_Value(pRuntime, "G")); 67 array->SetElement(pRuntime, 1, CJS_Value(pRuntime, color.fColor1)); 68 break; 69 case COLORTYPE_RGB: 70 array->SetElement(pRuntime, 0, CJS_Value(pRuntime, "RGB")); 71 array->SetElement(pRuntime, 1, CJS_Value(pRuntime, color.fColor1)); 72 array->SetElement(pRuntime, 2, CJS_Value(pRuntime, color.fColor2)); 73 array->SetElement(pRuntime, 3, CJS_Value(pRuntime, color.fColor3)); 74 break; 75 case COLORTYPE_CMYK: 76 array->SetElement(pRuntime, 0, CJS_Value(pRuntime, "CMYK")); 77 array->SetElement(pRuntime, 1, CJS_Value(pRuntime, color.fColor1)); 78 array->SetElement(pRuntime, 2, CJS_Value(pRuntime, color.fColor2)); 79 array->SetElement(pRuntime, 3, CJS_Value(pRuntime, color.fColor3)); 80 array->SetElement(pRuntime, 4, CJS_Value(pRuntime, color.fColor4)); 81 break; 82 } 83 } 84 85 void color::ConvertArrayToPWLColor(CJS_Runtime* pRuntime, 86 const CJS_Array& array, 87 CPWL_Color* color) { 88 int nArrayLen = array.GetLength(pRuntime); 89 if (nArrayLen < 1) 90 return; 91 92 CJS_Value value(pRuntime); 93 array.GetElement(pRuntime, 0, value); 94 CFX_ByteString sSpace = value.ToCFXByteString(pRuntime); 95 96 double d1 = 0; 97 double d2 = 0; 98 double d3 = 0; 99 double d4 = 0; 100 101 if (nArrayLen > 1) { 102 array.GetElement(pRuntime, 1, value); 103 d1 = value.ToDouble(pRuntime); 104 } 105 106 if (nArrayLen > 2) { 107 array.GetElement(pRuntime, 2, value); 108 d2 = value.ToDouble(pRuntime); 109 } 110 111 if (nArrayLen > 3) { 112 array.GetElement(pRuntime, 3, value); 113 d3 = value.ToDouble(pRuntime); 114 } 115 116 if (nArrayLen > 4) { 117 array.GetElement(pRuntime, 4, value); 118 d4 = value.ToDouble(pRuntime); 119 } 120 121 if (sSpace == "T") { 122 *color = CPWL_Color(COLORTYPE_TRANSPARENT); 123 } else if (sSpace == "G") { 124 *color = CPWL_Color(COLORTYPE_GRAY, (FX_FLOAT)d1); 125 } else if (sSpace == "RGB") { 126 *color = 127 CPWL_Color(COLORTYPE_RGB, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3); 128 } else if (sSpace == "CMYK") { 129 *color = CPWL_Color(COLORTYPE_CMYK, (FX_FLOAT)d1, (FX_FLOAT)d2, 130 (FX_FLOAT)d3, (FX_FLOAT)d4); 131 } 132 } 133 134 bool color::transparent(CJS_Runtime* pRuntime, 135 CJS_PropValue& vp, 136 CFX_WideString& sError) { 137 return PropertyHelper(pRuntime, vp, &m_crTransparent); 138 } 139 140 bool color::black(CJS_Runtime* pRuntime, 141 CJS_PropValue& vp, 142 CFX_WideString& sError) { 143 return PropertyHelper(pRuntime, vp, &m_crBlack); 144 } 145 146 bool color::white(CJS_Runtime* pRuntime, 147 CJS_PropValue& vp, 148 CFX_WideString& sError) { 149 return PropertyHelper(pRuntime, vp, &m_crWhite); 150 } 151 152 bool color::red(CJS_Runtime* pRuntime, 153 CJS_PropValue& vp, 154 CFX_WideString& sError) { 155 return PropertyHelper(pRuntime, vp, &m_crRed); 156 } 157 158 bool color::green(CJS_Runtime* pRuntime, 159 CJS_PropValue& vp, 160 CFX_WideString& sError) { 161 return PropertyHelper(pRuntime, vp, &m_crGreen); 162 } 163 164 bool color::blue(CJS_Runtime* pRuntime, 165 CJS_PropValue& vp, 166 CFX_WideString& sError) { 167 return PropertyHelper(pRuntime, vp, &m_crBlue); 168 } 169 170 bool color::cyan(CJS_Runtime* pRuntime, 171 CJS_PropValue& vp, 172 CFX_WideString& sError) { 173 return PropertyHelper(pRuntime, vp, &m_crCyan); 174 } 175 176 bool color::magenta(CJS_Runtime* pRuntime, 177 CJS_PropValue& vp, 178 CFX_WideString& sError) { 179 return PropertyHelper(pRuntime, vp, &m_crMagenta); 180 } 181 182 bool color::yellow(CJS_Runtime* pRuntime, 183 CJS_PropValue& vp, 184 CFX_WideString& sError) { 185 return PropertyHelper(pRuntime, vp, &m_crYellow); 186 } 187 188 bool color::dkGray(CJS_Runtime* pRuntime, 189 CJS_PropValue& vp, 190 CFX_WideString& sError) { 191 return PropertyHelper(pRuntime, vp, &m_crDKGray); 192 } 193 194 bool color::gray(CJS_Runtime* pRuntime, 195 CJS_PropValue& vp, 196 CFX_WideString& sError) { 197 return PropertyHelper(pRuntime, vp, &m_crGray); 198 } 199 200 bool color::ltGray(CJS_Runtime* pRuntime, 201 CJS_PropValue& vp, 202 CFX_WideString& sError) { 203 return PropertyHelper(pRuntime, vp, &m_crLTGray); 204 } 205 206 bool color::PropertyHelper(CJS_Runtime* pRuntime, 207 CJS_PropValue& vp, 208 CPWL_Color* var) { 209 CJS_Array array; 210 if (vp.IsGetting()) { 211 ConvertPWLColorToArray(pRuntime, *var, &array); 212 vp << array; 213 return true; 214 } 215 if (!vp.GetJSValue()->ConvertToArray(pRuntime, array)) 216 return false; 217 218 ConvertArrayToPWLColor(pRuntime, array, var); 219 return true; 220 } 221 222 bool color::convert(CJS_Runtime* pRuntime, 223 const std::vector<CJS_Value>& params, 224 CJS_Value& vRet, 225 CFX_WideString& sError) { 226 int iSize = params.size(); 227 if (iSize < 2) 228 return false; 229 230 CJS_Array aSource; 231 if (!params[0].ConvertToArray(pRuntime, aSource)) 232 return false; 233 234 CPWL_Color crSource; 235 ConvertArrayToPWLColor(pRuntime, aSource, &crSource); 236 237 CFX_ByteString sDestSpace = params[1].ToCFXByteString(pRuntime); 238 int nColorType = COLORTYPE_TRANSPARENT; 239 240 if (sDestSpace == "T") { 241 nColorType = COLORTYPE_TRANSPARENT; 242 } else if (sDestSpace == "G") { 243 nColorType = COLORTYPE_GRAY; 244 } else if (sDestSpace == "RGB") { 245 nColorType = COLORTYPE_RGB; 246 } else if (sDestSpace == "CMYK") { 247 nColorType = COLORTYPE_CMYK; 248 } 249 250 CJS_Array aDest; 251 CPWL_Color crDest = crSource.ConvertColorType(nColorType); 252 ConvertPWLColorToArray(pRuntime, crDest, &aDest); 253 vRet = CJS_Value(pRuntime, aDest); 254 255 return true; 256 } 257 258 bool color::equal(CJS_Runtime* pRuntime, 259 const std::vector<CJS_Value>& params, 260 CJS_Value& vRet, 261 CFX_WideString& sError) { 262 if (params.size() < 2) 263 return false; 264 265 CJS_Array array1; 266 CJS_Array array2; 267 if (!params[0].ConvertToArray(pRuntime, array1)) 268 return false; 269 if (!params[1].ConvertToArray(pRuntime, array2)) 270 return false; 271 272 CPWL_Color color1; 273 CPWL_Color color2; 274 ConvertArrayToPWLColor(pRuntime, array1, &color1); 275 ConvertArrayToPWLColor(pRuntime, array2, &color2); 276 color1 = color1.ConvertColorType(color2.nColorType); 277 vRet = CJS_Value(pRuntime, color1 == color2); 278 return true; 279 } 280