PdfRenderer.cpp revision 76f6a86de25e1bf74717e047e55fd44b089673f3
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
46static Mutex sLock;
47
48static int sUnmatchedInitRequestCount = 0;
49
50static void initializeLibraryIfNeeded() {
51    Mutex::Autolock _l(sLock);
52    if (sUnmatchedInitRequestCount == 0) {
53        FPDF_InitLibrary(NULL);
54    }
55    sUnmatchedInitRequestCount++;
56}
57
58static void destroyLibraryIfNeeded() {
59    Mutex::Autolock _l(sLock);
60    sUnmatchedInitRequestCount--;
61    if (sUnmatchedInitRequestCount == 0) {
62       FPDF_DestroyLibrary();
63    }
64}
65
66static 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
77static 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();
89        switch (error) {
90            case FPDF_ERR_PASSWORD:
91            case FPDF_ERR_SECURITY: {
92                jniThrowExceptionFmt(env, "java/lang/SecurityException",
93                        "cannot create document. Error: %ld", error);
94            } break;
95            default: {
96                jniThrowExceptionFmt(env, "java/io/IOException",
97                        "cannot create document. Error: %ld", error);
98            } break;
99        }
100        destroyLibraryIfNeeded();
101        return -1;
102    }
103
104    return reinterpret_cast<jlong>(document);
105}
106
107static 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
136static void nativeClosePage(JNIEnv* env, jclass thiz, jlong pagePtr) {
137    FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr);
138    FPDF_ClosePage(page);
139}
140
141static 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
147static jint nativeGetPageCount(JNIEnv* env, jclass thiz, jlong documentPtr) {
148    FPDF_DOCUMENT document = reinterpret_cast<FPDF_DOCUMENT>(documentPtr);
149    return FPDF_GetPageCount(document);
150}
151
152static 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
157static void DropContext(void* data) {
158    delete (CRenderContext*) data;
159}
160
161static 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());
220
221        SkScalar transformValues[6];
222        transform->asAffine(transformValues);
223
224        matrix.Concat(transformValues[SkMatrix::kAScaleX], transformValues[SkMatrix::kASkewY],
225                transformValues[SkMatrix::kASkewX], transformValues[SkMatrix::kAScaleY],
226                transformValues[SkMatrix::kATransX], transformValues[SkMatrix::kATransY]);
227    }
228    pageContext->AppendObjectList(pPage, &matrix);
229
230    pContext->m_pRenderer = FX_NEW CPDF_ProgressiveRenderer;
231    pContext->m_pRenderer->Start(pageContext, fxgeDevice, renderOptions, NULL);
232
233    fxgeDevice->RestoreState();
234
235    pPage->RemovePrivateData((void*) 1);
236
237    delete pContext;
238}
239
240static void nativeRenderPage(JNIEnv* env, jclass thiz, jlong documentPtr, jlong pagePtr,
241        jlong bitmapPtr, jint destLeft, jint destTop, jint destRight, jint destBottom,
242        jlong matrixPtr, jint renderMode) {
243
244    FPDF_PAGE page = reinterpret_cast<FPDF_PAGE>(pagePtr);
245    SkBitmap* skBitmap = reinterpret_cast<SkBitmap*>(bitmapPtr);
246    SkMatrix* skMatrix = reinterpret_cast<SkMatrix*>(matrixPtr);
247
248    skBitmap->lockPixels();
249
250    const int stride = skBitmap->width() * 4;
251
252    FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(skBitmap->width(), skBitmap->height(),
253            FPDFBitmap_BGRA, skBitmap->getPixels(), stride);
254
255    if (!bitmap) {
256        ALOGE("Erorr creating bitmap");
257        return;
258    }
259
260    int renderFlags = 0;
261    if (renderMode == RENDER_MODE_FOR_DISPLAY) {
262        renderFlags |= FPDF_LCD_TEXT;
263    } else if (renderMode == RENDER_MODE_FOR_PRINT) {
264        renderFlags |= FPDF_PRINTING;
265    }
266
267    renderPageBitmap(bitmap, page, destLeft, destTop, destRight,
268            destBottom, skMatrix, renderFlags);
269
270    skBitmap->notifyPixelsChanged();
271    skBitmap->unlockPixels();
272}
273
274static const JNINativeMethod gPdfRenderer_Methods[] = {
275    {"nativeCreate", "(IJ)J", (void*) nativeCreate},
276    {"nativeClose", "(J)V", (void*) nativeClose},
277    {"nativeGetPageCount", "(J)I", (void*) nativeGetPageCount},
278    {"nativeScaleForPrinting", "(J)Z", (void*) nativeScaleForPrinting},
279    {"nativeRenderPage", "(JJJIIIIJI)V", (void*) nativeRenderPage},
280    {"nativeOpenPageAndGetSize", "(JILandroid/graphics/Point;)J", (void*) nativeOpenPageAndGetSize},
281    {"nativeClosePage", "(J)V", (void*) nativeClosePage}
282};
283
284int register_android_graphics_pdf_PdfRenderer(JNIEnv* env) {
285    int result = RegisterMethodsOrDie(
286            env, "android/graphics/pdf/PdfRenderer", gPdfRenderer_Methods,
287            NELEM(gPdfRenderer_Methods));
288
289    jclass clazz = FindClassOrDie(env, "android/graphics/Point");
290    gPointClassInfo.x = GetFieldIDOrDie(env, clazz, "x", "I");
291    gPointClassInfo.y = GetFieldIDOrDie(env, clazz, "y", "I");
292
293    return result;
294};
295
296};
297