blob: 59c5be66737d05e5b0edb92d015ac9ff59b2bc2c [file] [log] [blame]
Svetoslav62ce3322014-09-04 21:17:17 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mark Salyzyn121ab702016-10-07 12:13:43 -070016#define LOG_TAG "PdfEditor"
17
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <vector>
22
Mark Salyzyn96bf5982016-09-28 16:15:30 -070023#include <log/log.h>
Mark Salyzyn121ab702016-10-07 12:13:43 -070024#include <utils/Log.h>
Svetoslav62ce3322014-09-04 21:17:17 -070025
26#include "jni.h"
27#include "JNIHelp.h"
Andreas Gampef4e341d2014-11-13 16:07:13 -080028
29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
Svetoslav62ce3322014-09-04 21:17:17 -070031#include "fpdfview.h"
Svet Ganovfbd02882015-06-04 14:49:16 -070032#include "fpdf_edit.h"
33#include "fpdf_save.h"
Svetoslavbec22be2014-09-25 13:03:20 -070034#include "fsdk_rendercontext.h"
35#include "fpdf_transformpage.h"
Andreas Gampef4e341d2014-11-13 16:07:13 -080036#pragma GCC diagnostic pop
37
Svetoslavbec22be2014-09-25 13:03:20 -070038#include "SkMatrix.h"
Svetoslav62ce3322014-09-04 21:17:17 -070039
Andreas Gampeed6b9df2014-11-20 22:02:20 -080040#include <core_jni_helpers.h>
Svetoslav62ce3322014-09-04 21:17:17 -070041
42namespace android {
43
Svetoslavbec22be2014-09-25 13:03:20 -070044enum PageBox {PAGE_BOX_MEDIA, PAGE_BOX_CROP};
45
46static struct {
47 jfieldID x;
48 jfieldID y;
49} gPointClassInfo;
50
51static struct {
52 jfieldID left;
53 jfieldID top;
54 jfieldID right;
55 jfieldID bottom;
56} gRectClassInfo;
57
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070058// Also used in PdfRenderer.cpp
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070059int sUnmatchedPdfiumInitRequestCount = 0;
Svetoslav62ce3322014-09-04 21:17:17 -070060
61static void initializeLibraryIfNeeded() {
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070062 if (sUnmatchedPdfiumInitRequestCount == 0) {
Svet Ganovfbd02882015-06-04 14:49:16 -070063 FPDF_InitLibrary();
Svetoslav62ce3322014-09-04 21:17:17 -070064 }
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070065 sUnmatchedPdfiumInitRequestCount++;
Svetoslav62ce3322014-09-04 21:17:17 -070066}
67
68static void destroyLibraryIfNeeded() {
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070069 sUnmatchedPdfiumInitRequestCount--;
70 if (sUnmatchedPdfiumInitRequestCount == 0) {
Svetoslav62ce3322014-09-04 21:17:17 -070071 FPDF_DestroyLibrary();
72 }
73}
74
75static int getBlock(void* param, unsigned long position, unsigned char* outBuffer,
76 unsigned long size) {
77 const int fd = reinterpret_cast<intptr_t>(param);
78 const int readCount = pread(fd, outBuffer, size, position);
79 if (readCount < 0) {
80 ALOGE("Cannot read from file descriptor. Error:%d", errno);
81 return 0;
82 }
83 return 1;
84}
85
86static jlong nativeOpen(JNIEnv* env, jclass thiz, jint fd, jlong size) {
87 initializeLibraryIfNeeded();
88
89 FPDF_FILEACCESS loader;
90 loader.m_FileLen = size;
91 loader.m_Param = reinterpret_cast<void*>(intptr_t(fd));
92 loader.m_GetBlock = &getBlock;
93
94 FPDF_DOCUMENT document = FPDF_LoadCustomDocument(&loader, NULL);
95
96 if (!document) {
97 const long error = FPDF_GetLastError();
Svet Ganovfce84f02014-10-31 16:56:52 -070098 switch (error) {
99 case FPDF_ERR_PASSWORD:
100 case FPDF_ERR_SECURITY: {
Dan Albert3a091b72014-11-20 15:41:25 -0800101 jniThrowExceptionFmt(env, "java/lang/SecurityException",
102 "cannot create document. Error: %ld", error);
Svet Ganovfce84f02014-10-31 16:56:52 -0700103 } break;
104 default: {
Dan Albert3a091b72014-11-20 15:41:25 -0800105 jniThrowExceptionFmt(env, "java/io/IOException",
106 "cannot create document. Error: %ld", error);
Svet Ganovfce84f02014-10-31 16:56:52 -0700107 } break;
108 }
Svetoslav62ce3322014-09-04 21:17:17 -0700109 destroyLibraryIfNeeded();
110 return -1;
111 }
112
113 return reinterpret_cast<jlong>(document);
114}
115
116static void nativeClose(JNIEnv* env, jclass thiz, jlong documentPtr) {
117 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
118 FPDF_CloseDocument(document);
119 destroyLibraryIfNeeded();
120}
121
122static jint nativeGetPageCount(JNIEnv* env, jclass thiz, jlong documentPtr) {
123 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
124 return FPDF_GetPageCount(document);
125}
126
127static jint nativeRemovePage(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex) {
128 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
129 FPDFPage_Delete(document, pageIndex);
130 return FPDF_GetPageCount(document);
131}
132
133struct PdfToFdWriter : FPDF_FILEWRITE {
134 int dstFd;
135};
136
137static bool writeAllBytes(const int fd, const void* buffer, const size_t byteCount) {
138 char* writeBuffer = static_cast<char*>(const_cast<void*>(buffer));
139 size_t remainingBytes = byteCount;
140 while (remainingBytes > 0) {
141 ssize_t writtenByteCount = write(fd, writeBuffer, remainingBytes);
142 if (writtenByteCount == -1) {
143 if (errno == EINTR) {
144 continue;
145 }
Mark Salyzyn121ab702016-10-07 12:13:43 -0700146 ALOGE("Error writing to buffer: %d", errno);
Svetoslav62ce3322014-09-04 21:17:17 -0700147 return false;
148 }
149 remainingBytes -= writtenByteCount;
150 writeBuffer += writtenByteCount;
151 }
152 return true;
153}
154
155static int writeBlock(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size) {
156 const PdfToFdWriter* writer = reinterpret_cast<PdfToFdWriter*>(owner);
157 const bool success = writeAllBytes(writer->dstFd, buffer, size);
Bernhard Rosenkränzer92ddfcf2014-11-17 22:01:27 +0100158 if (!success) {
Svetoslav62ce3322014-09-04 21:17:17 -0700159 ALOGE("Cannot write to file descriptor. Error:%d", errno);
160 return 0;
161 }
162 return 1;
163}
164
165static void nativeWrite(JNIEnv* env, jclass thiz, jlong documentPtr, jint fd) {
166 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
167 PdfToFdWriter writer;
168 writer.dstFd = fd;
169 writer.WriteBlock = &writeBlock;
170 const bool success = FPDF_SaveAsCopy(document, &writer, FPDF_NO_INCREMENTAL);
171 if (!success) {
Dan Albert46d84442014-11-18 16:07:51 -0800172 jniThrowExceptionFmt(env, "java/io/IOException",
173 "cannot write to fd. Error: %d", errno);
Svetoslav62ce3322014-09-04 21:17:17 -0700174 destroyLibraryIfNeeded();
175 }
176}
177
Svetoslavbec22be2014-09-25 13:03:20 -0700178static void nativeSetTransformAndClip(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
179 jlong transformPtr, jint clipLeft, jint clipTop, jint clipRight, jint clipBottom) {
180 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
181
182 CPDF_Page* page = (CPDF_Page*) FPDF_LoadPage(document, pageIndex);
183 if (!page) {
184 jniThrowException(env, "java/lang/IllegalStateException",
185 "cannot open page");
186 return;
187 }
188
189 double width = 0;
190 double height = 0;
191
192 const int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height);
193 if (!result) {
194 jniThrowException(env, "java/lang/IllegalStateException",
195 "cannot get page size");
196 return;
197 }
198
Philip P. Moltmann79bd8d42016-03-07 09:19:45 -0800199 CFX_Matrix matrix;
Svetoslavbec22be2014-09-25 13:03:20 -0700200
201 SkMatrix* skTransform = reinterpret_cast<SkMatrix*>(transformPtr);
202
203 SkScalar transformValues[6];
Mike Reed71487eb2014-11-19 16:13:20 -0500204 if (!skTransform->asAffine(transformValues)) {
205 jniThrowException(env, "java/lang/IllegalArgumentException",
206 "transform matrix has perspective. Only affine matrices are allowed.");
207 return;
208 }
Svetoslavbec22be2014-09-25 13:03:20 -0700209
210 // PDF's coordinate system origin is left-bottom while in graphics it
211 // is the top-left. So, translate the PDF coordinates to ours.
212 matrix.Set(1, 0, 0, -1, 0, page->GetPageHeight());
213
214 // Apply the transformation what was created in our coordinates.
215 matrix.Concat(transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
216 transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
217 transformValues[SkMatrix::kATransX], transformValues[SkMatrix::kATransY]);
218
219 // Translate the result back to PDF coordinates.
220 matrix.Concat(1, 0, 0, -1, 0, page->GetPageHeight());
221
222 FS_MATRIX transform = {matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f};
223 FS_RECTF clip = {(float) clipLeft, (float) clipTop, (float) clipRight, (float) clipBottom};
224
225 FPDFPage_TransFormWithClip(page, &transform, &clip);
226
227 FPDF_ClosePage(page);
228}
229
230static void nativeGetPageSize(JNIEnv* env, jclass thiz, jlong documentPtr,
231 jint pageIndex, jobject outSize) {
232 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
233
234 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
235 if (!page) {
236 jniThrowException(env, "java/lang/IllegalStateException",
237 "cannot open page");
238 return;
239 }
240
241 double width = 0;
242 double height = 0;
243
244 const int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height);
245 if (!result) {
246 jniThrowException(env, "java/lang/IllegalStateException",
247 "cannot get page size");
248 return;
249 }
250
251 env->SetIntField(outSize, gPointClassInfo.x, width);
252 env->SetIntField(outSize, gPointClassInfo.y, height);
253
254 FPDF_ClosePage(page);
255}
256
257static jboolean nativeScaleForPrinting(JNIEnv* env, jclass thiz, jlong documentPtr) {
258 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
259 FPDF_BOOL success = FPDF_VIEWERREF_GetPrintScaling(document);
260 return success ? JNI_TRUE : JNI_FALSE;
261}
262
263static bool nativeGetPageBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
264 PageBox pageBox, jobject outBox) {
265 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
266
267 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
268 if (!page) {
269 jniThrowException(env, "java/lang/IllegalStateException",
270 "cannot open page");
271 return false;
272 }
273
274 float left;
275 float top;
276 float right;
277 float bottom;
278
279 const FPDF_BOOL success = (pageBox == PAGE_BOX_MEDIA)
280 ? FPDFPage_GetMediaBox(page, &left, &top, &right, &bottom)
281 : FPDFPage_GetCropBox(page, &left, &top, &right, &bottom);
282
283 FPDF_ClosePage(page);
284
285 if (!success) {
286 return false;
287 }
288
289 env->SetIntField(outBox, gRectClassInfo.left, (int) left);
290 env->SetIntField(outBox, gRectClassInfo.top, (int) top);
291 env->SetIntField(outBox, gRectClassInfo.right, (int) right);
292 env->SetIntField(outBox, gRectClassInfo.bottom, (int) bottom);
293
294 return true;
295}
296
297static jboolean nativeGetPageMediaBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
298 jobject outMediaBox) {
299 const bool success = nativeGetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_MEDIA,
300 outMediaBox);
301 return success ? JNI_TRUE : JNI_FALSE;
302}
303
304static jboolean nativeGetPageCropBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
305 jobject outMediaBox) {
306 const bool success = nativeGetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_CROP,
307 outMediaBox);
308 return success ? JNI_TRUE : JNI_FALSE;
309}
310
311static void nativeSetPageBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
312 PageBox pageBox, jobject box) {
313 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
314
315 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
316 if (!page) {
317 jniThrowException(env, "java/lang/IllegalStateException",
318 "cannot open page");
319 return;
320 }
321
322 const int left = env->GetIntField(box, gRectClassInfo.left);
323 const int top = env->GetIntField(box, gRectClassInfo.top);
324 const int right = env->GetIntField(box, gRectClassInfo.right);
325 const int bottom = env->GetIntField(box, gRectClassInfo.bottom);
326
327 if (pageBox == PAGE_BOX_MEDIA) {
328 FPDFPage_SetMediaBox(page, left, top, right, bottom);
329 } else {
330 FPDFPage_SetCropBox(page, left, top, right, bottom);
331 }
332
333 FPDF_ClosePage(page);
334}
335
336static void nativeSetPageMediaBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
337 jobject mediaBox) {
338 nativeSetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_MEDIA, mediaBox);
339}
340
341static void nativeSetPageCropBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
342 jobject mediaBox) {
343 nativeSetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_CROP, mediaBox);
344}
345
Daniel Micay76f6a862015-09-19 17:31:01 -0400346static const JNINativeMethod gPdfEditor_Methods[] = {
Svetoslav62ce3322014-09-04 21:17:17 -0700347 {"nativeOpen", "(IJ)J", (void*) nativeOpen},
348 {"nativeClose", "(J)V", (void*) nativeClose},
349 {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount},
350 {"nativeRemovePage", "(JI)I", (void*) nativeRemovePage},
Svetoslavbec22be2014-09-25 13:03:20 -0700351 {"nativeWrite", "(JI)V", (void*) nativeWrite},
352 {"nativeSetTransformAndClip", "(JIJIIII)V", (void*) nativeSetTransformAndClip},
353 {"nativeGetPageSize", "(JILandroid/graphics/Point;)V", (void*) nativeGetPageSize},
354 {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting},
355 {"nativeGetPageMediaBox", "(JILandroid/graphics/Rect;)Z", (void*) nativeGetPageMediaBox},
356 {"nativeSetPageMediaBox", "(JILandroid/graphics/Rect;)V", (void*) nativeSetPageMediaBox},
357 {"nativeGetPageCropBox", "(JILandroid/graphics/Rect;)Z", (void*) nativeGetPageCropBox},
358 {"nativeSetPageCropBox", "(JILandroid/graphics/Rect;)V", (void*) nativeSetPageCropBox}
Svetoslav62ce3322014-09-04 21:17:17 -0700359};
360
361int register_android_graphics_pdf_PdfEditor(JNIEnv* env) {
Andreas Gampe7d13d9d2014-11-21 10:36:14 -0800362 const int result = RegisterMethodsOrDie(
Svetoslav62ce3322014-09-04 21:17:17 -0700363 env, "android/graphics/pdf/PdfEditor", gPdfEditor_Methods,
364 NELEM(gPdfEditor_Methods));
Svetoslavbec22be2014-09-25 13:03:20 -0700365
Andreas Gampe7d13d9d2014-11-21 10:36:14 -0800366 jclass pointClass = FindClassOrDie(env, "android/graphics/Point");
367 gPointClassInfo.x = GetFieldIDOrDie(env, pointClass, "x", "I");
368 gPointClassInfo.y = GetFieldIDOrDie(env, pointClass, "y", "I");
Svetoslavbec22be2014-09-25 13:03:20 -0700369
Andreas Gampe7d13d9d2014-11-21 10:36:14 -0800370 jclass rectClass = FindClassOrDie(env, "android/graphics/Rect");
371 gRectClassInfo.left = GetFieldIDOrDie(env, rectClass, "left", "I");
372 gRectClassInfo.top = GetFieldIDOrDie(env, rectClass, "top", "I");
373 gRectClassInfo.right = GetFieldIDOrDie(env, rectClass, "right", "I");
374 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClass, "bottom", "I");
Svetoslavbec22be2014-09-25 13:03:20 -0700375
376 return result;
Svetoslav62ce3322014-09-04 21:17:17 -0700377};
378
379};