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 "PdfUtils.h"
18
19 #include "GraphicsJNI.h"
20 #include "SkBitmap.h"
21 #include "SkMatrix.h"
22 #include "fpdfview.h"
23
24 #include <vector>
25 #include <utils/Log.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 namespace android {
31
32 static const int RENDER_MODE_FOR_DISPLAY = 1;
33 static const int RENDER_MODE_FOR_PRINT = 2;
34
35 static struct {
36 jfieldID x;
37 jfieldID y;
38 } gPointClassInfo;
39
nativeOpenPageAndGetSize(JNIEnv * env,jclass thiz,jlong documentPtr,jint pageIndex,jobject outSize)40 static jlong nativeOpenPageAndGetSize(JNIEnv* env, jclass thiz, jlong documentPtr,
41 jint pageIndex, jobject outSize) {
42 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
43
44 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
45 if (!page) {
46 jniThrowException(env, "java/lang/IllegalStateException",
47 "cannot load page");
48 return -1;
49 }
50
51 double width = 0;
52 double height = 0;
53
54 int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height);
55 if (!result) {
56 jniThrowException(env, "java/lang/IllegalStateException",
57 "cannot get page size");
58 return -1;
59 }
60
61 env->SetIntField(outSize, gPointClassInfo.x, width);
62 env->SetIntField(outSize, gPointClassInfo.y, height);
63
64 return reinterpret_cast<jlong>(page);
65 }
66
nativeClosePage(JNIEnv * env,jclass thiz,jlong pagePtr)67 static void nativeClosePage(JNIEnv* env, jclass thiz, jlong pagePtr) {
68 FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr);
69 FPDF_ClosePage(page);
70 }
71
nativeRenderPage(JNIEnv * env,jclass thiz,jlong documentPtr,jlong pagePtr,jlong bitmapPtr,jint clipLeft,jint clipTop,jint clipRight,jint clipBottom,jlong transformPtr,jint renderMode)72 static void nativeRenderPage(JNIEnv* env, jclass thiz, jlong documentPtr, jlong pagePtr,
73 jlong bitmapPtr, jint clipLeft, jint clipTop, jint clipRight, jint clipBottom,
74 jlong transformPtr, jint renderMode) {
75 FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr);
76
77 SkBitmap skBitmap;
78 bitmap::toBitmap(bitmapPtr).getSkBitmap(&skBitmap);
79
80 const int stride = skBitmap.width() * 4;
81
82 FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(skBitmap.width(), skBitmap.height(),
83 FPDFBitmap_BGRA, skBitmap.getPixels(), stride);
84
85 int renderFlags = FPDF_REVERSE_BYTE_ORDER;
86 if (renderMode == RENDER_MODE_FOR_DISPLAY) {
87 renderFlags |= FPDF_LCD_TEXT;
88 } else if (renderMode == RENDER_MODE_FOR_PRINT) {
89 renderFlags |= FPDF_PRINTING;
90 }
91
92 SkMatrix matrix = *reinterpret_cast<SkMatrix*>(transformPtr);
93 SkScalar transformValues[6];
94 if (!matrix.asAffine(transformValues)) {
95 jniThrowException(env, "java/lang/IllegalArgumentException",
96 "transform matrix has perspective. Only affine matrices are allowed.");
97 return;
98 }
99
100 FS_MATRIX transform = {transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
101 transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
102 transformValues[SkMatrix::kATransX],
103 transformValues[SkMatrix::kATransY]};
104
105 FS_RECTF clip = {(float) clipLeft, (float) clipTop, (float) clipRight, (float) clipBottom};
106
107 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &transform, &clip, renderFlags);
108
109 skBitmap.notifyPixelsChanged();
110 }
111
112 static const JNINativeMethod gPdfRenderer_Methods[] = {
113 {"nativeCreate", "(IJ)J", (void*) nativeOpen},
114 {"nativeClose", "(J)V", (void*) nativeClose},
115 {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount},
116 {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting},
117 {"nativeRenderPage", "(JJJIIIIJI)V", (void*) nativeRenderPage},
118 {"nativeOpenPageAndGetSize", "(JILandroid/graphics/Point;)J", (void*) nativeOpenPageAndGetSize},
119 {"nativeClosePage", "(J)V", (void*) nativeClosePage}
120 };
121
register_android_graphics_pdf_PdfRenderer(JNIEnv * env)122 int register_android_graphics_pdf_PdfRenderer(JNIEnv* env) {
123 int result = RegisterMethodsOrDie(
124 env, "android/graphics/pdf/PdfRenderer", gPdfRenderer_Methods,
125 NELEM(gPdfRenderer_Methods));
126
127 jclass clazz = FindClassOrDie(env, "android/graphics/Point");
128 gPointClassInfo.x = GetFieldIDOrDie(env, clazz, "x", "I");
129 gPointClassInfo.y = GetFieldIDOrDie(env, clazz, "y", "I");
130
131 return result;
132 };
133
134 };
135