• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 <vector>
11 
12 #include "build/build_config.h"
13 #include "core/fpdfapi/page/cpdf_page.h"
14 #include "core/fpdfapi/parser/cpdf_parser.h"
15 #include "core/fxcrt/retain_ptr.h"
16 #include "core/fxge/cfx_path.h"
17 #include "core/fxge/dib/cfx_dibitmap.h"
18 #include "public/fpdf_doc.h"
19 #include "public/fpdf_ext.h"
20 #include "public/fpdfview.h"
21 
22 #ifdef PDF_ENABLE_XFA
23 #include "core/fxcrt/fx_stream.h"
24 #endif  // PDF_ENABLE_XFA
25 
26 class CPDF_Annot;
27 class CPDF_AnnotContext;
28 class CPDF_ClipPath;
29 class CPDF_ContentMarkItem;
30 class CPDF_Object;
31 class CPDF_Font;
32 class CPDF_LinkExtract;
33 class CPDF_PageObject;
34 class CPDF_RenderOptions;
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 struct CPDF_JavaScript;
43 struct XObjectContext;
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(const CPDF_Array * range)154 inline FPDF_PAGERANGE FPDFPageRangeFromCPDFArray(const CPDF_Array* range) {
155   return reinterpret_cast<FPDF_PAGERANGE>(range);
156 }
CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range)157 inline const CPDF_Array* CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range) {
158   return reinterpret_cast<const CPDF_Array*>(range);
159 }
160 
FPDFPathSegmentFromFXPathPoint(const CFX_Path::Point * segment)161 inline FPDF_PATHSEGMENT FPDFPathSegmentFromFXPathPoint(
162     const CFX_Path::Point* segment) {
163   return reinterpret_cast<FPDF_PATHSEGMENT>(segment);
164 }
FXPathPointFromFPDFPathSegment(FPDF_PATHSEGMENT segment)165 inline const CFX_Path::Point* FXPathPointFromFPDFPathSegment(
166     FPDF_PATHSEGMENT segment) {
167   return reinterpret_cast<const CFX_Path::Point*>(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 
FPDFStructElementAttrFromCPDFDictionary(const CPDF_Dictionary * dictionary)188 inline FPDF_STRUCTELEMENT_ATTR FPDFStructElementAttrFromCPDFDictionary(
189     const CPDF_Dictionary* dictionary) {
190   return reinterpret_cast<FPDF_STRUCTELEMENT_ATTR>(dictionary);
191 }
CPDFDictionaryFromFPDFStructElementAttr(FPDF_STRUCTELEMENT_ATTR struct_element_attr)192 inline const CPDF_Dictionary* CPDFDictionaryFromFPDFStructElementAttr(
193     FPDF_STRUCTELEMENT_ATTR struct_element_attr) {
194   return reinterpret_cast<const CPDF_Dictionary*>(struct_element_attr);
195 }
196 
FPDFTextPageFromCPDFTextPage(CPDF_TextPage * page)197 inline FPDF_TEXTPAGE FPDFTextPageFromCPDFTextPage(CPDF_TextPage* page) {
198   return reinterpret_cast<FPDF_TEXTPAGE>(page);
199 }
CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE page)200 inline CPDF_TextPage* CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE page) {
201   return reinterpret_cast<CPDF_TextPage*>(page);
202 }
203 
FPDFSchHandleFromCPDFTextPageFind(CPDF_TextPageFind * handle)204 inline FPDF_SCHHANDLE FPDFSchHandleFromCPDFTextPageFind(
205     CPDF_TextPageFind* handle) {
206   return reinterpret_cast<FPDF_SCHHANDLE>(handle);
207 }
CPDFTextPageFindFromFPDFSchHandle(FPDF_SCHHANDLE handle)208 inline CPDF_TextPageFind* CPDFTextPageFindFromFPDFSchHandle(
209     FPDF_SCHHANDLE handle) {
210   return reinterpret_cast<CPDF_TextPageFind*>(handle);
211 }
212 
FPDFFormHandleFromCPDFSDKFormFillEnvironment(CPDFSDK_FormFillEnvironment * handle)213 inline FPDF_FORMHANDLE FPDFFormHandleFromCPDFSDKFormFillEnvironment(
214     CPDFSDK_FormFillEnvironment* handle) {
215   return reinterpret_cast<FPDF_FORMHANDLE>(handle);
216 }
217 inline CPDFSDK_FormFillEnvironment*
CPDFSDKFormFillEnvironmentFromFPDFFormHandle(FPDF_FORMHANDLE handle)218 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(FPDF_FORMHANDLE handle) {
219   return reinterpret_cast<CPDFSDK_FormFillEnvironment*>(handle);
220 }
221 
FPDFSignatureFromCPDFDictionary(const CPDF_Dictionary * dictionary)222 inline FPDF_SIGNATURE FPDFSignatureFromCPDFDictionary(
223     const CPDF_Dictionary* dictionary) {
224   return reinterpret_cast<FPDF_SIGNATURE>(dictionary);
225 }
CPDFDictionaryFromFPDFSignature(FPDF_SIGNATURE signature)226 inline const CPDF_Dictionary* CPDFDictionaryFromFPDFSignature(
227     FPDF_SIGNATURE signature) {
228   return reinterpret_cast<const CPDF_Dictionary*>(signature);
229 }
230 
FPDFXObjectFromXObjectContext(XObjectContext * xobject)231 inline FPDF_XOBJECT FPDFXObjectFromXObjectContext(XObjectContext* xobject) {
232   return reinterpret_cast<FPDF_XOBJECT>(xobject);
233 }
234 
XObjectContextFromFPDFXObject(FPDF_XOBJECT xobject)235 inline XObjectContext* XObjectContextFromFPDFXObject(FPDF_XOBJECT xobject) {
236   return reinterpret_cast<XObjectContext*>(xobject);
237 }
238 
239 CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle);
240 
241 ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
242 WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
243 
244 #ifdef PDF_ENABLE_XFA
245 // Layering prevents fxcrt from knowing about FPDF_FILEHANDLER, so this can't
246 // be a static method of IFX_SeekableStream.
247 RetainPtr<IFX_SeekableStream> MakeSeekableStream(
248     FPDF_FILEHANDLER* pFileHandler);
249 #endif  // PDF_ENABLE_XFA
250 
251 RetainPtr<const CPDF_Array> GetQuadPointsArrayFromDictionary(
252     const CPDF_Dictionary* dict);
253 RetainPtr<CPDF_Array> GetMutableQuadPointsArrayFromDictionary(
254     CPDF_Dictionary* dict);
255 RetainPtr<CPDF_Array> AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict);
256 bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index);
257 bool GetQuadPointsAtIndex(RetainPtr<const CPDF_Array> array,
258                           size_t quad_index,
259                           FS_QUADPOINTSF* quad_points);
260 
261 CFX_PointF CFXPointFFromFSPointF(const FS_POINTF& point);
262 
263 CFX_FloatRect CFXFloatRectFromFSRectF(const FS_RECTF& rect);
264 FS_RECTF FSRectFFromCFXFloatRect(const CFX_FloatRect& rect);
265 
266 CFX_Matrix CFXMatrixFromFSMatrix(const FS_MATRIX& matrix);
267 FS_MATRIX FSMatrixFromCFXMatrix(const CFX_Matrix& matrix);
268 
269 unsigned long NulTerminateMaybeCopyAndReturnLength(const ByteString& text,
270                                                    void* buffer,
271                                                    unsigned long buflen);
272 
273 unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text,
274                                                   void* buffer,
275                                                   unsigned long buflen);
276 
277 // Returns the length of the raw stream data from |stream|. The raw data is the
278 // stream's data as stored in the PDF without applying any filters. If |buffer|
279 // is non-empty and its length is large enough to contain the raw data, then
280 // the raw data is copied into |buffer|.
281 unsigned long GetRawStreamMaybeCopyAndReturnLength(
282     RetainPtr<const CPDF_Stream> stream,
283     pdfium::span<uint8_t> buffer);
284 
285 // Return the length of the decoded stream data of |stream|. The decoded data is
286 // the uncompressed stream data, i.e. the raw stream data after having all
287 // filters applied. If |buffer| is non-empty and its length is large enough to
288 // contain the decoded data, then the decoded data is copied into |buffer|.
289 unsigned long DecodeStreamMaybeCopyAndReturnLength(
290     RetainPtr<const CPDF_Stream> stream,
291     pdfium::span<uint8_t> buffer);
292 
293 void SetPDFSandboxPolicy(FPDF_DWORD policy, FPDF_BOOL enable);
294 FPDF_BOOL IsPDFSandboxPolicyEnabled(FPDF_DWORD policy);
295 
296 void SetPDFUnsupportInfo(UNSUPPORT_INFO* unsp_info);
297 void ReportUnsupportedFeatures(const CPDF_Document* pDoc);
298 void ReportUnsupportedXFA(const CPDF_Document* pDoc);
299 void CheckForUnsupportedAnnot(const CPDF_Annot* pAnnot);
300 void ProcessParseError(CPDF_Parser::Error err);
301 void SetColorFromScheme(const FPDF_COLORSCHEME* pColorScheme,
302                         CPDF_RenderOptions* pRenderOptions);
303 
304 // Returns a vector of page indices given a page range string.
305 std::vector<uint32_t> ParsePageRangeString(const ByteString& bsPageRange,
306                                            uint32_t nCount);
307 
308 #endif  // FPDFSDK_CPDFSDK_HELPERS_H_
309