SvgBackend.cc 1.4 KB

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