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