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 "PWL_Wnd.h"
11#include "core/include/fpdfapi/fpdf_parser.h"  // For CPDF_Point.
12#include "core/include/fpdfdoc/fpdf_vt.h"      // For CPVT_WordRange.
13#include "fpdfsdk/include/fxedit/fx_edit.h"
14
15class CFX_PathData;
16struct CPWL_Color;
17
18template <class T>
19T PWL_MIN(const T& i, const T& j) {
20  return ((i < j) ? i : j);
21}
22template <class T>
23T PWL_MAX(const T& i, const T& j) {
24  return ((i > j) ? i : j);
25}
26
27#define PWL_PDF2WIN(color) (uint8_t(color * 255))
28#define PWL_WIN2PDF(color) ((FX_FLOAT)((FX_FLOAT)color / 255.0f))
29
30#define PWL_MAKEDWORD(low, high) \
31  ((FX_DWORD)((FX_WORD)(low) | (FX_DWORD)(((FX_WORD)(high)) << 16)))
32#define PWL_GETLOWWORD(dword) ((FX_WORD)(dword))
33#define PWL_GETHIGHWORD(dword) ((FX_WORD)(dword >> 16))
34
35#define PWL_ICONTYPE_CHECKMARK 0
36#define PWL_ICONTYPE_CIRCLE 1
37#define PWL_ICONTYPE_COMMENT 2
38#define PWL_ICONTYPE_CROSS 3
39#define PWL_ICONTYPE_HELP 4
40#define PWL_ICONTYPE_INSERTTEXT 5
41#define PWL_ICONTYPE_KEY 6
42#define PWL_ICONTYPE_NEWPARAGRAPH 7
43#define PWL_ICONTYPE_TEXTNOTE 8
44#define PWL_ICONTYPE_PARAGRAPH 9
45#define PWL_ICONTYPE_RIGHTARROW 10
46#define PWL_ICONTYPE_RIGHTPOINTER 11
47#define PWL_ICONTYPE_STAR 12
48#define PWL_ICONTYPE_UPARROW 13
49#define PWL_ICONTYPE_UPLEFTARROW 14
50
51#define PWL_ICONTYPE_GRAPH 15
52#define PWL_ICONTYPE_PAPERCLIP 16
53#define PWL_ICONTYPE_ATTACHMENT 17
54#define PWL_ICONTYPE_TAG 18
55
56#define PWL_ICONTYPE_FOXIT 19
57
58#define PWL_ICONTYPE_UNKNOWN -1
59
60// checkbox & radiobutton style
61#define PCS_CHECK 0
62#define PCS_CIRCLE 1
63#define PCS_CROSS 2
64#define PCS_DIAMOND 3
65#define PCS_SQUARE 4
66#define PCS_STAR 5
67
68#define PWL_PI 3.14159265358979f
69#define PWL_BEZIER 0.5522847498308f
70
71// pushbutton layout style
72#define PPBL_LABEL 0
73#define PPBL_ICON 1
74#define PPBL_ICONTOPLABELBOTTOM 2
75#define PPBL_LABELTOPICONBOTTOM 3
76#define PPBL_ICONLEFTLABELRIGHT 4
77#define PPBL_LABELLEFTICONRIGHT 5
78#define PPBL_LABELOVERICON 6
79
80class CPWL_Point : public CPDF_Point {
81 public:
82  CPWL_Point() : CPDF_Point(0.0f, 0.0f) {}
83  CPWL_Point(FX_FLOAT fx, FX_FLOAT fy) : CPDF_Point(fx, fy) {}
84  CPWL_Point(const CPWL_Point& point) : CPDF_Point(point.x, point.y) {}
85};
86
87enum PWL_PATHDATA_TYPE {
88  PWLPT_MOVETO,
89  PWLPT_LINETO,
90  PWLPT_BEZIERTO,
91  PWLPT_UNKNOWN
92};
93
94enum PWL_PATH_TYPE { PWLPT_PATHDATA, PWLPT_STREAM };
95
96class CPWL_PathData {
97 public:
98  CPWL_PathData() : point(), type(PWLPT_UNKNOWN) {}
99  CPWL_PathData(const CPWL_Point& pt, PWL_PATHDATA_TYPE tp)
100      : point(pt), type(tp) {}
101
102  CPWL_Point point;
103  PWL_PATHDATA_TYPE type;
104};
105
106class IPWL_SpellCheck;
107
108class CPWL_Utils {
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,
114                              const CPDF_Rect& rcChild);
115  static CPDF_Rect ScaleRect(const CPDF_Rect& rcRect, FX_FLOAT fScale);
116  static CPVT_WordRange OverlapWordRange(const CPVT_WordRange& wr1,
117                                         const CPVT_WordRange& wr2);
118  static CPDF_Rect GetCenterSquare(const CPDF_Rect& rect);
119  static CPWL_Color SubstractColor(const CPWL_Color& sColor,
120                                   FX_FLOAT fColorSub);
121  static CPWL_Color DevideColor(const CPWL_Color& sColor,
122                                FX_FLOAT fColorDevide);
123  static CPDF_Rect MaxRect(const CPDF_Rect& rect1, const CPDF_Rect& rect2);
124  static CPDF_Rect OffsetRect(const CPDF_Rect& rect, FX_FLOAT x, FX_FLOAT y);
125  static CPDF_Point OffsetPoint(const CPDF_Point& point,
126                                FX_FLOAT x,
127                                FX_FLOAT y);
128  static FX_COLORREF PWLColorToFXColor(const CPWL_Color& color,
129                                       int32_t nTransparancy = 255);
130  static FX_BOOL IsBlackOrWhite(const CPWL_Color& color);
131  static CPWL_Color GetReverseColor(const CPWL_Color& color);
132
133  static CFX_ByteString GetColorAppStream(const CPWL_Color& color,
134                                          const FX_BOOL& bFillOrStroke = TRUE);
135  static CFX_ByteString GetBorderAppStream(const CPDF_Rect& rect,
136                                           FX_FLOAT fWidth,
137                                           const CPWL_Color& color,
138                                           const CPWL_Color& crLeftTop,
139                                           const CPWL_Color& crRightBottom,
140                                           int32_t nStyle,
141                                           const CPWL_Dash& dash);
142  static CFX_ByteString GetCircleBorderAppStream(
143      const CPDF_Rect& rect,
144      FX_FLOAT fWidth,
145      const CPWL_Color& color,
146      const CPWL_Color& crLeftTop,
147      const CPWL_Color& crRightBottom,
148      int32_t nStyle,
149      const CPWL_Dash& dash);
150  static CFX_ByteString GetRectFillAppStream(const CPDF_Rect& rect,
151                                             const CPWL_Color& color);
152  static CFX_ByteString GetCircleFillAppStream(const CPDF_Rect& rect,
153                                               const CPWL_Color& color);
154
155  static CFX_ByteString GetPushButtonAppStream(const CPDF_Rect& rcBBox,
156                                               IFX_Edit_FontMap* pFontMap,
157                                               CPDF_Stream* pIconStream,
158                                               CPDF_IconFit& IconFit,
159                                               const CFX_WideString& sLabel,
160                                               const CPWL_Color& crText,
161                                               FX_FLOAT fFontSize,
162                                               int32_t nLayOut);
163  static CFX_ByteString GetCheckBoxAppStream(const CPDF_Rect& rcBBox,
164                                             int32_t nStyle,
165                                             const CPWL_Color& crText);
166  static CFX_ByteString GetRadioButtonAppStream(const CPDF_Rect& rcBBox,
167                                                int32_t nStyle,
168                                                const CPWL_Color& crText);
169
170  static CFX_ByteString GetEditAppStream(IFX_Edit* pEdit,
171                                         const CPDF_Point& ptOffset,
172                                         const CPVT_WordRange* pRange = NULL,
173                                         FX_BOOL bContinuous = TRUE,
174                                         FX_WORD SubWord = 0);
175  static CFX_ByteString GetEditSelAppStream(
176      IFX_Edit* pEdit,
177      const CPDF_Point& ptOffset,
178      const CPVT_WordRange* pRange = NULL);
179  static CFX_ByteString GetSpellCheckAppStream(
180      IFX_Edit* pEdit,
181      IPWL_SpellCheck* pSpellCheck,
182      const CPDF_Point& ptOffset,
183      const CPVT_WordRange* pRange = NULL);
184  static CFX_ByteString GetTextAppStream(const CPDF_Rect& rcBBox,
185                                         IFX_Edit_FontMap* pFontMap,
186                                         const CFX_WideString& sText,
187                                         int32_t nAlignmentH,
188                                         int32_t nAlignmentV,
189                                         FX_FLOAT fFontSize,
190                                         FX_BOOL bMultiLine,
191                                         FX_BOOL bAutoReturn,
192                                         const CPWL_Color& crText);
193  static CFX_ByteString GetDropButtonAppStream(const CPDF_Rect& rcBBox);
194
195  static void DrawFillRect(CFX_RenderDevice* pDevice,
196                           CFX_Matrix* pUser2Device,
197                           const CPDF_Rect& rect,
198                           const CPWL_Color& color,
199                           int32_t nTransparancy);
200  static void DrawFillRect(CFX_RenderDevice* pDevice,
201                           CFX_Matrix* pUser2Device,
202                           const CPDF_Rect& rect,
203                           const FX_COLORREF& color);
204  static void DrawStrokeRect(CFX_RenderDevice* pDevice,
205                             CFX_Matrix* pUser2Device,
206                             const CPDF_Rect& rect,
207                             const FX_COLORREF& color,
208                             FX_FLOAT fWidth);
209  static void DrawStrokeLine(CFX_RenderDevice* pDevice,
210                             CFX_Matrix* pUser2Device,
211                             const CPDF_Point& ptMoveTo,
212                             const CPDF_Point& ptLineTo,
213                             const FX_COLORREF& color,
214                             FX_FLOAT fWidth);
215  static void DrawBorder(CFX_RenderDevice* pDevice,
216                         CFX_Matrix* pUser2Device,
217                         const CPDF_Rect& rect,
218                         FX_FLOAT fWidth,
219                         const CPWL_Color& color,
220                         const CPWL_Color& crLeftTop,
221                         const CPWL_Color& crRightBottom,
222                         int32_t nStyle,
223                         int32_t nTransparancy);
224  static void DrawFillArea(CFX_RenderDevice* pDevice,
225                           CFX_Matrix* pUser2Device,
226                           const CPDF_Point* pPts,
227                           int32_t nCount,
228                           const FX_COLORREF& color);
229  static void DrawShadow(CFX_RenderDevice* pDevice,
230                         CFX_Matrix* pUser2Device,
231                         FX_BOOL bVertical,
232                         FX_BOOL bHorizontal,
233                         CPDF_Rect rect,
234                         int32_t nTransparancy,
235                         int32_t nStartGray,
236                         int32_t nEndGray);
237  static void DrawEditSpellCheck(CFX_RenderDevice* pDevice,
238                                 CFX_Matrix* pUser2Device,
239                                 IFX_Edit* pEdit,
240                                 const CPDF_Rect& rcClip,
241                                 const CPDF_Point& ptOffset,
242                                 const CPVT_WordRange* pRange,
243                                 IPWL_SpellCheck* pSpellCheck);
244
245 public:
246  static void ConvertCMYK2RGB(FX_FLOAT dC,
247                              FX_FLOAT dM,
248                              FX_FLOAT dY,
249                              FX_FLOAT dK,
250                              FX_FLOAT& dR,
251                              FX_FLOAT& dG,
252                              FX_FLOAT& dB);
253  static void ConvertRGB2CMYK(FX_FLOAT dR,
254                              FX_FLOAT dG,
255                              FX_FLOAT dB,
256                              FX_FLOAT& dC,
257                              FX_FLOAT& dM,
258                              FX_FLOAT& dY,
259                              FX_FLOAT& dK);
260
261  static void ConvertRGB2GRAY(FX_FLOAT dR,
262                              FX_FLOAT dG,
263                              FX_FLOAT dB,
264                              FX_FLOAT& dGray);
265  static void ConvertGRAY2RGB(FX_FLOAT dGray,
266                              FX_FLOAT& dR,
267                              FX_FLOAT& dG,
268                              FX_FLOAT& dB);
269
270  static void ConvertCMYK2GRAY(FX_FLOAT dC,
271                               FX_FLOAT dM,
272                               FX_FLOAT dY,
273                               FX_FLOAT dK,
274                               FX_FLOAT& dGray);
275  static void ConvertGRAY2CMYK(FX_FLOAT dGray,
276                               FX_FLOAT& dC,
277                               FX_FLOAT& dM,
278                               FX_FLOAT& dY,
279                               FX_FLOAT& dK);
280
281  static void PWLColorToARGB(const CPWL_Color& color,
282                             int32_t& alpha,
283                             FX_FLOAT& red,
284                             FX_FLOAT& green,
285                             FX_FLOAT& blue);
286
287 public:
288  static CFX_ByteString GetIconAppStream(
289      int32_t nType,
290      const CPDF_Rect& rect,
291      const CPWL_Color& crFill,
292      const CPWL_Color& crStroke = PWL_DEFAULT_BLACKCOLOR);
293  static void DrawIconAppStream(CFX_RenderDevice* pDevice,
294                                CFX_Matrix* pUser2Device,
295                                int32_t nType,
296                                const CPDF_Rect& rect,
297                                const CPWL_Color& crFill,
298                                const CPWL_Color& crStroke,
299                                const int32_t nTransparancy);
300
301 private:
302  static CFX_ByteString GetAppStreamFromArray(const CPWL_PathData* pPathData,
303                                              int32_t nCount);
304  static void GetPathDataFromArray(CFX_PathData& path,
305                                   const CPWL_PathData* pPathData,
306                                   int32_t nCount);
307
308  static CFX_ByteString GetAppStream_Check(const CPDF_Rect& rcBBox,
309                                           const CPWL_Color& crText);
310  static CFX_ByteString GetAppStream_Circle(const CPDF_Rect& rcBBox,
311                                            const CPWL_Color& crText);
312  static CFX_ByteString GetAppStream_Cross(const CPDF_Rect& rcBBox,
313                                           const CPWL_Color& crText);
314  static CFX_ByteString GetAppStream_Diamond(const CPDF_Rect& rcBBox,
315                                             const CPWL_Color& crText);
316  static CFX_ByteString GetAppStream_Square(const CPDF_Rect& rcBBox,
317                                            const CPWL_Color& crText);
318  static CFX_ByteString GetAppStream_Star(const CPDF_Rect& rcBBox,
319                                          const CPWL_Color& crText);
320
321  static CFX_ByteString GetAP_Check(const CPDF_Rect& crBBox);
322  static CFX_ByteString GetAP_Circle(const CPDF_Rect& crBBox);
323  static CFX_ByteString GetAP_Cross(const CPDF_Rect& crBBox);
324  static CFX_ByteString GetAP_Diamond(const CPDF_Rect& crBBox);
325  static CFX_ByteString GetAP_Square(const CPDF_Rect& crBBox);
326  static CFX_ByteString GetAP_Star(const CPDF_Rect& crBBox);
327  static CFX_ByteString GetAP_HalfCircle(const CPDF_Rect& crBBox,
328                                         FX_FLOAT fRotate);
329
330  static void GetGraphics_Checkmark(CFX_ByteString& sPathData,
331                                    CFX_PathData& path,
332                                    const CPDF_Rect& crBBox,
333                                    const PWL_PATH_TYPE type);
334  static void GetGraphics_Circle(CFX_ByteString& sPathData,
335                                 CFX_PathData& path,
336                                 const CPDF_Rect& crBBox,
337                                 const PWL_PATH_TYPE type);
338  static void GetGraphics_Comment(CFX_ByteString& sPathData,
339                                  CFX_PathData& path,
340                                  const CPDF_Rect& crBBox,
341                                  const PWL_PATH_TYPE type);
342  static void GetGraphics_Cross(CFX_ByteString& sPathData,
343                                CFX_PathData& path,
344                                const CPDF_Rect& crBBox,
345                                const PWL_PATH_TYPE type);
346  static void GetGraphics_Help(CFX_ByteString& sPathData,
347                               CFX_PathData& path,
348                               const CPDF_Rect& crBBox,
349                               const PWL_PATH_TYPE type);
350  static void GetGraphics_InsertText(CFX_ByteString& sPathData,
351                                     CFX_PathData& path,
352                                     const CPDF_Rect& crBBox,
353                                     const PWL_PATH_TYPE type);
354  static void GetGraphics_Key(CFX_ByteString& sPathData,
355                              CFX_PathData& path,
356                              const CPDF_Rect& crBBox,
357                              const PWL_PATH_TYPE type);
358  static void GetGraphics_NewParagraph(CFX_ByteString& sPathData,
359                                       CFX_PathData& path,
360                                       const CPDF_Rect& crBBox,
361                                       const PWL_PATH_TYPE type);
362  static void GetGraphics_TextNote(CFX_ByteString& sPathData,
363                                   CFX_PathData& path,
364                                   const CPDF_Rect& crBBox,
365                                   const PWL_PATH_TYPE type);
366  static void GetGraphics_Paragraph(CFX_ByteString& sPathData,
367                                    CFX_PathData& path,
368                                    const CPDF_Rect& crBBox,
369                                    const PWL_PATH_TYPE type);
370  static void GetGraphics_RightArrow(CFX_ByteString& sPathData,
371                                     CFX_PathData& path,
372                                     const CPDF_Rect& crBBox,
373                                     const PWL_PATH_TYPE type);
374  static void GetGraphics_RightPointer(CFX_ByteString& sPathData,
375                                       CFX_PathData& path,
376                                       const CPDF_Rect& crBBox,
377                                       const PWL_PATH_TYPE type);
378  static void GetGraphics_Star(CFX_ByteString& sPathData,
379                               CFX_PathData& path,
380                               const CPDF_Rect& crBBox,
381                               const PWL_PATH_TYPE type);
382  static void GetGraphics_UpArrow(CFX_ByteString& sPathData,
383                                  CFX_PathData& path,
384                                  const CPDF_Rect& crBBox,
385                                  const PWL_PATH_TYPE type);
386  static void GetGraphics_UpLeftArrow(CFX_ByteString& sPathData,
387                                      CFX_PathData& path,
388                                      const CPDF_Rect& crBBox,
389                                      const PWL_PATH_TYPE type);
390  static void GetGraphics_Graph(CFX_ByteString& sPathData,
391                                CFX_PathData& path,
392                                const CPDF_Rect& crBBox,
393                                const PWL_PATH_TYPE type);
394  static void GetGraphics_Paperclip(CFX_ByteString& sPathData,
395                                    CFX_PathData& path,
396                                    const CPDF_Rect& crBBox,
397                                    const PWL_PATH_TYPE type);
398  static void GetGraphics_Attachment(CFX_ByteString& sPathData,
399                                     CFX_PathData& path,
400                                     const CPDF_Rect& crBBox,
401                                     const PWL_PATH_TYPE type);
402  static void GetGraphics_Tag(CFX_ByteString& sPathData,
403                              CFX_PathData& path,
404                              const CPDF_Rect& crBBox,
405                              const PWL_PATH_TYPE type);
406  static void GetGraphics_Foxit(CFX_ByteString& sPathData,
407                                CFX_PathData& path,
408                                const CPDF_Rect& crBBox,
409                                const PWL_PATH_TYPE type);
410};
411
412#endif  // FPDFSDK_INCLUDE_PDFWINDOW_PWL_UTILS_H_
413