PdfDocument.cpp revision 6811f4e92cbb64e72a0d13eb9b99b5894bd59c76
1/*
2 * Copyright (C) 2013 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 "GraphicsJNI.h"
19#include <android_runtime/AndroidRuntime.h>
20
21#include "SkCanvas.h"
22#include "SkPDFDevice.h"
23#include "SkPDFDocument.h"
24#include "SkRect.h"
25#include "SkSize.h"
26#include "CreateJavaOutputStreamAdaptor.h"
27#include "JNIHelp.h"
28
29namespace android {
30
31#define LOGD(x...) do { Log::Instance()->printf(Log::ELogD, x); } while(0)
32
33static jint nativeCreateDocument(JNIEnv* env, jobject clazz) {
34    return reinterpret_cast<jint>(new SkPDFDocument());
35}
36
37static void nativeFinalize(JNIEnv* env, jobject thiz, jint documentPtr) {
38    delete reinterpret_cast<SkPDFDocument*>(documentPtr);
39}
40
41static jint nativeCreatePage(JNIEnv* env, jobject thiz, jint pageWidth, jint pageHeight,
42        jint contentLeft, jint contentTop, jint contentRight, jint contentBottom) {
43
44    SkMatrix transformation;
45    transformation.setTranslate(contentLeft, contentTop);
46
47    SkISize skPageSize = SkISize::Make(pageWidth, pageHeight);
48    SkISize skContentSize = SkISize::Make(contentRight - contentLeft, contentBottom - contentTop);
49
50    SkPDFDevice* skPdfDevice = new SkPDFDevice(skPageSize, skContentSize, transformation);
51    return reinterpret_cast<jint>(new SkCanvas(skPdfDevice));
52}
53
54static void nativeAppendPage(JNIEnv* env, jobject thiz, jint documentPtr, jint pagePtr) {
55    SkCanvas* page = reinterpret_cast<SkCanvas*>(pagePtr);
56    SkPDFDocument* document = reinterpret_cast<SkPDFDocument*>(documentPtr);
57    SkPDFDevice* device = static_cast<SkPDFDevice*>(page->getDevice());
58    document->appendPage(device);
59}
60
61static void nativeWriteTo(JNIEnv* env, jobject clazz, jint documentPtr,
62        jobject out, jbyteArray chunk) {
63    SkWStream* skWStream = CreateJavaOutputStreamAdaptor(env, out, chunk);
64    SkPDFDocument* document = reinterpret_cast<SkPDFDocument*>(documentPtr);
65    document->emitPDF(skWStream);
66    delete skWStream;
67}
68
69static JNINativeMethod gPdfDocument_Methods[] = {
70    {"nativeCreateDocument", "()I", (void*) nativeCreateDocument},
71    {"nativeFinalize", "(I)V", (void*) nativeFinalize},
72    {"nativeCreatePage", "(IIIIII)I",
73            (void*) nativeCreatePage},
74    {"nativeAppendPage", "(II)V", (void*) nativeAppendPage},
75    {"nativeWriteTo", "(ILjava/io/OutputStream;[B)V", (void*) nativeWriteTo}
76};
77
78int register_android_graphics_pdf_PdfDocument(JNIEnv* env) {
79    int result = android::AndroidRuntime::registerNativeMethods(
80            env, "android/graphics/pdf/PdfDocument", gPdfDocument_Methods,
81            NELEM(gPdfDocument_Methods));
82    return result;
83}
84
85};
86