PdfDocument.cpp revision 7c90661a6f36b98fd86757c8206c67c8bf71669d
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 "core_jni_helpers.h" 20#include <vector> 21 22#include "Canvas.h" 23#include "CreateJavaOutputStreamAdaptor.h" 24 25#include "SkDocument.h" 26#include "SkPicture.h" 27#include "SkPictureRecorder.h" 28#include "SkStream.h" 29#include "SkRect.h" 30 31namespace android { 32 33struct PageRecord { 34 35 PageRecord(int width, int height, const SkRect& contentRect) 36 : mPictureRecorder(new SkPictureRecorder()) 37 , mPicture(NULL) 38 , mWidth(width) 39 , mHeight(height) { 40 mContentRect = contentRect; 41 } 42 43 ~PageRecord() { 44 delete mPictureRecorder; 45 if (NULL != mPicture) { 46 mPicture->unref(); 47 } 48 } 49 50 SkPictureRecorder* mPictureRecorder; 51 SkPicture* mPicture; 52 const int mWidth; 53 const int mHeight; 54 SkRect mContentRect; 55}; 56 57class PdfDocument { 58public: 59 PdfDocument() { 60 mCurrentPage = NULL; 61 } 62 63 SkCanvas* startPage(int width, int height, 64 int contentLeft, int contentTop, int contentRight, int contentBottom) { 65 assert(mCurrentPage == NULL); 66 67 SkRect contentRect = SkRect::MakeLTRB( 68 contentLeft, contentTop, contentRight, contentBottom); 69 PageRecord* page = new PageRecord(width, height, contentRect); 70 mPages.push_back(page); 71 mCurrentPage = page; 72 73 SkCanvas* canvas = page->mPictureRecorder->beginRecording( 74 SkRect::MakeWH(contentRect.width(), contentRect.height())); 75 76 return canvas; 77 } 78 79 void finishPage() { 80 assert(mCurrentPage != NULL); 81 assert(mCurrentPage->mPictureRecorder != NULL); 82 assert(mCurrentPage->mPicture == NULL); 83 mCurrentPage->mPicture = mCurrentPage->mPictureRecorder->endRecording(); 84 delete mCurrentPage->mPictureRecorder; 85 mCurrentPage->mPictureRecorder = NULL; 86 mCurrentPage = NULL; 87 } 88 89 void write(SkWStream* stream) { 90 SkDocument* document = SkDocument::CreatePDF(stream); 91 for (unsigned i = 0; i < mPages.size(); i++) { 92 PageRecord* page = mPages[i]; 93 94 SkCanvas* canvas = document->beginPage(page->mWidth, page->mHeight, 95 &(page->mContentRect)); 96 97 canvas->drawPicture(page->mPicture); 98 99 document->endPage(); 100 } 101 document->close(); 102 } 103 104 void close() { 105 assert(NULL == mCurrentPage); 106 for (unsigned i = 0; i < mPages.size(); i++) { 107 delete mPages[i]; 108 } 109 } 110 111private: 112 ~PdfDocument() { 113 close(); 114 } 115 116 std::vector<PageRecord*> mPages; 117 PageRecord* mCurrentPage; 118}; 119 120static jlong nativeCreateDocument(JNIEnv* env, jobject thiz) { 121 return reinterpret_cast<jlong>(new PdfDocument()); 122} 123 124static jlong nativeStartPage(JNIEnv* env, jobject thiz, jlong documentPtr, 125 jint pageWidth, jint pageHeight, 126 jint contentLeft, jint contentTop, jint contentRight, jint contentBottom) { 127 PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr); 128 SkCanvas* canvas = document->startPage(pageWidth, pageHeight, 129 contentLeft, contentTop, contentRight, contentBottom); 130 return reinterpret_cast<jlong>(Canvas::create_canvas(canvas)); 131} 132 133static void nativeFinishPage(JNIEnv* env, jobject thiz, jlong documentPtr) { 134 PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr); 135 document->finishPage(); 136} 137 138static void nativeWriteTo(JNIEnv* env, jobject thiz, jlong documentPtr, jobject out, 139 jbyteArray chunk) { 140 PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr); 141 SkWStream* skWStream = CreateJavaOutputStreamAdaptor(env, out, chunk); 142 document->write(skWStream); 143 delete skWStream; 144} 145 146static void nativeClose(JNIEnv* env, jobject thiz, jlong documentPtr) { 147 PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr); 148 document->close(); 149} 150 151static const JNINativeMethod gPdfDocument_Methods[] = { 152 {"nativeCreateDocument", "()J", (void*) nativeCreateDocument}, 153 {"nativeStartPage", "(JIIIIII)J", (void*) nativeStartPage}, 154 {"nativeFinishPage", "(J)V", (void*) nativeFinishPage}, 155 {"nativeWriteTo", "(JLjava/io/OutputStream;[B)V", (void*) nativeWriteTo}, 156 {"nativeClose", "(J)V", (void*) nativeClose} 157}; 158 159int register_android_graphics_pdf_PdfDocument(JNIEnv* env) { 160 return RegisterMethodsOrDie( 161 env, "android/graphics/pdf/PdfDocument", gPdfDocument_Methods, 162 NELEM(gPdfDocument_Methods)); 163} 164 165}; 166