javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/GraphicsContext.java
Refactored names to unpopular naming scheme, because (a) bck2brwsr Canvas implementation must be named Canvas due to mapping in bck2brwsr and I don't want interface and impl to have the same name. (b) with the "I" prefix at least it's consistent, even if nobody uses that anymore. (c) names are the same as in HTML5 Canvas Javascript API, so it's more familiar...
2 * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
3 * <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program. Look for COPYING file in the top folder. If not, see
16 * http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.htmlpage.api;
21 import net.java.html.canvas.IGraphicsContext;
22 import net.java.html.canvas.IImage;
23 import net.java.html.canvas.IImageData;
24 import net.java.html.canvas.ILinearGradient;
25 import net.java.html.canvas.IPattern;
26 import net.java.html.canvas.IRadialGradient;
27 import net.java.html.canvas.ITextMetrics;
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
33 * @author Anton Epple <toni.epple@eppleton.de>
35 public class GraphicsContext implements IGraphicsContext {
39 GraphicsContext(Object contextImpl) {
40 this.context = contextImpl;
43 @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
44 body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
46 public native void arc(double centerX,
53 @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
54 body = "this._context().arcTo(x1,y1,x2,y2,r);")
56 public native void arcTo(double x1,
62 @JavaScriptBody(args = {"x", "y"},
63 body = "return this._context().isPointInPath(x,y);")
65 public native boolean isPointInPath(double x, double y);
67 @JavaScriptBody(args = {}, body = "this._context().fill();")
69 public native void fill();
71 @JavaScriptBody(args = {}, body = "this._context().stroke();")
73 public native void stroke();
75 @JavaScriptBody(args = {}, body = "this._context().beginPath();")
77 public native void beginPath();
79 @JavaScriptBody(args = {}, body = "this._context().closePath();")
81 public native void closePath();
83 @JavaScriptBody(args = {}, body = "this._context().clip();")
85 public native void clip();
87 @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
89 public native void moveTo(double x, double y);
91 @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
93 public native void lineTo(double x, double y);
95 @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
97 public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
99 @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
100 body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
102 public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
104 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
106 public native void fillRect(double x, double y, double width, double height);
108 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
110 public native void strokeRect(double x, double y, double width, double height);
112 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
114 public native void clearRect(double x, double y, double width, double height);
116 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
118 public native void rect(double x, double y, double width, double height);
120 @JavaScriptBody(args = {}, body = "this._context().save();")
122 public native void save();
124 @JavaScriptBody(args = {}, body = "this._context().restore();")
126 public native void restore();
128 @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
130 public native void rotate(double angle);
132 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
134 public native void transform(double a, double b, double c, double d, double e, double f);
136 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
138 public native void setTransform(double a, double b, double c, double d, double e, double f);
140 @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
142 public native void translate(double x, double y);
144 @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
146 public native void scale(double x, double y);
149 public void drawImage(IImage image, double x, double y) {
150 drawImageImpl(context, Element.getElementById((Image) image), x, y);
154 public void drawImage(IImage image, double x, double y, double width, double height) {
155 drawImageImpl(context, Element.getElementById((Image) image), x, y, width, height);
159 public void drawImage(IImage image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) {
160 drawImageImpl(context, Element.getElementById((Image) image), sx, sy, sWidth, sHeight, x, y, width, height);
163 @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height);")
164 private native static void drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
166 @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);")
167 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);
169 @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y);")
170 private native static void drawImageImpl(Object ctx, Object img, double x, double y);
172 @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style.valueOf();")
174 public native void setFillStyle(String style);
176 @JavaScriptBody(args = {}, body = "return this._context().fillStyle;")
178 public native String getFillStyle();
181 public void setFillStyle(ILinearGradient style) {
182 setFillStyleImpl(context, ((LinearGradient) style).object());
186 public void setFillStyle(IRadialGradient style) {
187 setFillStyleImpl(context, ((RadialGradient) style).object());
191 public void setFillStyle(IPattern style) {
192 setFillStyleImpl(context, ((Pattern) style).object());
195 @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
196 private native void setFillStyleImpl(Object context, Object obj);
198 @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
200 public native void setStrokeStyle(String style);
203 public void setStrokeStyle(ILinearGradient style) {
204 setStrokeStyleImpl(context, ((LinearGradient) style).object());
208 public void setStrokeStyle(IRadialGradient style) {
209 setStrokeStyleImpl(context, ((RadialGradient) style).object());
212 @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style;")
214 public void setStrokeStyle(IPattern style) {
215 setStrokeStyleImpl(context, ((LinearGradient) style).object());
218 @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
219 private native void setStrokeStyleImpl(Object context, Object obj);
221 @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
223 public native void setShadowColor(String color);
225 @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
227 public native void setShadowBlur(double blur);
229 @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
231 public native void setShadowOffsetX(double x);
233 @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
235 public native void setShadowOffsetY(double y);
237 @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
239 public native String getStrokeStyle();
241 @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
243 public native String getShadowColor();
245 @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
247 public native double getShadowBlur();
249 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
251 public native double getShadowOffsetX();
253 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
255 public native double getShadowOffsetY();
257 @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
259 public native String getLineCap();
261 @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
263 public native void setLineCap(String style);
265 @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
267 public native String getLineJoin();
269 @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
271 public native void setLineJoin(String style);
273 @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
275 public native double getLineWidth();
277 @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
279 public native void setLineWidth(double width);
281 @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
283 public native double getMiterLimit();
285 @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
287 public native void setMiterLimit(double limit);
289 @JavaScriptBody(args = {}, body = "return this._context().font;")
291 public native String getFont();
293 @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
295 public native void setFont(String font);
297 @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
299 public native String getTextAlign();
301 @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
303 public native void setTextAlign(String textAlign);
305 @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
307 public native String getTextBaseline();
309 @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
311 public native void setTextBaseline(String textbaseline);
313 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
315 public native void fillText(String text, double x, double y);
317 @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
319 public void fillText(String text, double x, double y, double maxWidth) {
323 public ITextMetrics measureText(String text) {
324 return new TextMetrics(measureTextImpl(text));
327 @JavaScriptBody(args = {"text"},
328 body = "return this._context().measureText(text);")
329 private native Object measureTextImpl(String text);
331 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
333 public native void strokeText(String text, double x, double y);
335 @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
337 public native void strokeText(String text, double x, double y, double maxWidth);
340 public IImageData createImageData(double x, double y) {
341 return new ImageData(createImageDataImpl(x, y));
344 @JavaScriptBody(args = {"x", "y"},
345 body = "return this._context().createImageData(x,y);")
346 private native Object createImageDataImpl(double x, double y);
349 public IImageData createImageData(IImageData imageData) {
350 return new ImageData(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
354 public IImageData getImageData(double x, double y, double width, double height) {
355 return new ImageData(getImageDataImpl(x, y, width, height));
358 @JavaScriptBody(args = {"x", "y", "width", "height"},
359 body = "return this._context().getImageData(x,y,width,height);")
360 private native Object getImageDataImpl(double x, double y, double width, double height);
363 public void putImageData(IImageData imageData, double x, double y) {
364 putImageDataImpl(((ImageData) imageData).object(), x, y);
367 @JavaScriptBody(args = {"imageData", "x", "y"},
368 body = "this._context().putImageData(imageData,x,y);")
369 private native void putImageDataImpl(Object imageData, double x, double y);
372 public void putImageData(IImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
373 putImageDataImpl(((ImageData) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
376 @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
377 body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
378 private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
380 @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
382 public native void setGlobalAlpha(double alpha);
384 @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
386 public native double getGlobalAlpha();
388 @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
390 public native void setGlobalCompositeOperation(String operation);
392 @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
394 public native String getGlobalCompositeOperation();
397 public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
398 return new LinearGradient(createLinearGradientImpl(context, x0, y0, x1, y1));
401 @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
402 private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
405 public IPattern createPattern(IImage image, String repeat) {
406 return new Pattern(createPatternImpl(context, image, repeat));
409 @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
410 private static native Object createPatternImpl(Object context, IImage image, String repeat);
413 public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
414 return new RadialGradient(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
417 @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
418 private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
420 @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
422 public IImage getImageForPath(String path) {
423 throw new UnsupportedOperationException("getImageForPath is not yet supported");