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_CPDFSDK_HELPERS_H_
8 #define FPDFSDK_CPDFSDK_HELPERS_H_
9
10 #include "build/build_config.h"
11 #include "core/fpdfapi/page/cpdf_page.h"
12 #include "core/fpdfapi/parser/cpdf_parser.h"
13 #include "core/fxge/dib/cfx_dibitmap.h"
14 #include "public/fpdf_doc.h"
15 #include "public/fpdf_ext.h"
16 #include "public/fpdfview.h"
17
18 #ifdef PDF_ENABLE_XFA
19 #include "core/fxcrt/fx_stream.h"
20 #endif // PDF_ENABLE_XFA
21
22 #if defined(OS_WIN)
23 #include <math.h>
24 #include <tchar.h>
25 #endif
26
27 class CPDF_Annot;
28 class CPDF_AnnotContext;
29 class CPDF_ClipPath;
30 class CPDF_ContentMarkItem;
31 class CPDF_Object;
32 class CPDF_Font;
33 class CPDF_LinkExtract;
34 class CPDF_PageObject;
35 class CPDF_Stream;
36 class CPDF_StructElement;
37 class CPDF_StructTree;
38 class CPDF_TextPage;
39 class CPDF_TextPageFind;
40 class CPDFSDK_FormFillEnvironment;
41 class CPDFSDK_InteractiveForm;
42 class FX_PATHPOINT;
43 struct CPDF_JavaScript;
44
45 // Conversions to/from underlying types.
46 IPDF_Page* IPDFPageFromFPDFPage(FPDF_PAGE page);
47 FPDF_PAGE FPDFPageFromIPDFPage(IPDF_Page* page);
48 CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page);
49 FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc);
50 CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc);
51
52 // Conversions to/from incomplete FPDF_ API types.
FPDFActionFromCPDFDictionary(const CPDF_Dictionary * action)53 inline FPDF_ACTION FPDFActionFromCPDFDictionary(const CPDF_Dictionary* action) {
54 return reinterpret_cast<FPDF_ACTION>(const_cast<CPDF_Dictionary*>(action));
55 }
CPDFDictionaryFromFPDFAction(FPDF_ACTION action)56 inline CPDF_Dictionary* CPDFDictionaryFromFPDFAction(FPDF_ACTION action) {
57 return reinterpret_cast<CPDF_Dictionary*>(action);
58 }
59
FPDFAnnotationFromCPDFAnnotContext(CPDF_AnnotContext * annot)60 inline FPDF_ANNOTATION FPDFAnnotationFromCPDFAnnotContext(
61 CPDF_AnnotContext* annot) {
62 return reinterpret_cast<FPDF_ANNOTATION>(annot);
63 }
CPDFAnnotContextFromFPDFAnnotation(FPDF_ANNOTATION annot)64 inline CPDF_AnnotContext* CPDFAnnotContextFromFPDFAnnotation(
65 FPDF_ANNOTATION annot) {
66 return reinterpret_cast<CPDF_AnnotContext*>(annot);
67 }
68
FPDFAttachmentFromCPDFObject(CPDF_Object * attachment)69 inline FPDF_ATTACHMENT FPDFAttachmentFromCPDFObject(CPDF_Object* attachment) {
70 return reinterpret_cast<FPDF_ATTACHMENT>(attachment);
71 }
CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment)72 inline CPDF_Object* CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment) {
73 return reinterpret_cast<CPDF_Object*>(attachment);
74 }
75
FPDFBitmapFromCFXDIBitmap(CFX_DIBitmap * bitmap)76 inline FPDF_BITMAP FPDFBitmapFromCFXDIBitmap(CFX_DIBitmap* bitmap) {
77 return reinterpret_cast<FPDF_BITMAP>(bitmap);
78 }
CFXDIBitmapFromFPDFBitmap(FPDF_BITMAP bitmap)79 inline CFX_DIBitmap* CFXDIBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
80 return reinterpret_cast<CFX_DIBitmap*>(bitmap);
81 }
82
FPDFBookmarkFromCPDFDictionary(const CPDF_Dictionary * bookmark)83 inline FPDF_BOOKMARK FPDFBookmarkFromCPDFDictionary(
84 const CPDF_Dictionary* bookmark) {
85 return reinterpret_cast<FPDF_BOOKMARK>(
86 const_cast<CPDF_Dictionary*>(bookmark));
87 }
CPDFDictionaryFromFPDFBookmark(FPDF_BOOKMARK bookmark)88 inline CPDF_Dictionary* CPDFDictionaryFromFPDFBookmark(FPDF_BOOKMARK bookmark) {
89 return reinterpret_cast<CPDF_Dictionary*>(bookmark);
90 }
91
FPDFClipPathFromCPDFClipPath(CPDF_ClipPath * path)92 inline FPDF_CLIPPATH FPDFClipPathFromCPDFClipPath(CPDF_ClipPath* path) {
93 return reinterpret_cast<FPDF_CLIPPATH>(path);
94 }
CPDFClipPathFromFPDFClipPath(FPDF_CLIPPATH path)95 inline CPDF_ClipPath* CPDFClipPathFromFPDFClipPath(FPDF_CLIPPATH path) {
96 return reinterpret_cast<CPDF_ClipPath*>(path);
97 }
98
FPDFDestFromCPDFArray(const CPDF_Array * dest)99 inline FPDF_DEST FPDFDestFromCPDFArray(const CPDF_Array* dest) {
100 return reinterpret_cast<FPDF_DEST>(const_cast<CPDF_Array*>(dest));
101 }
CPDFArrayFromFPDFDest(FPDF_DEST dest)102 inline CPDF_Array* CPDFArrayFromFPDFDest(FPDF_DEST dest) {
103 return reinterpret_cast<CPDF_Array*>(dest);
104 }
105
FPDFFontFromCPDFFont(CPDF_Font * font)106 inline FPDF_FONT FPDFFontFromCPDFFont(CPDF_Font* font) {
107 return reinterpret_cast<FPDF_FONT>(font);
108 }
CPDFFontFromFPDFFont(FPDF_FONT font)109 inline CPDF_Font* CPDFFontFromFPDFFont(FPDF_FONT font) {
110 return reinterpret_cast<CPDF_Font*>(font);
111 }
112
FPDFJavaScriptActionFromCPDFJavaScriptAction(CPDF_JavaScript * javascript)113 inline FPDF_JAVASCRIPT_ACTION FPDFJavaScriptActionFromCPDFJavaScriptAction(
114 CPDF_JavaScript* javascript) {
115 return reinterpret_cast<FPDF_JAVASCRIPT_ACTION>(javascript);
116 }
CPDFJavaScriptActionFromFPDFJavaScriptAction(FPDF_JAVASCRIPT_ACTION javascript)117 inline CPDF_JavaScript* CPDFJavaScriptActionFromFPDFJavaScriptAction(
118 FPDF_JAVASCRIPT_ACTION javascript) {
119 return reinterpret_cast<CPDF_JavaScript*>(javascript);
120 }
121
FPDFLinkFromCPDFDictionary(CPDF_Dictionary * link)122 inline FPDF_LINK FPDFLinkFromCPDFDictionary(CPDF_Dictionary* link) {
123 return reinterpret_cast<FPDF_LINK>(link);
124 }
CPDFDictionaryFromFPDFLink(FPDF_LINK link)125 inline CPDF_Dictionary* CPDFDictionaryFromFPDFLink(FPDF_LINK link) {
126 return reinterpret_cast<CPDF_Dictionary*>(link);
127 }
128
FPDFPageLinkFromCPDFLinkExtract(CPDF_LinkExtract * link)129 inline FPDF_PAGELINK FPDFPageLinkFromCPDFLinkExtract(CPDF_LinkExtract* link) {
130 return reinterpret_cast<FPDF_PAGELINK>(link);
131 }
CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link)132 inline CPDF_LinkExtract* CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link) {
133 return reinterpret_cast<CPDF_LinkExtract*>(link);
134 }
135
FPDFPageObjectFromCPDFPageObject(CPDF_PageObject * page_object)136 inline FPDF_PAGEOBJECT FPDFPageObjectFromCPDFPageObject(
137 CPDF_PageObject* page_object) {
138 return reinterpret_cast<FPDF_PAGEOBJECT>(page_object);
139 }
CPDFPageObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object)140 inline CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(
141 FPDF_PAGEOBJECT page_object) {
142 return reinterpret_cast<CPDF_PageObject*>(page_object);
143 }
144
FPDFPageObjectMarkFromCPDFContentMarkItem(CPDF_ContentMarkItem * mark)145 inline FPDF_PAGEOBJECTMARK FPDFPageObjectMarkFromCPDFContentMarkItem(
146 CPDF_ContentMarkItem* mark) {
147 return reinterpret_cast<FPDF_PAGEOBJECTMARK>(mark);
148 }
CPDFContentMarkItemFromFPDFPageObjectMark(FPDF_PAGEOBJECTMARK mark)149 inline CPDF_ContentMarkItem* CPDFContentMarkItemFromFPDFPageObjectMark(
150 FPDF_PAGEOBJECTMARK mark) {
151 return reinterpret_cast<CPDF_ContentMarkItem*>(mark);
152 }
153
FPDFPageRangeFromCPDFArray(CPDF_Array * range)154 inline FPDF_PAGERANGE FPDFPageRangeFromCPDFArray(CPDF_Array* range) {
155 return reinterpret_cast<FPDF_PAGERANGE>(range);
156 }
CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range)157 inline CPDF_Array* CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range) {
158 return reinterpret_cast<CPDF_Array*>(range);
159 }
160
FPDFPathSegmentFromFXPathPoint(const FX_PATHPOINT * segment)161 inline FPDF_PATHSEGMENT FPDFPathSegmentFromFXPathPoint(
162 const FX_PATHPOINT* segment) {
163 return reinterpret_cast<FPDF_PATHSEGMENT>(segment);
164 }
FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment)165 inline const FX_PATHPOINT* FXPathPointFromFPDFPathSegment(
166 FPDF_PATHSEGMENT segment) {
167 return reinterpret_cast<const FX_PATHPOINT*>(segment);
168 }
169
FPDFStructTreeFromCPDFStructTree(CPDF_StructTree * struct_tree)170 inline FPDF_STRUCTTREE FPDFStructTreeFromCPDFStructTree(
171 CPDF_StructTree* struct_tree) {
172 return reinterpret_cast<FPDF_STRUCTTREE>(struct_tree);
173 }
CPDFStructTreeFromFPDFStructTree(FPDF_STRUCTTREE struct_tree)174 inline CPDF_StructTree* CPDFStructTreeFromFPDFStructTree(
175 FPDF_STRUCTTREE struct_tree) {
176 return reinterpret_cast<CPDF_StructTree*>(struct_tree);
177 }
178
FPDFStructElementFromCPDFStructElement(CPDF_StructElement * struct_element)179 inline FPDF_STRUCTELEMENT FPDFStructElementFromCPDFStructElement(
180 CPDF_StructElement* struct_element) {
181 return reinterpret_cast<FPDF_STRUCTELEMENT>(struct_element);
182 }
CPDFStructElementFromFPDFStructElement(FPDF_STRUCTELEMENT struct_element)183 inline CPDF_StructElement* CPDFStructElementFromFPDFStructElement(
184 FPDF_STRUCTELEMENT struct_element) {
185 return reinterpret_cast<CPDF_StructElement*>(struct_element);
186 }
187
FPDFTextPageFromCPDFTextPage(CPDF_TextPage * page)188 inline FPDF_TEXTPAGE FPDFTextPageFromCPDFTextPage(CPDF_TextPage* page) {
189 return reinterpret_cast<FPDF_TEXTPAGE>(page);
190 }
CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE page)191 inline CPDF_TextPage* CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE page) {
192 return reinterpret_cast<CPDF_TextPage*>(page);
193 }
194
FPDFSchHandleFromCPDFTextPageFind(CPDF_TextPageFind * handle)195 inline FPDF_SCHHANDLE FPDFSchHandleFromCPDFTextPageFind(
196 CPDF_TextPageFind* handle) {
197 return reinterpret_cast<FPDF_SCHHANDLE>(handle);
198 }
CPDFTextPageFindFromFPDFSchHandle(FPDF_SCHHANDLE handle)199 inline CPDF_TextPageFind* CPDFTextPageFindFromFPDFSchHandle(
200 FPDF_SCHHANDLE handle) {
201 return reinterpret_cast<CPDF_TextPageFind*>(handle);
202 }
203
FPDFFormHandleFromCPDFSDKFormFillEnvironment(CPDFSDK_FormFillEnvironment * handle)204 inline FPDF_FORMHANDLE FPDFFormHandleFromCPDFSDKFormFillEnvironment(
205 CPDFSDK_FormFillEnvironment* handle) {
206 return reinterpret_cast<FPDF_FORMHANDLE>(handle);
207 }
208 inline CPDFSDK_FormFillEnvironment*
CPDFSDKFormFillEnvironmentFromFPDFFormHandle(FPDF_FORMHANDLE handle)209 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(FPDF_FORMHANDLE handle) {
210 return reinterpret_cast<CPDFSDK_FormFillEnvironment*>(handle);
211 }
212
213 CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle);
214
215 ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
216 WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
217
218 #ifdef PDF_ENABLE_XFA
219 // Layering prevents fxcrt from knowing about FPDF_FILEHANDLER, so this can't
220 // be a static method of IFX_SeekableStream.
221 RetainPtr<IFX_SeekableStream> MakeSeekableStream(
222 FPDF_FILEHANDLER* pFileHandler);
223 #endif // PDF_ENABLE_XFA
224
225 const CPDF_Array* GetQuadPointsArrayFromDictionary(const CPDF_Dictionary* dict);
226 CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict);
227 CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict);
228 bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index);
229 bool GetQuadPointsAtIndex(const CPDF_Array* array,
230 size_t quad_index,
231 FS_QUADPOINTSF* quad_points);
232
233 CFX_PointF CFXPointFFromFSPointF(const FS_POINTF& point);
234
235 CFX_FloatRect CFXFloatRectFromFSRectF(const FS_RECTF& rect);
236 FS_RECTF FSRectFFromCFXFloatRect(const CFX_FloatRect& rect);
237
238 CFX_Matrix CFXMatrixFromFSMatrix(const FS_MATRIX& matrix);
239 FS_MATRIX FSMatrixFromCFXMatrix(const CFX_Matrix& matrix);
240
241 unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text,
242 void* buffer,
243 unsigned long buflen);
244
245 // Returns the length of the raw stream data from |stream|. The raw data is the
246 // stream's data as stored in the PDF without applying any filters. If |buffer|
247 // is non-nullptr and |buflen| is large enough to contain the raw data, then
248 // the raw data is copied into |buffer|.
249 unsigned long GetRawStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
250 void* buffer,
251 unsigned long buflen);
252
253 // Return the length of the decoded stream data of |stream|. The decoded data is
254 // the uncompressed stream data, i.e. the raw stream data after having all
255 // filters applied. If |buffer| is non-nullptr and |buflen| is large enough to
256 // contain the decoded data, then the decoded data is copied into |buffer|.
257 unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
258 void* buffer,
259 unsigned long buflen);
260
261 void SetPDFSandboxPolicy(FPDF_DWORD policy, FPDF_BOOL enable);
262 FPDF_BOOL IsPDFSandboxPolicyEnabled(FPDF_DWORD policy);
263
264 void SetPDFUnsupportInfo(UNSUPPORT_INFO* unsp_info);
265 UNSUPPORT_INFO* GetPDFUnssuportInto();
266 void ReportUnsupportedFeatures(CPDF_Document* pDoc);
267 void ReportUnsupportedXFA(CPDF_Document* pDoc);
268 void CheckForUnsupportedAnnot(const CPDF_Annot* pAnnot);
269 void ProcessParseError(CPDF_Parser::Error err);
270
271 #endif // FPDFSDK_CPDFSDK_HELPERS_H_
272