Svetoslav Ganov | ff4adde5 | 2013-06-10 08:47:27 -0700 | [diff] [blame^] | 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 | |
| 28 | namespace android { |
| 29 | |
| 30 | static jint nativeCreateDocument(JNIEnv* env, jobject clazz) { |
| 31 | return reinterpret_cast<jint>(new SkPDFDocument()); |
| 32 | } |
| 33 | |
| 34 | static void nativeFinalize(JNIEnv* env, jobject thiz, jint documentPtr) { |
| 35 | delete reinterpret_cast<SkPDFDocument*>(documentPtr); |
| 36 | } |
| 37 | |
| 38 | static jint nativeCreatePage(JNIEnv* env, jobject thiz, |
| 39 | jobject pageSize, jobject contentSize, jint initialTransformation) { |
| 40 | SkIRect skPageSizeRect; |
| 41 | GraphicsJNI::jrect_to_irect(env, pageSize, &skPageSizeRect); |
| 42 | SkISize skPageSize = SkISize::Make(skPageSizeRect.width(), |
| 43 | skPageSizeRect.height()); |
| 44 | |
| 45 | SkIRect skContentRect; |
| 46 | GraphicsJNI::jrect_to_irect(env, contentSize, &skContentRect); |
| 47 | SkISize skContentSize = SkISize::Make(skContentRect.width(), |
| 48 | skContentRect.height()); |
| 49 | |
| 50 | SkMatrix* transformation = reinterpret_cast<SkMatrix*>(initialTransformation); |
| 51 | SkPDFDevice* skPdfDevice = new SkPDFDevice(skPageSize, skContentSize, *transformation); |
| 52 | |
| 53 | return reinterpret_cast<jint>(new SkCanvas(skPdfDevice)); |
| 54 | } |
| 55 | |
| 56 | static void nativeAppendPage(JNIEnv* env, jobject thiz, jint documentPtr, jint pagePtr) { |
| 57 | SkCanvas* page = reinterpret_cast<SkCanvas*>(pagePtr); |
| 58 | SkPDFDocument* document = reinterpret_cast<SkPDFDocument*>(documentPtr); |
| 59 | SkPDFDevice* device = static_cast<SkPDFDevice*>(page->getDevice()); |
| 60 | document->appendPage(device); |
| 61 | } |
| 62 | |
| 63 | static void nativeWriteTo(JNIEnv* env, jobject clazz, jint documentPtr, |
| 64 | jobject out, jbyteArray chunk) { |
| 65 | SkWStream* skWStream = CreateJavaOutputStreamAdaptor(env, out, chunk); |
| 66 | SkPDFDocument* document = reinterpret_cast<SkPDFDocument*>(documentPtr); |
| 67 | document->emitPDF(skWStream); |
| 68 | delete skWStream; |
| 69 | } |
| 70 | |
| 71 | static JNINativeMethod gPdfDocument_Methods[] = { |
| 72 | {"nativeCreateDocument", "()I", (void*) nativeCreateDocument}, |
| 73 | {"nativeFinalize", "(I)V", (void*) nativeFinalize}, |
| 74 | {"nativeCreatePage", "(Landroid/graphics/Rect;Landroid/graphics/Rect;I)I", |
| 75 | (void*) nativeCreatePage}, |
| 76 | {"nativeAppendPage", "(II)V", (void*) nativeAppendPage}, |
| 77 | {"nativeWriteTo", "(ILjava/io/OutputStream;[B)V", (void*) nativeWriteTo} |
| 78 | }; |
| 79 | |
| 80 | int register_android_print_pdf_PdfDocument(JNIEnv* env) { |
| 81 | int result = android::AndroidRuntime::registerNativeMethods( |
| 82 | env, "android/print/pdf/PdfDocument", gPdfDocument_Methods, |
| 83 | NELEM(gPdfDocument_Methods)); |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | }; |