javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/HTML5GraphicsEnvironment.java
author Anton Epple <toni.epple@eppleton.de>
Mon, 27 May 2013 08:30:30 +0200
branchcanvas
changeset 1145 0e2c3676d77a
parent 1142 7db01893aaf8
child 1149 78c3cdffe719
permissions -rw-r--r--
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.
     1 /**
     2  * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
     3  * <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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
    12  * details.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage;
    19 
    20 import java.util.HashMap;
    21 import java.util.Set;
    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;
    33 
    34 /**
    35  *
    36  * @author Anton Epple <toni.epple@eppleton.de>
    37  */
    38 public class HTML5GraphicsEnvironment implements GraphicsEnvironment {
    39 
    40     Object context;
    41     Canvas canvas;
    42 
    43     public HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
    44         this.context = contextImpl;
    45         this.canvas = canvas;
    46     }
    47 
    48     @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
    49             body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
    50     @Override
    51     public native void arc(double centerX,
    52             double centerY,
    53             double startAngle,
    54             double radius,
    55             double endAngle,
    56             boolean ccw);
    57 
    58     @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
    59             body = "this._context().arcTo(x1,y1,x2,y2,r);")
    60     @Override
    61     public native void arcTo(double x1,
    62             double y1,
    63             double x2,
    64             double y2,
    65             double r);
    66 
    67     @JavaScriptBody(args = {"x", "y"},
    68             body = "return this._context().isPointInPath(x,y);")
    69     @Override
    70     public native boolean isPointInPath(double x, double y);
    71 
    72     @JavaScriptBody(args = {}, body = "this._context().fill();")
    73     @Override
    74     public native void fill();
    75 
    76     @JavaScriptBody(args = {}, body = "this._context().stroke();")
    77     @Override
    78     public native void stroke();
    79 
    80     @JavaScriptBody(args = {}, body = "this._context().beginPath();")
    81     @Override
    82     public native void beginPath();
    83 
    84     @JavaScriptBody(args = {}, body = "this._context().closePath();")
    85     @Override
    86     public native void closePath();
    87 
    88     @JavaScriptBody(args = {}, body = "this._context().clip();")
    89     @Override
    90     public native void clip();
    91 
    92     @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
    93     @Override
    94     public native void moveTo(double x, double y);
    95 
    96     @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
    97     @Override
    98     public native void lineTo(double x, double y);
    99 
   100     @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
   101     @Override
   102     public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
   103 
   104     @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
   105             body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
   106     @Override
   107     public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
   108 
   109     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
   110     @Override
   111     public native void fillRect(double x, double y, double width, double height);
   112 
   113     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
   114     @Override
   115     public native void strokeRect(double x, double y, double width, double height);
   116 
   117     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
   118     @Override
   119     public native void clearRect(double x, double y, double width, double height);
   120 
   121     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rect(x,y,width,height);")
   122     @Override
   123     public native void rect(double x, double y, double width, double height);
   124 
   125     @JavaScriptBody(args = {}, body = "this._context().save();")
   126     @Override
   127     public native void save();
   128 
   129     @JavaScriptBody(args = {}, body = "this._context().restore();")
   130     @Override
   131     public native void restore();
   132 
   133     @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
   134     @Override
   135     public native void rotate(double angle);
   136 
   137     @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
   138     @Override
   139     public native void transform(double a, double b, double c, double d, double e, double f);
   140 
   141     @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
   142     @Override
   143     public native void setTransform(double a, double b, double c, double d, double e, double f);
   144 
   145     @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
   146     @Override
   147     public native void translate(double x, double y);
   148 
   149     @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
   150     @Override
   151     public native void scale(double x, double y);
   152 
   153     @Override
   154     public Object drawImage(Image image, double x, double y, Object nativeImage) {
   155         if (nativeImage == null) {
   156             nativeImage = createImage(image.getSrc());
   157         }
   158         return drawImageImpl(context, nativeImage, x, y);
   159     }
   160 
   161     @Override
   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());
   165         }
   166         return drawImageImpl(context, nativeImage, x, y, width, height);
   167     }
   168 
   169     @Override
   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());
   173         }
   174         return drawImageImpl(context, nativeImage, sx, sy, sWidth, sHeight, x, y, width, height);
   175     }
   176 
   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);
   179 
   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);
   182 
   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);
   185 
   186     public Object setFillStyle(Style style, Object nativeStyle) {
   187         if (nativeStyle == null) {
   188             nativeStyle = createNativeStyle(style);
   189         }
   190         setFillStyleImpl(context, nativeStyle);
   191         return nativeStyle;
   192     }
   193 
   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));
   207             }
   208             return gradient;
   209 
   210 
   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));
   221             }
   222             return gradient;
   223         } else if (style instanceof Pattern) {
   224 //            return createPatternWrapper(((Pattern) style).getImage(), ((Pattern) style).getRepeat());
   225         }
   226         return null;
   227     }
   228 
   229     @JavaScriptBody(args = {"gradient", "position", "color"}, body =
   230             "gradient.addColorStop(position,color)")
   231     private static native void addColorStopImpl(Object gradient, double position, String color);
   232 
   233     @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
   234     private native void setFillStyleImpl(Object context, Object obj);
   235 
   236     @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
   237     public native void setStrokeStyle(String style);
   238 
   239     @Override
   240     public Object setStrokeStyle(Style style, Object nativeStyle) {
   241         if (nativeStyle == null) {
   242             nativeStyle = createNativeStyle(style);
   243         }
   244         setStrokeStyleImpl(context, nativeStyle);
   245         return nativeStyle;
   246     }
   247 
   248     @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
   249     private native void setStrokeStyleImpl(Object context, Object obj);
   250 
   251     @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
   252     @Override
   253     public native void setShadowColor(String color);
   254 
   255     @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
   256     @Override
   257     public native void setShadowBlur(double blur);
   258 
   259     @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
   260     @Override
   261     public native void setShadowOffsetX(double x);
   262 
   263     @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
   264     @Override
   265     public native void setShadowOffsetY(double y);
   266 
   267     @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
   268     public native String getStrokeStyle();
   269 
   270     @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
   271     @Override
   272     public native String getShadowColor();
   273 
   274     @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
   275     @Override
   276     public native double getShadowBlur();
   277 
   278     @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
   279     @Override
   280     public native double getShadowOffsetX();
   281 
   282     @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
   283     @Override
   284     public native double getShadowOffsetY();
   285 
   286     @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
   287     @Override
   288     public native String getLineCap();
   289 
   290     @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
   291     @Override
   292     public native void setLineCap(String style);
   293 
   294     @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
   295     @Override
   296     public native String getLineJoin();
   297 
   298     @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
   299     @Override
   300     public native void setLineJoin(String style);
   301 
   302     @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
   303     @Override
   304     public native double getLineWidth();
   305 
   306     @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
   307     @Override
   308     public native void setLineWidth(double width);
   309 
   310     @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
   311     @Override
   312     public native double getMiterLimit();
   313 
   314     @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
   315     @Override
   316     public native void setMiterLimit(double limit);
   317 
   318     @JavaScriptBody(args = {}, body = "return this._context().font;")
   319     @Override
   320     public native String getFont();
   321 
   322     @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
   323     @Override
   324     public native void setFont(String font);
   325 
   326     @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
   327     @Override
   328     public native String getTextAlign();
   329 
   330     @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
   331     @Override
   332     public native void setTextAlign(String textAlign);
   333 
   334     @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
   335     @Override
   336     public native String getTextBaseline();
   337 
   338     @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
   339     @Override
   340     public native void setTextBaseline(String textbaseline);
   341 
   342     @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
   343 //    @JavaScriptBody(args = {"text", "x", "y"}, body = "console.log(text);")
   344     @Override
   345     public native void fillText(String text, double x, double y);
   346 
   347     @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
   348     @Override
   349     public void fillText(String text, double x, double y, double maxWidth) {
   350     }
   351 
   352     @Override
   353     public Dimension measureText(String text) {
   354         measureTextImpl(text);
   355         return new Dimension(1, 1);
   356     }
   357 
   358     @JavaScriptBody(args = {"text"},
   359             body = "return this._context().measureText(text);")
   360     private native Object measureTextImpl(String text);
   361 
   362     @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
   363     @Override
   364     public native void strokeText(String text, double x, double y);
   365 
   366     @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
   367     @Override
   368     public native void strokeText(String text, double x, double y, double maxWidth);
   369 
   370     @Override
   371     public ImageData createPixelMap(double x, double y) {
   372         return new ImageDataWrapper(createPixelMapImpl(x, y));
   373     }
   374 
   375     @JavaScriptBody(args = {"x", "y"},
   376             body = "return this._context().createImageData(x,y);")
   377     private native Object createPixelMapImpl(double x, double y);
   378 
   379     @Override
   380     public ImageData createPixelMap(ImageData imageData) {
   381         return new ImageDataWrapper(createPixelMapImpl(imageData.getWidth(), imageData.getHeight()));
   382     }
   383 
   384     @Override
   385     public ImageData getPixelMap(double x, double y, double width, double height) {
   386         return new ImageDataWrapper(getPixelMapImpl(x, y, width, height));
   387     }
   388 
   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);
   392 
   393     @Override
   394     public void putPixelMap(ImageData imageData, double x, double y) {
   395         putPixelMapImpl(((ImageDataWrapper) imageData).object(), x, y);
   396     }
   397 
   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);
   401 
   402     @Override
   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);
   405     }
   406 
   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);
   410 
   411     @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
   412     @Override
   413     public native void setGlobalAlpha(double alpha);
   414 
   415     @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
   416     @Override
   417     public native double getGlobalAlpha();
   418 
   419     @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
   420     @Override
   421     public native void setGlobalCompositeOperation(String operation);
   422 
   423     @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
   424     @Override
   425     public native String getGlobalCompositeOperation();
   426 
   427     public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
   428         return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
   429     }
   430 
   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);
   433 
   434     public PatternWrapper createPatternWrapper(Image image, String repeat) {
   435         return new PatternWrapper(createPatternImpl(context, image, repeat));
   436     }
   437 
   438     @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
   439     private static native Object createPatternImpl(Object context, Image image, String repeat);
   440 
   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));
   443     }
   444 
   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);
   447 
   448     @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
   449     public native Image getImageForPath(String path);
   450 
   451     @Override
   452     public int getHeight() {
   453         return canvas.getHeight();
   454     }
   455 
   456     @Override
   457     public int getWidth() {
   458         return canvas.getWidth();
   459     }
   460 
   461     @Override
   462     public void setHeight(int height) {
   463         canvas.setHeight(height);
   464     }
   465 
   466     @Override
   467     public void setWidth(int width) {
   468         canvas.setWidth(width);
   469     }
   470 
   471     @JavaScriptBody(args = {"src"}, body = "var image = new Image(); image.src = src; return image;")
   472     private static native Object createImage(String src);
   473 }