Clean up CPDF_AnnotList.
[pdfium.git] / fpdfsdk / include / pdfwindow / PWL_Utils.h
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
18 template <class T>
19 T PWL_MIN(const T& i, const T& j) {
20   return ((i < j) ? i : j);
21 }
22 template <class T>
23 T 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
80 class 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
87 enum PWL_PATHDATA_TYPE {
88   PWLPT_MOVETO,
89   PWLPT_LINETO,
90   PWLPT_BEZIERTO,
91   PWLPT_UNKNOWN
92 };
93
94 enum PWL_PATH_TYPE { PWLPT_PATHDATA, PWLPT_STREAM };
95
96 class 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
106 class IPWL_SpellCheck;
107
108 class 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                            CPDF_Matrix* pUser2Device,
197                            const CPDF_Rect& rect,
198                            const CPWL_Color& color,
199                            int32_t nTransparancy);
200   static void DrawFillRect(CFX_RenderDevice* pDevice,
201                            CPDF_Matrix* pUser2Device,
202                            const CPDF_Rect& rect,
203                            const FX_COLORREF& color);
204   static void DrawStrokeRect(CFX_RenderDevice* pDevice,
205                              CPDF_Matrix* pUser2Device,
206                              const CPDF_Rect& rect,
207                              const FX_COLORREF& color,
208                              FX_FLOAT fWidth);
209   static void DrawStrokeLine(CFX_RenderDevice* pDevice,
210                              CPDF_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                          CPDF_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                          const CPWL_Dash& dash,
224                          int32_t nTransparancy);
225   static void DrawFillArea(CFX_RenderDevice* pDevice,
226                            CPDF_Matrix* pUser2Device,
227                            const CPDF_Point* pPts,
228                            int32_t nCount,
229                            const FX_COLORREF& color);
230   static void DrawShadow(CFX_RenderDevice* pDevice,
231                          CPDF_Matrix* pUser2Device,
232                          FX_BOOL bVertical,
233                          FX_BOOL bHorizontal,
234                          CPDF_Rect rect,
235                          int32_t nTransparancy,
236                          int32_t nStartGray,
237                          int32_t nEndGray);
238   static void DrawEditSpellCheck(CFX_RenderDevice* pDevice,
239                                  CPDF_Matrix* pUser2Device,
240                                  IFX_Edit* pEdit,
241                                  const CPDF_Rect& rcClip,
242                                  const CPDF_Point& ptOffset,
243                                  const CPVT_WordRange* pRange,
244                                  IPWL_SpellCheck* pSpellCheck);
245
246  public:
247   static void ConvertCMYK2RGB(FX_FLOAT dC,
248                               FX_FLOAT dM,
249                               FX_FLOAT dY,
250                               FX_FLOAT dK,
251                               FX_FLOAT& dR,
252                               FX_FLOAT& dG,
253                               FX_FLOAT& dB);
254   static void ConvertRGB2CMYK(FX_FLOAT dR,
255                               FX_FLOAT dG,
256                               FX_FLOAT dB,
257                               FX_FLOAT& dC,
258                               FX_FLOAT& dM,
259                               FX_FLOAT& dY,
260                               FX_FLOAT& dK);
261
262   static void ConvertRGB2GRAY(FX_FLOAT dR,
263                               FX_FLOAT dG,
264                               FX_FLOAT dB,
265                               FX_FLOAT& dGray);
266   static void ConvertGRAY2RGB(FX_FLOAT dGray,
267                               FX_FLOAT& dR,
268                               FX_FLOAT& dG,
269                               FX_FLOAT& dB);
270
271   static void ConvertCMYK2GRAY(FX_FLOAT dC,
272                                FX_FLOAT dM,
273                                FX_FLOAT dY,
274                                FX_FLOAT dK,
275                                FX_FLOAT& dGray);
276   static void ConvertGRAY2CMYK(FX_FLOAT dGray,
277                                FX_FLOAT& dC,
278                                FX_FLOAT& dM,
279                                FX_FLOAT& dY,
280                                FX_FLOAT& dK);
281
282   static void PWLColorToARGB(const CPWL_Color& color,
283                              int32_t& alpha,
284                              FX_FLOAT& red,
285                              FX_FLOAT& green,
286                              FX_FLOAT& blue);
287
288  public:
289   static CFX_ByteString GetIconAppStream(
290       int32_t nType,
291       const CPDF_Rect& rect,
292       const CPWL_Color& crFill,
293       const CPWL_Color& crStroke = PWL_DEFAULT_BLACKCOLOR);
294   static void DrawIconAppStream(CFX_RenderDevice* pDevice,
295                                 CPDF_Matrix* pUser2Device,
296                                 int32_t nType,
297                                 const CPDF_Rect& rect,
298                                 const CPWL_Color& crFill,
299                                 const CPWL_Color& crStroke,
300                                 const int32_t nTransparancy);
301
302  private:
303   static CFX_ByteString GetAppStreamFromArray(const CPWL_PathData* pPathData,
304                                               int32_t nCount);
305   static void GetPathDataFromArray(CFX_PathData& path,
306                                    const CPWL_PathData* pPathData,
307                                    int32_t nCount);
308
309   static CFX_ByteString GetAppStream_Check(const CPDF_Rect& rcBBox,
310                                            const CPWL_Color& crText);
311   static CFX_ByteString GetAppStream_Circle(const CPDF_Rect& rcBBox,
312                                             const CPWL_Color& crText);
313   static CFX_ByteString GetAppStream_Cross(const CPDF_Rect& rcBBox,
314                                            const CPWL_Color& crText);
315   static CFX_ByteString GetAppStream_Diamond(const CPDF_Rect& rcBBox,
316                                              const CPWL_Color& crText);
317   static CFX_ByteString GetAppStream_Square(const CPDF_Rect& rcBBox,
318                                             const CPWL_Color& crText);
319   static CFX_ByteString GetAppStream_Star(const CPDF_Rect& rcBBox,
320                                           const CPWL_Color& crText);
321
322   static CFX_ByteString GetAP_Check(const CPDF_Rect& crBBox);
323   static CFX_ByteString GetAP_Circle(const CPDF_Rect& crBBox);
324   static CFX_ByteString GetAP_Cross(const CPDF_Rect& crBBox);
325   static CFX_ByteString GetAP_Diamond(const CPDF_Rect& crBBox);
326   static CFX_ByteString GetAP_Square(const CPDF_Rect& crBBox);
327   static CFX_ByteString GetAP_Star(const CPDF_Rect& crBBox);
328   static CFX_ByteString GetAP_HalfCircle(const CPDF_Rect& crBBox,
329                                          FX_FLOAT fRotate);
330
331   static void GetGraphics_Checkmark(CFX_ByteString& sPathData,
332                                     CFX_PathData& path,
333                                     const CPDF_Rect& crBBox,
334                                     const PWL_PATH_TYPE type);
335   static void GetGraphics_Circle(CFX_ByteString& sPathData,
336                                  CFX_PathData& path,
337                                  const CPDF_Rect& crBBox,
338                                  const PWL_PATH_TYPE type);
339   static void GetGraphics_Comment(CFX_ByteString& sPathData,
340                                   CFX_PathData& path,
341                                   const CPDF_Rect& crBBox,
342                                   const PWL_PATH_TYPE type);
343   static void GetGraphics_Cross(CFX_ByteString& sPathData,
344                                 CFX_PathData& path,
345                                 const CPDF_Rect& crBBox,
346                                 const PWL_PATH_TYPE type);
347   static void GetGraphics_Help(CFX_ByteString& sPathData,
348                                CFX_PathData& path,
349                                const CPDF_Rect& crBBox,
350                                const PWL_PATH_TYPE type);
351   static void GetGraphics_InsertText(CFX_ByteString& sPathData,
352                                      CFX_PathData& path,
353                                      const CPDF_Rect& crBBox,
354                                      const PWL_PATH_TYPE type);
355   static void GetGraphics_Key(CFX_ByteString& sPathData,
356                               CFX_PathData& path,
357                               const CPDF_Rect& crBBox,
358                               const PWL_PATH_TYPE type);
359   static void GetGraphics_NewParagraph(CFX_ByteString& sPathData,
360                                        CFX_PathData& path,
361                                        const CPDF_Rect& crBBox,
362                                        const PWL_PATH_TYPE type);
363   static void GetGraphics_TextNote(CFX_ByteString& sPathData,
364                                    CFX_PathData& path,
365                                    const CPDF_Rect& crBBox,
366                                    const PWL_PATH_TYPE type);
367   static void GetGraphics_Paragraph(CFX_ByteString& sPathData,
368                                     CFX_PathData& path,
369                                     const CPDF_Rect& crBBox,
370                                     const PWL_PATH_TYPE type);
371   static void GetGraphics_RightArrow(CFX_ByteString& sPathData,
372                                      CFX_PathData& path,
373                                      const CPDF_Rect& crBBox,
374                                      const PWL_PATH_TYPE type);
375   static void GetGraphics_RightPointer(CFX_ByteString& sPathData,
376                                        CFX_PathData& path,
377                                        const CPDF_Rect& crBBox,
378                                        const PWL_PATH_TYPE type);
379   static void GetGraphics_Star(CFX_ByteString& sPathData,
380                                CFX_PathData& path,
381                                const CPDF_Rect& crBBox,
382                                const PWL_PATH_TYPE type);
383   static void GetGraphics_UpArrow(CFX_ByteString& sPathData,
384                                   CFX_PathData& path,
385                                   const CPDF_Rect& crBBox,
386                                   const PWL_PATH_TYPE type);
387   static void GetGraphics_UpLeftArrow(CFX_ByteString& sPathData,
388                                       CFX_PathData& path,
389                                       const CPDF_Rect& crBBox,
390                                       const PWL_PATH_TYPE type);
391   static void GetGraphics_Graph(CFX_ByteString& sPathData,
392                                 CFX_PathData& path,
393                                 const CPDF_Rect& crBBox,
394                                 const PWL_PATH_TYPE type);
395   static void GetGraphics_Paperclip(CFX_ByteString& sPathData,
396                                     CFX_PathData& path,
397                                     const CPDF_Rect& crBBox,
398                                     const PWL_PATH_TYPE type);
399   static void GetGraphics_Attachment(CFX_ByteString& sPathData,
400                                      CFX_PathData& path,
401                                      const CPDF_Rect& crBBox,
402                                      const PWL_PATH_TYPE type);
403   static void GetGraphics_Tag(CFX_ByteString& sPathData,
404                               CFX_PathData& path,
405                               const CPDF_Rect& crBBox,
406                               const PWL_PATH_TYPE type);
407   static void GetGraphics_Foxit(CFX_ByteString& sPathData,
408                                 CFX_PathData& path,
409                                 const CPDF_Rect& crBBox,
410                                 const PWL_PATH_TYPE type);
411 };
412
413 #endif  // FPDFSDK_INCLUDE_PDFWINDOW_PWL_UTILS_H_