UNPKG

1.48 kBtext/x-cView Raw
1#include "SvgBackend.h"
2
3#include <cairo-svg.h>
4#include "../Canvas.h"
5#include "../closure.h"
6#include <cassert>
7
8using namespace v8;
9
10SvgBackend::SvgBackend(int width, int height)
11 : Backend("svg", width, height) {
12 createSurface();
13}
14
15SvgBackend::~SvgBackend() {
16 cairo_surface_finish(surface);
17 if (_closure) {
18 delete _closure;
19 _closure = nullptr;
20 }
21 destroySurface();
22}
23
24Backend *SvgBackend::construct(int width, int height){
25 return new SvgBackend(width, height);
26}
27
28cairo_surface_t* SvgBackend::createSurface() {
29 assert(!_closure);
30 _closure = new PdfSvgClosure(canvas);
31 surface = cairo_svg_surface_create_for_stream(PdfSvgClosure::writeVec, _closure, width, height);
32 return surface;
33}
34
35cairo_surface_t* SvgBackend::recreateSurface() {
36 cairo_surface_finish(surface);
37 delete _closure;
38 _closure = nullptr;
39 cairo_surface_destroy(surface);
40
41 return createSurface();
42 }
43
44
45Nan::Persistent<FunctionTemplate> SvgBackend::constructor;
46
47void SvgBackend::Initialize(Local<Object> target) {
48 Nan::HandleScope scope;
49
50 Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(SvgBackend::New);
51 SvgBackend::constructor.Reset(ctor);
52 ctor->InstanceTemplate()->SetInternalFieldCount(1);
53 ctor->SetClassName(Nan::New<String>("SvgBackend").ToLocalChecked());
54 Nan::Set(target,
55 Nan::New<String>("SvgBackend").ToLocalChecked(),
56 Nan::GetFunction(ctor).ToLocalChecked()).Check();
57}
58
59NAN_METHOD(SvgBackend::New) {
60 init(info);
61}