|
17 | 17 | #include "jni.h" |
18 | 18 | #include "GraphicsJNI.h" |
19 | 19 | #include <android_runtime/AndroidRuntime.h> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include "CreateJavaOutputStreamAdaptor.h" |
20 | 23 |
|
21 | 24 | #include "SkCanvas.h" |
22 | | -#include "SkPDFDevice.h" |
23 | | -#include "SkPDFDocument.h" |
| 25 | +#include "SkDocument.h" |
| 26 | +#include "SkPicture.h" |
| 27 | +#include "SkStream.h" |
24 | 28 | #include "SkRect.h" |
25 | | -#include "SkSize.h" |
26 | | -#include "CreateJavaOutputStreamAdaptor.h" |
27 | | -#include "JNIHelp.h" |
28 | 29 |
|
29 | 30 | namespace android { |
30 | 31 |
|
31 | | -#define LOGD(x...) do { Log::Instance()->printf(Log::ELogD, x); } while(0) |
| 32 | +struct PageRecord { |
32 | 33 |
|
33 | | -static jint nativeCreateDocument(JNIEnv* env, jobject clazz) { |
34 | | - return reinterpret_cast<jint>(new SkPDFDocument()); |
35 | | -} |
| 34 | + PageRecord(int width, int height, const SkRect& contentRect) |
| 35 | + : mPicture(new SkPicture()), mWidth(width), mHeight(height) { |
| 36 | + mContentRect = contentRect; |
| 37 | + } |
36 | 38 |
|
37 | | -static void nativeFinalize(JNIEnv* env, jobject thiz, jint documentPtr) { |
38 | | - delete reinterpret_cast<SkPDFDocument*>(documentPtr); |
39 | | -} |
| 39 | + ~PageRecord() { |
| 40 | + mPicture->unref(); |
| 41 | + } |
40 | 42 |
|
41 | | -static jint nativeCreatePage(JNIEnv* env, jobject thiz, jint pageWidth, jint pageHeight, |
42 | | - jint contentLeft, jint contentTop, jint contentRight, jint contentBottom) { |
| 43 | + SkPicture* const mPicture; |
| 44 | + const int mWidth; |
| 45 | + const int mHeight; |
| 46 | + SkRect mContentRect; |
| 47 | +}; |
| 48 | + |
| 49 | +class PdfDocument { |
| 50 | +public: |
| 51 | + PdfDocument() { |
| 52 | + mCurrentPage = NULL; |
| 53 | + } |
| 54 | + |
| 55 | + SkCanvas* startPage(int width, int height, |
| 56 | + int contentLeft, int contentTop, int contentRight, int contentBottom) { |
| 57 | + assert(mCurrentPage == NULL); |
| 58 | + |
| 59 | + SkRect contentRect = SkRect::MakeLTRB( |
| 60 | + contentLeft, contentTop, contentRight, contentBottom); |
| 61 | + PageRecord* page = new PageRecord(width, height, contentRect); |
| 62 | + mPages.push_back(page); |
| 63 | + mCurrentPage = page; |
| 64 | + |
| 65 | + SkCanvas* canvas = page->mPicture->beginRecording( |
| 66 | + contentRect.width(), contentRect.height(), 0); |
| 67 | + |
| 68 | + // We pass this canvas to Java where it is used to construct |
| 69 | + // a Java Canvas object which dereferences the pointer when it |
| 70 | + // is destroyed, so we have to bump up the reference count. |
| 71 | + canvas->ref(); |
| 72 | + |
| 73 | + return canvas; |
| 74 | + } |
43 | 75 |
|
44 | | - SkMatrix transformation; |
45 | | - transformation.setTranslate(contentLeft, contentTop); |
| 76 | + void finishPage() { |
| 77 | + assert(mCurrentPage != NULL); |
| 78 | + mCurrentPage->mPicture->endRecording(); |
| 79 | + mCurrentPage = NULL; |
| 80 | + } |
46 | 81 |
|
47 | | - SkISize skPageSize = SkISize::Make(pageWidth, pageHeight); |
48 | | - SkISize skContentSize = SkISize::Make(contentRight - contentLeft, contentBottom - contentTop); |
| 82 | + void write(SkWStream* stream) { |
| 83 | + SkDocument* document = SkDocument::CreatePDF(stream); |
| 84 | + for (unsigned i = 0; i < mPages.size(); i++) { |
| 85 | + PageRecord* page = mPages[i]; |
49 | 86 |
|
50 | | - SkPDFDevice* skPdfDevice = new SkPDFDevice(skPageSize, skContentSize, transformation); |
51 | | - return reinterpret_cast<jint>(new SkCanvas(skPdfDevice)); |
| 87 | + SkCanvas* canvas = document->beginPage(page->mWidth, page->mHeight, |
| 88 | + &(page->mContentRect)); |
| 89 | + |
| 90 | + canvas->clipRect(page->mContentRect); |
| 91 | + canvas->translate(page->mContentRect.left(), page->mContentRect.top()); |
| 92 | + canvas->drawPicture(*page->mPicture); |
| 93 | + |
| 94 | + document->endPage(); |
| 95 | + } |
| 96 | + document->close(); |
| 97 | + } |
| 98 | + |
| 99 | + void close() { |
| 100 | + for (unsigned i = 0; i < mPages.size(); i++) { |
| 101 | + delete mPages[i]; |
| 102 | + } |
| 103 | + delete mCurrentPage; |
| 104 | + mCurrentPage = NULL; |
| 105 | + } |
| 106 | + |
| 107 | +private: |
| 108 | + ~PdfDocument() { |
| 109 | + close(); |
| 110 | + } |
| 111 | + |
| 112 | + std::vector<PageRecord*> mPages; |
| 113 | + PageRecord* mCurrentPage; |
| 114 | +}; |
| 115 | + |
| 116 | +static jint nativeCreateDocument(JNIEnv* env, jobject thiz) { |
| 117 | + return reinterpret_cast<jint>(new PdfDocument()); |
| 118 | +} |
| 119 | + |
| 120 | +static jint nativeStartPage(JNIEnv* env, jobject thiz, jint documentPtr, |
| 121 | + jint pageWidth, jint pageHeight, |
| 122 | + jint contentLeft, jint contentTop, jint contentRight, jint contentBottom) { |
| 123 | + PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr); |
| 124 | + return reinterpret_cast<jint>(document->startPage(pageWidth, pageHeight, |
| 125 | + contentLeft, contentTop, contentRight, contentBottom)); |
52 | 126 | } |
53 | 127 |
|
54 | | -static 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); |
| 128 | +static void nativeFinishPage(JNIEnv* env, jobject thiz, jint documentPtr) { |
| 129 | + PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr); |
| 130 | + document->finishPage(); |
59 | 131 | } |
60 | 132 |
|
61 | | -static void nativeWriteTo(JNIEnv* env, jobject clazz, jint documentPtr, |
62 | | - jobject out, jbyteArray chunk) { |
| 133 | +static void nativeWriteTo(JNIEnv* env, jobject thiz, jint documentPtr, jobject out, |
| 134 | + jbyteArray chunk) { |
| 135 | + PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr); |
63 | 136 | SkWStream* skWStream = CreateJavaOutputStreamAdaptor(env, out, chunk); |
64 | | - SkPDFDocument* document = reinterpret_cast<SkPDFDocument*>(documentPtr); |
65 | | - document->emitPDF(skWStream); |
| 137 | + document->write(skWStream); |
66 | 138 | delete skWStream; |
67 | 139 | } |
68 | 140 |
|
| 141 | +static void nativeClose(JNIEnv* env, jobject thiz, jint documentPtr) { |
| 142 | + PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr); |
| 143 | + document->close(); |
| 144 | +} |
| 145 | + |
69 | 146 | static JNINativeMethod gPdfDocument_Methods[] = { |
70 | 147 | {"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} |
| 148 | + {"nativeStartPage", "(IIIIIII)I", (void*) nativeStartPage}, |
| 149 | + {"nativeFinishPage", "(I)V", (void*) nativeFinishPage}, |
| 150 | + {"nativeWriteTo", "(ILjava/io/OutputStream;[B)V", (void*) nativeWriteTo}, |
| 151 | + {"nativeClose", "(I)V", (void*) nativeClose} |
76 | 152 | }; |
77 | 153 |
|
78 | 154 | int register_android_graphics_pdf_PdfDocument(JNIEnv* env) { |
|
0 commit comments