aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/SkV8Example/DrawingMethods.cpp
blob: 5fcdd6a5e158eee1ce007e4645b09fb384d130c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright 2014 Google Inc.
 *
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 */
#include <v8.h>

#include "Global.h"
#include "DrawingMethods.h"
#include "Path2D.h"
#include "SkCanvas.h"
#include "SkPaint.h"


DrawingMethods* DrawingMethods::Unwrap(v8::Handle<v8::Object> obj) {
    v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(obj->GetInternalField(0));
    void* ptr = field->Value();
    return static_cast<DrawingMethods*>(ptr);
}


void DrawingMethods::Save(const v8::FunctionCallbackInfo<v8::Value>& args) {
    DrawingMethods* drawingMethods = Unwrap(args.This());
    SkCanvas* canvas = drawingMethods->getCanvas();
    if (NULL == canvas) {
        return;
    }

    canvas->save();
}

void DrawingMethods::Restore(const v8::FunctionCallbackInfo<v8::Value>& args) {
    DrawingMethods* drawingMethods = Unwrap(args.This());
    SkCanvas* canvas = drawingMethods->getCanvas();
    if (NULL == canvas) {
        return;
    }

    canvas->restore();
}

void DrawingMethods::Rotate(const v8::FunctionCallbackInfo<v8::Value>& args) {
    DrawingMethods* drawingMethods = Unwrap(args.This());
    SkCanvas* canvas = drawingMethods->getCanvas();
    if (NULL == canvas) {
        return;
    }

    if (args.Length() != 1) {
        args.GetIsolate()->ThrowException(
                v8::String::NewFromUtf8(
                        args.GetIsolate(), "Error: 1 arguments required."));
        return;
    }
    double angle = args[0]->NumberValue();
    canvas->rotate(SkRadiansToDegrees(angle));
}

void DrawingMethods::Translate(const v8::FunctionCallbackInfo<v8::Value>& args) {
    DrawingMethods* drawingMethods = Unwrap(args.This());
    SkCanvas* canvas = drawingMethods->getCanvas();
    if (NULL == canvas) {
        return;
    }

    if (args.Length() != 2) {
        args.GetIsolate()->ThrowException(
                v8::String::NewFromUtf8(
                        args.GetIsolate(), "Error: 2 arguments required."));
        return;
    }
    double dx = args[0]->NumberValue();
    double dy = args[1]->NumberValue();
    canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy));
}

void DrawingMethods::ResetTransform(const v8::FunctionCallbackInfo<v8::Value>& args) {
    DrawingMethods* drawingMethods = Unwrap(args.This());
    SkCanvas* canvas = drawingMethods->getCanvas();
    if (NULL == canvas) {
        return;
    }

    canvas->resetMatrix();
}

void DrawingMethods::DrawPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
    DrawingMethods* drawingMethods = Unwrap(args.This());
    SkCanvas* canvas = drawingMethods->getCanvas();
    if (NULL == canvas) {
        return;
    }

    if (args.Length() != 1) {
        args.GetIsolate()->ThrowException(
                v8::String::NewFromUtf8(
                        args.GetIsolate(), "Error: 1 argument required."));
        return;
    }

    v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast(
            args[0]->ToObject()->GetInternalField(0));
    void* ptr = field->Value();
    Path2D* path = static_cast<Path2D*>(ptr);
    if (NULL == path) {
        return;
    }
    // TODO(jcgregorio) Add support for Paint2D parameter after Paint2D is
    // implemented.
    SkPaint fillStyle;
    fillStyle.setColor(SK_ColorBLACK);
    fillStyle.setAntiAlias(true);
    fillStyle.setStyle(SkPaint::kFill_Style);
    canvas->drawPath(*(path->path()), fillStyle);
}


void DrawingMethods::GetWidth(v8::Local<v8::String> name,
        const v8::PropertyCallbackInfo<v8::Value>& info) {
    DrawingMethods* drawingMethods = Unwrap(info.This());
    SkCanvas* canvas = drawingMethods->getCanvas();
    if (NULL == canvas) {
        return;
    }

    info.GetReturnValue().Set(
            v8::Int32::New(
                drawingMethods->fGlobal->getIsolate(), canvas->imageInfo().width()));
}

void DrawingMethods::GetHeight(v8::Local<v8::String> name,
        const v8::PropertyCallbackInfo<v8::Value>& info) {
    DrawingMethods* drawingMethods = Unwrap(info.This());
    SkCanvas* canvas = drawingMethods->getCanvas();
    if (NULL == canvas) {
        return;
    }

    info.GetReturnValue().Set(
            v8::Int32::New(
                drawingMethods->fGlobal->getIsolate(), canvas->imageInfo().height()));
}

#define ADD_METHOD(name, fn) \
    tmpl->Set(v8::String::NewFromUtf8( \
         fGlobal->getIsolate(), name, \
         v8::String::kInternalizedString), \
             v8::FunctionTemplate::New(fGlobal->getIsolate(), fn))

void DrawingMethods::addAttributesAndMethods(v8::Handle<v8::ObjectTemplate> tmpl) {
    v8::HandleScope scope(fGlobal->getIsolate());

    // Add accessors for each of the fields of the context object.
    tmpl->SetAccessor(v8::String::NewFromUtf8(
        fGlobal->getIsolate(), "width", v8::String::kInternalizedString),
            GetWidth);
    tmpl->SetAccessor(v8::String::NewFromUtf8(
        fGlobal->getIsolate(), "height", v8::String::kInternalizedString),
            GetHeight);

    // Add methods.
    ADD_METHOD("save", Save);
    ADD_METHOD("restore", Restore);
    ADD_METHOD("rotate", Rotate);
    ADD_METHOD("translate", Translate);
    ADD_METHOD("resetTransform", ResetTransform);

    ADD_METHOD("drawPath", DrawPath);
}