CanvasGradient.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2010 LearnBoost <tj@learnboost.com>
  2. #include "CanvasGradient.h"
  3. #include "Canvas.h"
  4. #include "color.h"
  5. using namespace v8;
  6. Nan::Persistent<FunctionTemplate> Gradient::constructor;
  7. /*
  8. * Initialize CanvasGradient.
  9. */
  10. void
  11. Gradient::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
  12. Nan::HandleScope scope;
  13. // Constructor
  14. Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(Gradient::New);
  15. constructor.Reset(ctor);
  16. ctor->InstanceTemplate()->SetInternalFieldCount(1);
  17. ctor->SetClassName(Nan::New("CanvasGradient").ToLocalChecked());
  18. // Prototype
  19. Nan::SetPrototypeMethod(ctor, "addColorStop", AddColorStop);
  20. Local<Context> ctx = Nan::GetCurrentContext();
  21. Nan::Set(target,
  22. Nan::New("CanvasGradient").ToLocalChecked(),
  23. ctor->GetFunction(ctx).ToLocalChecked());
  24. }
  25. /*
  26. * Initialize a new CanvasGradient.
  27. */
  28. NAN_METHOD(Gradient::New) {
  29. if (!info.IsConstructCall()) {
  30. return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
  31. }
  32. // Linear
  33. if (4 == info.Length()) {
  34. Gradient *grad = new Gradient(
  35. Nan::To<double>(info[0]).FromMaybe(0)
  36. , Nan::To<double>(info[1]).FromMaybe(0)
  37. , Nan::To<double>(info[2]).FromMaybe(0)
  38. , Nan::To<double>(info[3]).FromMaybe(0));
  39. grad->Wrap(info.This());
  40. info.GetReturnValue().Set(info.This());
  41. return;
  42. }
  43. // Radial
  44. if (6 == info.Length()) {
  45. Gradient *grad = new Gradient(
  46. Nan::To<double>(info[0]).FromMaybe(0)
  47. , Nan::To<double>(info[1]).FromMaybe(0)
  48. , Nan::To<double>(info[2]).FromMaybe(0)
  49. , Nan::To<double>(info[3]).FromMaybe(0)
  50. , Nan::To<double>(info[4]).FromMaybe(0)
  51. , Nan::To<double>(info[5]).FromMaybe(0));
  52. grad->Wrap(info.This());
  53. info.GetReturnValue().Set(info.This());
  54. return;
  55. }
  56. return Nan::ThrowTypeError("invalid arguments");
  57. }
  58. /*
  59. * Add color stop.
  60. */
  61. NAN_METHOD(Gradient::AddColorStop) {
  62. if (!info[0]->IsNumber())
  63. return Nan::ThrowTypeError("offset required");
  64. if (!info[1]->IsString())
  65. return Nan::ThrowTypeError("color string required");
  66. Gradient *grad = Nan::ObjectWrap::Unwrap<Gradient>(info.This());
  67. short ok;
  68. Nan::Utf8String str(info[1]);
  69. uint32_t rgba = rgba_from_string(*str, &ok);
  70. if (ok) {
  71. rgba_t color = rgba_create(rgba);
  72. cairo_pattern_add_color_stop_rgba(
  73. grad->pattern()
  74. , Nan::To<double>(info[0]).FromMaybe(0)
  75. , color.r
  76. , color.g
  77. , color.b
  78. , color.a);
  79. } else {
  80. return Nan::ThrowTypeError("parse color failed");
  81. }
  82. }
  83. /*
  84. * Initialize linear gradient.
  85. */
  86. Gradient::Gradient(double x0, double y0, double x1, double y1) {
  87. _pattern = cairo_pattern_create_linear(x0, y0, x1, y1);
  88. }
  89. /*
  90. * Initialize radial gradient.
  91. */
  92. Gradient::Gradient(double x0, double y0, double r0, double x1, double y1, double r1) {
  93. _pattern = cairo_pattern_create_radial(x0, y0, r0, x1, y1, r1);
  94. }
  95. /*
  96. * Destroy the pattern.
  97. */
  98. Gradient::~Gradient() {
  99. cairo_pattern_destroy(_pattern);
  100. }