javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/HTML5GraphicsEnvironment.java
temporarily disabled ImageData before we found a nice solution. Maybe introduce PixelReader PixelWriter for GraphicsContext.
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;
20 import java.awt.Dimension;
21 import java.util.HashMap;
23 import net.java.html.canvas.GraphicsEnvironment;
24 import net.java.html.canvas.Image;
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;
33 * @author Anton Epple <toni.epple@eppleton.de>
35 public class HTML5GraphicsEnvironment implements GraphicsEnvironment {
39 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((ImageWrapper) image), x, y);
155 public void drawImage(Image image, double x, double y, double width, double height) {
156 drawImageImpl(context, Element.getElementById((ImageWrapper) 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((ImageWrapper) 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));
215 } else if (style instanceof Pattern) {
216 return createPatternWrapper(((Pattern) style).getImageData(), ((Pattern) style).getRepeat());
222 @JavaScriptBody(args = {"gradient", "position", "color"}, body =
223 "gradient.addColorStop(position,color)")
224 private static native void addColorStopImpl(Object gradient, double position, String color);
226 @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
227 private native void setFillStyleImpl(Object context, Object obj);
229 @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
231 public native void setStrokeStyle(String style);
234 public void setStrokeStyle(Style style) {
235 setStrokeStyleImpl(context, createFillStyle(style));
238 @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
239 private native void setStrokeStyleImpl(Object context, Object obj);
241 @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
243 public native void setShadowColor(String color);
245 @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
247 public native void setShadowBlur(double blur);
249 @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
251 public native void setShadowOffsetX(double x);
253 @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
255 public native void setShadowOffsetY(double y);
257 @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
259 public native String getStrokeStyle();
261 @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
263 public native String getShadowColor();
265 @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
267 public native double getShadowBlur();
269 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
271 public native double getShadowOffsetX();
273 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
275 public native double getShadowOffsetY();
277 @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
279 public native String getLineCap();
281 @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
283 public native void setLineCap(String style);
285 @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
287 public native String getLineJoin();
289 @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
291 public native void setLineJoin(String style);
293 @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
295 public native double getLineWidth();
297 @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
299 public native void setLineWidth(double width);
301 @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
303 public native double getMiterLimit();
305 @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
307 public native void setMiterLimit(double limit);
309 @JavaScriptBody(args = {}, body = "return this._context().font;")
311 public native String getFont();
313 @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
315 public native void setFont(String font);
317 @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
319 public native String getTextAlign();
321 @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
323 public native void setTextAlign(String textAlign);
325 @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
327 public native String getTextBaseline();
329 @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
331 public native void setTextBaseline(String textbaseline);
333 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
335 public native void fillText(String text, double x, double y);
337 @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
339 public void fillText(String text, double x, double y, double maxWidth) {
343 public Dimension measureText(String text) {
344 TextMetrics textMetrics = new TextMetrics(measureTextImpl(text));
345 return new Dimension((int) textMetrics.getWidth(), (int) textMetrics.getHeight());
348 @JavaScriptBody(args = {"text"},
349 body = "return this._context().measureText(text);")
350 private native Object measureTextImpl(String text);
352 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
354 public native void strokeText(String text, double x, double y);
356 @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
358 public native void strokeText(String text, double x, double y, double maxWidth);
361 // public Image createImageData(double x, double y) {
362 // return new ImageDataWrapper(createImageDataImpl(x, y));
365 // @JavaScriptBody(args = {"x", "y"},
366 // body = "return this._context().createImageData(x,y);")
367 // private native Object createImageDataImpl(double x, double y);
370 // public Image createImageData(Image imageData) {
371 // return new ImageDataWrapper(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
375 // public Image getImageData(double x, double y, double width, double height) {
376 // return new ImageDataWrapper(getImageDataImpl(x, y, width, height));
379 @JavaScriptBody(args = {"x", "y", "width", "height"},
380 body = "return this._context().getImageData(x,y,width,height);")
381 private native Object getImageDataImpl(double x, double y, double width, double height);
384 // public void putImageData(Image imageData, double x, double y) {
385 // putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y);
388 // @JavaScriptBody(args = {"imageData", "x", "y"},
389 // body = "this._context().putImageData(imageData,x,y);")
390 // private native void putImageDataImpl(Object imageData, double x, double y);
393 // public void putImageData(Image imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
394 // putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
397 @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
398 body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
399 private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
401 @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
403 public native void setGlobalAlpha(double alpha);
405 @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
407 public native double getGlobalAlpha();
409 @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
411 public native void setGlobalCompositeOperation(String operation);
413 @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
415 public native String getGlobalCompositeOperation();
417 public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
418 return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
421 @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
422 private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
424 public PatternWrapper createPatternWrapper(Image image, String repeat) {
425 return new PatternWrapper(createPatternImpl(context, image, repeat));
428 @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
429 private static native Object createPatternImpl(Object context, Image image, String repeat);
431 public RadialGradientWrapper createRadialGradientWrapper(double x0, double y0, double r0, double x1, double y1, double r1) {
432 return new RadialGradientWrapper(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
435 @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
436 private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
439 @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
440 public native ImageWrapper getImageForPath(String path);
445 public int getHeight() {
446 return canvas.getHeight();
450 public int getWidth() {
451 return canvas.getWidth();
455 public void setHeight(int height) {
456 canvas.setHeight(height);
460 public void setWidth(int width) {
461 canvas.setWidth(width);