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