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 {
39 public HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
40 this.context = contextImpl;
44 @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
45 body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
47 public native void arc(double centerX,
54 @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
55 body = "this._context().arcTo(x1,y1,x2,y2,r);")
57 public native void arcTo(double x1,
63 @JavaScriptBody(args = {"x", "y"},
64 body = "return this._context().isPointInPath(x,y);")
66 public native boolean isPointInPath(double x, double y);
68 @JavaScriptBody(args = {}, body = "this._context().fill();")
70 public native void fill();
72 @JavaScriptBody(args = {}, body = "this._context().stroke();")
74 public native void stroke();
76 @JavaScriptBody(args = {}, body = "this._context().beginPath();")
78 public native void beginPath();
80 @JavaScriptBody(args = {}, body = "this._context().closePath();")
82 public native void closePath();
84 @JavaScriptBody(args = {}, body = "this._context().clip();")
86 public native void clip();
88 @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
90 public native void moveTo(double x, double y);
92 @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
94 public native void lineTo(double x, double y);
96 @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
98 public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
100 @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
101 body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
103 public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
105 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
107 public native void fillRect(double x, double y, double width, double height);
109 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
111 public native void strokeRect(double x, double y, double width, double height);
113 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
115 public native void clearRect(double x, double y, double width, double height);
117 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
119 public native void rect(double x, double y, double width, double height);
121 @JavaScriptBody(args = {}, body = "this._context().save();")
123 public native void save();
125 @JavaScriptBody(args = {}, body = "this._context().restore();")
127 public native void restore();
129 @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
131 public native void rotate(double angle);
133 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
135 public native void transform(double a, double b, double c, double d, double e, double f);
137 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
139 public native void setTransform(double a, double b, double c, double d, double e, double f);
141 @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
143 public native void translate(double x, double y);
145 @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
147 public native void scale(double x, double y);
150 //// public void drawImage(Image image, double x, double y) {
151 //// drawImageImpl(context, Element.getElementById((Image) image), x, y);
155 //// public void drawImage(Image image, double x, double y, double width, double height) {
156 //// drawImageImpl(context, Element.getElementById((Image) image), x, y, width, height);
160 //// public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) {
161 //// drawImageImpl(context, Element.getElementById((Image) image), sx, sy, sWidth, sHeight, x, y, width, height);
164 //// @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height);")
165 //// private native static void drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
167 //// @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);")
168 //// 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);
170 //// @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y);")
171 //// private native static void drawImageImpl(Object ctx, Object img, double x, double y);
173 @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style.valueOf();")
175 public native void setFillStyle(String style);
177 @JavaScriptBody(args = {}, body = "return this._context().fillStyle;")
179 public native String getFillStyle();
182 public void setFillStyle(Style style) {
183 setFillStyleImpl(context, createFillStyle(style));
186 private Object createFillStyle(Style style) {
187 if (style instanceof RadialGradient) {
188 RadialGradientWrapper gradient = createRadialGradientWrapper(
189 ((RadialGradient) style).getX0(),
190 ((RadialGradient) style).getY0(),
191 ((RadialGradient) style).getR0(),
192 ((RadialGradient) style).getX1(),
193 ((RadialGradient) style).getY1(),
194 ((RadialGradient) style).getR1());
195 HashMap<Double, String> stops = ((LinearGradient) style).getStops();
196 Set<Double> keySet = stops.keySet();
197 for (Double double1 : keySet) {
198 addColorStopImpl(style, double1, stops.get(double1));
203 } else if (style instanceof LinearGradient) {
204 LinearGradientWrapper gradient = createLinearGradientWrapper(
205 ((LinearGradient) style).getX0(),
206 ((LinearGradient) style).getY0(),
207 ((LinearGradient) style).getX1(),
208 ((LinearGradient) style).getY1());
209 HashMap<Double, String> stops = ((LinearGradient) style).getStops();
210 Set<Double> keySet = stops.keySet();
211 for (Double double1 : keySet) {
212 addColorStopImpl(style, double1, stops.get(double1));
216 else if (style instanceof Pattern) {
217 // return createPatternWrapper(((Pattern) style).getImage(), ((Pattern) style).getRepeat());
223 @JavaScriptBody(args = {"gradient", "position", "color"}, body =
224 "gradient.addColorStop(position,color)")
225 private static native void addColorStopImpl(Object gradient, double position, String color);
227 @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
228 private native void setFillStyleImpl(Object context, Object obj);
230 @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
232 public native void setStrokeStyle(String style);
235 public void setStrokeStyle(Style style) {
236 setStrokeStyleImpl(context, createFillStyle(style));
239 @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
240 private native void setStrokeStyleImpl(Object context, Object obj);
242 @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
244 public native void setShadowColor(String color);
246 @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
248 public native void setShadowBlur(double blur);
250 @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
252 public native void setShadowOffsetX(double x);
254 @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
256 public native void setShadowOffsetY(double y);
258 @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
260 public native String getStrokeStyle();
262 @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
264 public native String getShadowColor();
266 @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
268 public native double getShadowBlur();
270 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
272 public native double getShadowOffsetX();
274 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
276 public native double getShadowOffsetY();
278 @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
280 public native String getLineCap();
282 @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
284 public native void setLineCap(String style);
286 @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
288 public native String getLineJoin();
290 @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
292 public native void setLineJoin(String style);
294 @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
296 public native double getLineWidth();
298 @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
300 public native void setLineWidth(double width);
302 @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
304 public native double getMiterLimit();
306 @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
308 public native void setMiterLimit(double limit);
310 @JavaScriptBody(args = {}, body = "return this._context().font;")
312 public native String getFont();
314 @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
316 public native void setFont(String font);
318 @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
320 public native String getTextAlign();
322 @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
324 public native void setTextAlign(String textAlign);
326 @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
328 public native String getTextBaseline();
330 @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
332 public native void setTextBaseline(String textbaseline);
334 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
335 // @JavaScriptBody(args = {"text", "x", "y"}, body = "console.log(text);")
337 public native void fillText(String text, double x, double y);
339 @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
341 public void fillText(String text, double x, double y, double maxWidth) {
345 public Dimension measureText(String text) {
346 measureTextImpl(text);
347 return new Dimension(1,1);
350 @JavaScriptBody(args = {"text"},
351 body = "return this._context().measureText(text);")
352 private native Object measureTextImpl(String text);
354 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
356 public native void strokeText(String text, double x, double y);
358 @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
360 public native void strokeText(String text, double x, double y, double maxWidth);
363 //// public ImageData createImageData(double x, double y) {
364 //// return new ImageDataWrapper(createImageDataImpl(x, y));
367 //// @JavaScriptBody(args = {"x", "y"},
368 //// body = "return this._context().createImageData(x,y);")
369 //// private native Object createImageDataImpl(double x, double y);
372 //// public ImageData createImageData(ImageData imageData) {
373 //// return new ImageDataWrapper(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
377 //// public ImageData getImageData(double x, double y, double width, double height) {
378 //// return new ImageDataWrapper(getImageDataImpl(x, y, width, height));
381 // @JavaScriptBody(args = {"x", "y", "width", "height"},
382 // body = "return this._context().getImageData(x,y,width,height);")
383 // private native Object getImageDataImpl(double x, double y, double width, double height);
386 //// public void putImageData(ImageData imageData, double x, double y) {
387 //// putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y);
390 //// @JavaScriptBody(args = {"imageData", "x", "y"},
391 //// body = "this._context().putImageData(imageData,x,y);")
392 //// private native void putImageDataImpl(Object imageData, double x, double y);
395 //// public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
396 //// putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
399 @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
400 body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
401 private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
403 @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
405 public native void setGlobalAlpha(double alpha);
407 @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
409 public native double getGlobalAlpha();
411 @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
413 public native void setGlobalCompositeOperation(String operation);
415 @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
417 public native String getGlobalCompositeOperation();
419 public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
420 return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
423 @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
424 private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
426 // public PatternWrapper createPatternWrapper(Image image, String repeat) {
427 // return new PatternWrapper(createPatternImpl(context, image, repeat));
430 // @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
431 // private static native Object createPatternImpl(Object context, Image image, String repeat);
433 public RadialGradientWrapper createRadialGradientWrapper(double x0, double y0, double r0, double x1, double y1, double r1) {
434 return new RadialGradientWrapper(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
437 @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
438 private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
441 //// @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
442 //// public native Image getImageForPath(String path);
447 public int getHeight() {
448 return canvas.getHeight();
452 public int getWidth() {
453 return canvas.getWidth();
457 public void setHeight(int height) {
458 canvas.setHeight(height);
462 public void setWidth(int width) {
463 canvas.setWidth(width);