blob: 0468b644862dbecc13c659057e1795e1f1aaafb9 [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 */
16
17#include "jni.h"
18#include "JNIHelp.h"
Andreas Gampef4e341d2014-11-13 16:07:13 -080019
20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
Svetoslav62ce3322014-09-04 21:17:17 -070022#include "fpdfview.h"
Svet Ganovfbd02882015-06-04 14:49:16 -070023#include "fpdf_edit.h"
24#include "fpdf_save.h"
Svetoslavbec22be2014-09-25 13:03:20 -070025#include "fsdk_rendercontext.h"
26#include "fpdf_transformpage.h"
Andreas Gampef4e341d2014-11-13 16:07:13 -080027#pragma GCC diagnostic pop
28
Svetoslavbec22be2014-09-25 13:03:20 -070029#include "SkMatrix.h"
Svetoslav62ce3322014-09-04 21:17:17 -070030
Andreas Gampeed6b9df2014-11-20 22:02:20 -080031#include <core_jni_helpers.h>
Svetoslav62ce3322014-09-04 21:17:17 -070032#include <vector>
33#include <utils/Log.h>
34#include <unistd.h>
35#include <sys/types.h>
36#include <unistd.h>
37
38namespace android {
39
Svetoslavbec22be2014-09-25 13:03:20 -070040enum PageBox {PAGE_BOX_MEDIA, PAGE_BOX_CROP};
41
42static struct {
43 jfieldID x;
44 jfieldID y;
45} gPointClassInfo;
46
47static struct {
48 jfieldID left;
49 jfieldID top;
50 jfieldID right;
51 jfieldID bottom;
52} gRectClassInfo;
53
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070054// Also used in PdfRenderer.cpp
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070055int sUnmatchedPdfiumInitRequestCount = 0;
Svetoslav62ce3322014-09-04 21:17:17 -070056
57static void initializeLibraryIfNeeded() {
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070058 if (sUnmatchedPdfiumInitRequestCount == 0) {
Svet Ganovfbd02882015-06-04 14:49:16 -070059 FPDF_InitLibrary();
Svetoslav62ce3322014-09-04 21:17:17 -070060 }
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070061 sUnmatchedPdfiumInitRequestCount++;
Svetoslav62ce3322014-09-04 21:17:17 -070062}
63
64static void destroyLibraryIfNeeded() {
Philip P. Moltmannbe2e7122016-04-27 09:46:48 -070065 sUnmatchedPdfiumInitRequestCount--;
66 if (sUnmatchedPdfiumInitRequestCount == 0) {
Svetoslav62ce3322014-09-04 21:17:17 -070067 FPDF_DestroyLibrary();
68 }
69}
70
71static int getBlock(void* param, unsigned long position, unsigned char* outBuffer,
72 unsigned long size) {
73 const int fd = reinterpret_cast<intptr_t>(param);
74 const int readCount = pread(fd, outBuffer, size, position);
75 if (readCount < 0) {
76 ALOGE("Cannot read from file descriptor. Error:%d", errno);
77 return 0;
78 }
79 return 1;
80}
81
82static jlong nativeOpen(JNIEnv* env, jclass thiz, jint fd, jlong size) {
83 initializeLibraryIfNeeded();
84
85 FPDF_FILEACCESS loader;
86 loader.m_FileLen = size;
87 loader.m_Param = reinterpret_cast<void*>(intptr_t(fd));
88 loader.m_GetBlock = &getBlock;
89
90 FPDF_DOCUMENT document = FPDF_LoadCustomDocument(&loader, NULL);
91
92 if (!document) {
93 const long error = FPDF_GetLastError();
Svet Ganovfce84f02014-10-31 16:56:52 -070094 switch (error) {
95 case FPDF_ERR_PASSWORD:
96 case FPDF_ERR_SECURITY: {
Dan Albert3a091b72014-11-20 15:41:25 -080097 jniThrowExceptionFmt(env, "java/lang/SecurityException",
98 "cannot create document. Error: %ld", error);
Svet Ganovfce84f02014-10-31 16:56:52 -070099 } break;
100 default: {
Dan Albert3a091b72014-11-20 15:41:25 -0800101 jniThrowExceptionFmt(env, "java/io/IOException",
102 "cannot create document. Error: %ld", error);
Svet Ganovfce84f02014-10-31 16:56:52 -0700103 } break;
104 }
Svetoslav62ce3322014-09-04 21:17:17 -0700105 destroyLibraryIfNeeded();
106 return -1;
107 }
108
109 return reinterpret_cast<jlong>(document);
110}
111
112static void nativeClose(JNIEnv* env, jclass thiz, jlong documentPtr) {
113 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
114 FPDF_CloseDocument(document);
115 destroyLibraryIfNeeded();
116}
117
118static jint nativeGetPageCount(JNIEnv* env, jclass thiz, jlong documentPtr) {
119 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
120 return FPDF_GetPageCount(document);
121}
122
123static jint nativeRemovePage(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex) {
124 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
125 FPDFPage_Delete(document, pageIndex);
126 return FPDF_GetPageCount(document);
127}
128
129struct PdfToFdWriter : FPDF_FILEWRITE {
130 int dstFd;
131};
132
133static bool writeAllBytes(const int fd, const void* buffer, const size_t byteCount) {
134 char* writeBuffer = static_cast<char*>(const_cast<void*>(buffer));
135 size_t remainingBytes = byteCount;
136 while (remainingBytes > 0) {
137 ssize_t writtenByteCount = write(fd, writeBuffer, remainingBytes);
138 if (writtenByteCount == -1) {
139 if (errno == EINTR) {
140 continue;
141 }
142 __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
143 "Error writing to buffer: %d", errno);
144 return false;
145 }
146 remainingBytes -= writtenByteCount;
147 writeBuffer += writtenByteCount;
148 }
149 return true;
150}
151
152static int writeBlock(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size) {
153 const PdfToFdWriter* writer = reinterpret_cast<PdfToFdWriter*>(owner);
154 const bool success = writeAllBytes(writer->dstFd, buffer, size);
Bernhard Rosenkränzer92ddfcf2014-11-17 22:01:27 +0100155 if (!success) {
Svetoslav62ce3322014-09-04 21:17:17 -0700156 ALOGE("Cannot write to file descriptor. Error:%d", errno);
157 return 0;
158 }
159 return 1;
160}
161
162static void nativeWrite(JNIEnv* env, jclass thiz, jlong documentPtr, jint fd) {
163 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
164 PdfToFdWriter writer;
165 writer.dstFd = fd;
166 writer.WriteBlock = &writeBlock;
167 const bool success = FPDF_SaveAsCopy(document, &writer, FPDF_NO_INCREMENTAL);
168 if (!success) {
Dan Albert46d84442014-11-18 16:07:51 -0800169 jniThrowExceptionFmt(env, "java/io/IOException",
170 "cannot write to fd. Error: %d", errno);
Svetoslav62ce3322014-09-04 21:17:17 -0700171 destroyLibraryIfNeeded();
172 }
173}
174
Svetoslavbec22be2014-09-25 13:03:20 -0700175static void nativeSetTransformAndClip(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
176 jlong transformPtr, jint clipLeft, jint clipTop, jint clipRight, jint clipBottom) {
177 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
178
179 CPDF_Page* page = (CPDF_Page*) FPDF_LoadPage(document, pageIndex);
180 if (!page) {
181 jniThrowException(env, "java/lang/IllegalStateException",
182 "cannot open page");
183 return;
184 }
185
186 double width = 0;
187 double height = 0;
188
189 const int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height);
190 if (!result) {
191 jniThrowException(env, "java/lang/IllegalStateException",
192 "cannot get page size");
193 return;
194 }
195
Philip P. Moltmann79bd8d42016-03-07 09:19:45 -0800196 CFX_Matrix matrix;
Svetoslavbec22be2014-09-25 13:03:20 -0700197
198 SkMatrix* skTransform = reinterpret_cast<SkMatrix*>(transformPtr);
199
200 SkScalar transformValues[6];
Mike Reed71487eb2014-11-19 16:13:20 -0500201 if (!skTransform->asAffine(transformValues)) {
202 jniThrowException(env, "java/lang/IllegalArgumentException",
203 "transform matrix has perspective. Only affine matrices are allowed.");
204 return;
205 }
Svetoslavbec22be2014-09-25 13:03:20 -0700206
207 // PDF's coordinate system origin is left-bottom while in graphics it
208 // is the top-left. So, translate the PDF coordinates to ours.
209 matrix.Set(1, 0, 0, -1, 0, page->GetPageHeight());
210
211 // Apply the transformation what was created in our coordinates.
212 matrix.Concat(transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
213 transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
214 transformValues[SkMatrix::kATransX], transformValues[SkMatrix::kATransY]);
215
216 // Translate the result back to PDF coordinates.
217 matrix.Concat(1, 0, 0, -1, 0, page->GetPageHeight());
218
219 FS_MATRIX transform = {matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f};
220 FS_RECTF clip = {(float) clipLeft, (float) clipTop, (float) clipRight, (float) clipBottom};
221
222 FPDFPage_TransFormWithClip(page, &transform, &clip);
223
224 FPDF_ClosePage(page);
225}
226
227static void nativeGetPageSize(JNIEnv* env, jclass thiz, jlong documentPtr,
228 jint pageIndex, jobject outSize) {
229 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
230
231 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
232 if (!page) {
233 jniThrowException(env, "java/lang/IllegalStateException",
234 "cannot open page");
235 return;
236 }
237
238 double width = 0;
239 double height = 0;
240
241 const int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height);
242 if (!result) {
243 jniThrowException(env, "java/lang/IllegalStateException",
244 "cannot get page size");
245 return;
246 }
247
248 env->SetIntField(outSize, gPointClassInfo.x, width);
249 env->SetIntField(outSize, gPointClassInfo.y, height);
250
251 FPDF_ClosePage(page);
252}
253
254static jboolean nativeScaleForPrinting(JNIEnv* env, jclass thiz, jlong documentPtr) {
255 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
256 FPDF_BOOL success = FPDF_VIEWERREF_GetPrintScaling(document);
257 return success ? JNI_TRUE : JNI_FALSE;
258}
259
260static bool nativeGetPageBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
261 PageBox pageBox, jobject outBox) {
262 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
263
264 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
265 if (!page) {
266 jniThrowException(env, "java/lang/IllegalStateException",
267 "cannot open page");
268 return false;
269 }
270
271 float left;
272 float top;
273 float right;
274 float bottom;
275
276 const FPDF_BOOL success = (pageBox == PAGE_BOX_MEDIA)
277 ? FPDFPage_GetMediaBox(page, &left, &top, &right, &bottom)
278 : FPDFPage_GetCropBox(page, &left, &top, &right, &bottom);
279
280 FPDF_ClosePage(page);
281
282 if (!success) {
283 return false;
284 }
285
286 env->SetIntField(outBox, gRectClassInfo.left, (int) left);
287 env->SetIntField(outBox, gRectClassInfo.top, (int) top);
288 env->SetIntField(outBox, gRectClassInfo.right, (int) right);
289 env->SetIntField(outBox, gRectClassInfo.bottom, (int) bottom);
290
291 return true;
292}
293
294static jboolean nativeGetPageMediaBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
295 jobject outMediaBox) {
296 const bool success = nativeGetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_MEDIA,
297 outMediaBox);
298 return success ? JNI_TRUE : JNI_FALSE;
299}
300
301static jboolean nativeGetPageCropBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
302 jobject outMediaBox) {
303 const bool success = nativeGetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_CROP,
304 outMediaBox);
305 return success ? JNI_TRUE : JNI_FALSE;
306}
307
308static void nativeSetPageBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
309 PageBox pageBox, jobject box) {
310 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
311
312 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
313 if (!page) {
314 jniThrowException(env, "java/lang/IllegalStateException",
315 "cannot open page");
316 return;
317 }
318
319 const int left = env->GetIntField(box, gRectClassInfo.left);
320 const int top = env->GetIntField(box, gRectClassInfo.top);
321 const int right = env->GetIntField(box, gRectClassInfo.right);
322 const int bottom = env->GetIntField(box, gRectClassInfo.bottom);
323
324 if (pageBox == PAGE_BOX_MEDIA) {
325 FPDFPage_SetMediaBox(page, left, top, right, bottom);
326 } else {
327 FPDFPage_SetCropBox(page, left, top, right, bottom);
328 }
329
330 FPDF_ClosePage(page);
331}
332
333static void nativeSetPageMediaBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
334 jobject mediaBox) {
335 nativeSetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_MEDIA, mediaBox);
336}
337
338static void nativeSetPageCropBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
339 jobject mediaBox) {
340 nativeSetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_CROP, mediaBox);
341}
342
Daniel Micay76f6a862015-09-19 17:31:01 -0400343static const JNINativeMethod gPdfEditor_Methods[] = {
Svetoslav62ce3322014-09-04 21:17:17 -0700344 {"nativeOpen", "(IJ)J", (void*) nativeOpen},
345 {"nativeClose", "(J)V", (void*) nativeClose},
346 {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount},
347 {"nativeRemovePage", "(JI)I", (void*) nativeRemovePage},
Svetoslavbec22be2014-09-25 13:03:20 -0700348 {"nativeWrite", "(JI)V", (void*) nativeWrite},
349 {"nativeSetTransformAndClip", "(JIJIIII)V", (void*) nativeSetTransformAndClip},
350 {"nativeGetPageSize", "(JILandroid/graphics/Point;)V", (void*) nativeGetPageSize},
351 {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting},
352 {"nativeGetPageMediaBox", "(JILandroid/graphics/Rect;)Z", (void*) nativeGetPageMediaBox},
353 {"nativeSetPageMediaBox", "(JILandroid/graphics/Rect;)V", (void*) nativeSetPageMediaBox},
354 {"nativeGetPageCropBox", "(JILandroid/graphics/Rect;)Z", (void*) nativeGetPageCropBox},
355 {"nativeSetPageCropBox", "(JILandroid/graphics/Rect;)V", (void*) nativeSetPageCropBox}
Svetoslav62ce3322014-09-04 21:17:17 -0700356};
357
358int register_android_graphics_pdf_PdfEditor(JNIEnv* env) {
Andreas Gampe7d13d9d2014-11-21 10:36:14 -0800359 const int result = RegisterMethodsOrDie(
Svetoslav62ce3322014-09-04 21:17:17 -0700360 env, "android/graphics/pdf/PdfEditor", gPdfEditor_Methods,
361 NELEM(gPdfEditor_Methods));
Svetoslavbec22be2014-09-25 13:03:20 -0700362
Andreas Gampe7d13d9d2014-11-21 10:36:14 -0800363 jclass pointClass = FindClassOrDie(env, "android/graphics/Point");
364 gPointClassInfo.x = GetFieldIDOrDie(env, pointClass, "x", "I");
365 gPointClassInfo.y = GetFieldIDOrDie(env, pointClass, "y", "I");
Svetoslavbec22be2014-09-25 13:03:20 -0700366
Andreas Gampe7d13d9d2014-11-21 10:36:14 -0800367 jclass rectClass = FindClassOrDie(env, "android/graphics/Rect");
368 gRectClassInfo.left = GetFieldIDOrDie(env, rectClass, "left", "I");
369 gRectClassInfo.top = GetFieldIDOrDie(env, rectClass, "top", "I");
370 gRectClassInfo.right = GetFieldIDOrDie(env, rectClass, "right", "I");
371 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClass, "bottom", "I");
Svetoslavbec22be2014-09-25 13:03:20 -0700372
373 return result;
Svetoslav62ce3322014-09-04 21:17:17 -0700374};
375
376};