Fix confusion between length in bytes and length in characters in app::response().
[pdfium.git] / fpdfsdk / src / javascript / color.cpp
index de5a53f..3841706 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#include "../../include/javascript/JavaScript.h"\r
-#include "../../include/javascript/IJavaScript.h"\r
-#include "../../include/javascript/JS_Define.h"\r
-#include "../../include/javascript/JS_Object.h"\r
-#include "../../include/javascript/JS_Value.h"\r
-#include "../../include/javascript/color.h"\r
-#include "../../include/javascript/JS_EventHandler.h"\r
-#include "../../include/javascript/JS_Context.h"\r
-#include "../../include/javascript/JS_Runtime.h"\r
-\r
-static v8::Isolate* GetIsolate(IFXJS_Context* cc)\r
-{\r
-       CJS_Context* pContext = (CJS_Context *)cc;\r
-       ASSERT(pContext != NULL);\r
-\r
-       CJS_Runtime* pRuntime = pContext->GetJSRuntime();\r
-       ASSERT(pRuntime != NULL);\r
-\r
-       return pRuntime->GetIsolate();\r
-}\r
-/* -------------------------- color -------------------------- */\r
-\r
-BEGIN_JS_STATIC_CONST(CJS_Color)\r
-END_JS_STATIC_CONST()\r
-\r
-BEGIN_JS_STATIC_PROP(CJS_Color)\r
-       JS_STATIC_PROP_ENTRY(black)\r
-       JS_STATIC_PROP_ENTRY(blue)\r
-       JS_STATIC_PROP_ENTRY(cyan)      \r
-       JS_STATIC_PROP_ENTRY(dkGray)\r
-       JS_STATIC_PROP_ENTRY(gray)\r
-       JS_STATIC_PROP_ENTRY(green)\r
-       JS_STATIC_PROP_ENTRY(ltGray)\r
-       JS_STATIC_PROP_ENTRY(magenta)\r
-       JS_STATIC_PROP_ENTRY(red)       \r
-       JS_STATIC_PROP_ENTRY(transparent)\r
-       JS_STATIC_PROP_ENTRY(white)\r
-       JS_STATIC_PROP_ENTRY(yellow)\r
-END_JS_STATIC_PROP()\r
-\r
-BEGIN_JS_STATIC_METHOD(CJS_Color)\r
-       JS_STATIC_METHOD_ENTRY(convert, 2)\r
-       JS_STATIC_METHOD_ENTRY(equal, 2)\r
-END_JS_STATIC_METHOD()\r
-\r
-IMPLEMENT_JS_CLASS(CJS_Color,color)\r
-\r
-color::color(CJS_Object* pJSObject): CJS_EmbedObj(pJSObject)\r
-{\r
-       m_crTransparent = CPWL_Color(COLORTYPE_TRANSPARENT);\r
-       m_crBlack = CPWL_Color(COLORTYPE_GRAY, 0);\r
-       m_crWhite = CPWL_Color(COLORTYPE_GRAY, 1);\r
-       m_crRed = CPWL_Color(COLORTYPE_RGB, 1, 0 ,0);\r
-       m_crGreen = CPWL_Color(COLORTYPE_RGB, 0, 1 ,0);\r
-       m_crBlue = CPWL_Color(COLORTYPE_RGB, 0, 0 ,1);\r
-       m_crCyan = CPWL_Color(COLORTYPE_CMYK, 1, 0 ,0, 0);\r
-       m_crMagenta = CPWL_Color(COLORTYPE_CMYK, 0, 1 ,0, 0);\r
-       m_crYellow = CPWL_Color(COLORTYPE_CMYK, 0, 0 ,1, 0);\r
-       m_crDKGray = CPWL_Color(COLORTYPE_GRAY, 0.25);\r
-       m_crGray = CPWL_Color(COLORTYPE_GRAY, 0.5);\r
-       m_crLTGray = CPWL_Color(COLORTYPE_GRAY, 0.75);\r
-}\r
-\r
-color::~color(void)\r
-{\r
-}\r
-\r
-void color::ConvertPWLColorToArray(const CPWL_Color& color, CJS_Array& array)\r
-{\r
-       switch (color.nColorType)\r
-       {\r
-       case COLORTYPE_TRANSPARENT:\r
-               array.SetElement(0, CJS_Value(array.GetIsolate(), "T"));\r
-               break;\r
-       case COLORTYPE_GRAY:\r
-               array.SetElement(0, CJS_Value(array.GetIsolate(),"G"));\r
-               array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));\r
-               break;\r
-       case COLORTYPE_RGB:\r
-               array.SetElement(0, CJS_Value(array.GetIsolate(),"RGB"));\r
-               array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));\r
-               array.SetElement(2, CJS_Value(array.GetIsolate(),color.fColor2));\r
-               array.SetElement(3, CJS_Value(array.GetIsolate(),color.fColor3));\r
-               break;\r
-       case COLORTYPE_CMYK:\r
-               array.SetElement(0, CJS_Value(array.GetIsolate(),"CMYK"));\r
-               array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));\r
-               array.SetElement(2, CJS_Value(array.GetIsolate(),color.fColor2));\r
-               array.SetElement(3, CJS_Value(array.GetIsolate(),color.fColor3));\r
-               array.SetElement(4, CJS_Value(array.GetIsolate(),color.fColor4));\r
-               break;\r
-       }\r
-}\r
-\r
-void color::ConvertArrayToPWLColor(CJS_Array& array, CPWL_Color& color)\r
-{\r
-       int nArrayLen = array.GetLength();\r
-       if (nArrayLen < 1) return;\r
-\r
-       CJS_Value value(array.GetIsolate());\r
-       CFX_ByteString sSpace;\r
-       array.GetElement(0, value);\r
-       sSpace = value;\r
-\r
-       double d1 = 0;\r
-       double d2 = 0;\r
-       double d3 = 0;\r
-       double d4 = 0;\r
-\r
-       if (nArrayLen > 1)\r
-       {\r
-               array.GetElement(1, value);\r
-               d1 = value;\r
-       }\r
-\r
-       if (nArrayLen > 2)\r
-       {\r
-               array.GetElement(2, value);\r
-               d2 = value;\r
-       }\r
-\r
-       if (nArrayLen > 3)\r
-       {\r
-               array.GetElement(3, value);\r
-               d3 = value;\r
-       }\r
-\r
-       if (nArrayLen > 4)\r
-       {\r
-               array.GetElement(4, value);\r
-               d4 = value;\r
-       }\r
-\r
-       if (sSpace == "T")\r
-       {\r
-               color = CPWL_Color(COLORTYPE_TRANSPARENT);\r
-       }\r
-       else if (sSpace == "G")\r
-       {\r
-               color = CPWL_Color(COLORTYPE_GRAY, (FX_FLOAT)d1);\r
-       }\r
-       else if (sSpace == "RGB")\r
-       {\r
-               color = CPWL_Color(COLORTYPE_RGB, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3);\r
-       }\r
-       else if (sSpace == "CMYK")\r
-       {\r
-               color = CPWL_Color(COLORTYPE_CMYK, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3, (FX_FLOAT)d4);\r
-       }\r
-}\r
-\r
-#define JS_IMPLEMENT_COLORPROP(prop, var)\\r
-FX_BOOL color::prop(OBJ_PROP_PARAMS)\\r
-{\\r
-       CJS_Context* pContext = (CJS_Context*)cc;\\r
-       v8::Isolate* isolate = pContext->GetJSRuntime()->GetIsolate();\\r
-       if (vp.IsGetting())\\r
-       {\\r
-               CJS_Array array(isolate);\\r
-               ConvertPWLColorToArray(var, array);\\r
-               vp << array;\\r
-       }\\r
-       else\\r
-       {\\r
-               CJS_Array array(isolate);\\r
-               if (!vp.ConvertToArray(array)) return FALSE;\\r
-               ConvertArrayToPWLColor(array, var);\\r
-       }\\r
-       return TRUE;\\r
-}\r
-\r
-JS_IMPLEMENT_COLORPROP(transparent, m_crTransparent)\r
-JS_IMPLEMENT_COLORPROP(black, m_crBlack)\r
-JS_IMPLEMENT_COLORPROP(white, m_crWhite)\r
-JS_IMPLEMENT_COLORPROP(red, m_crRed)\r
-JS_IMPLEMENT_COLORPROP(green, m_crGreen)\r
-JS_IMPLEMENT_COLORPROP(blue, m_crBlue)\r
-JS_IMPLEMENT_COLORPROP(cyan, m_crCyan)\r
-JS_IMPLEMENT_COLORPROP(magenta, m_crMagenta)\r
-JS_IMPLEMENT_COLORPROP(yellow, m_crYellow)\r
-JS_IMPLEMENT_COLORPROP(dkGray, m_crDKGray)\r
-JS_IMPLEMENT_COLORPROP(gray, m_crGray)\r
-JS_IMPLEMENT_COLORPROP(ltGray, m_crLTGray)\r
-\r
-FX_BOOL color::convert(OBJ_METHOD_PARAMS)\r
-{\r
-       v8::Isolate* isolate = GetIsolate(cc);\r
-       int iSize = params.size();\r
-       if (iSize < 2) return FALSE;\r
-       CJS_Array aSource(isolate);\r
-       if (!params[0].ConvertToArray(aSource)) return FALSE;\r
-\r
-       CPWL_Color crSource;\r
-       ConvertArrayToPWLColor(aSource, crSource);\r
-\r
-       CFX_ByteString sDestSpace = params[1];\r
-\r
-       int nColorType = COLORTYPE_TRANSPARENT;\r
-       \r
-       if (sDestSpace == "T")\r
-       {\r
-               nColorType = COLORTYPE_TRANSPARENT;\r
-       }\r
-       else if (sDestSpace == "G")\r
-       {\r
-               nColorType = COLORTYPE_GRAY;\r
-       }\r
-       else if (sDestSpace == "RGB")\r
-       {\r
-               nColorType = COLORTYPE_RGB;\r
-       }\r
-       else if (sDestSpace == "CMYK")\r
-       {\r
-               nColorType = COLORTYPE_CMYK;\r
-       }\r
-\r
-       CJS_Array aDest(isolate);\r
-       CPWL_Color crDest = crSource;\r
-       crDest.ConvertColorType(nColorType);\r
-       ConvertPWLColorToArray(crDest, aDest);\r
-       vRet = aDest;\r
-\r
-       return TRUE;\r
-}\r
-\r
-FX_BOOL color::equal(OBJ_METHOD_PARAMS)\r
-{\r
-       v8::Isolate* isolate = GetIsolate(cc);\r
-       if (params.size() < 2) return FALSE;\r
-\r
-       CJS_Array array1(isolate), array2(isolate);\r
-\r
-       if (!params[0].ConvertToArray(array1)) return FALSE;\r
-       if (!params[1].ConvertToArray(array2)) return FALSE;\r
-\r
-       CPWL_Color color1;\r
-       CPWL_Color color2;\r
-\r
-       ConvertArrayToPWLColor(array1, color1);\r
-       ConvertArrayToPWLColor(array2, color2);\r
-\r
-       color1.ConvertColorType(color2.nColorType);\r
-\r
-       vRet = color1 == color2;\r
-       return TRUE;\r
-}\r
-\r
+// 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 "../../include/javascript/JavaScript.h"
+#include "../../include/javascript/IJavaScript.h"
+#include "../../include/javascript/JS_Define.h"
+#include "../../include/javascript/JS_Object.h"
+#include "../../include/javascript/JS_Value.h"
+#include "../../include/javascript/color.h"
+#include "../../include/javascript/JS_EventHandler.h"
+#include "../../include/javascript/JS_Context.h"
+#include "../../include/javascript/JS_Runtime.h"
+
+static v8::Isolate* GetIsolate(IFXJS_Context* cc)
+{
+       CJS_Context* pContext = (CJS_Context *)cc;
+       ASSERT(pContext != NULL);
+
+       CJS_Runtime* pRuntime = pContext->GetJSRuntime();
+       ASSERT(pRuntime != NULL);
+
+       return pRuntime->GetIsolate();
+}
+/* -------------------------- color -------------------------- */
+
+BEGIN_JS_STATIC_CONST(CJS_Color)
+END_JS_STATIC_CONST()
+
+BEGIN_JS_STATIC_PROP(CJS_Color)
+       JS_STATIC_PROP_ENTRY(black)
+       JS_STATIC_PROP_ENTRY(blue)
+       JS_STATIC_PROP_ENTRY(cyan)      
+       JS_STATIC_PROP_ENTRY(dkGray)
+       JS_STATIC_PROP_ENTRY(gray)
+       JS_STATIC_PROP_ENTRY(green)
+       JS_STATIC_PROP_ENTRY(ltGray)
+       JS_STATIC_PROP_ENTRY(magenta)
+       JS_STATIC_PROP_ENTRY(red)       
+       JS_STATIC_PROP_ENTRY(transparent)
+       JS_STATIC_PROP_ENTRY(white)
+       JS_STATIC_PROP_ENTRY(yellow)
+END_JS_STATIC_PROP()
+
+BEGIN_JS_STATIC_METHOD(CJS_Color)
+       JS_STATIC_METHOD_ENTRY(convert, 2)
+       JS_STATIC_METHOD_ENTRY(equal, 2)
+END_JS_STATIC_METHOD()
+
+IMPLEMENT_JS_CLASS(CJS_Color,color)
+
+color::color(CJS_Object* pJSObject): CJS_EmbedObj(pJSObject)
+{
+       m_crTransparent = CPWL_Color(COLORTYPE_TRANSPARENT);
+       m_crBlack = CPWL_Color(COLORTYPE_GRAY, 0);
+       m_crWhite = CPWL_Color(COLORTYPE_GRAY, 1);
+       m_crRed = CPWL_Color(COLORTYPE_RGB, 1, 0 ,0);
+       m_crGreen = CPWL_Color(COLORTYPE_RGB, 0, 1 ,0);
+       m_crBlue = CPWL_Color(COLORTYPE_RGB, 0, 0 ,1);
+       m_crCyan = CPWL_Color(COLORTYPE_CMYK, 1, 0 ,0, 0);
+       m_crMagenta = CPWL_Color(COLORTYPE_CMYK, 0, 1 ,0, 0);
+       m_crYellow = CPWL_Color(COLORTYPE_CMYK, 0, 0 ,1, 0);
+       m_crDKGray = CPWL_Color(COLORTYPE_GRAY, 0.25);
+       m_crGray = CPWL_Color(COLORTYPE_GRAY, 0.5);
+       m_crLTGray = CPWL_Color(COLORTYPE_GRAY, 0.75);
+}
+
+color::~color(void)
+{
+}
+
+void color::ConvertPWLColorToArray(const CPWL_Color& color, CJS_Array& array)
+{
+       switch (color.nColorType)
+       {
+       case COLORTYPE_TRANSPARENT:
+               array.SetElement(0, CJS_Value(array.GetIsolate(), "T"));
+               break;
+       case COLORTYPE_GRAY:
+               array.SetElement(0, CJS_Value(array.GetIsolate(),"G"));
+               array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));
+               break;
+       case COLORTYPE_RGB:
+               array.SetElement(0, CJS_Value(array.GetIsolate(),"RGB"));
+               array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));
+               array.SetElement(2, CJS_Value(array.GetIsolate(),color.fColor2));
+               array.SetElement(3, CJS_Value(array.GetIsolate(),color.fColor3));
+               break;
+       case COLORTYPE_CMYK:
+               array.SetElement(0, CJS_Value(array.GetIsolate(),"CMYK"));
+               array.SetElement(1, CJS_Value(array.GetIsolate(),color.fColor1));
+               array.SetElement(2, CJS_Value(array.GetIsolate(),color.fColor2));
+               array.SetElement(3, CJS_Value(array.GetIsolate(),color.fColor3));
+               array.SetElement(4, CJS_Value(array.GetIsolate(),color.fColor4));
+               break;
+       }
+}
+
+void color::ConvertArrayToPWLColor(CJS_Array& array, CPWL_Color& color)
+{
+       int nArrayLen = array.GetLength();
+       if (nArrayLen < 1) return;
+
+       CJS_Value value(array.GetIsolate());
+       CFX_ByteString sSpace;
+       array.GetElement(0, value);
+       sSpace = value;
+
+       double d1 = 0;
+       double d2 = 0;
+       double d3 = 0;
+       double d4 = 0;
+
+       if (nArrayLen > 1)
+       {
+               array.GetElement(1, value);
+               d1 = value;
+       }
+
+       if (nArrayLen > 2)
+       {
+               array.GetElement(2, value);
+               d2 = value;
+       }
+
+       if (nArrayLen > 3)
+       {
+               array.GetElement(3, value);
+               d3 = value;
+       }
+
+       if (nArrayLen > 4)
+       {
+               array.GetElement(4, value);
+               d4 = value;
+       }
+
+       if (sSpace == "T")
+       {
+               color = CPWL_Color(COLORTYPE_TRANSPARENT);
+       }
+       else if (sSpace == "G")
+       {
+               color = CPWL_Color(COLORTYPE_GRAY, (FX_FLOAT)d1);
+       }
+       else if (sSpace == "RGB")
+       {
+               color = CPWL_Color(COLORTYPE_RGB, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3);
+       }
+       else if (sSpace == "CMYK")
+       {
+               color = CPWL_Color(COLORTYPE_CMYK, (FX_FLOAT)d1, (FX_FLOAT)d2, (FX_FLOAT)d3, (FX_FLOAT)d4);
+       }
+}
+
+#define JS_IMPLEMENT_COLORPROP(prop, var)\
+FX_BOOL color::prop(OBJ_PROP_PARAMS)\
+{\
+       CJS_Context* pContext = (CJS_Context*)cc;\
+       v8::Isolate* isolate = pContext->GetJSRuntime()->GetIsolate();\
+       if (vp.IsGetting())\
+       {\
+               CJS_Array array(isolate);\
+               ConvertPWLColorToArray(var, array);\
+               vp << array;\
+       }\
+       else\
+       {\
+               CJS_Array array(isolate);\
+               if (!vp.ConvertToArray(array)) return FALSE;\
+               ConvertArrayToPWLColor(array, var);\
+       }\
+       return TRUE;\
+}
+
+JS_IMPLEMENT_COLORPROP(transparent, m_crTransparent)
+JS_IMPLEMENT_COLORPROP(black, m_crBlack)
+JS_IMPLEMENT_COLORPROP(white, m_crWhite)
+JS_IMPLEMENT_COLORPROP(red, m_crRed)
+JS_IMPLEMENT_COLORPROP(green, m_crGreen)
+JS_IMPLEMENT_COLORPROP(blue, m_crBlue)
+JS_IMPLEMENT_COLORPROP(cyan, m_crCyan)
+JS_IMPLEMENT_COLORPROP(magenta, m_crMagenta)
+JS_IMPLEMENT_COLORPROP(yellow, m_crYellow)
+JS_IMPLEMENT_COLORPROP(dkGray, m_crDKGray)
+JS_IMPLEMENT_COLORPROP(gray, m_crGray)
+JS_IMPLEMENT_COLORPROP(ltGray, m_crLTGray)
+
+FX_BOOL color::convert(OBJ_METHOD_PARAMS)
+{
+       v8::Isolate* isolate = GetIsolate(cc);
+       int iSize = params.size();
+       if (iSize < 2) return FALSE;
+       CJS_Array aSource(isolate);
+       if (!params[0].ConvertToArray(aSource)) return FALSE;
+
+       CPWL_Color crSource;
+       ConvertArrayToPWLColor(aSource, crSource);
+
+       CFX_ByteString sDestSpace = params[1];
+
+       int nColorType = COLORTYPE_TRANSPARENT;
+       
+       if (sDestSpace == "T")
+       {
+               nColorType = COLORTYPE_TRANSPARENT;
+       }
+       else if (sDestSpace == "G")
+       {
+               nColorType = COLORTYPE_GRAY;
+       }
+       else if (sDestSpace == "RGB")
+       {
+               nColorType = COLORTYPE_RGB;
+       }
+       else if (sDestSpace == "CMYK")
+       {
+               nColorType = COLORTYPE_CMYK;
+       }
+
+       CJS_Array aDest(isolate);
+       CPWL_Color crDest = crSource;
+       crDest.ConvertColorType(nColorType);
+       ConvertPWLColorToArray(crDest, aDest);
+       vRet = aDest;
+
+       return TRUE;
+}
+
+FX_BOOL color::equal(OBJ_METHOD_PARAMS)
+{
+       v8::Isolate* isolate = GetIsolate(cc);
+       if (params.size() < 2) return FALSE;
+
+       CJS_Array array1(isolate), array2(isolate);
+
+       if (!params[0].ConvertToArray(array1)) return FALSE;
+       if (!params[1].ConvertToArray(array2)) return FALSE;
+
+       CPWL_Color color1;
+       CPWL_Color color2;
+
+       ConvertArrayToPWLColor(array1, color1);
+       ConvertArrayToPWLColor(array2, color2);
+
+       color1.ConvertColorType(color2.nColorType);
+
+       vRet = color1 == color2;
+       return TRUE;
+}
+