/
GraphicsContext.java
335 lines (243 loc) · 14.9 KB
/
GraphicsContext.java
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Back 2 Browser Bytecode Translator
* Copyright (C) 2012-2018 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. Look for COPYING file in the top folder.
* If not, see http://opensource.org/licenses/GPL-2.0.
*/
package org.apidesign.bck2brwsr.htmlpage.api;
import org.apidesign.bck2brwsr.core.JavaScriptBody;
/**
*
* @author Anton Epple <toni.epple@eppleton.de>
*/
public class GraphicsContext {
Object context;
GraphicsContext(Object contextImpl) {
this.context = contextImpl;
}
@JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
public native void arc(double centerX,
double centerY,
double startAngle,
double radius,
double endAngle,
boolean ccw);
@JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
body = "this._context().arcTo(x1,y1,x2,y2,r);")
public native void arcTo(double x1,
double y1,
double x2,
double y2,
double r);
@JavaScriptBody(args = {"x", "y"},
body = "return this._context().isPointInPath(x,y);")
public native boolean isPointInPath(double x, double y);
@JavaScriptBody(args = {}, body = "this._context().fill();")
public native void fill();
@JavaScriptBody(args = {}, body = "this._context().stroke();")
public native void stroke();
@JavaScriptBody(args = {}, body = "this._context().beginPath();")
public native void beginPath();
@JavaScriptBody(args = {}, body = "this._context().closePath();")
public native void closePath();
@JavaScriptBody(args = {}, body = "this._context().clip();")
public native void clip();
@JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
public native void moveTo(double x, double y);
@JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
public native void lineTo(double x, double y);
@JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
@JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
@JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
public native void fillRect(double x, double y, double width, double height);
@JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
public native void strokeRect(double x, double y, double width, double height);
@JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
public native void clearRect(double x, double y, double width, double height);
@JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
public native void rect(double x, double y, double width, double height);
@JavaScriptBody(args = {}, body = "this._context().save();")
public native void save();
@JavaScriptBody(args = {}, body = "this._context().restore();")
public native void restore();
@JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
public native void rotate(double angle);
@JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
public native void transform(double a, double b, double c, double d, double e, double f);
@JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
public native void setTransform(double a, double b, double c, double d, double e, double f);
@JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
public native void translate(double x, double y);
@JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
public native void scale(double x, double y);
public void drawImage(Image image, double x, double y) {
drawImageImpl(context, Element.getElementById(image), x, y);
}
public void drawImage(Image image, double x, double y, double width, double height) {
drawImageImpl(context, Element.getElementById(image), x, y, width, height);
}
public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) {
drawImageImpl(context, Element.getElementById(image), sx, sy, sWidth, sHeight, x, y, width, height);
}
@JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height);")
private native static void drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
@JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);")
private native static void drawImageImpl(Object ctx, Object img, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height);
@JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y);")
private native static void drawImageImpl(Object ctx, Object img, double x, double y);
@JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style.valueOf();")
public native void setFillStyle(String style);
@JavaScriptBody(args = {}, body = "return this._context().fillStyle;")
public native String getFillStyle();
public void setFillStyle(LinearGradient style) {
setFillStyleImpl(context, style.object());
}
public void setFillStyle(RadialGradient style) {
setFillStyleImpl(context, style.object());
}
public void setFillStyle(Pattern style) {
setFillStyleImpl(context, style.object());
}
@JavaScriptBody(args = {"context","obj"}, body = "context.fillStyle=obj;")
private native void setFillStyleImpl(Object context, Object obj);
@JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
public native void setStrokeStyle(String style);
public void setStrokeStyle(LinearGradient style) {
setStrokeStyleImpl(context, style.object());
}
public void setStrokeStyle(RadialGradient style) {
setStrokeStyleImpl(context, style.object());
}
@JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style;")
public void setStrokeStyle(Pattern style) {
setStrokeStyleImpl(context, style.object());
}
@JavaScriptBody(args = {"context","obj"}, body = "context.strokeStyle=obj;")
private native void setStrokeStyleImpl(Object context, Object obj);
@JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
public native void setShadowColor(String color);
@JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
public native void setShadowBlur(double blur);
@JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
public native void setShadowOffsetX(double x);
@JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
public native void setShadowOffsetY(double y);
@JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
public native String getStrokeStyle();
@JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
public native String getShadowColor();
@JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
public native double getShadowBlur();
@JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
public native double getShadowOffsetX();
@JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
public native double getShadowOffsetY();
@JavaScriptBody(args = {}, body = "return this._context().lineCap;")
public native String getLineCap();
@JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
public native void setLineCap(String style);
@JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
public native String getLineJoin();
@JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
public native void setLineJoin(String style) ;
@JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
public native double getLineWidth();
@JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
public native void setLineWidth(double width);
@JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
public native double getMiterLimit();
@JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
public native void setMiterLimit(double limit);
@JavaScriptBody(args = {}, body = "return this._context().font;")
public native String getFont();
@JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
public native void setFont(String font);
@JavaScriptBody(args = {}, body = "return this._context().textAlign;")
public native String getTextAlign();
@JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
public native void setTextAlign(String textAlign);
@JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
public native String getTextBaseline();
@JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
public native void setTextBaseline(String textbaseline);
@JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
public native void fillText(String text, double x, double y);
@JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
public void fillText(String text, double x, double y, double maxWidth) {
}
public TextMetrics measureText(String text) {
return new TextMetrics(measureTextImpl(text));
}
@JavaScriptBody(args = {"text"},
body = "return this._context().measureText(text);")
private native Object measureTextImpl(String text);
@JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
public native void strokeText(String text, double x, double y);
@JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
public native void strokeText(String text, double x, double y, double maxWidth) ;
public ImageData createImageData(double x, double y) {
return new ImageData(createImageDataImpl(x, y));
}
@JavaScriptBody(args = {"x", "y"},
body = "return this._context().createImageData(x,y);")
private native Object createImageDataImpl(double x, double y);
public ImageData createImageData(ImageData imageData) {
return new ImageData(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
}
public ImageData getImageData(double x, double y, double width, double height) {
return new ImageData(getImageDataImpl(x, y, width, height));
}
@JavaScriptBody(args = {"x", "y", "width", "height"},
body = "return this._context().getImageData(x,y,width,height);")
private native Object getImageDataImpl(double x, double y, double width, double height);
public void putImageData(ImageData imageData, double x, double y) {
putImageDataImpl(imageData.object(), x, y);
}
@JavaScriptBody(args = {"imageData", "x", "y"},
body = "this._context().putImageData(imageData,x,y);")
private native void putImageDataImpl(Object imageData, double x, double y);
public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
putImageDataImpl(imageData.object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
}
@JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
@JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
public native void setGlobalAlpha(double alpha) ;
@JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
public native double getGlobalAlpha();
@JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
public native void setGlobalCompositeOperation(String operation);
@JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
public native String getGlobalCompositeOperation();
public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
return new LinearGradient(createLinearGradientImpl(context, x0, y0, x1, y1));
}
@JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
public Pattern createPattern(Image image, String repeat) {
return new Pattern(createPatternImpl(context, image, repeat));
}
@JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
private static native Object createPatternImpl(Object context, Image image, String repeat);
public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
return new RadialGradient(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
}
@JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
}