javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/HTML5GraphicsEnvironment.java
Readded Image and ImageData to have the complete API again. No need to use Data in API anymore. Added caching to Image. Image are not required to be added to the page anymore, but are created in javaScript instead.
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.util.HashMap;
22 import net.java.html.canvas.Dimension;
23 import net.java.html.canvas.Image;
24 import net.java.html.canvas.LinearGradient;
25 import net.java.html.canvas.Pattern;
26 import net.java.html.canvas.ImageData;
27 import net.java.html.canvas.RadialGradient;
28 import net.java.html.canvas.Style;
29 import net.java.html.canvas.spi.GraphicsEnvironment;
30 import org.apidesign.bck2brwsr.core.JavaScriptBody;
31 import org.apidesign.bck2brwsr.htmlpage.api.Canvas;
32 import org.apidesign.bck2brwsr.htmlpage.api.Element;
36 * @author Anton Epple <toni.epple@eppleton.de>
38 public class HTML5GraphicsEnvironment implements GraphicsEnvironment {
43 public HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
44 this.context = contextImpl;
48 @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
49 body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
51 public native void arc(double centerX,
58 @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
59 body = "this._context().arcTo(x1,y1,x2,y2,r);")
61 public native void arcTo(double x1,
67 @JavaScriptBody(args = {"x", "y"},
68 body = "return this._context().isPointInPath(x,y);")
70 public native boolean isPointInPath(double x, double y);
72 @JavaScriptBody(args = {}, body = "this._context().fill();")
74 public native void fill();
76 @JavaScriptBody(args = {}, body = "this._context().stroke();")
78 public native void stroke();
80 @JavaScriptBody(args = {}, body = "this._context().beginPath();")
82 public native void beginPath();
84 @JavaScriptBody(args = {}, body = "this._context().closePath();")
86 public native void closePath();
88 @JavaScriptBody(args = {}, body = "this._context().clip();")
90 public native void clip();
92 @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
94 public native void moveTo(double x, double y);
96 @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
98 public native void lineTo(double x, double y);
100 @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
102 public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
104 @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
105 body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
107 public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
109 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
111 public native void fillRect(double x, double y, double width, double height);
113 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
115 public native void strokeRect(double x, double y, double width, double height);
117 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
119 public native void clearRect(double x, double y, double width, double height);
121 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rect(x,y,width,height);")
123 public native void rect(double x, double y, double width, double height);
125 @JavaScriptBody(args = {}, body = "this._context().save();")
127 public native void save();
129 @JavaScriptBody(args = {}, body = "this._context().restore();")
131 public native void restore();
133 @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
135 public native void rotate(double angle);
137 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
139 public native void transform(double a, double b, double c, double d, double e, double f);
141 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
143 public native void setTransform(double a, double b, double c, double d, double e, double f);
145 @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
147 public native void translate(double x, double y);
149 @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
151 public native void scale(double x, double y);
154 public Object drawImage(Image image, double x, double y, Object nativeImage) {
155 if (nativeImage == null) {
156 nativeImage = createImage(image.getSrc());
158 return drawImageImpl(context, nativeImage, x, y);
162 public Object drawImage(Image image, double x, double y, double width, double height, Object nativeImage) {
163 if (nativeImage == null) {
164 nativeImage = createImage(image.getSrc());
166 return drawImageImpl(context, nativeImage, x, y, width, height);
170 public Object drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height, Object nativeImage) {
171 if (nativeImage == null) {
172 nativeImage = createImage(image.getSrc());
174 return drawImageImpl(context, nativeImage, sx, sy, sWidth, sHeight, x, y, width, height);
177 @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height); return img;")
178 private native static Object drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
180 @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); return img;")
181 private native static Object drawImageImpl(Object ctx, Object img, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height);
183 @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y); return img;")
184 private native static Object drawImageImpl(Object ctx, Object img, double x, double y);
186 public Object setFillStyle(Style style, Object nativeStyle) {
187 if (nativeStyle == null) {
188 nativeStyle = createNativeStyle(style);
190 setFillStyleImpl(context, nativeStyle);
194 private Object createNativeStyle(Style style) {
195 if (style instanceof RadialGradient) {
196 RadialGradientWrapper gradient = createRadialGradientWrapper(
197 ((RadialGradient) style).getX0(),
198 ((RadialGradient) style).getY0(),
199 ((RadialGradient) style).getR0(),
200 ((RadialGradient) style).getX1(),
201 ((RadialGradient) style).getY1(),
202 ((RadialGradient) style).getR1());
203 HashMap<Double, String> stops = ((LinearGradient) style).getStops();
204 Set<Double> keySet = stops.keySet();
205 for (Double double1 : keySet) {
206 addColorStopImpl(style, double1, stops.get(double1));
211 } else if (style instanceof LinearGradient) {
212 LinearGradientWrapper gradient = createLinearGradientWrapper(
213 ((LinearGradient) style).getX0(),
214 ((LinearGradient) style).getY0(),
215 ((LinearGradient) style).getX1(),
216 ((LinearGradient) style).getY1());
217 HashMap<Double, String> stops = ((LinearGradient) style).getStops();
218 Set<Double> keySet = stops.keySet();
219 for (Double double1 : keySet) {
220 addColorStopImpl(style, double1, stops.get(double1));
223 } else if (style instanceof Pattern) {
224 // return createPatternWrapper(((Pattern) style).getImage(), ((Pattern) style).getRepeat());
229 @JavaScriptBody(args = {"gradient", "position", "color"}, body =
230 "gradient.addColorStop(position,color)")
231 private static native void addColorStopImpl(Object gradient, double position, String color);
233 @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
234 private native void setFillStyleImpl(Object context, Object obj);
236 @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
237 public native void setStrokeStyle(String style);
240 public Object setStrokeStyle(Style style, Object nativeStyle) {
241 if (nativeStyle == null) {
242 nativeStyle = createNativeStyle(style);
244 setStrokeStyleImpl(context, nativeStyle);
248 @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
249 private native void setStrokeStyleImpl(Object context, Object obj);
251 @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
253 public native void setShadowColor(String color);
255 @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
257 public native void setShadowBlur(double blur);
259 @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
261 public native void setShadowOffsetX(double x);
263 @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
265 public native void setShadowOffsetY(double y);
267 @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
268 public native String getStrokeStyle();
270 @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
272 public native String getShadowColor();
274 @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
276 public native double getShadowBlur();
278 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
280 public native double getShadowOffsetX();
282 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
284 public native double getShadowOffsetY();
286 @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
288 public native String getLineCap();
290 @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
292 public native void setLineCap(String style);
294 @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
296 public native String getLineJoin();
298 @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
300 public native void setLineJoin(String style);
302 @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
304 public native double getLineWidth();
306 @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
308 public native void setLineWidth(double width);
310 @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
312 public native double getMiterLimit();
314 @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
316 public native void setMiterLimit(double limit);
318 @JavaScriptBody(args = {}, body = "return this._context().font;")
320 public native String getFont();
322 @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
324 public native void setFont(String font);
326 @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
328 public native String getTextAlign();
330 @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
332 public native void setTextAlign(String textAlign);
334 @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
336 public native String getTextBaseline();
338 @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
340 public native void setTextBaseline(String textbaseline);
342 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
343 // @JavaScriptBody(args = {"text", "x", "y"}, body = "console.log(text);")
345 public native void fillText(String text, double x, double y);
347 @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
349 public void fillText(String text, double x, double y, double maxWidth) {
353 public Dimension measureText(String text) {
354 measureTextImpl(text);
355 return new Dimension(1, 1);
358 @JavaScriptBody(args = {"text"},
359 body = "return this._context().measureText(text);")
360 private native Object measureTextImpl(String text);
362 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
364 public native void strokeText(String text, double x, double y);
366 @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
368 public native void strokeText(String text, double x, double y, double maxWidth);
371 public ImageData createPixelMap(double x, double y) {
372 return new ImageDataWrapper(createPixelMapImpl(x, y));
375 @JavaScriptBody(args = {"x", "y"},
376 body = "return this._context().createImageData(x,y);")
377 private native Object createPixelMapImpl(double x, double y);
380 public ImageData createPixelMap(ImageData imageData) {
381 return new ImageDataWrapper(createPixelMapImpl(imageData.getWidth(), imageData.getHeight()));
385 public ImageData getPixelMap(double x, double y, double width, double height) {
386 return new ImageDataWrapper(getPixelMapImpl(x, y, width, height));
389 @JavaScriptBody(args = {"x", "y", "width", "height"},
390 body = "return this._context().getImageData(x,y,width,height);")
391 private native Object getPixelMapImpl(double x, double y, double width, double height);
394 public void putPixelMap(ImageData imageData, double x, double y) {
395 putPixelMapImpl(((ImageDataWrapper) imageData).object(), x, y);
398 @JavaScriptBody(args = {"imageData", "x", "y"},
399 body = "this._context().putImageData(imageData,x,y);")
400 private native void putPixelMapImpl(Object imageData, double x, double y);
403 public void putPixelMap(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
404 putPixelMapImpl(((ImageDataWrapper) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
407 @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
408 body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
409 private native void putPixelMapImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
411 @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
413 public native void setGlobalAlpha(double alpha);
415 @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
417 public native double getGlobalAlpha();
419 @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
421 public native void setGlobalCompositeOperation(String operation);
423 @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
425 public native String getGlobalCompositeOperation();
427 public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
428 return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
431 @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
432 private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
434 public PatternWrapper createPatternWrapper(Image image, String repeat) {
435 return new PatternWrapper(createPatternImpl(context, image, repeat));
438 @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
439 private static native Object createPatternImpl(Object context, Image image, String repeat);
441 public RadialGradientWrapper createRadialGradientWrapper(double x0, double y0, double r0, double x1, double y1, double r1) {
442 return new RadialGradientWrapper(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
445 @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
446 private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
448 @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
449 public native Image getImageForPath(String path);
452 public int getHeight() {
453 return canvas.getHeight();
457 public int getWidth() {
458 return canvas.getWidth();
462 public void setHeight(int height) {
463 canvas.setHeight(height);
467 public void setWidth(int width) {
468 canvas.setWidth(width);
471 @JavaScriptBody(args = {"src"}, body = "var image = new Image(); image.src = src; return image;")
472 private static native Object createImage(String src);