Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 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 | |
Philip P. Moltmann | 12328c0 | 2016-06-10 14:47:16 -0700 | [diff] [blame] | 17 | #include "PdfUtils.h" |
| 18 | |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 19 | #include "jni.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 20 | #include <nativehelper/JNIHelp.h> |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 21 | #include "GraphicsJNI.h" |
| 22 | #include "SkBitmap.h" |
| 23 | #include "SkMatrix.h" |
| 24 | #include "fpdfview.h" |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 25 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 26 | #include "core_jni_helpers.h" |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | #include <utils/Log.h> |
| 29 | #include <unistd.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | static const int RENDER_MODE_FOR_DISPLAY = 1; |
| 36 | static const int RENDER_MODE_FOR_PRINT = 2; |
| 37 | |
| 38 | static struct { |
| 39 | jfieldID x; |
| 40 | jfieldID y; |
| 41 | } gPointClassInfo; |
| 42 | |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 43 | static 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); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 48 | if (!page) { |
| 49 | jniThrowException(env, "java/lang/IllegalStateException", |
| 50 | "cannot load page"); |
| 51 | return -1; |
| 52 | } |
Philip P. Moltmann | 12328c0 | 2016-06-10 14:47:16 -0700 | [diff] [blame] | 53 | HANDLE_PDFIUM_ERROR_STATE_WITH_RET_CODE(env, -1) |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 54 | |
| 55 | double width = 0; |
| 56 | double height = 0; |
| 57 | |
Philip P. Moltmann | 12328c0 | 2016-06-10 14:47:16 -0700 | [diff] [blame] | 58 | int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 59 | if (!result) { |
| 60 | jniThrowException(env, "java/lang/IllegalStateException", |
| 61 | "cannot get page size"); |
| 62 | return -1; |
| 63 | } |
Philip P. Moltmann | 12328c0 | 2016-06-10 14:47:16 -0700 | [diff] [blame] | 64 | HANDLE_PDFIUM_ERROR_STATE_WITH_RET_CODE(env, -1) |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 65 | |
| 66 | env->SetIntField(outSize, gPointClassInfo.x, width); |
| 67 | env->SetIntField(outSize, gPointClassInfo.y, height); |
| 68 | |
| 69 | return reinterpret_cast<jlong>(page); |
| 70 | } |
| 71 | |
| 72 | static void nativeClosePage(JNIEnv* env, jclass thiz, jlong pagePtr) { |
| 73 | FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr); |
| 74 | FPDF_ClosePage(page); |
Philip P. Moltmann | 12328c0 | 2016-06-10 14:47:16 -0700 | [diff] [blame] | 75 | HANDLE_PDFIUM_ERROR_STATE(env) |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 78 | static void nativeRenderPage(JNIEnv* env, jclass thiz, jlong documentPtr, jlong pagePtr, |
Philip P. Moltmann | d55f20d | 2017-02-16 15:18:40 -0800 | [diff] [blame] | 79 | jobject jbitmap, jint clipLeft, jint clipTop, jint clipRight, jint clipBottom, |
| 80 | jlong transformPtr, jint renderMode) { |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 81 | FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 82 | |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 83 | SkBitmap skBitmap; |
| 84 | GraphicsJNI::getSkBitmap(env, jbitmap, &skBitmap); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 85 | |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 86 | 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. Moltmann | d55f20d | 2017-02-16 15:18:40 -0800 | [diff] [blame] | 90 | bool isExceptionPending = forwardPdfiumError(env); |
| 91 | if (isExceptionPending || bitmap == NULL) { |
| 92 | ALOGE("Error creating bitmap"); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
Philip P. Moltmann | d55f20d | 2017-02-16 15:18:40 -0800 | [diff] [blame] | 96 | int renderFlags = FPDF_REVERSE_BYTE_ORDER; |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 97 | 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. Moltmann | d55f20d | 2017-02-16 15:18:40 -0800 | [diff] [blame] | 103 | // 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 Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 119 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 120 | "transform matrix has perspective. Only affine matrices are allowed."); |
| 121 | return; |
| 122 | } |
| 123 | |
Philip P. Moltmann | d55f20d | 2017-02-16 15:18:40 -0800 | [diff] [blame] | 124 | 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); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 133 | |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 134 | skBitmap.notifyPixelsChanged(); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 137 | static const JNINativeMethod gPdfRenderer_Methods[] = { |
Philip P. Moltmann | 12328c0 | 2016-06-10 14:47:16 -0700 | [diff] [blame] | 138 | {"nativeCreate", "(IJ)J", (void*) nativeOpen}, |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 139 | {"nativeClose", "(J)V", (void*) nativeClose}, |
| 140 | {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount}, |
| 141 | {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting}, |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 142 | {"nativeRenderPage", "(JJLandroid/graphics/Bitmap;IIIIJI)V", (void*) nativeRenderPage}, |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 143 | {"nativeOpenPageAndGetSize", "(JILandroid/graphics/Point;)J", (void*) nativeOpenPageAndGetSize}, |
| 144 | {"nativeClosePage", "(J)V", (void*) nativeClosePage} |
| 145 | }; |
| 146 | |
| 147 | int register_android_graphics_pdf_PdfRenderer(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 148 | int result = RegisterMethodsOrDie( |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 149 | env, "android/graphics/pdf/PdfRenderer", gPdfRenderer_Methods, |
| 150 | NELEM(gPdfRenderer_Methods)); |
| 151 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 152 | jclass clazz = FindClassOrDie(env, "android/graphics/Point"); |
| 153 | gPointClassInfo.x = GetFieldIDOrDie(env, clazz, "x", "I"); |
| 154 | gPointClassInfo.y = GetFieldIDOrDie(env, clazz, "y", "I"); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 155 | |
| 156 | return result; |
| 157 | }; |
| 158 | |
| 159 | }; |