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.util.HashMap;
22 import net.java.html.canvas.Dimension;
23 import net.java.html.canvas.LinearGradient;
24 import net.java.html.canvas.Pattern;
25 import net.java.html.canvas.RadialGradient;
26 import net.java.html.canvas.Style;
27 import net.java.html.canvas.spi.GraphicsEnvironment;
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
29 import org.apidesign.bck2brwsr.htmlpage.api.Canvas;
33 * @author Anton Epple <toni.epple@eppleton.de>
35 public class HTML5GraphicsEnvironment implements GraphicsEnvironment {
40 public HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
41 this.context = contextImpl;
45 @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
46 body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
48 public native void arc(double centerX,
55 @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
56 body = "this._context().arcTo(x1,y1,x2,y2,r);")
58 public native void arcTo(double x1,
64 @JavaScriptBody(args = {"x", "y"},
65 body = "return this._context().isPointInPath(x,y);")
67 public native boolean isPointInPath(double x, double y);
69 @JavaScriptBody(args = {}, body = "this._context().fill();")
71 public native void fill();
73 @JavaScriptBody(args = {}, body = "this._context().stroke();")
75 public native void stroke();
77 @JavaScriptBody(args = {}, body = "this._context().beginPath();")
79 public native void beginPath();
81 @JavaScriptBody(args = {}, body = "this._context().closePath();")
83 public native void closePath();
85 @JavaScriptBody(args = {}, body = "this._context().clip();")
87 public native void clip();
89 @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
91 public native void moveTo(double x, double y);
93 @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
95 public native void lineTo(double x, double y);
97 @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
99 public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
101 @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
102 body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
104 public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
106 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
108 public native void fillRect(double x, double y, double width, double height);
110 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
112 public native void strokeRect(double x, double y, double width, double height);
114 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
116 public native void clearRect(double x, double y, double width, double height);
118 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
120 public native void rect(double x, double y, double width, double height);
122 @JavaScriptBody(args = {}, body = "this._context().save();")
124 public native void save();
126 @JavaScriptBody(args = {}, body = "this._context().restore();")
128 public native void restore();
130 @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
132 public native void rotate(double angle);
134 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
136 public native void transform(double a, double b, double c, double d, double e, double f);
138 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
140 public native void setTransform(double a, double b, double c, double d, double e, double f);
142 @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
144 public native void translate(double x, double y);
146 @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
148 public native void scale(double x, double y);
151 //// public void drawImage(Image image, double x, double y) {
152 //// drawImageImpl(context, Element.getElementById((Image) image), x, y);
156 //// public void drawImage(Image image, double x, double y, double width, double height) {
157 //// drawImageImpl(context, Element.getElementById((Image) image), x, y, width, height);
161 //// public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) {
162 //// drawImageImpl(context, Element.getElementById((Image) image), sx, sy, sWidth, sHeight, x, y, width, height);
165 //// @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height);")
166 //// private native static void drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
168 //// @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);")
169 //// 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);
171 //// @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y);")
172 //// private native static void drawImageImpl(Object ctx, Object img, double x, double y);
173 public Object setFillStyle(Style style, Object nativeStyle) {
174 if (nativeStyle == null) {
175 nativeStyle = createNativeStyle(style);
177 setFillStyleImpl(context, nativeStyle);
181 private Object createNativeStyle(Style style) {
182 if (style instanceof RadialGradient) {
183 RadialGradientWrapper gradient = createRadialGradientWrapper(
184 ((RadialGradient) style).getX0(),
185 ((RadialGradient) style).getY0(),
186 ((RadialGradient) style).getR0(),
187 ((RadialGradient) style).getX1(),
188 ((RadialGradient) style).getY1(),
189 ((RadialGradient) style).getR1());
190 HashMap<Double, String> stops = ((LinearGradient) style).getStops();
191 Set<Double> keySet = stops.keySet();
192 for (Double double1 : keySet) {
193 addColorStopImpl(style, double1, stops.get(double1));
198 } else if (style instanceof LinearGradient) {
199 LinearGradientWrapper gradient = createLinearGradientWrapper(
200 ((LinearGradient) style).getX0(),
201 ((LinearGradient) style).getY0(),
202 ((LinearGradient) style).getX1(),
203 ((LinearGradient) style).getY1());
204 HashMap<Double, String> stops = ((LinearGradient) style).getStops();
205 Set<Double> keySet = stops.keySet();
206 for (Double double1 : keySet) {
207 addColorStopImpl(style, double1, stops.get(double1));
210 } else if (style instanceof Pattern) {
211 // return createPatternWrapper(((Pattern) style).getImage(), ((Pattern) style).getRepeat());
216 @JavaScriptBody(args = {"gradient", "position", "color"}, body =
217 "gradient.addColorStop(position,color)")
218 private static native void addColorStopImpl(Object gradient, double position, String color);
220 @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
221 private native void setFillStyleImpl(Object context, Object obj);
223 @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
224 public native void setStrokeStyle(String style);
227 public Object setStrokeStyle(Style style, Object nativeStyle) {
228 if (nativeStyle == null){
229 nativeStyle = createNativeStyle(style);
231 setStrokeStyleImpl(context, nativeStyle);
235 @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
236 private native void setStrokeStyleImpl(Object context, Object obj);
238 @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
240 public native void setShadowColor(String color);
242 @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
244 public native void setShadowBlur(double blur);
246 @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
248 public native void setShadowOffsetX(double x);
250 @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
252 public native void setShadowOffsetY(double y);
254 @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
255 public native String getStrokeStyle();
257 @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
259 public native String getShadowColor();
261 @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
263 public native double getShadowBlur();
265 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
267 public native double getShadowOffsetX();
269 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
271 public native double getShadowOffsetY();
273 @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
275 public native String getLineCap();
277 @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
279 public native void setLineCap(String style);
281 @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
283 public native String getLineJoin();
285 @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
287 public native void setLineJoin(String style);
289 @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
291 public native double getLineWidth();
293 @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
295 public native void setLineWidth(double width);
297 @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
299 public native double getMiterLimit();
301 @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
303 public native void setMiterLimit(double limit);
305 @JavaScriptBody(args = {}, body = "return this._context().font;")
307 public native String getFont();
309 @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
311 public native void setFont(String font);
313 @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
315 public native String getTextAlign();
317 @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
319 public native void setTextAlign(String textAlign);
321 @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
323 public native String getTextBaseline();
325 @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
327 public native void setTextBaseline(String textbaseline);
329 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
330 // @JavaScriptBody(args = {"text", "x", "y"}, body = "console.log(text);")
332 public native void fillText(String text, double x, double y);
334 @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
336 public void fillText(String text, double x, double y, double maxWidth) {
340 public Dimension measureText(String text) {
341 measureTextImpl(text);
342 return new Dimension(1, 1);
345 @JavaScriptBody(args = {"text"},
346 body = "return this._context().measureText(text);")
347 private native Object measureTextImpl(String text);
349 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
351 public native void strokeText(String text, double x, double y);
353 @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
355 public native void strokeText(String text, double x, double y, double maxWidth);
358 //// public ImageData createImageData(double x, double y) {
359 //// return new ImageDataWrapper(createImageDataImpl(x, y));
362 //// @JavaScriptBody(args = {"x", "y"},
363 //// body = "return this._context().createImageData(x,y);")
364 //// private native Object createImageDataImpl(double x, double y);
367 //// public ImageData createImageData(ImageData imageData) {
368 //// return new ImageDataWrapper(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
372 //// public ImageData getImageData(double x, double y, double width, double height) {
373 //// return new ImageDataWrapper(getImageDataImpl(x, y, width, height));
376 // @JavaScriptBody(args = {"x", "y", "width", "height"},
377 // body = "return this._context().getImageData(x,y,width,height);")
378 // private native Object getImageDataImpl(double x, double y, double width, double height);
381 //// public void putImageData(ImageData imageData, double x, double y) {
382 //// putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y);
385 //// @JavaScriptBody(args = {"imageData", "x", "y"},
386 //// body = "this._context().putImageData(imageData,x,y);")
387 //// private native void putImageDataImpl(Object imageData, double x, double y);
390 //// public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
391 //// putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
393 @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
394 body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
395 private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
397 @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
399 public native void setGlobalAlpha(double alpha);
401 @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
403 public native double getGlobalAlpha();
405 @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
407 public native void setGlobalCompositeOperation(String operation);
409 @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
411 public native String getGlobalCompositeOperation();
413 public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
414 return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
417 @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
418 private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
420 // public PatternWrapper createPatternWrapper(Image image, String repeat) {
421 // return new PatternWrapper(createPatternImpl(context, image, repeat));
424 // @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
425 // private static native Object createPatternImpl(Object context, Image image, String repeat);
426 public RadialGradientWrapper createRadialGradientWrapper(double x0, double y0, double r0, double x1, double y1, double r1) {
427 return new RadialGradientWrapper(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
430 @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
431 private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
434 //// @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
435 //// public native Image getImageForPath(String path);
437 public int getHeight() {
438 return canvas.getHeight();
442 public int getWidth() {
443 return canvas.getWidth();
447 public void setHeight(int height) {
448 canvas.setHeight(height);
452 public void setWidth(int width) {
453 canvas.setWidth(width);