Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary Bindings for Skia's PDF Backend #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

sgopi93
Copy link

@sgopi93 sgopi93 commented Oct 22, 2020

  1. Binding for PDF module.
  2. Measuring text width is exposed from the native font.

@tonsky
Copy link
Collaborator

tonsky commented Oct 22, 2020

Thank you for submitting this PR.

Font measure looks fine.

About PDF bindings, I see Skija as a very thin binding around Skia, without adding extra logic or inventing new APIs. In that light your Document does too much. I would prefer if you just expose SkPDF and SkDocument APIs to the Java, and then people can build the same logic you’ve build in Document.cc, but using JVM APIs they already have (Pictures and PictureRecorders are already avaliable).

I understand that it might require some effort to actually create a PDF document using Skia-provided APIs and every user of it might need to go through the same problem, and you convinently wrote the code you think everyone will need for them. This is fine, but I prefer that to be solved in an external library, and Skija to only provide necessary Skia APIs.

Could you change your PR to address that? That would be awesome, and I am looking forward to merging your contributions.

Thanks!

@tonsky
Copy link
Collaborator

tonsky commented Oct 23, 2020

I just merged your Font.measureTextWidth in db62ccd. Please rebase

@sgopi93
Copy link
Author

sgopi93 commented Oct 29, 2020

I am tested covid positive, being away from the computer. will work on this as soon as possible.

@tonsky
Copy link
Collaborator

tonsky commented Oct 29, 2020

OMG hope you get better soon! Wish you strong health!

@Henny20
Copy link

Henny20 commented Nov 6, 2021

#include "SkCanvas.h"
#include "SkRect.h"
#include "SkStream.h"
#include "include/docs/SkPDFDocument.h"
#include <cassert>
#include <iomanip>
#include <iostream>
#include <jni.h>

class Document {
public:
  Document() {}
  Document(sk_sp<SkDocument> document) 
      : document_(document) {}
  SkCanvas *startPage(SkScalar wi, SkScalar he) {
    return document_->beginPage(wi, he);
  }
  sk_sp<SkDocument> create(SkWStream *stream) {
    document_ = SkPDF::MakeDocument(stream);
    return document_;
  }
  void endPage() { document_->endPage(); }
  void close() { document_->close(); }

private:
  ~Document() { }

  SkScalar width_;
  SkScalar height_;
  sk_sp<SkDocument> document_;
};

extern "C" JNIEXPORT jlong JNICALL
Java_org_jetbrains_skija_pdf_Document__1nMake(JNIEnv *env, jclass jclass,
                                              jlong wstreamPtr,
                                              jlong metadataPtr) {

  SkWStream *wstream =
      reinterpret_cast<SkWStream *>(static_cast<uintptr_t>(wstreamPtr));

  const SkPDF::Metadata *metadata =
      reinterpret_cast<SkPDF::Metadata *>(static_cast<uintptr_t>(metadataPtr));
  
  // auto document = new Document(nullptr);
  // auto document = new Document();
  // sk_sp<SkDocument> doc = document->create(wstream);

  sk_sp<SkDocument> doc = SkPDF::MakeDocument(wstream, *metadata);

  return reinterpret_cast<jlong>(new Document(doc));
}

extern "C" JNIEXPORT jlong JNICALL
Java_org_jetbrains_skija_pdf_Document__1nBeginPage(JNIEnv *env, jclass jclass,
                                                   jlong ptr, jfloat width,
                                                   jfloat height) {

  Document *instance = reinterpret_cast<Document *>(ptr);

  SkCanvas *canvas = instance->startPage(width, height);
  
  return reinterpret_cast<jlong>(canvas);
}

extern "C" JNIEXPORT void JNICALL
Java_org_jetbrains_skija_pdf_Document__1nEndPage(JNIEnv *env, jclass jclass,
                                                 jlong ptr) {

  Document *instance = reinterpret_cast<Document *>(ptr);
  instance->endPage();
}

extern "C" JNIEXPORT void JNICALL
Java_org_jetbrains_skija_pdf_Document__1nClose(JNIEnv *env, jclass jclass,
                                               jlong ptr) {
  Document *instance =
      reinterpret_cast<Document *>(static_cast<uintptr_t>(ptr));
  instance->close();
}

@JasonTheKitten
Copy link

JasonTheKitten commented Nov 29, 2021

@Henny20 Would you mind using the code block formatting feature, and also indenting? I can't read that as-is.

EDIT: Thank you for doing so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants