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