blob: 95cdbb5cba256f2659612a49677b4e916ac61178 [file] [log] [blame]
Svetoslav29617692014-04-24 18:40:42 -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
Philip P. Moltmann12328c02016-06-10 14:47:16 -070017#include "PdfUtils.h"
18
Svetoslav29617692014-04-24 18:40:42 -070019#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070020#include <nativehelper/JNIHelp.h>
Svetoslav29617692014-04-24 18:40:42 -070021#include "GraphicsJNI.h"
22#include "SkBitmap.h"
23#include "SkMatrix.h"
24#include "fpdfview.h"
Andreas Gampe0f0b4912014-11-12 08:03:48 -080025
Andreas Gampeed6b9df2014-11-20 22:02:20 -080026#include "core_jni_helpers.h"
Svetoslav29617692014-04-24 18:40:42 -070027#include <vector>
28#include <utils/Log.h>
29#include <unistd.h>
30#include <sys/types.h>
31#include <unistd.h>
32
33namespace android {
34
35static const int RENDER_MODE_FOR_DISPLAY = 1;
36static const int RENDER_MODE_FOR_PRINT = 2;
37
38static struct {
39 jfieldID x;
40 jfieldID y;
41} gPointClassInfo;
42
Svetoslav29617692014-04-24 18:40:42 -070043static jlong nativeOpenPageAndGetSize(JNIEnv* env, jclass thiz, jlong documentPtr,
44 jint pageIndex, jobject outSize) {
45 FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
46
47 FPDF_PAGE page = FPDF_LoadPage(document, pageIndex);
Svetoslav29617692014-04-24 18:40:42 -070048 if (!page) {
49 jniThrowException(env, "java/lang/IllegalStateException",
50 "cannot load page");
51 return -1;
52 }
Philip P. Moltmann12328c02016-06-10 14:47:16 -070053 HANDLE_PDFIUM_ERROR_STATE_WITH_RET_CODE(env, -1)
Svetoslav29617692014-04-24 18:40:42 -070054
55 double width = 0;
56 double height = 0;
57
Philip P. Moltmann12328c02016-06-10 14:47:16 -070058 int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height);
Svetoslav29617692014-04-24 18:40:42 -070059 if (!result) {
60 jniThrowException(env, "java/lang/IllegalStateException",
61 "cannot get page size");
62 return -1;
63 }
Philip P. Moltmann12328c02016-06-10 14:47:16 -070064 HANDLE_PDFIUM_ERROR_STATE_WITH_RET_CODE(env, -1)
Svetoslav29617692014-04-24 18:40:42 -070065
66 env->SetIntField(outSize, gPointClassInfo.x, width);
67 env->SetIntField(outSize, gPointClassInfo.y, height);
68
69 return reinterpret_cast<jlong>(page);
70}
71
72static void nativeClosePage(JNIEnv* env, jclass thiz, jlong pagePtr) {
73 FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr);
74 FPDF_ClosePage(page);
Philip P. Moltmann12328c02016-06-10 14:47:16 -070075 HANDLE_PDFIUM_ERROR_STATE(env)
Svetoslav29617692014-04-24 18:40:42 -070076}
77
Svetoslav29617692014-04-24 18:40:42 -070078static void nativeRenderPage(JNIEnv* env, jclass thiz, jlong documentPtr, jlong pagePtr,
Philip P. Moltmannd55f20d2017-02-16 15:18:40 -080079 jobject jbitmap, jint clipLeft, jint clipTop, jint clipRight, jint clipBottom,
80 jlong transformPtr, jint renderMode) {
Svetoslav29617692014-04-24 18:40:42 -070081 FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr);
Svetoslav29617692014-04-24 18:40:42 -070082
John Recked207b92015-04-10 13:52:57 -070083 SkBitmap skBitmap;
84 GraphicsJNI::getSkBitmap(env, jbitmap, &skBitmap);
Svetoslav29617692014-04-24 18:40:42 -070085
John Recked207b92015-04-10 13:52:57 -070086 const int stride = skBitmap.width() * 4;
87
88 FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(skBitmap.width(), skBitmap.height(),
89 FPDFBitmap_BGRA, skBitmap.getPixels(), stride);
Philip P. Moltmannd55f20d2017-02-16 15:18:40 -080090 bool isExceptionPending = forwardPdfiumError(env);
91 if (isExceptionPending || bitmap == NULL) {
92 ALOGE("Error creating bitmap");
Svetoslav29617692014-04-24 18:40:42 -070093 return;
94 }
95
Philip P. Moltmannd55f20d2017-02-16 15:18:40 -080096 int renderFlags = FPDF_REVERSE_BYTE_ORDER;
Svetoslav29617692014-04-24 18:40:42 -070097 if (renderMode == RENDER_MODE_FOR_DISPLAY) {
98 renderFlags |= FPDF_LCD_TEXT;
99 } else if (renderMode == RENDER_MODE_FOR_PRINT) {
100 renderFlags |= FPDF_PRINTING;
101 }
102
Philip P. Moltmannd55f20d2017-02-16 15:18:40 -0800103 // PDF's coordinate system origin is left-bottom while in graphics it
104 // is the top-left. So, translate the PDF coordinates to ours.
105 SkMatrix reflectOnX = SkMatrix::MakeScale(1, -1);
106 SkMatrix moveUp = SkMatrix::MakeTrans(0, FPDF_GetPageHeight(page));
107 SkMatrix coordinateChange = SkMatrix::Concat(moveUp, reflectOnX);
108
109 // Apply the transformation
110 SkMatrix matrix;
111 if (transformPtr == 0) {
112 matrix = coordinateChange;
113 } else {
114 matrix = SkMatrix::Concat(*reinterpret_cast<SkMatrix*>(transformPtr), coordinateChange);
115 }
116
117 SkScalar transformValues[6];
118 if (!matrix.asAffine(transformValues)) {
Mike Reed71487eb2014-11-19 16:13:20 -0500119 jniThrowException(env, "java/lang/IllegalArgumentException",
120 "transform matrix has perspective. Only affine matrices are allowed.");
121 return;
122 }
123
Philip P. Moltmannd55f20d2017-02-16 15:18:40 -0800124 FS_MATRIX transform = {transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
125 transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
126 transformValues[SkMatrix::kATransX],
127 transformValues[SkMatrix::kATransY]};
128
129 FS_RECTF clip = {(float) clipLeft, (float) clipTop, (float) clipRight, (float) clipBottom};
130
131 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &transform, &clip, renderFlags);
132 HANDLE_PDFIUM_ERROR_STATE(env);
Svetoslav29617692014-04-24 18:40:42 -0700133
John Recked207b92015-04-10 13:52:57 -0700134 skBitmap.notifyPixelsChanged();
Svetoslav29617692014-04-24 18:40:42 -0700135}
136
Daniel Micay76f6a862015-09-19 17:31:01 -0400137static const JNINativeMethod gPdfRenderer_Methods[] = {
Philip P. Moltmann12328c02016-06-10 14:47:16 -0700138 {"nativeCreate", "(IJ)J", (void*) nativeOpen},
Svetoslav29617692014-04-24 18:40:42 -0700139 {"nativeClose", "(J)V", (void*) nativeClose},
140 {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount},
141 {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting},
John Recked207b92015-04-10 13:52:57 -0700142 {"nativeRenderPage", "(JJLandroid/graphics/Bitmap;IIIIJI)V", (void*) nativeRenderPage},
Svetoslav29617692014-04-24 18:40:42 -0700143 {"nativeOpenPageAndGetSize", "(JILandroid/graphics/Point;)J", (void*) nativeOpenPageAndGetSize},
144 {"nativeClosePage", "(J)V", (void*) nativeClosePage}
145};
146
147int register_android_graphics_pdf_PdfRenderer(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800148 int result = RegisterMethodsOrDie(
Svetoslav29617692014-04-24 18:40:42 -0700149 env, "android/graphics/pdf/PdfRenderer", gPdfRenderer_Methods,
150 NELEM(gPdfRenderer_Methods));
151
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800152 jclass clazz = FindClassOrDie(env, "android/graphics/Point");
153 gPointClassInfo.x = GetFieldIDOrDie(env, clazz, "x", "I");
154 gPointClassInfo.y = GetFieldIDOrDie(env, clazz, "y", "I");
Svetoslav29617692014-04-24 18:40:42 -0700155
156 return result;
157};
158
159};