From 9d5e9d00f251ad381bc8ef793acb903fb5588ff2 Mon Sep 17 00:00:00 2001 From: Gopi Date: Mon, 12 Oct 2020 14:37:46 +0530 Subject: [PATCH 1/4] JNI test --- CMakeLists.txt | 3 +- examples/jogl/build.gradle | 4 +- src/main/cc/pdf/Document.cc | 47 +++++++++++++++++ .../org/jetbrains/skija/pdf/Document.java | 50 +++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 src/main/cc/pdf/Document.cc create mode 100644 src/main/java/org/jetbrains/skija/pdf/Document.java diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d72cae3..8427a255 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,9 @@ endif() file(GLOB SOURCES ${CMAKE_SOURCE_DIR}/src/main/cc/*.cc) file(GLOB IMPL_SOURCES ${CMAKE_SOURCE_DIR}/src/main/cc/impl/*.cc) file(GLOB PARAGRAPH_SOURCES ${CMAKE_SOURCE_DIR}/src/main/cc/paragraph/*.cc) +file(GLOB PDF_SOURCES ${CMAKE_SOURCE_DIR}/src/main/cc/pdf/*.cc) add_definitions(-DSK_GL) -add_library(skija SHARED ${SOURCES} ${IMPL_SOURCES} ${PARAGRAPH_SOURCES}) +add_library(skija SHARED ${SOURCES} ${IMPL_SOURCES} ${PARAGRAPH_SOURCES} ${PDF_SOURCES}) # JNI if(APPLE) diff --git a/examples/jogl/build.gradle b/examples/jogl/build.gradle index 4b773a2f..61909a3e 100644 --- a/examples/jogl/build.gradle +++ b/examples/jogl/build.gradle @@ -36,8 +36,8 @@ kotlin { implementation kotlin('stdlib-jdk8') api files("$joglDir/gluegen-rt.jar") api files("$joglDir/jogl-all.jar") - api "org.jetbrains.skija:skija:0.0.0-local" - api "org.jetbrains.skija:skija:0.0.0-local:natives-mac-x64" + api "org.jetbrains.skija:skija-platform:0.0.0-SNAPSHOT" + //api "org.jetbrains.skija:skija-platform:0.0.0-SNAPSHOT:natives-mac-x64" implementation files("$joglDir/gluegen-rt-natives-macosx-universal.jar") implementation files("$joglDir/jogl-all-natives-macosx-universal.jar") diff --git a/src/main/cc/pdf/Document.cc b/src/main/cc/pdf/Document.cc new file mode 100644 index 00000000..ad567564 --- /dev/null +++ b/src/main/cc/pdf/Document.cc @@ -0,0 +1,47 @@ +#include +#include +#include "include/docs/SkPDFDocument.h" +#include "SkDocument.h" +#include "SkCanvas.h" +#include "SkStream.h" +#include "SkRect.h" + +/*static void deleteDocument(SkDocument* pr) { + // std::cout << "Deleting [SkPictureRecorder " << PictureRecorder << "]" << std::endl; + delete pr; +}*/ + +extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_pdf_Document__1nMake(JNIEnv* env, jclass jclass) { + SkPDF::Metadata metadata; + metadata.fTitle = "documentTitle"; + metadata.fCreator = "Example WritePDF() Function"; + metadata.fCreation = {0, 2019, 1, 4, 31, 12, 34, 56}; + metadata.fModified = {0, 2019, 1, 4, 31, 12, 34, 56}; + SkFILEWStream out("~/Downloads/1.pdf"); + //SkFont* obj = new SkFont(sk_sp(SkRef(typeface))); + sk_sp pdfDocument = SkPDF::MakeDocument(&out, metadata); + return reinterpret_cast(&pdfDocument); +} + +/*extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_pdf_Document__1nGetFinalizer + (JNIEnv* env, jclass jclass) { + return static_cast(reinterpret_cast(&deleteDocument)); +}*/ + +extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_pdf_Document__1nBeginPage + (JNIEnv* env, jclass jclass, jlong ptr, jfloat width, jfloat height, jfloat left, jfloat top, jfloat right, jfloat bottom) { + SkDocument* instance = reinterpret_cast(static_cast(ptr)); + SkRect contentRect = SkRect::MakeLTRB(left, top, right, bottom); + SkCanvas* canvas = instance->beginPage(width, height, &contentRect); + return reinterpret_cast(canvas); +} + +extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nEndPage(JNIEnv* env, jclass jclass, jlong ptr) { + SkDocument* instance = reinterpret_cast(static_cast(ptr)); + instance->endPage(); +} +extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nClosePage(JNIEnv* env, jclass jclass, jlong ptr) { + SkDocument* instance = reinterpret_cast(static_cast(ptr)); + instance->close(); +} + diff --git a/src/main/java/org/jetbrains/skija/pdf/Document.java b/src/main/java/org/jetbrains/skija/pdf/Document.java new file mode 100644 index 00000000..ae60c6c8 --- /dev/null +++ b/src/main/java/org/jetbrains/skija/pdf/Document.java @@ -0,0 +1,50 @@ +package org.jetbrains.skija.pdf; + +import org.jetbrains.skija.Canvas; +import org.jetbrains.skija.impl.Native; +import org.jetbrains.skija.impl.Stats; + +public class Document extends Native { + + public Document(long ptr) { + super(ptr/*, _FinalizerHolder.PTR*/); + } + + public static Document createPDF(String path) { + Stats.onNativeCall(); + return new Document(_nMake()); + } + + public static class _FinalizerHolder { + static { + Stats.onNativeCall(); + } + + //public static final long PTR = _nGetFinalizer(); + } + + public Canvas beginPage(float width, float height) { + Stats.onNativeCall(); + return new Canvas(_nBeginPage(_ptr, width, height)); + } + + public void endPage() { + Stats.onNativeCall(); + _nEndPage(_ptr); + } + + public void close() { + Stats.onNativeCall(); + _nClose(_ptr); + } + + public static native long _nMake(); + + public static native long _nGetFinalizer(); + + public static native long _nBeginPage(long ptr, float width, float height); + + public static native void _nEndPage(long ptr); + + public static native void _nClose(long ptr); +} From f30fedfbe1af02b00649a626b758f58b548e9f27 Mon Sep 17 00:00:00 2001 From: Gopi Date: Wed, 21 Oct 2020 17:34:16 +0530 Subject: [PATCH 2/4] 1. binding for PDF --- src/main/cc/pdf/Document.cc | 116 ++++++++++++++---- .../org/jetbrains/skija/pdf/Document.java | 18 ++- 2 files changed, 102 insertions(+), 32 deletions(-) diff --git a/src/main/cc/pdf/Document.cc b/src/main/cc/pdf/Document.cc index ad567564..26faec84 100644 --- a/src/main/cc/pdf/Document.cc +++ b/src/main/cc/pdf/Document.cc @@ -1,47 +1,109 @@ #include #include #include "include/docs/SkPDFDocument.h" -#include "SkDocument.h" #include "SkCanvas.h" #include "SkStream.h" #include "SkRect.h" +#include "SkPictureRecorder.h" -/*static void deleteDocument(SkDocument* pr) { - // std::cout << "Deleting [SkPictureRecorder " << PictureRecorder << "]" << std::endl; - delete pr; -}*/ +struct PageRecord { + PageRecord(int width, int height, const SkRect& contentRect) + : mPictureRecorder(new SkPictureRecorder()) + , mPicture(NULL) + , mWidth(width) + , mHeight(height) { + mContentRect = contentRect; + } + ~PageRecord() { + delete mPictureRecorder; + if (NULL != mPicture) { + mPicture->unref(); + } + } + SkPictureRecorder* mPictureRecorder; + SkPicture* mPicture; + const int mWidth; + const int mHeight; + SkRect mContentRect; +}; + +class PdfDocument { +public: + PdfDocument() { + mCurrentPage = NULL; + } + SkCanvas* startPage(int width, int height, + int left, int top, int right, int bottom) { + assert(mCurrentPage == NULL); + SkRect contentRect = SkRect::MakeLTRB( + left, top, right, bottom); + PageRecord* page = new PageRecord(width, height, contentRect); + mPages.push_back(page); + mCurrentPage = page; + SkCanvas* canvas = page->mPictureRecorder->beginRecording( + SkRect::MakeWH(contentRect.width(), contentRect.height())); + return canvas; + } + void finishPage() { + assert(mCurrentPage != NULL); + assert(mCurrentPage->mPictureRecorder != NULL); + assert(mCurrentPage->mPicture == NULL); + mCurrentPage->mPicture = mCurrentPage->mPictureRecorder->finishRecordingAsPicture().release(); + delete mCurrentPage->mPictureRecorder; + mCurrentPage->mPictureRecorder = NULL; + mCurrentPage = NULL; + } + void write(SkWStream* stream) { + sk_sp document = SkPDF::MakeDocument(stream); + for (unsigned i = 0; i < mPages.size(); i++) { + PageRecord* page = mPages[i]; + SkCanvas* canvas = document->beginPage(page->mWidth, page->mHeight, + &(page->mContentRect)); + canvas->drawPicture(page->mPicture); + document->endPage(); + } + document->close(); + } + void close() { + assert(NULL == mCurrentPage); + for (unsigned i = 0; i < mPages.size(); i++) { + delete mPages[i]; + } + } +private: + ~PdfDocument() { + close(); + } + std::vector mPages; + PageRecord* mCurrentPage; +}; extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_pdf_Document__1nMake(JNIEnv* env, jclass jclass) { - SkPDF::Metadata metadata; - metadata.fTitle = "documentTitle"; - metadata.fCreator = "Example WritePDF() Function"; - metadata.fCreation = {0, 2019, 1, 4, 31, 12, 34, 56}; - metadata.fModified = {0, 2019, 1, 4, 31, 12, 34, 56}; - SkFILEWStream out("~/Downloads/1.pdf"); - //SkFont* obj = new SkFont(sk_sp(SkRef(typeface))); - sk_sp pdfDocument = SkPDF::MakeDocument(&out, metadata); - return reinterpret_cast(&pdfDocument); + return reinterpret_cast(new PdfDocument()); } -/*extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_pdf_Document__1nGetFinalizer - (JNIEnv* env, jclass jclass) { - return static_cast(reinterpret_cast(&deleteDocument)); -}*/ - extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_pdf_Document__1nBeginPage (JNIEnv* env, jclass jclass, jlong ptr, jfloat width, jfloat height, jfloat left, jfloat top, jfloat right, jfloat bottom) { - SkDocument* instance = reinterpret_cast(static_cast(ptr)); - SkRect contentRect = SkRect::MakeLTRB(left, top, right, bottom); - SkCanvas* canvas = instance->beginPage(width, height, &contentRect); + PdfDocument* instance = reinterpret_cast(static_cast(ptr)); + SkCanvas* canvas = instance->startPage(width, height, + left, top, right, bottom); return reinterpret_cast(canvas); } +extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nWrite(JNIEnv* env, jclass jclass, jlong ptr, jstring pathStr) { + PdfDocument* document = reinterpret_cast(ptr); + const char* path = env->GetStringUTFChars(pathStr, nullptr); + SkFILEWStream out(path); + env->ReleaseStringUTFChars(pathStr, path); + document->write(&out); +} + extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nEndPage(JNIEnv* env, jclass jclass, jlong ptr) { - SkDocument* instance = reinterpret_cast(static_cast(ptr)); - instance->endPage(); + PdfDocument* document = reinterpret_cast(ptr); + document->finishPage(); } -extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nClosePage(JNIEnv* env, jclass jclass, jlong ptr) { - SkDocument* instance = reinterpret_cast(static_cast(ptr)); - instance->close(); +extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nClose(JNIEnv* env, jclass jclass, jlong ptr) { + PdfDocument* document = reinterpret_cast(ptr); + document->close(); } diff --git a/src/main/java/org/jetbrains/skija/pdf/Document.java b/src/main/java/org/jetbrains/skija/pdf/Document.java index ae60c6c8..0e6c2f6c 100644 --- a/src/main/java/org/jetbrains/skija/pdf/Document.java +++ b/src/main/java/org/jetbrains/skija/pdf/Document.java @@ -7,12 +7,13 @@ public class Document extends Native { public Document(long ptr) { - super(ptr/*, _FinalizerHolder.PTR*/); + super(ptr); } - public static Document createPDF(String path) { + public static Document createPDF() { Stats.onNativeCall(); - return new Document(_nMake()); + long ptr = _nMake(); + return new Document(ptr); } public static class _FinalizerHolder { @@ -25,7 +26,7 @@ public static class _FinalizerHolder { public Canvas beginPage(float width, float height) { Stats.onNativeCall(); - return new Canvas(_nBeginPage(_ptr, width, height)); + return new Canvas(_nBeginPage(_ptr, width, height, 0f, 0f, width, height)); } public void endPage() { @@ -33,6 +34,11 @@ public void endPage() { _nEndPage(_ptr); } + public void write(String path) { + Stats.onNativeCall(); + _nWrite(_ptr, path); + } + public void close() { Stats.onNativeCall(); _nClose(_ptr); @@ -42,9 +48,11 @@ public void close() { public static native long _nGetFinalizer(); - public static native long _nBeginPage(long ptr, float width, float height); + public static native long _nBeginPage(long ptr, float width, float height, float left, float top, float right, float bottom); public static native void _nEndPage(long ptr); + public static native void _nWrite(long ptr, String path); + public static native void _nClose(long ptr); } From d4371a4047e962810f259f842771ac1c81071e5a Mon Sep 17 00:00:00 2001 From: Gopi Date: Thu, 22 Oct 2020 21:02:38 +0530 Subject: [PATCH 3/4] 1. Measuring text width is exposed from native font 2. Refactoring --- src/main/cc/Font.cc | 11 ++++++++ src/main/cc/pdf/Document.cc | 25 +++++++++---------- src/main/java/org/jetbrains/skija/Font.java | 11 ++++++++ .../org/jetbrains/skija/pdf/Document.java | 24 ++++++------------ 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/main/cc/Font.cc b/src/main/cc/Font.cc index bf3f26cb..763f23ab 100644 --- a/src/main/cc/Font.cc +++ b/src/main/cc/Font.cc @@ -253,6 +253,17 @@ extern "C" JNIEXPORT jobject JNICALL Java_org_jetbrains_skija_Font__1nMeasureTex return skija::Rect::fromSkRect(env, bounds); } +extern "C" JNIEXPORT jfloat JNICALL Java_org_jetbrains_skija_Font__1nMeasureTextWidth + (JNIEnv* env, jclass jclass, jlong ptr, jstring str, jlong paintPtr) { + SkFont* instance = reinterpret_cast(static_cast(ptr)); + SkPaint* paint = reinterpret_cast(static_cast(paintPtr)); + jsize len = env->GetStringLength(str); + const jchar* chars = env->GetStringChars(str, nullptr); + float width = instance->measureText(chars, len * sizeof(jchar), SkTextEncoding::kUTF16, nullptr, paint); + env->ReleaseStringChars(str, chars); + return width; +} + extern "C" JNIEXPORT jfloatArray JNICALL Java_org_jetbrains_skija_Font__1nGetWidths (JNIEnv* env, jclass jclass, jlong ptr, jshortArray glyphsArr) { SkFont* instance = reinterpret_cast(static_cast(ptr)); diff --git a/src/main/cc/pdf/Document.cc b/src/main/cc/pdf/Document.cc index 26faec84..e264a98b 100644 --- a/src/main/cc/pdf/Document.cc +++ b/src/main/cc/pdf/Document.cc @@ -27,16 +27,16 @@ struct PageRecord { SkRect mContentRect; }; -class PdfDocument { +class Document { public: - PdfDocument() { + Document() { mCurrentPage = NULL; } SkCanvas* startPage(int width, int height, - int left, int top, int right, int bottom) { + int contentLeft, int contentTop, int contentRight, int contentBottom) { assert(mCurrentPage == NULL); SkRect contentRect = SkRect::MakeLTRB( - left, top, right, bottom); + contentLeft, contentTop, contentRight, contentBottom); PageRecord* page = new PageRecord(width, height, contentRect); mPages.push_back(page); mCurrentPage = page; @@ -71,7 +71,7 @@ class PdfDocument { } } private: - ~PdfDocument() { + ~Document() { close(); } std::vector mPages; @@ -79,19 +79,18 @@ class PdfDocument { }; extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_pdf_Document__1nMake(JNIEnv* env, jclass jclass) { - return reinterpret_cast(new PdfDocument()); + return reinterpret_cast(new Document()); } extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_pdf_Document__1nBeginPage - (JNIEnv* env, jclass jclass, jlong ptr, jfloat width, jfloat height, jfloat left, jfloat top, jfloat right, jfloat bottom) { - PdfDocument* instance = reinterpret_cast(static_cast(ptr)); - SkCanvas* canvas = instance->startPage(width, height, - left, top, right, bottom); + (JNIEnv* env, jclass jclass, jlong ptr, jfloat width, jfloat height/*, jfloat left, jfloat top, jfloat right, jfloat bottom*/) { + Document* instance = reinterpret_cast(static_cast(ptr)); + SkCanvas* canvas = instance->startPage(width, height, 0, 0, width, height); return reinterpret_cast(canvas); } extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nWrite(JNIEnv* env, jclass jclass, jlong ptr, jstring pathStr) { - PdfDocument* document = reinterpret_cast(ptr); + Document* document = reinterpret_cast(ptr); const char* path = env->GetStringUTFChars(pathStr, nullptr); SkFILEWStream out(path); env->ReleaseStringUTFChars(pathStr, path); @@ -99,11 +98,11 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nWrite } extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nEndPage(JNIEnv* env, jclass jclass, jlong ptr) { - PdfDocument* document = reinterpret_cast(ptr); + Document* document = reinterpret_cast(ptr); document->finishPage(); } extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skija_pdf_Document__1nClose(JNIEnv* env, jclass jclass, jlong ptr) { - PdfDocument* document = reinterpret_cast(ptr); + Document* document = reinterpret_cast(ptr); document->close(); } diff --git a/src/main/java/org/jetbrains/skija/Font.java b/src/main/java/org/jetbrains/skija/Font.java index 80c0e791..141c2137 100644 --- a/src/main/java/org/jetbrains/skija/Font.java +++ b/src/main/java/org/jetbrains/skija/Font.java @@ -369,6 +369,16 @@ public Rect measureText(String s, Paint p) { return _nMeasureText(_ptr, s, Native.getPtr(p)); } + public float measureTextWidth(String s) { + Stats.onNativeCall(); + return measureTextWidth(s, null); + } + + public float measureTextWidth(String s, Paint p) { + Stats.onNativeCall(); + return _nMeasureTextWidth(_ptr, s, Native.getPtr(p)); + } + /** * Retrieves the advances for each glyph */ @@ -502,6 +512,7 @@ public TextBlob shape(String str, float width) { @ApiStatus.Internal public static native short[] _nGetUTF32GlyphIds(long ptr, int[] uni); @ApiStatus.Internal public static native int _nGetStringGlyphsCount(long ptr, String str); @ApiStatus.Internal public static native Rect _nMeasureText(long ptr, String str, long paintPtr); + @ApiStatus.Internal public static native float _nMeasureTextWidth(long ptr, String str, long paintPtr); @ApiStatus.Internal public static native float[] _nGetWidths(long ptr, short[] glyphs); @ApiStatus.Internal public static native Rect[] _nGetBounds(long ptr, short[] glyphs, long paintPtr); @ApiStatus.Internal public static native Point[] _nGetPositions(long ptr, short[] glyphs, float x, float y); diff --git a/src/main/java/org/jetbrains/skija/pdf/Document.java b/src/main/java/org/jetbrains/skija/pdf/Document.java index 0e6c2f6c..8971b6b6 100644 --- a/src/main/java/org/jetbrains/skija/pdf/Document.java +++ b/src/main/java/org/jetbrains/skija/pdf/Document.java @@ -5,28 +5,18 @@ import org.jetbrains.skija.impl.Stats; public class Document extends Native { - - public Document(long ptr) { - super(ptr); - } - - public static Document createPDF() { + public Document() { + this(_nMake()); Stats.onNativeCall(); - long ptr = _nMake(); - return new Document(ptr); } - public static class _FinalizerHolder { - static { - Stats.onNativeCall(); - } - - //public static final long PTR = _nGetFinalizer(); + public Document(long ptr) { + super(ptr); } public Canvas beginPage(float width, float height) { Stats.onNativeCall(); - return new Canvas(_nBeginPage(_ptr, width, height, 0f, 0f, width, height)); + return new Canvas(_nBeginPage(_ptr, width, height)); } public void endPage() { @@ -34,7 +24,7 @@ public void endPage() { _nEndPage(_ptr); } - public void write(String path) { + public void writeTo(String path) { Stats.onNativeCall(); _nWrite(_ptr, path); } @@ -48,7 +38,7 @@ public void close() { public static native long _nGetFinalizer(); - public static native long _nBeginPage(long ptr, float width, float height, float left, float top, float right, float bottom); + public static native long _nBeginPage(long ptr, float width, float height); public static native void _nEndPage(long ptr); From a3479ba26e1dec956486e19507c60e379482fce0 Mon Sep 17 00:00:00 2001 From: Gopi Date: Thu, 22 Oct 2020 21:09:05 +0530 Subject: [PATCH 4/4] 1. Reverted temporary changes. --- examples/jogl/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/jogl/build.gradle b/examples/jogl/build.gradle index 61909a3e..4b773a2f 100644 --- a/examples/jogl/build.gradle +++ b/examples/jogl/build.gradle @@ -36,8 +36,8 @@ kotlin { implementation kotlin('stdlib-jdk8') api files("$joglDir/gluegen-rt.jar") api files("$joglDir/jogl-all.jar") - api "org.jetbrains.skija:skija-platform:0.0.0-SNAPSHOT" - //api "org.jetbrains.skija:skija-platform:0.0.0-SNAPSHOT:natives-mac-x64" + api "org.jetbrains.skija:skija:0.0.0-local" + api "org.jetbrains.skija:skija:0.0.0-local:natives-mac-x64" implementation files("$joglDir/gluegen-rt-natives-macosx-universal.jar") implementation files("$joglDir/jogl-all-natives-macosx-universal.jar")