Home | History | Annotate | Download | only in pdfwindow
      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 #ifndef _PWL_UTILS_H_
      8 #define _PWL_UTILS_H_
      9 
     10 template<class T> T PWL_MIN (const T & i, const T & j) { return ((i < j) ? i : j); }
     11 template<class T> T PWL_MAX (const T & i, const T & j) { return ((i > j) ? i : j); }
     12 
     13 #define PWL_PDF2WIN(color)					(FX_BYTE(color*255))
     14 #define PWL_WIN2PDF(color)					((FX_FLOAT)((FX_FLOAT)color/255.0f))
     15 
     16 #define PWL_MAKEDWORD(low,high)				((FX_DWORD)((FX_WORD)(low) | (FX_DWORD)(((FX_WORD)(high))<<16)))
     17 #define PWL_GETLOWWORD(dword)				((FX_WORD)(dword))
     18 #define PWL_GETHIGHWORD(dword)				((FX_WORD)(dword>>16))
     19 
     20 #define PWL_ICONTYPE_CHECKMARK				0
     21 #define PWL_ICONTYPE_CIRCLE					1
     22 #define PWL_ICONTYPE_COMMENT				2
     23 #define PWL_ICONTYPE_CROSS					3
     24 #define PWL_ICONTYPE_HELP					4
     25 #define PWL_ICONTYPE_INSERTTEXT				5
     26 #define PWL_ICONTYPE_KEY					6
     27 #define PWL_ICONTYPE_NEWPARAGRAPH			7
     28 #define PWL_ICONTYPE_TEXTNOTE				8
     29 #define PWL_ICONTYPE_PARAGRAPH				9
     30 #define PWL_ICONTYPE_RIGHTARROW				10
     31 #define PWL_ICONTYPE_RIGHTPOINTER			11
     32 #define PWL_ICONTYPE_STAR					12
     33 #define PWL_ICONTYPE_UPARROW				13
     34 #define PWL_ICONTYPE_UPLEFTARROW			14
     35 
     36 #define PWL_ICONTYPE_GRAPH					15
     37 #define PWL_ICONTYPE_PAPERCLIP				16
     38 #define PWL_ICONTYPE_ATTACHMENT				17
     39 #define PWL_ICONTYPE_TAG					18
     40 
     41 #define PWL_ICONTYPE_FOXIT					19
     42 
     43 #define PWL_ICONTYPE_UNKNOWN				-1
     44 
     45 //checkbox & radiobutton style
     46 #define PCS_CHECK							0
     47 #define PCS_CIRCLE							1
     48 #define PCS_CROSS							2
     49 #define PCS_DIAMOND							3
     50 #define PCS_SQUARE							4
     51 #define PCS_STAR							5
     52 
     53 #define	PWL_PI								3.14159265358979f
     54 #define PWL_BEZIER							0.5522847498308f
     55 
     56 //pushbutton layout style
     57 #define PPBL_LABEL							0
     58 #define PPBL_ICON							1
     59 #define PPBL_ICONTOPLABELBOTTOM				2
     60 #define	PPBL_LABELTOPICONBOTTOM				3
     61 #define	PPBL_ICONLEFTLABELRIGHT				4
     62 #define PPBL_LABELLEFTICONRIGHT				5
     63 #define PPBL_LABELOVERICON					6
     64 
     65 class CPWL_Point : public CPDF_Point
     66 {
     67 public:
     68 	CPWL_Point() : CPDF_Point(0.0f,0.0f){}
     69 	CPWL_Point(FX_FLOAT fx, FX_FLOAT fy) : CPDF_Point(fx,fy) {}
     70 	CPWL_Point(const CPWL_Point& point) : CPDF_Point(point.x, point.y) {}
     71 };
     72 
     73 enum PWL_PATHDATA_TYPE
     74 {
     75 	PWLPT_MOVETO,
     76 	PWLPT_LINETO,
     77 	PWLPT_BEZIERTO,
     78 	PWLPT_UNKNOWN
     79 };
     80 
     81 enum PWL_PATH_TYPE
     82 {
     83 	PWLPT_PATHDATA,
     84 	PWLPT_STREAM
     85 };
     86 
     87 class CPWL_PathData
     88 {
     89 public:
     90 	CPWL_PathData() : point(), type(PWLPT_UNKNOWN){}
     91 	CPWL_PathData(const CPWL_Point& pt, PWL_PATHDATA_TYPE tp) : point(pt), type(tp) {}
     92 
     93 	CPWL_Point								point;
     94 	PWL_PATHDATA_TYPE						type;
     95 };
     96 
     97 class IPWL_SpellCheck;
     98 
     99 class PWL_CLASS CPWL_Utils
    100 {
    101 public:
    102 	static CPDF_Rect						InflateRect(const CPDF_Rect& rcRect, FX_FLOAT fSize);
    103 	static CPDF_Rect						DeflateRect(const CPDF_Rect& rcRect, FX_FLOAT fSize);
    104 	static FX_BOOL							IntersectRect(const CPDF_Rect& rect1, const CPDF_Rect& rect2);
    105 	static FX_BOOL							ContainsRect(const CPDF_Rect& rcParent, const CPDF_Rect& rcChild);
    106 	static CPDF_Rect						ScaleRect(const CPDF_Rect& rcRect,FX_FLOAT fScale);
    107 	static CPVT_WordRange					OverlapWordRange(const CPVT_WordRange& wr1, const CPVT_WordRange& wr2);
    108 	static CPDF_Rect						GetCenterSquare(const CPDF_Rect & rect);
    109 	static CPWL_Color						SubstractColor(const CPWL_Color & sColor,FX_FLOAT fColorSub);
    110 	static CPWL_Color						DevideColor(const CPWL_Color & sColor,FX_FLOAT fColorDevide);
    111 	static CPDF_Rect						MaxRect(const CPDF_Rect & rect1,const CPDF_Rect & rect2);
    112 	static CPDF_Rect						OffsetRect(const CPDF_Rect & rect,FX_FLOAT x,FX_FLOAT y);
    113 	static CPDF_Point						OffsetPoint(const  CPDF_Point & point,FX_FLOAT x,FX_FLOAT y);
    114 	static FX_COLORREF						PWLColorToFXColor(const CPWL_Color& color, FX_INT32 nTransparancy = 255);
    115 	static FX_BOOL							IsBlackOrWhite(const CPWL_Color& color);
    116 	static CPWL_Color						GetReverseColor(const CPWL_Color& color);
    117 
    118 	static CFX_ByteString					GetColorAppStream(const CPWL_Color & color,const FX_BOOL & bFillOrStroke = TRUE);
    119 	static CFX_ByteString					GetBorderAppStream(const CPDF_Rect & rect, FX_FLOAT fWidth,
    120 												const CPWL_Color & color, const CPWL_Color & crLeftTop, const CPWL_Color & crRightBottom,
    121 												FX_INT32 nStyle, const CPWL_Dash & dash);
    122 	static CFX_ByteString					GetCircleBorderAppStream(const CPDF_Rect & rect, FX_FLOAT fWidth,
    123 												const CPWL_Color & color, const CPWL_Color & crLeftTop, const CPWL_Color & crRightBottom,
    124 												FX_INT32 nStyle, const CPWL_Dash & dash);
    125 	static CFX_ByteString					GetRectFillAppStream(const CPDF_Rect & rect,const CPWL_Color & color);
    126 	static CFX_ByteString					GetCircleFillAppStream(const CPDF_Rect & rect,const CPWL_Color & color);
    127 
    128 	static CFX_ByteString					GetPushButtonAppStream(const CPDF_Rect & rcBBox,
    129 														IFX_Edit_FontMap * pFontMap,
    130 														CPDF_Stream * pIconStream,
    131 														CPDF_IconFit & IconFit,
    132 														const CFX_WideString & sLabel,
    133 														const CPWL_Color & crText,
    134 														FX_FLOAT fFontSize,
    135 														FX_INT32 nLayOut);
    136 	static CFX_ByteString					GetCheckBoxAppStream(const CPDF_Rect & rcBBox,
    137 														FX_INT32 nStyle,
    138 														const CPWL_Color & crText);
    139 	static CFX_ByteString					GetRadioButtonAppStream(const CPDF_Rect & rcBBox,
    140 														FX_INT32 nStyle,
    141 														const CPWL_Color & crText);
    142 
    143 	static CFX_ByteString					GetEditAppStream(IFX_Edit* pEdit, const CPDF_Point & ptOffset, const CPVT_WordRange * pRange = NULL,
    144 														FX_BOOL bContinuous = TRUE, FX_WORD SubWord = 0);
    145 	static CFX_ByteString					GetEditSelAppStream(IFX_Edit* pEdit, const CPDF_Point & ptOffset,
    146 														const CPVT_WordRange * pRange = NULL);
    147 	static CFX_ByteString					GetSpellCheckAppStream(IFX_Edit* pEdit, IPWL_SpellCheck* pSpellCheck,
    148 														const CPDF_Point & ptOffset,
    149 														const CPVT_WordRange * pRange = NULL);
    150 	static CFX_ByteString					GetTextAppStream(const CPDF_Rect & rcBBox,IFX_Edit_FontMap * pFontMap,
    151 														const CFX_WideString & sText, FX_INT32 nAlignmentH, FX_INT32 nAlignmentV,
    152 														FX_FLOAT fFontSize, FX_BOOL bMultiLine, FX_BOOL bAutoReturn, const CPWL_Color & crText);
    153 	static CFX_ByteString					GetDropButtonAppStream(const CPDF_Rect & rcBBox);
    154 
    155 	static void								DrawFillRect(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,const CPDF_Rect & rect,
    156 														const CPWL_Color & color, FX_INT32 nTransparancy);
    157 	static void								DrawFillRect(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,
    158 														const CPDF_Rect & rect,const FX_COLORREF & color);
    159 	static void								DrawStrokeRect(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,const CPDF_Rect & rect,
    160 														const FX_COLORREF & color, FX_FLOAT fWidth);
    161 	static void								DrawStrokeLine(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,
    162 														const CPDF_Point & ptMoveTo, const CPDF_Point & ptLineTo, const FX_COLORREF & color, FX_FLOAT fWidth);
    163 	static void								DrawBorder(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,
    164 												const CPDF_Rect & rect, FX_FLOAT fWidth,
    165 												const CPWL_Color & color, const CPWL_Color & crLeftTop, const CPWL_Color & crRightBottom,
    166 												FX_INT32 nStyle, const CPWL_Dash & dash, FX_INT32 nTransparancy);
    167 	static void								DrawFillArea(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,
    168 														const CPDF_Point* pPts, FX_INT32 nCount, const FX_COLORREF& color);
    169 	static void								DrawShadow(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,
    170 														FX_BOOL bVertical, FX_BOOL bHorizontal, CPDF_Rect rect,
    171 														FX_INT32 nTransparancy, FX_INT32 nStartGray, FX_INT32 nEndGray);
    172 	static void								DrawEditSpellCheck(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device, IFX_Edit* pEdit,
    173 														const CPDF_Rect& rcClip, const CPDF_Point& ptOffset, const CPVT_WordRange* pRange,
    174 														IPWL_SpellCheck* pSpellCheck);
    175 public:
    176 	static void								ConvertCMYK2RGB(FX_FLOAT dC,FX_FLOAT dM,FX_FLOAT dY,FX_FLOAT dK,FX_FLOAT &dR,FX_FLOAT &dG,FX_FLOAT &dB);
    177 	static void								ConvertRGB2CMYK(FX_FLOAT dR,FX_FLOAT dG,FX_FLOAT dB,FX_FLOAT &dC,FX_FLOAT &dM,FX_FLOAT &dY,FX_FLOAT &dK);
    178 
    179 	static void								ConvertRGB2GRAY(FX_FLOAT dR,FX_FLOAT dG,FX_FLOAT dB,FX_FLOAT &dGray);
    180 	static void								ConvertGRAY2RGB(FX_FLOAT dGray,FX_FLOAT &dR,FX_FLOAT &dG,FX_FLOAT &dB);
    181 
    182 	static void								ConvertCMYK2GRAY(FX_FLOAT dC,FX_FLOAT dM,FX_FLOAT dY,FX_FLOAT dK,FX_FLOAT &dGray);
    183 	static void								ConvertGRAY2CMYK(FX_FLOAT dGray,FX_FLOAT &dC,FX_FLOAT &dM,FX_FLOAT &dY,FX_FLOAT &dK);
    184 
    185 	static void								PWLColorToARGB(const CPWL_Color& color, FX_INT32& alpha, FX_FLOAT& red, FX_FLOAT& green, FX_FLOAT& blue);
    186 
    187 public:
    188 	static CFX_ByteString					GetIconAppStream(FX_INT32 nType, const CPDF_Rect& rect, const CPWL_Color& crFill,
    189 												const CPWL_Color& crStroke = PWL_DEFAULT_BLACKCOLOR);
    190 	static void								DrawIconAppStream(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,
    191 												FX_INT32 nType, const CPDF_Rect & rect, const CPWL_Color& crFill,
    192 												const CPWL_Color& crStroke, const FX_INT32 nTransparancy);
    193 
    194 private:
    195 	static CFX_ByteString					GetAppStreamFromArray(const CPWL_PathData* pPathData, FX_INT32 nCount);
    196 	static void								GetPathDataFromArray(CFX_PathData& path, const CPWL_PathData* pPathData, FX_INT32 nCount);
    197 
    198 	static CFX_ByteString					GetAppStream_Check(const CPDF_Rect & rcBBox, const CPWL_Color & crText);
    199 	static CFX_ByteString					GetAppStream_Circle(const CPDF_Rect & rcBBox, const CPWL_Color & crText);
    200 	static CFX_ByteString					GetAppStream_Cross(const CPDF_Rect & rcBBox, const CPWL_Color & crText);
    201 	static CFX_ByteString					GetAppStream_Diamond(const CPDF_Rect & rcBBox, const CPWL_Color & crText);
    202 	static CFX_ByteString					GetAppStream_Square(const CPDF_Rect & rcBBox, const CPWL_Color & crText);
    203 	static CFX_ByteString					GetAppStream_Star(const CPDF_Rect & rcBBox, const CPWL_Color & crText);
    204 
    205 	static CFX_ByteString					GetAP_Check(const CPDF_Rect & crBBox);
    206 	static CFX_ByteString					GetAP_Circle(const CPDF_Rect & crBBox);
    207 	static CFX_ByteString					GetAP_Cross(const CPDF_Rect & crBBox);
    208 	static CFX_ByteString					GetAP_Diamond(const CPDF_Rect & crBBox);
    209 	static CFX_ByteString					GetAP_Square(const CPDF_Rect & crBBox);
    210 	static CFX_ByteString					GetAP_Star(const CPDF_Rect & crBBox);
    211 	static CFX_ByteString					GetAP_HalfCircle(const CPDF_Rect & crBBox,FX_FLOAT fRotate);
    212 
    213 	static void								GetGraphics_Checkmark(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    214 	static void								GetGraphics_Circle(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    215 	static void								GetGraphics_Comment(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    216 	static void								GetGraphics_Cross(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    217 	static void								GetGraphics_Help(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    218 	static void								GetGraphics_InsertText(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    219 	static void								GetGraphics_Key(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    220 	static void								GetGraphics_NewParagraph(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    221 	static void								GetGraphics_TextNote(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    222 	static void								GetGraphics_Paragraph(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    223 	static void								GetGraphics_RightArrow(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    224 	static void								GetGraphics_RightPointer(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    225 	static void								GetGraphics_Star(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    226 	static void								GetGraphics_UpArrow(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    227 	static void								GetGraphics_UpLeftArrow(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    228 	static void								GetGraphics_Graph(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    229 	static void								GetGraphics_Paperclip(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    230 	static void								GetGraphics_Attachment(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    231 	static void								GetGraphics_Tag(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    232 	static void								GetGraphics_Foxit(CFX_ByteString& sPathData, CFX_PathData& path, const CPDF_Rect& crBBox, const PWL_PATH_TYPE type);
    233 };
    234 
    235 #endif // !defined(AFX_PWL_UTILS_H__D32812AD_A875_4E08_9D3C_0A57020987C6__INCLUDED_)
    236 
    237 
    238