blob: 5d9522a12cd69df9d26a523b5803d70168a62d27 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclair00d47a62018-03-28 18:39:04 +00007#ifndef FPDFSDK_CPDFSDK_HELPERS_H_
8#define FPDFSDK_CPDFSDK_HELPERS_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Lei Zhangbc106482019-05-30 23:55:19 +000010#include "build/build_config.h"
Tom Sepez101535f2018-06-12 13:36:05 +000011#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070012#include "core/fpdfapi/parser/cpdf_parser.h"
Nicolas Pena37cc5fb2017-04-04 12:12:49 -040013#include "core/fxge/dib/cfx_dibitmap.h"
Lei Zhangaaed6982018-03-22 18:39:05 +000014#include "public/fpdf_doc.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080015#include "public/fpdfview.h"
16
Tom Sepez51da0932015-11-25 16:05:49 -080017#ifdef PDF_ENABLE_XFA
Tom Sepeza1d34422018-04-24 20:54:41 +000018#include "core/fxcrt/fx_stream.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080019#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Lei Zhangbc106482019-05-30 23:55:19 +000021#if defined(OS_WIN)
Tom Sepez75f43c42015-07-15 16:35:53 -070022#include <math.h>
Dan Sinclair61046b92016-02-18 14:48:48 -050023#include <tchar.h>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024#endif
25
dsinclaircac704d2016-07-28 12:59:09 -070026class CPDF_Annot;
Tom Sepez525147a2018-05-03 17:19:53 +000027class CPDF_AnnotContext;
28class CPDF_ClipPath;
29class CPDF_ContentMarkItem;
30class CPDF_Object;
31class CPDF_Font;
32class CPDF_LinkExtract;
Jane Liubaa7ff42017-06-29 19:18:23 -040033class CPDF_PageObject;
weili9f515bc2016-07-24 08:08:24 -070034class CPDF_PageRenderContext;
Jane Liu548334e2017-08-03 16:33:40 -040035class CPDF_Stream;
Tom Sepez525147a2018-05-03 17:19:53 +000036class CPDF_StructElement;
37class CPDF_StructTree;
38class CPDF_TextPage;
39class CPDF_TextPageFind;
Tom Sepez2563fc32018-08-01 19:13:06 +000040class CPDFSDK_FormFillEnvironment;
Lei Zhange6fcdfa2019-02-14 04:07:09 +000041class CPDFSDK_InteractiveForm;
Dan Sinclair7d125322018-03-28 18:49:34 +000042class IPDFSDK_PauseAdapter;
Miklos Vajna36eed872017-09-20 22:52:43 +020043class FX_PATHPOINT;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Tom Sepeza1d34422018-04-24 20:54:41 +000045#ifdef PDF_ENABLE_XFA
46class CPDFXFA_Context;
47class CPDFXFA_Page;
Tom Sepez525147a2018-05-03 17:19:53 +000048class CXFA_FFWidget;
Tom Sepeza1d34422018-04-24 20:54:41 +000049#endif // PDF_ENABLE_XFA
50
Tom Sepez50d12ad2015-11-24 09:50:51 -080051// Conversions to/from underlying types.
Tom Sepez101535f2018-06-12 13:36:05 +000052IPDF_Page* IPDFPageFromFPDFPage(FPDF_PAGE page);
53FPDF_PAGE FPDFPageFromIPDFPage(IPDF_Page* page);
Tom Sepezdb0be962015-10-16 14:00:21 -070054CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page);
Tom Sepez525147a2018-05-03 17:19:53 +000055FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc);
56CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc);
57
58// Conversions to/from incomplete FPDF_ API types.
Tom Sepez22923602019-07-15 21:33:35 +000059inline FPDF_ACTION FPDFActionFromCPDFDictionary(CPDF_Dictionary* action) {
Tom Sepez525147a2018-05-03 17:19:53 +000060 return reinterpret_cast<FPDF_ACTION>(action);
61}
Tom Sepez22923602019-07-15 21:33:35 +000062inline CPDF_Dictionary* CPDFDictionaryFromFPDFAction(FPDF_ACTION action) {
63 return reinterpret_cast<CPDF_Dictionary*>(action);
Tom Sepez525147a2018-05-03 17:19:53 +000064}
65
66inline FPDF_ANNOTATION FPDFAnnotationFromCPDFAnnotContext(
67 CPDF_AnnotContext* annot) {
68 return reinterpret_cast<FPDF_ANNOTATION>(annot);
69}
70inline CPDF_AnnotContext* CPDFAnnotContextFromFPDFAnnotation(
71 FPDF_ANNOTATION annot) {
72 return reinterpret_cast<CPDF_AnnotContext*>(annot);
73}
74
75inline FPDF_ATTACHMENT FPDFAttachmentFromCPDFObject(CPDF_Object* attachment) {
76 return reinterpret_cast<FPDF_ATTACHMENT>(attachment);
77}
78inline CPDF_Object* CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment) {
79 return reinterpret_cast<CPDF_Object*>(attachment);
80}
81
82inline FPDF_BITMAP FPDFBitmapFromCFXDIBitmap(CFX_DIBitmap* bitmap) {
83 return reinterpret_cast<FPDF_BITMAP>(bitmap);
84}
85inline CFX_DIBitmap* CFXDIBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) {
86 return reinterpret_cast<CFX_DIBitmap*>(bitmap);
87}
88
Tom Sepez22923602019-07-15 21:33:35 +000089inline FPDF_BOOKMARK FPDFBookmarkFromCPDFDictionary(CPDF_Dictionary* bookmark) {
Tom Sepez525147a2018-05-03 17:19:53 +000090 return reinterpret_cast<FPDF_BOOKMARK>(bookmark);
91}
Tom Sepez22923602019-07-15 21:33:35 +000092inline CPDF_Dictionary* CPDFDictionaryFromFPDFBookmark(FPDF_BOOKMARK bookmark) {
93 return reinterpret_cast<CPDF_Dictionary*>(bookmark);
Tom Sepez525147a2018-05-03 17:19:53 +000094}
95
96inline FPDF_CLIPPATH FPDFClipPathFromCPDFClipPath(CPDF_ClipPath* path) {
97 return reinterpret_cast<FPDF_CLIPPATH>(path);
98}
99inline CPDF_ClipPath* CPDFClipPathFromFPDFClipPath(FPDF_CLIPPATH path) {
100 return reinterpret_cast<CPDF_ClipPath*>(path);
101}
102
Tom Sepez22923602019-07-15 21:33:35 +0000103inline FPDF_DEST FPDFDestFromCPDFArray(CPDF_Array* dest) {
Tom Sepez525147a2018-05-03 17:19:53 +0000104 return reinterpret_cast<FPDF_DEST>(dest);
105}
Tom Sepez22923602019-07-15 21:33:35 +0000106inline CPDF_Array* CPDFArrayFromFPDFDest(FPDF_DEST dest) {
107 return reinterpret_cast<CPDF_Array*>(dest);
Tom Sepez525147a2018-05-03 17:19:53 +0000108}
109
110inline FPDF_FONT FPDFFontFromCPDFFont(CPDF_Font* font) {
111 return reinterpret_cast<FPDF_FONT>(font);
112}
113inline CPDF_Font* CPDFFontFromFPDFFont(FPDF_FONT font) {
114 return reinterpret_cast<CPDF_Font*>(font);
115}
116
117inline FPDF_LINK FPDFLinkFromCPDFDictionary(CPDF_Dictionary* link) {
118 return reinterpret_cast<FPDF_LINK>(link);
119}
120inline CPDF_Dictionary* CPDFDictionaryFromFPDFLink(FPDF_LINK link) {
121 return reinterpret_cast<CPDF_Dictionary*>(link);
122}
123
124inline FPDF_PAGELINK FPDFPageLinkFromCPDFLinkExtract(CPDF_LinkExtract* link) {
125 return reinterpret_cast<FPDF_PAGELINK>(link);
126}
127inline CPDF_LinkExtract* CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link) {
128 return reinterpret_cast<CPDF_LinkExtract*>(link);
129}
130
131inline FPDF_PAGEOBJECT FPDFPageObjectFromCPDFPageObject(
132 CPDF_PageObject* page_object) {
133 return reinterpret_cast<FPDF_PAGEOBJECT>(page_object);
134}
135inline CPDF_PageObject* CPDFPageObjectFromFPDFPageObject(
136 FPDF_PAGEOBJECT page_object) {
137 return reinterpret_cast<CPDF_PageObject*>(page_object);
138}
139
140inline FPDF_PAGEOBJECTMARK FPDFPageObjectMarkFromCPDFContentMarkItem(
Henrique Nakashima144107d2018-07-10 21:04:05 +0000141 CPDF_ContentMarkItem* mark) {
Tom Sepez525147a2018-05-03 17:19:53 +0000142 return reinterpret_cast<FPDF_PAGEOBJECTMARK>(mark);
143}
Henrique Nakashima144107d2018-07-10 21:04:05 +0000144inline CPDF_ContentMarkItem* CPDFContentMarkItemFromFPDFPageObjectMark(
Tom Sepez525147a2018-05-03 17:19:53 +0000145 FPDF_PAGEOBJECTMARK mark) {
Henrique Nakashima144107d2018-07-10 21:04:05 +0000146 return reinterpret_cast<CPDF_ContentMarkItem*>(mark);
Tom Sepez525147a2018-05-03 17:19:53 +0000147}
148
Tom Sepez22923602019-07-15 21:33:35 +0000149inline FPDF_PAGERANGE FPDFPageRangeFromCPDFArray(CPDF_Array* range) {
Tom Sepez525147a2018-05-03 17:19:53 +0000150 return reinterpret_cast<FPDF_PAGERANGE>(range);
151}
Tom Sepez22923602019-07-15 21:33:35 +0000152inline CPDF_Array* CPDFArrayFromFPDFPageRange(FPDF_PAGERANGE range) {
153 return reinterpret_cast<CPDF_Array*>(range);
Tom Sepez525147a2018-05-03 17:19:53 +0000154}
155
156inline FPDF_PATHSEGMENT FPDFPathSegmentFromFXPathPoint(
157 const FX_PATHPOINT* segment) {
158 return reinterpret_cast<FPDF_PATHSEGMENT>(segment);
159}
160inline const FX_PATHPOINT* FXPathPointFromFPDFPathSegment(
161 FPDF_PATHSEGMENT segment) {
162 return reinterpret_cast<const FX_PATHPOINT*>(segment);
163}
164
165inline FPDF_STRUCTTREE FPDFStructTreeFromCPDFStructTree(
166 CPDF_StructTree* struct_tree) {
167 return reinterpret_cast<FPDF_STRUCTTREE>(struct_tree);
168}
169inline CPDF_StructTree* CPDFStructTreeFromFPDFStructTree(
170 FPDF_STRUCTTREE struct_tree) {
171 return reinterpret_cast<CPDF_StructTree*>(struct_tree);
172}
173
174inline FPDF_STRUCTELEMENT FPDFStructElementFromCPDFStructElement(
175 CPDF_StructElement* struct_element) {
176 return reinterpret_cast<FPDF_STRUCTELEMENT>(struct_element);
177}
178inline CPDF_StructElement* CPDFStructElementFromFPDFStructElement(
179 FPDF_STRUCTELEMENT struct_element) {
180 return reinterpret_cast<CPDF_StructElement*>(struct_element);
181}
182
183inline FPDF_TEXTPAGE FPDFTextPageFromCPDFTextPage(CPDF_TextPage* page) {
184 return reinterpret_cast<FPDF_TEXTPAGE>(page);
185}
186inline CPDF_TextPage* CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE page) {
187 return reinterpret_cast<CPDF_TextPage*>(page);
188}
189
190inline FPDF_SCHHANDLE FPDFSchHandleFromCPDFTextPageFind(
191 CPDF_TextPageFind* handle) {
192 return reinterpret_cast<FPDF_SCHHANDLE>(handle);
193}
194inline CPDF_TextPageFind* CPDFTextPageFindFromFPDFSchHandle(
195 FPDF_SCHHANDLE handle) {
196 return reinterpret_cast<CPDF_TextPageFind*>(handle);
197}
198
Tom Sepez2563fc32018-08-01 19:13:06 +0000199inline FPDF_FORMHANDLE FPDFFormHandleFromCPDFSDKFormFillEnvironment(
200 CPDFSDK_FormFillEnvironment* handle) {
201 return reinterpret_cast<FPDF_FORMHANDLE>(handle);
202}
203inline CPDFSDK_FormFillEnvironment*
204CPDFSDKFormFillEnvironmentFromFPDFFormHandle(FPDF_FORMHANDLE handle) {
205 return reinterpret_cast<CPDFSDK_FormFillEnvironment*>(handle);
206}
207
Lei Zhange6fcdfa2019-02-14 04:07:09 +0000208CPDFSDK_InteractiveForm* FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle);
209
Lei Zhangb46a7632019-01-09 02:56:16 +0000210ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
thestigbefa4502016-05-26 20:15:19 -0700211
Lei Zhangf5fcd9e2018-12-23 03:11:50 +0000212WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
213
Dan Sinclair7aba4722018-03-28 17:04:16 +0000214#ifdef PDF_ENABLE_XFA
Tom Sepez525147a2018-05-03 17:19:53 +0000215inline FPDF_WIDGET FPDFWidgetFromCXFAFFWidget(CXFA_FFWidget* widget) {
216 return reinterpret_cast<FPDF_WIDGET>(widget);
217}
218inline CXFA_FFWidget* CXFAFFWidgetFromFPDFWidget(FPDF_WIDGET widget) {
219 return reinterpret_cast<CXFA_FFWidget*>(widget);
220}
221
Dan Sinclair7aba4722018-03-28 17:04:16 +0000222// Layering prevents fxcrt from knowing about FPDF_FILEHANDLER, so this can't
223// be a static method of IFX_SeekableStream.
224RetainPtr<IFX_SeekableStream> MakeSeekableStream(
225 FPDF_FILEHANDLER* pFileHandler);
226#endif // PDF_ENABLE_XFA
Lei Zhang11767d32018-03-21 18:59:44 +0000227
Lei Zhang5cee3f22018-05-25 21:48:49 +0000228const CPDF_Array* GetQuadPointsArrayFromDictionary(const CPDF_Dictionary* dict);
229CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict);
Ralf Sippl16381792018-04-12 21:20:26 +0000230CPDF_Array* AddQuadPointsArrayToDictionary(CPDF_Dictionary* dict);
231bool IsValidQuadPointsIndex(const CPDF_Array* array, size_t index);
232bool GetQuadPointsAtIndex(const CPDF_Array* array,
233 size_t quad_index,
234 FS_QUADPOINTSF* quad_points);
Lei Zhangaaed6982018-03-22 18:39:05 +0000235
Lei Zhang367e7de2017-10-31 13:32:17 +0000236CFX_FloatRect CFXFloatRectFromFSRECTF(const FS_RECTF& rect);
237void FSRECTFFromCFXFloatRect(const CFX_FloatRect& rect, FS_RECTF* out_rect);
238
Lei Zhang6fef1e42018-12-20 19:14:02 +0000239CFX_Matrix CFXMatrixFromFSMatrix(const FS_MATRIX& matrix);
240
Ryan Harrison275e2602017-09-18 14:23:18 -0400241unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString& text,
Jane Liu18ae06d2017-07-18 10:15:16 -0400242 void* buffer,
243 unsigned long buflen);
Jeremy Chinsen617a2e82019-06-20 00:11:12 +0000244
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|.
249unsigned 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|.
Jane Liu548334e2017-08-03 16:33:40 -0400257unsigned long DecodeStreamMaybeCopyAndReturnLength(const CPDF_Stream* stream,
258 void* buffer,
259 unsigned long buflen);
260
Tom Sepez75f43c42015-07-15 16:35:53 -0700261void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable);
262FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy);
Dan Sinclair7aba4722018-03-28 17:04:16 +0000263
264// TODO(dsinclair): Where should this live?
Tom Sepez59d2a592018-09-14 19:51:10 +0000265void RenderPageWithContext(CPDF_PageRenderContext* pContext,
266 FPDF_PAGE page,
267 int start_x,
268 int start_y,
269 int size_x,
270 int size_y,
271 int rotate,
272 int flags,
273 bool bNeedToRestore,
274 IPDFSDK_PauseAdapter* pause);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Tom Sepez81284852018-06-12 19:26:15 +0000276void ReportUnsupportedFeatures(CPDF_Document* pDoc);
Ryan Smith69fce9b2019-03-11 21:53:42 +0000277void CheckForUnsupportedAnnot(const CPDF_Annot* pAnnot);
Dan Sinclair7aba4722018-03-28 17:04:16 +0000278
Lei Zhangbc106482019-05-30 23:55:19 +0000279#if !defined(OS_WIN)
Dan Sinclair7aba4722018-03-28 17:04:16 +0000280void SetLastError(int err);
281int GetLastError();
Lei Zhangbc106482019-05-30 23:55:19 +0000282#endif
Dan Sinclair7aba4722018-03-28 17:04:16 +0000283
Tom Sepezf10ae632016-01-26 14:19:52 -0800284void ProcessParseError(CPDF_Parser::Error err);
Dan Sinclair7aba4722018-03-28 17:04:16 +0000285
Dan Sinclair00d47a62018-03-28 18:39:04 +0000286#endif // FPDFSDK_CPDFSDK_HELPERS_H_