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 "jni.h" 20#include "JNIHelp.h" 21#include "GraphicsJNI.h" 22#include "SkBitmap.h" 23#include "SkMatrix.h" 24#include "fpdfview.h" 25 26#include "core_jni_helpers.h" 27#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 43static 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); 48 if (!page) { 49 jniThrowException(env, "java/lang/IllegalStateException", 50 "cannot load page"); 51 return -1; 52 } 53 HANDLE_PDFIUM_ERROR_STATE_WITH_RET_CODE(env, -1) 54 55 double width = 0; 56 double height = 0; 57 58 int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height); 59 if (!result) { 60 jniThrowException(env, "java/lang/IllegalStateException", 61 "cannot get page size"); 62 return -1; 63 } 64 HANDLE_PDFIUM_ERROR_STATE_WITH_RET_CODE(env, -1) 65 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); 75 HANDLE_PDFIUM_ERROR_STATE(env) 76} 77 78static void nativeRenderPage(JNIEnv* env, jclass thiz, jlong documentPtr, jlong pagePtr, 79 jobject jbitmap, jint clipLeft, jint clipTop, jint clipRight, jint clipBottom, 80 jlong transformPtr, jint renderMode) { 81 FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr); 82 83 SkBitmap skBitmap; 84 GraphicsJNI::getSkBitmap(env, jbitmap, &skBitmap); 85 86 SkAutoLockPixels alp(skBitmap); 87 88 const int stride = skBitmap.width() * 4; 89 90 FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(skBitmap.width(), skBitmap.height(), 91 FPDFBitmap_BGRA, skBitmap.getPixels(), stride); 92 bool isExceptionPending = forwardPdfiumError(env); 93 if (isExceptionPending || bitmap == NULL) { 94 ALOGE("Error creating bitmap"); 95 return; 96 } 97 98 int renderFlags = FPDF_REVERSE_BYTE_ORDER; 99 if (renderMode == RENDER_MODE_FOR_DISPLAY) { 100 renderFlags |= FPDF_LCD_TEXT; 101 } else if (renderMode == RENDER_MODE_FOR_PRINT) { 102 renderFlags |= FPDF_PRINTING; 103 } 104 105 // PDF's coordinate system origin is left-bottom while in graphics it 106 // is the top-left. So, translate the PDF coordinates to ours. 107 SkMatrix reflectOnX = SkMatrix::MakeScale(1, -1); 108 SkMatrix moveUp = SkMatrix::MakeTrans(0, FPDF_GetPageHeight(page)); 109 SkMatrix coordinateChange = SkMatrix::Concat(moveUp, reflectOnX); 110 111 // Apply the transformation 112 SkMatrix matrix; 113 if (transformPtr == 0) { 114 matrix = coordinateChange; 115 } else { 116 matrix = SkMatrix::Concat(*reinterpret_cast<SkMatrix*>(transformPtr), coordinateChange); 117 } 118 119 SkScalar transformValues[6]; 120 if (!matrix.asAffine(transformValues)) { 121 jniThrowException(env, "java/lang/IllegalArgumentException", 122 "transform matrix has perspective. Only affine matrices are allowed."); 123 return; 124 } 125 126 FS_MATRIX transform = {transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY], 127 transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY], 128 transformValues[SkMatrix::kATransX], 129 transformValues[SkMatrix::kATransY]}; 130 131 FS_RECTF clip = {(float) clipLeft, (float) clipTop, (float) clipRight, (float) clipBottom}; 132 133 FPDF_RenderPageBitmapWithMatrix(bitmap, page, &transform, &clip, renderFlags); 134 HANDLE_PDFIUM_ERROR_STATE(env); 135 136 skBitmap.notifyPixelsChanged(); 137} 138 139static const JNINativeMethod gPdfRenderer_Methods[] = { 140 {"nativeCreate", "(IJ)J", (void*) nativeOpen}, 141 {"nativeClose", "(J)V", (void*) nativeClose}, 142 {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount}, 143 {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting}, 144 {"nativeRenderPage", "(JJLandroid/graphics/Bitmap;IIIIJI)V", (void*) nativeRenderPage}, 145 {"nativeOpenPageAndGetSize", "(JILandroid/graphics/Point;)J", (void*) nativeOpenPageAndGetSize}, 146 {"nativeClosePage", "(J)V", (void*) nativeClosePage} 147}; 148 149int register_android_graphics_pdf_PdfRenderer(JNIEnv* env) { 150 int result = RegisterMethodsOrDie( 151 env, "android/graphics/pdf/PdfRenderer", gPdfRenderer_Methods, 152 NELEM(gPdfRenderer_Methods)); 153 154 jclass clazz = FindClassOrDie(env, "android/graphics/Point"); 155 gPointClassInfo.x = GetFieldIDOrDie(env, clazz, "x", "I"); 156 gPointClassInfo.y = GetFieldIDOrDie(env, clazz, "y", "I"); 157 158 return result; 159}; 160 161}; 162