CanvasRenderingContext2d.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright (c) 2010 LearnBoost <tj@learnboost.com>
  2. #pragma once
  3. #include "cairo.h"
  4. #include "Canvas.h"
  5. #include "color.h"
  6. #include "nan.h"
  7. #include <pango/pangocairo.h>
  8. #include <stack>
  9. /*
  10. * State struct.
  11. *
  12. * Used in conjunction with Save() / Restore() since
  13. * cairo's gstate maintains only a single source pattern at a time.
  14. */
  15. struct canvas_state_t {
  16. rgba_t fill = { 0, 0, 0, 1 };
  17. rgba_t stroke = { 0, 0, 0, 1 };
  18. rgba_t shadow = { 0, 0, 0, 0 };
  19. double shadowOffsetX = 0.;
  20. double shadowOffsetY = 0.;
  21. cairo_pattern_t* fillPattern = nullptr;
  22. cairo_pattern_t* strokePattern = nullptr;
  23. cairo_pattern_t* fillGradient = nullptr;
  24. cairo_pattern_t* strokeGradient = nullptr;
  25. PangoFontDescription* fontDescription = nullptr;
  26. cairo_filter_t patternQuality = CAIRO_FILTER_GOOD;
  27. float globalAlpha = 1.f;
  28. int shadowBlur = 0;
  29. text_align_t textAlignment = TEXT_ALIGNMENT_LEFT; // TODO default is supposed to be START
  30. text_baseline_t textBaseline = TEXT_BASELINE_ALPHABETIC;
  31. canvas_draw_mode_t textDrawingMode = TEXT_DRAW_PATHS;
  32. bool imageSmoothingEnabled = true;
  33. canvas_state_t() {
  34. fontDescription = pango_font_description_from_string("sans");
  35. pango_font_description_set_absolute_size(fontDescription, 10 * PANGO_SCALE);
  36. }
  37. canvas_state_t(const canvas_state_t& other) {
  38. fill = other.fill;
  39. stroke = other.stroke;
  40. patternQuality = other.patternQuality;
  41. fillPattern = other.fillPattern;
  42. strokePattern = other.strokePattern;
  43. fillGradient = other.fillGradient;
  44. strokeGradient = other.strokeGradient;
  45. globalAlpha = other.globalAlpha;
  46. textAlignment = other.textAlignment;
  47. textBaseline = other.textBaseline;
  48. shadow = other.shadow;
  49. shadowBlur = other.shadowBlur;
  50. shadowOffsetX = other.shadowOffsetX;
  51. shadowOffsetY = other.shadowOffsetY;
  52. textDrawingMode = other.textDrawingMode;
  53. fontDescription = pango_font_description_copy(other.fontDescription);
  54. imageSmoothingEnabled = other.imageSmoothingEnabled;
  55. }
  56. ~canvas_state_t() {
  57. pango_font_description_free(fontDescription);
  58. }
  59. };
  60. /*
  61. * Equivalent to a PangoRectangle but holds floats instead of ints
  62. * (software pixels are stored here instead of pango units)
  63. *
  64. * Should be compatible with PANGO_ASCENT, PANGO_LBEARING, etc.
  65. */
  66. typedef struct {
  67. float x;
  68. float y;
  69. float width;
  70. float height;
  71. } float_rectangle;
  72. class Context2d : public Nan::ObjectWrap {
  73. public:
  74. std::stack<canvas_state_t> states;
  75. canvas_state_t *state;
  76. Context2d(Canvas *canvas);
  77. static Nan::Persistent<v8::Function> _DOMMatrix;
  78. static Nan::Persistent<v8::Function> _parseFont;
  79. static Nan::Persistent<v8::FunctionTemplate> constructor;
  80. static void Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target);
  81. static NAN_METHOD(New);
  82. static NAN_METHOD(SaveExternalModules);
  83. static NAN_METHOD(DrawImage);
  84. static NAN_METHOD(PutImageData);
  85. static NAN_METHOD(Save);
  86. static NAN_METHOD(Restore);
  87. static NAN_METHOD(Rotate);
  88. static NAN_METHOD(Translate);
  89. static NAN_METHOD(Scale);
  90. static NAN_METHOD(Transform);
  91. static NAN_METHOD(GetTransform);
  92. static NAN_METHOD(ResetTransform);
  93. static NAN_METHOD(SetTransform);
  94. static NAN_METHOD(IsPointInPath);
  95. static NAN_METHOD(BeginPath);
  96. static NAN_METHOD(ClosePath);
  97. static NAN_METHOD(AddPage);
  98. static NAN_METHOD(Clip);
  99. static NAN_METHOD(Fill);
  100. static NAN_METHOD(Stroke);
  101. static NAN_METHOD(FillText);
  102. static NAN_METHOD(StrokeText);
  103. static NAN_METHOD(SetFont);
  104. static NAN_METHOD(SetFillColor);
  105. static NAN_METHOD(SetStrokeColor);
  106. static NAN_METHOD(SetStrokePattern);
  107. static NAN_METHOD(SetTextAlignment);
  108. static NAN_METHOD(SetLineDash);
  109. static NAN_METHOD(GetLineDash);
  110. static NAN_METHOD(MeasureText);
  111. static NAN_METHOD(BezierCurveTo);
  112. static NAN_METHOD(QuadraticCurveTo);
  113. static NAN_METHOD(LineTo);
  114. static NAN_METHOD(MoveTo);
  115. static NAN_METHOD(FillRect);
  116. static NAN_METHOD(StrokeRect);
  117. static NAN_METHOD(ClearRect);
  118. static NAN_METHOD(Rect);
  119. static NAN_METHOD(RoundRect);
  120. static NAN_METHOD(Arc);
  121. static NAN_METHOD(ArcTo);
  122. static NAN_METHOD(Ellipse);
  123. static NAN_METHOD(GetImageData);
  124. static NAN_METHOD(CreateImageData);
  125. static NAN_METHOD(GetStrokeColor);
  126. static NAN_METHOD(CreatePattern);
  127. static NAN_METHOD(CreateLinearGradient);
  128. static NAN_METHOD(CreateRadialGradient);
  129. static NAN_GETTER(GetFormat);
  130. static NAN_GETTER(GetPatternQuality);
  131. static NAN_GETTER(GetImageSmoothingEnabled);
  132. static NAN_GETTER(GetGlobalCompositeOperation);
  133. static NAN_GETTER(GetGlobalAlpha);
  134. static NAN_GETTER(GetShadowColor);
  135. static NAN_GETTER(GetMiterLimit);
  136. static NAN_GETTER(GetLineCap);
  137. static NAN_GETTER(GetLineJoin);
  138. static NAN_GETTER(GetLineWidth);
  139. static NAN_GETTER(GetLineDashOffset);
  140. static NAN_GETTER(GetShadowOffsetX);
  141. static NAN_GETTER(GetShadowOffsetY);
  142. static NAN_GETTER(GetShadowBlur);
  143. static NAN_GETTER(GetAntiAlias);
  144. static NAN_GETTER(GetTextDrawingMode);
  145. static NAN_GETTER(GetQuality);
  146. static NAN_GETTER(GetCurrentTransform);
  147. static NAN_GETTER(GetFillStyle);
  148. static NAN_GETTER(GetStrokeStyle);
  149. static NAN_GETTER(GetFont);
  150. static NAN_GETTER(GetTextBaseline);
  151. static NAN_GETTER(GetTextAlign);
  152. static NAN_SETTER(SetPatternQuality);
  153. static NAN_SETTER(SetImageSmoothingEnabled);
  154. static NAN_SETTER(SetGlobalCompositeOperation);
  155. static NAN_SETTER(SetGlobalAlpha);
  156. static NAN_SETTER(SetShadowColor);
  157. static NAN_SETTER(SetMiterLimit);
  158. static NAN_SETTER(SetLineCap);
  159. static NAN_SETTER(SetLineJoin);
  160. static NAN_SETTER(SetLineWidth);
  161. static NAN_SETTER(SetLineDashOffset);
  162. static NAN_SETTER(SetShadowOffsetX);
  163. static NAN_SETTER(SetShadowOffsetY);
  164. static NAN_SETTER(SetShadowBlur);
  165. static NAN_SETTER(SetAntiAlias);
  166. static NAN_SETTER(SetTextDrawingMode);
  167. static NAN_SETTER(SetQuality);
  168. static NAN_SETTER(SetCurrentTransform);
  169. static NAN_SETTER(SetFillStyle);
  170. static NAN_SETTER(SetStrokeStyle);
  171. static NAN_SETTER(SetFont);
  172. static NAN_SETTER(SetTextBaseline);
  173. static NAN_SETTER(SetTextAlign);
  174. inline void setContext(cairo_t *ctx) { _context = ctx; }
  175. inline cairo_t *context(){ return _context; }
  176. inline Canvas *canvas(){ return _canvas; }
  177. inline bool hasShadow();
  178. void inline setSourceRGBA(rgba_t color);
  179. void inline setSourceRGBA(cairo_t *ctx, rgba_t color);
  180. void setTextPath(double x, double y);
  181. void blur(cairo_surface_t *surface, int radius);
  182. void shadow(void (fn)(cairo_t *cr));
  183. void shadowStart();
  184. void shadowApply();
  185. void savePath();
  186. void restorePath();
  187. void saveState();
  188. void restoreState();
  189. void inline setFillRule(v8::Local<v8::Value> value);
  190. void fill(bool preserve = false);
  191. void stroke(bool preserve = false);
  192. void save();
  193. void restore();
  194. void setFontFromState();
  195. void resetState();
  196. inline PangoLayout *layout(){ return _layout; }
  197. private:
  198. ~Context2d();
  199. void _resetPersistentHandles();
  200. v8::Local<v8::Value> _getFillColor();
  201. v8::Local<v8::Value> _getStrokeColor();
  202. void _setFillColor(v8::Local<v8::Value> arg);
  203. void _setFillPattern(v8::Local<v8::Value> arg);
  204. void _setStrokeColor(v8::Local<v8::Value> arg);
  205. void _setStrokePattern(v8::Local<v8::Value> arg);
  206. Nan::Persistent<v8::Value> _fillStyle;
  207. Nan::Persistent<v8::Value> _strokeStyle;
  208. Nan::Persistent<v8::Value> _font;
  209. Canvas *_canvas;
  210. cairo_t *_context;
  211. cairo_path_t *_path;
  212. PangoLayout *_layout;
  213. };