Backend.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "Backend.h"
  2. #include <string>
  3. Backend::Backend(std::string name, int width, int height)
  4. : name(name)
  5. , width(width)
  6. , height(height)
  7. {}
  8. Backend::~Backend()
  9. {
  10. this->destroySurface();
  11. }
  12. void Backend::init(const Nan::FunctionCallbackInfo<v8::Value> &info) {
  13. int width = 0;
  14. int height = 0;
  15. if (info[0]->IsNumber()) width = Nan::To<uint32_t>(info[0]).FromMaybe(0);
  16. if (info[1]->IsNumber()) height = Nan::To<uint32_t>(info[1]).FromMaybe(0);
  17. Backend *backend = construct(width, height);
  18. backend->Wrap(info.This());
  19. info.GetReturnValue().Set(info.This());
  20. }
  21. void Backend::setCanvas(Canvas* _canvas)
  22. {
  23. this->canvas = _canvas;
  24. }
  25. cairo_surface_t* Backend::recreateSurface()
  26. {
  27. this->destroySurface();
  28. return this->createSurface();
  29. }
  30. DLL_PUBLIC cairo_surface_t* Backend::getSurface() {
  31. if (!surface) createSurface();
  32. return surface;
  33. }
  34. void Backend::destroySurface()
  35. {
  36. if(this->surface)
  37. {
  38. cairo_surface_destroy(this->surface);
  39. this->surface = NULL;
  40. }
  41. }
  42. std::string Backend::getName()
  43. {
  44. return name;
  45. }
  46. int Backend::getWidth()
  47. {
  48. return this->width;
  49. }
  50. void Backend::setWidth(int width_)
  51. {
  52. this->width = width_;
  53. this->recreateSurface();
  54. }
  55. int Backend::getHeight()
  56. {
  57. return this->height;
  58. }
  59. void Backend::setHeight(int height_)
  60. {
  61. this->height = height_;
  62. this->recreateSurface();
  63. }
  64. bool Backend::isSurfaceValid(){
  65. bool hadSurface = surface != NULL;
  66. bool isValid = true;
  67. cairo_status_t status = cairo_surface_status(getSurface());
  68. if (status != CAIRO_STATUS_SUCCESS) {
  69. error = cairo_status_to_string(status);
  70. isValid = false;
  71. }
  72. if (!hadSurface)
  73. destroySurface();
  74. return isValid;
  75. }
  76. BackendOperationNotAvailable::BackendOperationNotAvailable(Backend* backend,
  77. std::string operation_name)
  78. : backend(backend)
  79. , operation_name(operation_name)
  80. {
  81. msg = "operation " + operation_name +
  82. " not supported by backend " + backend->getName();
  83. };
  84. BackendOperationNotAvailable::~BackendOperationNotAvailable() throw() {};
  85. const char* BackendOperationNotAvailable::what() const throw()
  86. {
  87. return msg.c_str();
  88. };