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