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 | |
| 17 | #include "jni.h" |
| 18 | #include "JNIHelp.h" |
| 19 | #include "GraphicsJNI.h" |
| 20 | #include "SkBitmap.h" |
| 21 | #include "SkMatrix.h" |
| 22 | #include "fpdfview.h" |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 23 | |
| 24 | #pragma GCC diagnostic push |
| 25 | #pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor" |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 26 | #include "fsdk_rendercontext.h" |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 27 | #pragma GCC diagnostic pop |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 28 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 29 | #include "core_jni_helpers.h" |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 30 | #include <vector> |
| 31 | #include <utils/Log.h> |
| 32 | #include <unistd.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | static const int RENDER_MODE_FOR_DISPLAY = 1; |
| 39 | static const int RENDER_MODE_FOR_PRINT = 2; |
| 40 | |
| 41 | static struct { |
| 42 | jfieldID x; |
| 43 | jfieldID y; |
| 44 | } gPointClassInfo; |
| 45 | |
| 46 | static Mutex sLock; |
| 47 | |
| 48 | static int sUnmatchedInitRequestCount = 0; |
| 49 | |
| 50 | static void initializeLibraryIfNeeded() { |
| 51 | Mutex::Autolock _l(sLock); |
| 52 | if (sUnmatchedInitRequestCount == 0) { |
| 53 | FPDF_InitLibrary(NULL); |
| 54 | } |
| 55 | sUnmatchedInitRequestCount++; |
| 56 | } |
| 57 | |
| 58 | static void destroyLibraryIfNeeded() { |
| 59 | Mutex::Autolock _l(sLock); |
| 60 | sUnmatchedInitRequestCount--; |
| 61 | if (sUnmatchedInitRequestCount == 0) { |
| 62 | FPDF_DestroyLibrary(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static int getBlock(void* param, unsigned long position, unsigned char* outBuffer, |
| 67 | unsigned long size) { |
| 68 | const int fd = reinterpret_cast<intptr_t>(param); |
| 69 | const int readCount = pread(fd, outBuffer, size, position); |
| 70 | if (readCount < 0) { |
| 71 | ALOGE("Cannot read from file descriptor. Error:%d", errno); |
| 72 | return 0; |
| 73 | } |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static jlong nativeCreate(JNIEnv* env, jclass thiz, jint fd, jlong size) { |
| 78 | initializeLibraryIfNeeded(); |
| 79 | |
| 80 | FPDF_FILEACCESS loader; |
| 81 | loader.m_FileLen = size; |
| 82 | loader.m_Param = reinterpret_cast<void*>(intptr_t(fd)); |
| 83 | loader.m_GetBlock = &getBlock; |
| 84 | |
| 85 | FPDF_DOCUMENT document = FPDF_LoadCustomDocument(&loader, NULL); |
| 86 | |
| 87 | if (!document) { |
| 88 | const long error = FPDF_GetLastError(); |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 89 | switch (error) { |
| 90 | case FPDF_ERR_PASSWORD: |
| 91 | case FPDF_ERR_SECURITY: { |
Dan Albert | 3a091b7 | 2014-11-20 15:41:25 -0800 | [diff] [blame] | 92 | jniThrowExceptionFmt(env, "java/lang/SecurityException", |
| 93 | "cannot create document. Error: %ld", error); |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 94 | } break; |
| 95 | default: { |
Dan Albert | 3a091b7 | 2014-11-20 15:41:25 -0800 | [diff] [blame] | 96 | jniThrowExceptionFmt(env, "java/io/IOException", |
| 97 | "cannot create document. Error: %ld", error); |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 98 | } break; |
| 99 | } |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 100 | destroyLibraryIfNeeded(); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return reinterpret_cast<jlong>(document); |
| 105 | } |
| 106 | |
| 107 | static jlong nativeOpenPageAndGetSize(JNIEnv* env, jclass thiz, jlong documentPtr, |
| 108 | jint pageIndex, jobject outSize) { |
| 109 | FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr); |
| 110 | |
| 111 | FPDF_PAGE page = FPDF_LoadPage(document, pageIndex); |
| 112 | |
| 113 | if (!page) { |
| 114 | jniThrowException(env, "java/lang/IllegalStateException", |
| 115 | "cannot load page"); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | double width = 0; |
| 120 | double height = 0; |
| 121 | |
| 122 | const int result = FPDF_GetPageSizeByIndex(document, pageIndex, &width, &height); |
| 123 | |
| 124 | if (!result) { |
| 125 | jniThrowException(env, "java/lang/IllegalStateException", |
| 126 | "cannot get page size"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | env->SetIntField(outSize, gPointClassInfo.x, width); |
| 131 | env->SetIntField(outSize, gPointClassInfo.y, height); |
| 132 | |
| 133 | return reinterpret_cast<jlong>(page); |
| 134 | } |
| 135 | |
| 136 | static void nativeClosePage(JNIEnv* env, jclass thiz, jlong pagePtr) { |
| 137 | FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr); |
| 138 | FPDF_ClosePage(page); |
| 139 | } |
| 140 | |
| 141 | static void nativeClose(JNIEnv* env, jclass thiz, jlong documentPtr) { |
| 142 | FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr); |
| 143 | FPDF_CloseDocument(document); |
| 144 | destroyLibraryIfNeeded(); |
| 145 | } |
| 146 | |
| 147 | static jint nativeGetPageCount(JNIEnv* env, jclass thiz, jlong documentPtr) { |
| 148 | FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr); |
| 149 | return FPDF_GetPageCount(document); |
| 150 | } |
| 151 | |
| 152 | static jboolean nativeScaleForPrinting(JNIEnv* env, jclass thiz, jlong documentPtr) { |
| 153 | FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr); |
| 154 | return FPDF_VIEWERREF_GetPrintScaling(document); |
| 155 | } |
| 156 | |
| 157 | static void DropContext(void* data) { |
| 158 | delete (CRenderContext*) data; |
| 159 | } |
| 160 | |
| 161 | static void renderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page, int destLeft, int destTop, |
| 162 | int destRight, int destBottom, SkMatrix* transform, int flags) { |
| 163 | // Note: this code ignores the currently unused RENDER_NO_NATIVETEXT, |
| 164 | // FPDF_RENDER_LIMITEDIMAGECACHE, FPDF_RENDER_FORCEHALFTONE, FPDF_GRAYSCALE, |
| 165 | // and FPDF_ANNOT flags. To add support for that refer to FPDF_RenderPage_Retail |
| 166 | // in fpdfview.cpp |
| 167 | |
| 168 | CRenderContext* pContext = FX_NEW CRenderContext; |
| 169 | |
| 170 | CPDF_Page* pPage = (CPDF_Page*) page; |
| 171 | pPage->SetPrivateData((void*) 1, pContext, DropContext); |
| 172 | |
| 173 | CFX_FxgeDevice* fxgeDevice = FX_NEW CFX_FxgeDevice; |
| 174 | pContext->m_pDevice = fxgeDevice; |
| 175 | |
| 176 | // Reverse the bytes (last argument TRUE) since the Android |
| 177 | // format is ARGB while the renderer uses BGRA internally. |
| 178 | fxgeDevice->Attach((CFX_DIBitmap*) bitmap, 0, TRUE); |
| 179 | |
| 180 | CPDF_RenderOptions* renderOptions = pContext->m_pOptions; |
| 181 | |
| 182 | if (!renderOptions) { |
| 183 | renderOptions = FX_NEW CPDF_RenderOptions; |
| 184 | pContext->m_pOptions = renderOptions; |
| 185 | } |
| 186 | |
| 187 | if (flags & FPDF_LCD_TEXT) { |
| 188 | renderOptions->m_Flags |= RENDER_CLEARTYPE; |
| 189 | } else { |
| 190 | renderOptions->m_Flags &= ~RENDER_CLEARTYPE; |
| 191 | } |
| 192 | |
| 193 | const CPDF_OCContext::UsageType usage = (flags & FPDF_PRINTING) |
| 194 | ? CPDF_OCContext::Print : CPDF_OCContext::View; |
| 195 | |
| 196 | renderOptions->m_AddFlags = flags >> 8; |
| 197 | renderOptions->m_pOCContext = new CPDF_OCContext(pPage->m_pDocument, usage); |
| 198 | |
| 199 | fxgeDevice->SaveState(); |
| 200 | |
| 201 | FX_RECT clip; |
| 202 | clip.left = destLeft; |
| 203 | clip.right = destRight; |
| 204 | clip.top = destTop; |
| 205 | clip.bottom = destBottom; |
| 206 | fxgeDevice->SetClip_Rect(&clip); |
| 207 | |
| 208 | CPDF_RenderContext* pageContext = FX_NEW CPDF_RenderContext; |
| 209 | pContext->m_pContext = pageContext; |
| 210 | pageContext->Create(pPage); |
| 211 | |
| 212 | CFX_AffineMatrix matrix; |
| 213 | if (!transform) { |
| 214 | pPage->GetDisplayMatrix(matrix, destLeft, destTop, destRight - destLeft, |
| 215 | destBottom - destTop, 0); |
| 216 | } else { |
| 217 | // PDF's coordinate system origin is left-bottom while |
| 218 | // in graphics it is the top-left, so remap the origin. |
| 219 | matrix.Set(1, 0, 0, -1, 0, pPage->GetPageHeight()); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 220 | |
| 221 | SkScalar transformValues[6]; |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 222 | if (transform->asAffine(transformValues)) { |
| 223 | matrix.Concat(transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY], |
| 224 | transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY], |
| 225 | transformValues[SkMatrix::kATransX], transformValues[SkMatrix::kATransY]); |
| 226 | } else { |
| 227 | // Already checked for a return value of false in the caller, so this should never |
| 228 | // happen. |
| 229 | ALOGE("Error rendering page!"); |
| 230 | } |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 231 | |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 232 | } |
| 233 | pageContext->AppendObjectList(pPage, &matrix); |
| 234 | |
| 235 | pContext->m_pRenderer = FX_NEW CPDF_ProgressiveRenderer; |
| 236 | pContext->m_pRenderer->Start(pageContext, fxgeDevice, renderOptions, NULL); |
| 237 | |
| 238 | fxgeDevice->RestoreState(); |
| 239 | |
| 240 | pPage->RemovePrivateData((void*) 1); |
| 241 | |
| 242 | delete pContext; |
| 243 | } |
| 244 | |
| 245 | static void nativeRenderPage(JNIEnv* env, jclass thiz, jlong documentPtr, jlong pagePtr, |
John Reck | edc22fb | 2015-04-20 22:06:31 +0000 | [diff] [blame] | 246 | jlong bitmapPtr, jint destLeft, jint destTop, jint destRight, jint destBottom, |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 247 | jlong matrixPtr, jint renderMode) { |
| 248 | |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 249 | FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr); |
John Reck | edc22fb | 2015-04-20 22:06:31 +0000 | [diff] [blame] | 250 | SkBitmap* skBitmap = reinterpret_cast<SkBitmap*>(bitmapPtr); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 251 | SkMatrix* skMatrix = reinterpret_cast<SkMatrix*>(matrixPtr); |
| 252 | |
John Reck | edc22fb | 2015-04-20 22:06:31 +0000 | [diff] [blame] | 253 | skBitmap->lockPixels(); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 254 | |
John Reck | edc22fb | 2015-04-20 22:06:31 +0000 | [diff] [blame] | 255 | const int stride = skBitmap->width() * 4; |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 256 | |
John Reck | edc22fb | 2015-04-20 22:06:31 +0000 | [diff] [blame] | 257 | FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(skBitmap->width(), skBitmap->height(), |
| 258 | FPDFBitmap_BGRA, skBitmap->getPixels(), stride); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 259 | |
| 260 | if (!bitmap) { |
| 261 | ALOGE("Erorr creating bitmap"); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | int renderFlags = 0; |
| 266 | if (renderMode == RENDER_MODE_FOR_DISPLAY) { |
| 267 | renderFlags |= FPDF_LCD_TEXT; |
| 268 | } else if (renderMode == RENDER_MODE_FOR_PRINT) { |
| 269 | renderFlags |= FPDF_PRINTING; |
| 270 | } |
| 271 | |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 272 | if (skMatrix && !skMatrix->asAffine(NULL)) { |
| 273 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 274 | "transform matrix has perspective. Only affine matrices are allowed."); |
| 275 | return; |
| 276 | } |
| 277 | |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 278 | renderPageBitmap(bitmap, page, destLeft, destTop, destRight, |
| 279 | destBottom, skMatrix, renderFlags); |
| 280 | |
John Reck | edc22fb | 2015-04-20 22:06:31 +0000 | [diff] [blame] | 281 | skBitmap->notifyPixelsChanged(); |
| 282 | skBitmap->unlockPixels(); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static JNINativeMethod gPdfRenderer_Methods[] = { |
| 286 | {"nativeCreate", "(IJ)J", (void*) nativeCreate}, |
| 287 | {"nativeClose", "(J)V", (void*) nativeClose}, |
| 288 | {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount}, |
| 289 | {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting}, |
John Reck | edc22fb | 2015-04-20 22:06:31 +0000 | [diff] [blame] | 290 | {"nativeRenderPage", "(JJJIIIIJI)V", (void*) nativeRenderPage}, |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 291 | {"nativeOpenPageAndGetSize", "(JILandroid/graphics/Point;)J", (void*) nativeOpenPageAndGetSize}, |
| 292 | {"nativeClosePage", "(J)V", (void*) nativeClosePage} |
| 293 | }; |
| 294 | |
| 295 | int register_android_graphics_pdf_PdfRenderer(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 296 | int result = RegisterMethodsOrDie( |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 297 | env, "android/graphics/pdf/PdfRenderer", gPdfRenderer_Methods, |
| 298 | NELEM(gPdfRenderer_Methods)); |
| 299 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 300 | jclass clazz = FindClassOrDie(env, "android/graphics/Point"); |
| 301 | gPointClassInfo.x = GetFieldIDOrDie(env, clazz, "x", "I"); |
| 302 | gPointClassInfo.y = GetFieldIDOrDie(env, clazz, "y", "I"); |
Svetoslav | 2961769 | 2014-04-24 18:40:42 -0700 | [diff] [blame] | 303 | |
| 304 | return result; |
| 305 | }; |
| 306 | |
| 307 | }; |