1 /*
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 #undef LOG_TAG
18 #define LOG_TAG "PdfEditor"
19
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include <vector>
24
25 #include <log/log.h>
26 #include <utils/Log.h>
27
28 #include "PdfUtils.h"
29
30 #include "graphics_jni_helpers.h"
31
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
34 #include "fpdfview.h"
35 #include "fpdf_edit.h"
36 #include "fpdf_save.h"
37 #include "fpdf_transformpage.h"
38 #pragma GCC diagnostic pop
39
40 #include "SkMatrix.h"
41
42 namespace android {
43
44 enum PageBox {PAGE_BOX_MEDIA, PAGE_BOX_CROP};
45
46 static struct {
47 jfieldID x;
48 jfieldID y;
49 } gPointClassInfo;
50
51 static struct {
52 jfieldID left;
53 jfieldID top;
54 jfieldID right;
55 jfieldID bottom;
56 } gRectClassInfo;
57
nativeRemovePage(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex)58 static jint nativeRemovePage(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex) {
59 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
60
61 FPDFPage_Delete(document, pageIndex);
62 return FPDF_GetPageCount(document);
63 }
64
65 struct PdfToFdWriter : FPDF_FILEWRITE {
66 int dstFd;
67 };
68
writeAllBytes(const int fd,const void * buffer,const size_t byteCount)69 static bool writeAllBytes(const int fd, const void* buffer, const size_t byteCount) {
70 char* writeBuffer = static_cast<char*>(const_cast<void*>(buffer));
71 size_t remainingBytes = byteCount;
72 while (remainingBytes > 0) {
73 ssize_t writtenByteCount = write(fd, writeBuffer, remainingBytes);
74 if (writtenByteCount == -1) {
75 if (errno == EINTR) {
76 continue;
77 }
78 ALOGE("Error writing to buffer: %d", errno);
79 return false;
80 }
81 remainingBytes -= writtenByteCount;
82 writeBuffer += writtenByteCount;
83 }
84 return true;
85 }
86
writeBlock(FPDF_FILEWRITE * owner,const void * buffer,unsigned long size)87 static int writeBlock(FPDF_FILEWRITE* owner, const void* buffer, unsigned long size) {
88 const PdfToFdWriter* writer = reinterpret_cast<PdfToFdWriter*>(owner);
89 const bool success = writeAllBytes(writer->dstFd, buffer, size);
90 if (!success) {
91 ALOGE("Cannot write to file descriptor. Error:%d", errno);
92 return 0;
93 }
94 return 1;
95 }
96
nativeWrite(JNIEnv * env,jclass thiz,jlong documentPtr,jint fd)97 static void nativeWrite(JNIEnv* env, jclass thiz, jlong documentPtr, jint fd) {
98 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
99 PdfToFdWriter writer;
100 writer.dstFd = fd;
101 writer.WriteBlock = &writeBlock;
102 const bool success = FPDF_SaveAsCopy(document, &writer, FPDF_NO_INCREMENTAL);
103 if (!success) {
104 jniThrowExceptionFmt(env, "java/io/IOException",
105 "cannot write to fd. Error: %d", errno);
106 }
107 }
108
nativeSetTransformAndClip(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,jlong transformPtr,jint clipLeft,jint clipTop,jint clipRight,jint clipBottom)109 static void nativeSetTransformAndClip(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
110 jlong transformPtr, jint clipLeft, jint clipTop, jint clipRight, jint clipBottom) {
111 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
112
113 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
114 if (!page) {
115 jniThrowException(env, "java/lang/IllegalStateException",
116 "cannot open page");
117 return;
118 }
119
120 double width = 0;
121 double height = 0;
122
123 const int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height);
124 if (!result) {
125 jniThrowException(env, "java/lang/IllegalStateException",
126 "cannot get page size");
127 return;
128 }
129
130 // PDF's coordinate system origin is left-bottom while in graphics it
131 // is the top-left. So, translate the PDF coordinates to ours.
132 SkMatrix reflectOnX = SkMatrix::Scale(1, -1);
133 SkMatrix moveUp = SkMatrix::Translate(0, FPDF_GetPageHeight(page));
134 SkMatrix coordinateChange = SkMatrix::Concat(moveUp, reflectOnX);
135
136 // Apply the transformation what was created in our coordinates.
137 SkMatrix matrix = SkMatrix::Concat(*reinterpret_cast<SkMatrix*>(transformPtr),
138 coordinateChange);
139
140 // Translate the result back to PDF coordinates.
141 matrix.setConcat(coordinateChange, matrix);
142
143 SkScalar transformValues[6];
144 if (!matrix.asAffine(transformValues)) {
145 FPDF_ClosePage(page);
146
147 jniThrowException(env, "java/lang/IllegalArgumentException",
148 "transform matrix has perspective. Only affine matrices are allowed.");
149 return;
150 }
151
152 FS_MATRIX transform = {transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
153 transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
154 transformValues[SkMatrix::kATransX],
155 transformValues[SkMatrix::kATransY]};
156
157 FS_RECTF clip = {(float) clipLeft, (float) clipTop, (float) clipRight, (float) clipBottom};
158
159 FPDFPage_TransFormWithClip(page, &transform, &clip);
160
161 FPDF_ClosePage(page);
162 }
163
nativeGetPageSize(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,jobject outSize)164 static void nativeGetPageSize(JNIEnv* env, jclass thiz, jlong documentPtr,
165 jint pageIndex, jobject outSize) {
166 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
167
168 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
169 if (!page) {
170 jniThrowException(env, "java/lang/IllegalStateException",
171 "cannot open page");
172 return;
173 }
174
175 double width = 0;
176 double height = 0;
177
178 const int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height);
179 if (!result) {
180 jniThrowException(env, "java/lang/IllegalStateException",
181 "cannot get page size");
182 return;
183 }
184
185 env->SetIntField(outSize, gPointClassInfo.x, width);
186 env->SetIntField(outSize, gPointClassInfo.y, height);
187
188 FPDF_ClosePage(page);
189 }
190
nativeGetPageBox(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,PageBox pageBox,jobject outBox)191 static bool nativeGetPageBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
192 PageBox pageBox, jobject outBox) {
193 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
194
195 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
196 if (!page) {
197 jniThrowException(env, "java/lang/IllegalStateException",
198 "cannot open page");
199 return false;
200 }
201
202 float left;
203 float top;
204 float right;
205 float bottom;
206
207 const FPDF_BOOL success = (pageBox == PAGE_BOX_MEDIA)
208 ? FPDFPage_GetMediaBox(page, &left, &top, &right, &bottom)
209 : FPDFPage_GetCropBox(page, &left, &top, &right, &bottom);
210
211 FPDF_ClosePage(page);
212
213 if (!success) {
214 return false;
215 }
216
217 env->SetIntField(outBox, gRectClassInfo.left, (int) left);
218 env->SetIntField(outBox, gRectClassInfo.top, (int) top);
219 env->SetIntField(outBox, gRectClassInfo.right, (int) right);
220 env->SetIntField(outBox, gRectClassInfo.bottom, (int) bottom);
221
222 return true;
223 }
224
nativeGetPageMediaBox(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,jobject outMediaBox)225 static jboolean nativeGetPageMediaBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
226 jobject outMediaBox) {
227 const bool success = nativeGetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_MEDIA,
228 outMediaBox);
229 return success ? JNI_TRUE : JNI_FALSE;
230 }
231
nativeGetPageCropBox(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,jobject outMediaBox)232 static jboolean nativeGetPageCropBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
233 jobject outMediaBox) {
234 const bool success = nativeGetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_CROP,
235 outMediaBox);
236 return success ? JNI_TRUE : JNI_FALSE;
237 }
238
nativeSetPageBox(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,PageBox pageBox,jobject box)239 static void nativeSetPageBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
240 PageBox pageBox, jobject box) {
241 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
242
243 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
244 if (!page) {
245 jniThrowException(env, "java/lang/IllegalStateException",
246 "cannot open page");
247 return;
248 }
249
250 const int left = env->GetIntField(box, gRectClassInfo.left);
251 const int top = env->GetIntField(box, gRectClassInfo.top);
252 const int right = env->GetIntField(box, gRectClassInfo.right);
253 const int bottom = env->GetIntField(box, gRectClassInfo.bottom);
254
255 if (pageBox == PAGE_BOX_MEDIA) {
256 FPDFPage_SetMediaBox(page, left, top, right, bottom);
257 } else {
258 FPDFPage_SetCropBox(page, left, top, right, bottom);
259 }
260
261 FPDF_ClosePage(page);
262 }
263
nativeSetPageMediaBox(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,jobject mediaBox)264 static void nativeSetPageMediaBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
265 jobject mediaBox) {
266 nativeSetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_MEDIA, mediaBox);
267 }
268
nativeSetPageCropBox(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,jobject mediaBox)269 static void nativeSetPageCropBox(JNIEnv* env, jclass thiz, jlong documentPtr, jint pageIndex,
270 jobject mediaBox) {
271 nativeSetPageBox(env, thiz, documentPtr, pageIndex, PAGE_BOX_CROP, mediaBox);
272 }
273
274 static const JNINativeMethod gPdfEditor_Methods[] = {
275 {"nativeOpen", "(IJ)J", (void*) nativeOpen},
276 {"nativeClose", "(J)V", (void*) nativeClose},
277 {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount},
278 {"nativeRemovePage", "(JI)I", (void*) nativeRemovePage},
279 {"nativeWrite", "(JI)V", (void*) nativeWrite},
280 {"nativeSetTransformAndClip", "(JIJIIII)V", (void*) nativeSetTransformAndClip},
281 {"nativeGetPageSize", "(JILandroid/graphics/Point;)V", (void*) nativeGetPageSize},
282 {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting},
283 {"nativeGetPageMediaBox", "(JILandroid/graphics/Rect;)Z", (void*) nativeGetPageMediaBox},
284 {"nativeSetPageMediaBox", "(JILandroid/graphics/Rect;)V", (void*) nativeSetPageMediaBox},
285 {"nativeGetPageCropBox", "(JILandroid/graphics/Rect;)Z", (void*) nativeGetPageCropBox},
286 {"nativeSetPageCropBox", "(JILandroid/graphics/Rect;)V", (void*) nativeSetPageCropBox}
287 };
288
register_android_graphics_pdf_PdfEditor(JNIEnv * env)289 int register_android_graphics_pdf_PdfEditor(JNIEnv* env) {
290 const int result = RegisterMethodsOrDie(
291 env, "android/graphics/pdf/PdfEditor", gPdfEditor_Methods,
292 NELEM(gPdfEditor_Methods));
293
294 jclass pointClass = FindClassOrDie(env, "android/graphics/Point");
295 gPointClassInfo.x = GetFieldIDOrDie(env, pointClass, "x", "I");
296 gPointClassInfo.y = GetFieldIDOrDie(env, pointClass, "y", "I");
297
298 jclass rectClass = FindClassOrDie(env, "android/graphics/Rect");
299 gRectClassInfo.left = GetFieldIDOrDie(env, rectClass, "left", "I");
300 gRectClassInfo.top = GetFieldIDOrDie(env, rectClass, "top", "I");
301 gRectClassInfo.right = GetFieldIDOrDie(env, rectClass, "right", "I");
302 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClass, "bottom", "I");
303
304 return result;
305 };
306
307 };
308