javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/HTML5GraphicsEnvironment.java
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;
20 import java.awt.Dimension;
21 import java.util.HashMap;
23 import net.java.html.canvas.GraphicsEnvironment;
24 import net.java.html.canvas.ImageData;
25 import net.java.html.canvas.LinearGradient;
26 import net.java.html.canvas.Pattern;
27 import net.java.html.canvas.RadialGradient;
28 import net.java.html.canvas.Style;
29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
30 import org.apidesign.bck2brwsr.htmlpage.api.Canvas;
31 import org.apidesign.bck2brwsr.htmlpage.api.Element;
32 import org.apidesign.bck2brwsr.htmlpage.api.Image;
36 * @author Anton Epple <toni.epple@eppleton.de>
38 public class HTML5GraphicsEnvironment implements GraphicsEnvironment {
42 public HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
43 this.context = contextImpl;
47 @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
48 body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
50 public native void arc(double centerX,
57 @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
58 body = "this._context().arcTo(x1,y1,x2,y2,r);")
60 public native void arcTo(double x1,
66 @JavaScriptBody(args = {"x", "y"},
67 body = "return this._context().isPointInPath(x,y);")
69 public native boolean isPointInPath(double x, double y);
71 @JavaScriptBody(args = {}, body = "this._context().fill();")
73 public native void fill();
75 @JavaScriptBody(args = {}, body = "this._context().stroke();")
77 public native void stroke();
79 @JavaScriptBody(args = {}, body = "this._context().beginPath();")
81 public native void beginPath();
83 @JavaScriptBody(args = {}, body = "this._context().closePath();")
85 public native void closePath();
87 @JavaScriptBody(args = {}, body = "this._context().clip();")
89 public native void clip();
91 @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
93 public native void moveTo(double x, double y);
95 @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
97 public native void lineTo(double x, double y);
99 @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
101 public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
103 @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
104 body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
106 public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
108 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
110 public native void fillRect(double x, double y, double width, double height);
112 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
114 public native void strokeRect(double x, double y, double width, double height);
116 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
118 public native void clearRect(double x, double y, double width, double height);
120 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
122 public native void rect(double x, double y, double width, double height);
124 @JavaScriptBody(args = {}, body = "this._context().save();")
126 public native void save();
128 @JavaScriptBody(args = {}, body = "this._context().restore();")
130 public native void restore();
132 @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
134 public native void rotate(double angle);
136 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
138 public native void transform(double a, double b, double c, double d, double e, double f);
140 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
142 public native void setTransform(double a, double b, double c, double d, double e, double f);
144 @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
146 public native void translate(double x, double y);
148 @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
150 public native void scale(double x, double y);
153 public void drawImage(ImageData image, double x, double y) {
154 drawImageImpl(context, Element.getElementById((Image) image), x, y);
158 public void drawImage(ImageData image, double x, double y, double width, double height) {
159 drawImageImpl(context, Element.getElementById((Image) image), x, y, width, height);
163 public void drawImage(ImageData image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) {
164 drawImageImpl(context, Element.getElementById((Image) image), sx, sy, sWidth, sHeight, x, y, width, height);
167 @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height);")
168 private native static void drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
170 @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);")
171 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);
173 @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y);")
174 private native static void drawImageImpl(Object ctx, Object img, double x, double y);
176 @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style.valueOf();")
178 public native void setFillStyle(String style);
180 @JavaScriptBody(args = {}, body = "return this._context().fillStyle;")
182 public native String getFillStyle();
185 public void setFillStyle(Style style) {
186 setFillStyleImpl(context, createFillStyle(style));
189 private Object createFillStyle(Style style) {
190 if (style instanceof RadialGradient) {
191 RadialGradientWrapper gradient = createRadialGradientWrapper(
192 ((RadialGradient) style).getX0(),
193 ((RadialGradient) style).getY0(),
194 ((RadialGradient) style).getR0(),
195 ((RadialGradient) style).getX1(),
196 ((RadialGradient) style).getY1(),
197 ((RadialGradient) style).getR1());
198 HashMap<Double, String> stops = ((LinearGradient) style).getStops();
199 Set<Double> keySet = stops.keySet();
200 for (Double double1 : keySet) {
201 addColorStopImpl(style, double1, stops.get(double1));
206 } else if (style instanceof LinearGradient) {
207 LinearGradientWrapper gradient = createLinearGradientWrapper(
208 ((LinearGradient) style).getX0(),
209 ((LinearGradient) style).getY0(),
210 ((LinearGradient) style).getX1(),
211 ((LinearGradient) style).getY1());
212 HashMap<Double, String> stops = ((LinearGradient) style).getStops();
213 Set<Double> keySet = stops.keySet();
214 for (Double double1 : keySet) {
215 addColorStopImpl(style, double1, stops.get(double1));
218 } else if (style instanceof Pattern) {
219 return createPatternWrapper(((Pattern) style).getImageData(), ((Pattern) style).getRepeat());
225 @JavaScriptBody(args = {"gradient", "position", "color"}, body =
226 "gradient.addColorStop(position,color)")
227 private static native void addColorStopImpl(Object gradient, double position, String color);
229 @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
230 private native void setFillStyleImpl(Object context, Object obj);
232 @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
234 public native void setStrokeStyle(String style);
237 public void setStrokeStyle(Style style) {
238 setStrokeStyleImpl(context, createFillStyle(style));
241 @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
242 private native void setStrokeStyleImpl(Object context, Object obj);
244 @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
246 public native void setShadowColor(String color);
248 @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
250 public native void setShadowBlur(double blur);
252 @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
254 public native void setShadowOffsetX(double x);
256 @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
258 public native void setShadowOffsetY(double y);
260 @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
262 public native String getStrokeStyle();
264 @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
266 public native String getShadowColor();
268 @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
270 public native double getShadowBlur();
272 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
274 public native double getShadowOffsetX();
276 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
278 public native double getShadowOffsetY();
280 @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
282 public native String getLineCap();
284 @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
286 public native void setLineCap(String style);
288 @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
290 public native String getLineJoin();
292 @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
294 public native void setLineJoin(String style);
296 @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
298 public native double getLineWidth();
300 @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
302 public native void setLineWidth(double width);
304 @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
306 public native double getMiterLimit();
308 @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
310 public native void setMiterLimit(double limit);
312 @JavaScriptBody(args = {}, body = "return this._context().font;")
314 public native String getFont();
316 @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
318 public native void setFont(String font);
320 @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
322 public native String getTextAlign();
324 @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
326 public native void setTextAlign(String textAlign);
328 @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
330 public native String getTextBaseline();
332 @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
334 public native void setTextBaseline(String textbaseline);
336 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
338 public native void fillText(String text, double x, double y);
340 @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
342 public void fillText(String text, double x, double y, double maxWidth) {
346 public Dimension measureText(String text) {
347 TextMetrics textMetrics = new TextMetrics(measureTextImpl(text));
348 return new Dimension((int) textMetrics.getWidth(), (int) textMetrics.getHeight());
351 @JavaScriptBody(args = {"text"},
352 body = "return this._context().measureText(text);")
353 private native Object measureTextImpl(String text);
355 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
357 public native void strokeText(String text, double x, double y);
359 @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
361 public native void strokeText(String text, double x, double y, double maxWidth);
364 // public ImageData createImageData(double x, double y) {
365 // return new ImageDataWrapper(createImageDataImpl(x, y));
368 // @JavaScriptBody(args = {"x", "y"},
369 // body = "return this._context().createImageData(x,y);")
370 // private native Object createImageDataImpl(double x, double y);
373 // public ImageData createImageData(ImageData imageData) {
374 // return new ImageDataWrapper(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
378 // public ImageData getImageData(double x, double y, double width, double height) {
379 // return new ImageDataWrapper(getImageDataImpl(x, y, width, height));
382 @JavaScriptBody(args = {"x", "y", "width", "height"},
383 body = "return this._context().getImageData(x,y,width,height);")
384 private native Object getImageDataImpl(double x, double y, double width, double height);
387 // public void putImageData(ImageData imageData, double x, double y) {
388 // putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y);
391 // @JavaScriptBody(args = {"imageData", "x", "y"},
392 // body = "this._context().putImageData(imageData,x,y);")
393 // private native void putImageDataImpl(Object imageData, double x, double y);
396 // public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
397 // putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
400 @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
401 body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
402 private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
404 @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
406 public native void setGlobalAlpha(double alpha);
408 @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
410 public native double getGlobalAlpha();
412 @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
414 public native void setGlobalCompositeOperation(String operation);
416 @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
418 public native String getGlobalCompositeOperation();
420 public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
421 return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
424 @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
425 private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
427 public PatternWrapper createPatternWrapper(ImageData image, String repeat) {
428 return new PatternWrapper(createPatternImpl(context, image, repeat));
431 @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
432 private static native Object createPatternImpl(Object context, ImageData image, String repeat);
434 public RadialGradientWrapper createRadialGradientWrapper(double x0, double y0, double r0, double x1, double y1, double r1) {
435 return new RadialGradientWrapper(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
438 @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
439 private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
442 // @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
443 // public native Image getImageForPath(String path);
448 public int getHeight() {
449 return canvas.getHeight();
453 public int getWidth() {
454 return canvas.getWidth();
458 public void setHeight(int height) {
459 canvas.setHeight(height);
463 public void setWidth(int width) {
464 canvas.setWidth(width);