This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void init(Handle <Object> exports, Handle<Object> module) { | |
NODE_SET_METHOD(exports, "start", start); | |
NODE_SET_METHOD(exports, "render", render); | |
} | |
NODE_MODULE(PDFRenderer, init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
NewerOlder