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;
23 import net.java.html.canvas.Dimension;
24 import net.java.html.canvas.Image;
25 import net.java.html.canvas.ImageData;
26 import net.java.html.canvas.Style;
27 import net.java.html.canvas.Style.Color;
28 import net.java.html.canvas.Style.LinearGradient;
29 import net.java.html.canvas.Style.Pattern;
30 import net.java.html.canvas.Style.RadialGradient;
31 import net.java.html.canvas.spi.GraphicsEnvironment;
32 import org.apidesign.bck2brwsr.core.JavaScriptBody;
33 import org.apidesign.bck2brwsr.htmlpage.api.Canvas;
34 import org.openide.util.lookup.ServiceProvider;
38 * @author Anton Epple <toni.epple@eppleton.de>
40 @ServiceProvider(service = GraphicsEnvironment.class)
41 public class HTML5GraphicsEnvironment implements GraphicsEnvironment {
46 public HTML5GraphicsEnvironment() {
51 public HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
52 this.context = contextImpl;
56 @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
57 body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
59 public native void arc(double centerX,
66 @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
67 body = "this._context().arcTo(x1,y1,x2,y2,r);")
69 public native void arcTo(double x1,
75 @JavaScriptBody(args = {"x", "y"},
76 body = "return this._context().isPointInPath(x,y);")
78 public native boolean isPointInPath(double x, double y);
80 @JavaScriptBody(args = {}, body = "this._context().fill();")
82 public native void fill();
84 @JavaScriptBody(args = {}, body = "this._context().stroke();")
86 public native void stroke();
88 @JavaScriptBody(args = {}, body = "this._context().beginPath();")
90 public native void beginPath();
92 @JavaScriptBody(args = {}, body = "this._context().closePath();")
94 public native void closePath();
96 @JavaScriptBody(args = {}, body = "this._context().clip();")
98 public native void clip();
100 @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
102 public native void moveTo(double x, double y);
104 @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
106 public native void lineTo(double x, double y);
108 @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
110 public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
112 @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
113 body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
115 public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
117 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
119 public native void fillRect(double x, double y, double width, double height);
121 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
123 public native void strokeRect(double x, double y, double width, double height);
125 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
127 public native void clearRect(double x, double y, double width, double height);
129 @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rect(x,y,width,height);")
131 public native void rect(double x, double y, double width, double height);
133 @JavaScriptBody(args = {}, body = "this._context().save();")
135 public native void save();
137 @JavaScriptBody(args = {}, body = "this._context().restore();")
139 public native void restore();
141 @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
143 public native void rotate(double angle);
145 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
147 public native void transform(double a, double b, double c, double d, double e, double f);
149 @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
151 public native void setTransform(double a, double b, double c, double d, double e, double f);
153 @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
155 public native void translate(double x, double y);
157 @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
159 public native void scale(double x, double y);
162 public Object drawImage(Image image, double x, double y, Object nativeImage) {
163 if (nativeImage == null) {
164 nativeImage = createImage(image.getSrc());
166 return drawImageImpl(context, nativeImage, x, y);
170 public Object drawImage(Image image, double x, double y, double width, double height, Object nativeImage) {
171 if (nativeImage == null) {
172 nativeImage = createImage(image.getSrc());
174 return drawImageImpl(context, nativeImage, x, y, width, height);
178 public Object drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height, Object nativeImage) {
179 if (nativeImage == null) {
180 nativeImage = createImage(image.getSrc());
182 return drawImageImpl(context, nativeImage, sx, sy, sWidth, sHeight, x, y, width, height);
185 @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "img.onload=function(){ctx.drawImage(img,x,y,width,height);};ctx.drawImage(img,x,y,width,height); return img;")
186 private native static Object drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
188 @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "img.onload=function(){ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);}; ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); return img;")
189 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);
191 @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "img.onload=function(){ctx.drawImage(img,x,y);}; ctx.drawImage(img,x,y); return img;")
192 private native static Object drawImageImpl(Object ctx, Object img, double x, double y);
194 public Object setFillStyle(Style style, Object nativeStyle) {
195 if (nativeStyle == null) {
196 nativeStyle = createNativeStyle(style);
198 setFillStyleImpl(context, nativeStyle);
202 private Object createNativeStyle(Style style) {
203 if (style instanceof RadialGradient) {
204 RadialGradientWrapper gradient = createRadialGradientWrapper(
205 ((RadialGradient) style).getX0(),
206 ((RadialGradient) style).getY0(),
207 ((RadialGradient) style).getR0(),
208 ((RadialGradient) style).getX1(),
209 ((RadialGradient) style).getY1(),
210 ((RadialGradient) style).getR1());
211 Map<Double, String> stops = ((LinearGradient) style).getStops();
212 Set<Double> keySet = stops.keySet();
213 for (Double double1 : keySet) {
214 addColorStopImpl(style, double1, stops.get(double1));
219 } else if (style instanceof LinearGradient) {
220 LinearGradientWrapper gradient = createLinearGradientWrapper(
221 ((LinearGradient) style).getX0(),
222 ((LinearGradient) style).getY0(),
223 ((LinearGradient) style).getX1(),
224 ((LinearGradient) style).getY1());
225 Map<Double, String> stops = ((LinearGradient) style).getStops();
226 Set<Double> keySet = stops.keySet();
227 for (Double double1 : keySet) {
228 addColorStopImpl(style, double1, stops.get(double1));
231 } else if (style instanceof Pattern) {
232 return createPatternWrapper(((Pattern) style).getImageResource(), ((Pattern) style).getRepeat());
233 } else if (style instanceof Color) {
234 return ((Color) style)
240 @JavaScriptBody(args = {"gradient", "position", "color"}, body =
241 "gradient.addColorStop(position,color)")
242 private static native void addColorStopImpl(Object gradient, double position, String color);
244 @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
245 private native void setFillStyleImpl(Object context, Object obj);
247 @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
248 public native void setStrokeStyle(String style);
251 public Object setStrokeStyle(Style style, Object nativeStyle) {
252 if (nativeStyle == null) {
253 nativeStyle = createNativeStyle(style);
255 setStrokeStyleImpl(context, nativeStyle);
259 @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
260 private native void setStrokeStyleImpl(Object context, Object obj);
262 @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
264 public native void setShadowColor(String color);
266 @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
268 public native void setShadowBlur(double blur);
270 @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
272 public native void setShadowOffsetX(double x);
274 @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
276 public native void setShadowOffsetY(double y);
278 @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
279 public native String getStrokeStyle();
281 @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
283 public native String getShadowColor();
285 @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
287 public native double getShadowBlur();
289 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
291 public native double getShadowOffsetX();
293 @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
295 public native double getShadowOffsetY();
297 @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
299 public native String getLineCap();
301 @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
303 public native void setLineCap(String style);
305 @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
307 public native String getLineJoin();
309 @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
311 public native void setLineJoin(String style);
313 @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
315 public native double getLineWidth();
317 @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
319 public native void setLineWidth(double width);
321 @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
323 public native double getMiterLimit();
325 @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
327 public native void setMiterLimit(double limit);
329 @JavaScriptBody(args = {}, body = "return this._context().font;")
331 public native String getFont();
333 @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
335 public native void setFont(String font);
337 @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
339 public native String getTextAlign();
341 @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
343 public native void setTextAlign(String textAlign);
345 @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
347 public native String getTextBaseline();
349 @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
351 public native void setTextBaseline(String textbaseline);
353 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
354 // @JavaScriptBody(args = {"text", "x", "y"}, body = "console.log(text);")
356 public native void fillText(String text, double x, double y);
358 @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
360 public void fillText(String text, double x, double y, double maxWidth) {
364 public Dimension measureText(String text) {
365 measureTextImpl(text);
366 return new Dimension(1, 1);
369 @JavaScriptBody(args = {"text"},
370 body = "return this._context().measureText(text);")
371 private native Object measureTextImpl(String text);
373 @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
375 public native void strokeText(String text, double x, double y);
377 @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
379 public native void strokeText(String text, double x, double y, double maxWidth);
382 public ImageData createPixelMap(double x, double y) {
383 return new ImageDataWrapper(createPixelMapImpl(x, y));
386 @JavaScriptBody(args = {"x", "y"},
387 body = "return this._context().createImageData(x,y);")
388 private native Object createPixelMapImpl(double x, double y);
391 public ImageData createPixelMap(ImageData imageData) {
392 return new ImageDataWrapper(createPixelMapImpl(imageData.getWidth(), imageData.getHeight()));
396 public ImageData getPixelMap(double x, double y, double width, double height) {
397 return new ImageDataWrapper(getPixelMapImpl(x, y, width, height));
400 @JavaScriptBody(args = {"x", "y", "width", "height"},
401 body = "return this._context().getImageData(x,y,width,height);")
402 private native Object getPixelMapImpl(double x, double y, double width, double height);
405 public void putPixelMap(ImageData imageData, double x, double y) {
406 putPixelMapImpl(((ImageDataWrapper) imageData).object(), x, y);
409 @JavaScriptBody(args = {"imageData", "x", "y"},
410 body = "this._context().putImageData(imageData,x,y);")
411 private native void putPixelMapImpl(Object imageData, double x, double y);
414 public void putPixelMap(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
415 putPixelMapImpl(((ImageDataWrapper) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
418 @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
419 body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
420 private native void putPixelMapImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
422 @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
424 public native void setGlobalAlpha(double alpha);
426 @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
428 public native double getGlobalAlpha();
430 @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
432 public native void setGlobalCompositeOperation(String operation);
434 @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
436 public native String getGlobalCompositeOperation();
438 public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
439 return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
442 @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
443 private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
445 public PatternWrapper createPatternWrapper(Image image, String repeat) {
446 return new PatternWrapper(createPatternImpl(context, image, repeat));
449 @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
450 private static native Object createPatternImpl(Object context, Image image, String repeat);
452 public RadialGradientWrapper createRadialGradientWrapper(double x0, double y0, double r0, double x1, double y1, double r1) {
453 return new RadialGradientWrapper(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
456 @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
457 private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
459 // @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
460 // public native Image getImageForPathImpl(String path);
462 public int getHeight() {
463 return canvas.getHeight();
467 public int getWidth() {
468 return canvas.getWidth();
472 public void setHeight(int height) {
473 canvas.setHeight(height);
477 public void setWidth(int width) {
478 canvas.setWidth(width);
481 // @JavaScriptBody(args = {"src"}, body = "var image = new Image();console.log('image complete '+image.complete);image.src = './'+ src; return image;")
482 @JavaScriptBody(args = {"src"}, body = "console.log ('looking up image by id '+src);return document.getElementById(src);")
483 private static native Object createImage(String src);
486 public int getWidth(Image image, Object nativeImage) {
488 if (nativeImage == null) {
489 nativeImage = createImage(image.getSrc());
492 return getWidth(nativeImage);
495 @JavaScriptBody(args = {"nativeImage"}, body = "return nativeImage.naturalWidth;")
496 private static native int getWidth(Object nativeImage);
499 public int getHeight(Image image, Object nativeImage) {
500 if (nativeImage == null) {
501 nativeImage = createImage(image.getSrc());
503 return getHeight(nativeImage);
506 @JavaScriptBody(args = {"nativeImage"}, body = "return nativeImage.naturalHeight;")
507 private static native int getHeight(Object nativeImage);
510 public Object mergeImages(Image a, Image b, Object cachedA, Object cachedB) {
511 return mergeImages(cachedA, cachedB);
514 @JavaScriptBody(args = {"img1", "img2"}, body = "var canvas = document.createElement('img');\n"
515 + "var context = canvas.getContext(\"2d\");\n"
516 + "var width = img1.width;\n"
517 + "var height = img1.height;\n"
518 + "canvas.width = width;\n"
519 + "canvas.height = height;\n"
520 + "context.drawImage(img1, 0, 0);\n"
521 + "context.drawImage(img2, 0, 0);\n"
522 + "url = canvas.toDataURL();\n"
523 + "var resultImage = document.createElement('img');\n"
524 + "resultImage.src=url;\n"
525 + "return resultImage;")
526 public static native Object mergeImages(Object img1, Object img2);