blob: e2dc52b70fb659edf871a89ebff9c54af94d7288 [file] [log] [blame]
Svetoslav Ganovff4adde52013-06-10 08:47:27 -07001/*
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"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080019#include "core_jni_helpers.h"
Svetoslav35aacf22013-11-06 18:22:13 -080020#include <vector>
21
22#include "CreateJavaOutputStreamAdaptor.h"
Svetoslav Ganovff4adde52013-06-10 08:47:27 -070023
Svetoslav35aacf22013-11-06 18:22:13 -080024#include "SkDocument.h"
25#include "SkPicture.h"
Robert Phillipsb59508f2014-04-23 12:31:37 -040026#include "SkPictureRecorder.h"
Svetoslav35aacf22013-11-06 18:22:13 -080027#include "SkStream.h"
Svetoslav Ganovff4adde52013-06-10 08:47:27 -070028#include "SkRect.h"
Svetoslav Ganovff4adde52013-06-10 08:47:27 -070029
sergeyvdccca442016-03-21 15:38:21 -070030#include <hwui/Canvas.h>
31
Svetoslav Ganovff4adde52013-06-10 08:47:27 -070032namespace android {
33
Svetoslav35aacf22013-11-06 18:22:13 -080034struct PageRecord {
Svetoslav6811f4e2013-09-18 15:58:28 -070035
Svetoslav35aacf22013-11-06 18:22:13 -080036 PageRecord(int width, int height, const SkRect& contentRect)
Robert Phillipsb59508f2014-04-23 12:31:37 -040037 : mPictureRecorder(new SkPictureRecorder())
38 , mPicture(NULL)
39 , mWidth(width)
40 , mHeight(height) {
Svetoslav35aacf22013-11-06 18:22:13 -080041 mContentRect = contentRect;
42 }
43
44 ~PageRecord() {
Robert Phillipsb59508f2014-04-23 12:31:37 -040045 delete mPictureRecorder;
46 if (NULL != mPicture) {
47 mPicture->unref();
48 }
Svetoslav35aacf22013-11-06 18:22:13 -080049 }
50
Robert Phillipsb59508f2014-04-23 12:31:37 -040051 SkPictureRecorder* mPictureRecorder;
52 SkPicture* mPicture;
Svetoslav35aacf22013-11-06 18:22:13 -080053 const int mWidth;
54 const int mHeight;
55 SkRect mContentRect;
56};
57
58class PdfDocument {
59public:
60 PdfDocument() {
61 mCurrentPage = NULL;
62 }
63
64 SkCanvas* startPage(int width, int height,
65 int contentLeft, int contentTop, int contentRight, int contentBottom) {
66 assert(mCurrentPage == NULL);
67
68 SkRect contentRect = SkRect::MakeLTRB(
69 contentLeft, contentTop, contentRight, contentBottom);
70 PageRecord* page = new PageRecord(width, height, contentRect);
71 mPages.push_back(page);
72 mCurrentPage = page;
73
Robert Phillipsb59508f2014-04-23 12:31:37 -040074 SkCanvas* canvas = page->mPictureRecorder->beginRecording(
Mike Reed71487eb2014-11-19 16:13:20 -050075 SkRect::MakeWH(contentRect.width(), contentRect.height()));
Svetoslav35aacf22013-11-06 18:22:13 -080076
Svetoslav35aacf22013-11-06 18:22:13 -080077 return canvas;
78 }
79
80 void finishPage() {
81 assert(mCurrentPage != NULL);
Robert Phillipsb59508f2014-04-23 12:31:37 -040082 assert(mCurrentPage->mPictureRecorder != NULL);
83 assert(mCurrentPage->mPicture == NULL);
Mike Reed260ab722016-10-07 15:59:20 -040084 mCurrentPage->mPicture = mCurrentPage->mPictureRecorder->finishRecordingAsPicture().release();
Robert Phillipsb59508f2014-04-23 12:31:37 -040085 delete mCurrentPage->mPictureRecorder;
86 mCurrentPage->mPictureRecorder = NULL;
Svetoslav35aacf22013-11-06 18:22:13 -080087 mCurrentPage = NULL;
88 }
89
90 void write(SkWStream* stream) {
Hal Canary4cb7bb52016-05-02 15:27:51 -040091 sk_sp<SkDocument> document = SkDocument::MakePDF(stream);
Svetoslav35aacf22013-11-06 18:22:13 -080092 for (unsigned i = 0; i < mPages.size(); i++) {
93 PageRecord* page = mPages[i];
94
95 SkCanvas* canvas = document->beginPage(page->mWidth, page->mHeight,
96 &(page->mContentRect));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040097 canvas->drawPicture(page->mPicture);
Svetoslav35aacf22013-11-06 18:22:13 -080098
99 document->endPage();
100 }
101 document->close();
102 }
103
104 void close() {
Robert Phillipsb59508f2014-04-23 12:31:37 -0400105 assert(NULL == mCurrentPage);
Svetoslav35aacf22013-11-06 18:22:13 -0800106 for (unsigned i = 0; i < mPages.size(); i++) {
107 delete mPages[i];
108 }
Svetoslav35aacf22013-11-06 18:22:13 -0800109 }
110
111private:
112 ~PdfDocument() {
113 close();
114 }
115
116 std::vector<PageRecord*> mPages;
117 PageRecord* mCurrentPage;
118};
119
Ashok Bhatcdf34462014-01-23 15:29:55 +0000120static jlong nativeCreateDocument(JNIEnv* env, jobject thiz) {
121 return reinterpret_cast<jlong>(new PdfDocument());
Svetoslav Ganovff4adde52013-06-10 08:47:27 -0700122}
123
Ashok Bhatcdf34462014-01-23 15:29:55 +0000124static jlong nativeStartPage(JNIEnv* env, jobject thiz, jlong documentPtr,
Svetoslav35aacf22013-11-06 18:22:13 -0800125 jint pageWidth, jint pageHeight,
Svetoslav6811f4e2013-09-18 15:58:28 -0700126 jint contentLeft, jint contentTop, jint contentRight, jint contentBottom) {
Svetoslav35aacf22013-11-06 18:22:13 -0800127 PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400128 SkCanvas* canvas = document->startPage(pageWidth, pageHeight,
129 contentLeft, contentTop, contentRight, contentBottom);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400130 return reinterpret_cast<jlong>(Canvas::create_canvas(canvas));
Svetoslav Ganovff4adde52013-06-10 08:47:27 -0700131}
132
Ashok Bhatcdf34462014-01-23 15:29:55 +0000133static void nativeFinishPage(JNIEnv* env, jobject thiz, jlong documentPtr) {
Svetoslav35aacf22013-11-06 18:22:13 -0800134 PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr);
135 document->finishPage();
Svetoslav Ganovff4adde52013-06-10 08:47:27 -0700136}
137
Ashok Bhatcdf34462014-01-23 15:29:55 +0000138static void nativeWriteTo(JNIEnv* env, jobject thiz, jlong documentPtr, jobject out,
Svetoslav35aacf22013-11-06 18:22:13 -0800139 jbyteArray chunk) {
140 PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr);
Svetoslav Ganovff4adde52013-06-10 08:47:27 -0700141 SkWStream* skWStream = CreateJavaOutputStreamAdaptor(env, out, chunk);
Svetoslav35aacf22013-11-06 18:22:13 -0800142 document->write(skWStream);
Svetoslav Ganovff4adde52013-06-10 08:47:27 -0700143 delete skWStream;
144}
145
Ashok Bhatcdf34462014-01-23 15:29:55 +0000146static void nativeClose(JNIEnv* env, jobject thiz, jlong documentPtr) {
Svetoslav35aacf22013-11-06 18:22:13 -0800147 PdfDocument* document = reinterpret_cast<PdfDocument*>(documentPtr);
148 document->close();
149}
150
Daniel Micay76f6a862015-09-19 17:31:01 -0400151static const JNINativeMethod gPdfDocument_Methods[] = {
Ashok Bhatcdf34462014-01-23 15:29:55 +0000152 {"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}
Svetoslav Ganovff4adde52013-06-10 08:47:27 -0700157};
158
Svetoslav6811f4e2013-09-18 15:58:28 -0700159int register_android_graphics_pdf_PdfDocument(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800160 return RegisterMethodsOrDie(
Svetoslav6811f4e2013-09-18 15:58:28 -0700161 env, "android/graphics/pdf/PdfDocument", gPdfDocument_Methods,
Svetoslav Ganovff4adde52013-06-10 08:47:27 -0700162 NELEM(gPdfDocument_Methods));
Svetoslav Ganovff4adde52013-06-10 08:47:27 -0700163}
164
165};