1 /* 2 * Copyright 2014 Google Inc. 3 * 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 * 8 */ 9 10 #ifndef SkV8Example_BaseContext_DEFINED 11 #define SkV8Example_BaseContext_DEFINED 12 13 #include <v8.h> 14 15 #include "SkPaint.h" 16 17 using namespace v8; 18 19 class SkCanvas; 20 class Global; 21 22 // BaseContext contains common functionality for both JsContext 23 // and DisplayList. 24 class BaseContext { 25 public: BaseContext(Global * global)26 BaseContext(Global* global) 27 : fGlobal(global) 28 { 29 fFillStyle.setColor(SK_ColorBLACK); 30 fFillStyle.setAntiAlias(true); 31 fFillStyle.setStyle(SkPaint::kFill_Style); 32 fStrokeStyle.setColor(SK_ColorBLACK); 33 fStrokeStyle.setAntiAlias(true); 34 fStrokeStyle.setStyle(SkPaint::kStroke_Style); 35 } ~BaseContext()36 virtual ~BaseContext() {} 37 38 // Retrieve the SkCanvas to draw on. May return NULL. 39 virtual SkCanvas* getCanvas() = 0; 40 41 // Add the Javascript attributes and methods that BaseContext implements to the ObjectTemplate. 42 void addAttributesAndMethods(Handle<ObjectTemplate> tmpl); 43 44 protected: 45 // Get the pointer out of obj. 46 static BaseContext* Unwrap(Handle<Object> obj); 47 48 Global* fGlobal; 49 SkPaint fFillStyle; 50 SkPaint fStrokeStyle; 51 52 private: 53 static void GetStyle(Local<String> name, 54 const PropertyCallbackInfo<Value>& info, 55 const SkPaint& style); 56 static void SetStyle(Local<String> name, Local<Value> value, 57 const PropertyCallbackInfo<void>& info, 58 SkPaint& style); 59 // JS Attributes 60 static void GetFillStyle(Local<String> name, 61 const PropertyCallbackInfo<Value>& info); 62 static void SetFillStyle(Local<String> name, Local<Value> value, 63 const PropertyCallbackInfo<void>& info); 64 static void GetStrokeStyle(Local<String> name, 65 const PropertyCallbackInfo<Value>& info); 66 static void SetStrokeStyle(Local<String> name, Local<Value> value, 67 const PropertyCallbackInfo<void>& info); 68 static void GetWidth(Local<String> name, 69 const PropertyCallbackInfo<Value>& info); 70 static void GetHeight(Local<String> name, 71 const PropertyCallbackInfo<Value>& info); 72 73 // JS Methods 74 static void FillRect(const v8::FunctionCallbackInfo<Value>& args); 75 static void Stroke(const v8::FunctionCallbackInfo<Value>& args); 76 static void Fill(const v8::FunctionCallbackInfo<Value>& args); 77 static void Rotate(const v8::FunctionCallbackInfo<Value>& args); 78 static void Save(const v8::FunctionCallbackInfo<Value>& args); 79 static void Restore(const v8::FunctionCallbackInfo<Value>& args); 80 static void Translate(const v8::FunctionCallbackInfo<Value>& args); 81 static void ResetTransform(const v8::FunctionCallbackInfo<Value>& args); 82 }; 83 84 #endif 85