Skip to content

Instantly share code, notes, and snippets.

View grivescorbett's full-sized avatar

Gabe Rives-Corbett grivescorbett

  • Google
  • New York, NY
View GitHub Profile
void setup_printer(
GtkPageOrientation orientation,
gdouble top, gdouble bottom, gdouble left, gdouble right,
GtkUnit unit
);
uint8_t* render(
const uint8_t *html,
size_t html_length,
size_t *length // Out
static void uv_cb(uv_async_t *handle) {
auto p = (payload *)handle->data;
auto isolate = Isolate::GetCurrent();
HandleScope handle_scope(isolate);
const unsigned argc = 2;
Local<Value> argv[argc];
argv[0] = Null(isolate);
argv[1] = Nan::NewBuffer((char*)p->data, p->data_length).ToLocalChecked(); // This transfers ownership of p->data to Node
void done(uint8_t* data, size_t data_length) {
auto p = new payload();
p->data = data;
p->data_length = data_length;
p->func = &func;
p->work_ref = this;
handle->data = (void *)p;
uv_async_send(handle);
}
// Structure of this is borrowed from https://gist.github.com/matzoe/11082417
class WorkItem {
typedef struct payload {
v8_persistent_copyable_function *func;
WorkItem *work_ref;
uint8_t *data; // I own this but I will give it away later
size_t data_length;
} payload;
uv_async_t *handle; // I own this, play nice with libuv before freeing.
void init(Handle <Object> exports, Handle<Object> module) {
NODE_SET_METHOD(exports, "start", start);
NODE_SET_METHOD(exports, "render", render);
}
NODE_MODULE(PDFRenderer, init)
typedef Persistent<Function, CopyablePersistentTraits<Function>> v8_persistent_copyable_function;
void render(const FunctionCallbackInfo<Value>& args) {
auto isolate = args.GetIsolate();
// Arg 0 is our html in a buffer
// Arg 1 is our callback
auto func = v8_persistent_copyable_function(Isolate::GetCurrent(), Local<Function>::Cast(args[1]));
auto html = args[0]->ToObject();
auto html_data = node::Buffer::Data(html);
void start(const FunctionCallbackInfo<Value>& args) {
auto isolate = args.GetIsolate();
debug_print("Starting background thread\n");
int err = 0;
err = uv_mutex_init(&work_mutex);
if (err < 0) {
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Could not create uv mutex.")));
return;
}
std::queue<WorkItem*> work_queue;
uv_mutex_t work_mutex;
void worker_thread(void *arg) {
// TODO: This should be configurable from Node
setup_printer(
GTK_PAGE_ORIENTATION_PORTRAIT,
0.5, 0.5, 0.5, 0.5,
GTK_UNIT_INCH
);
uint8_t *render(const uint8_t *html, size_t html_length, size_t *length);
void setup_printer(GtkPageOrientation orientation, gdouble top, gdouble bottom, gdouble left, gdouble right, GtkUnit unit);
uint8_t *render(const uint8_t *html, size_t html_length, size_t *length) {
load_web_view(html, html_length);
gtk_main();
debug_print("done loop\n");
std::string filename;
do_print(filename);
debug_print("Sending filename %s back\n", filename.c_str());