javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/HTML5GraphicsEnvironment.java
author Anton Epple <toni.epple@eppleton.de>
Fri, 24 May 2013 12:34:38 +0200
branchcanvas
changeset 1142 7db01893aaf8
parent 1138 94bd7330ff58
child 1145 0e2c3676d77a
permissions -rw-r--r--
added caching of style to implementation
     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.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;
    30 
    31 /**
    32  *
    33  * @author Anton Epple <toni.epple@eppleton.de>
    34  */
    35 public class HTML5GraphicsEnvironment implements GraphicsEnvironment {
    36 
    37     Object context;
    38     Canvas canvas;
    39 
    40     public HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
    41         this.context = contextImpl;
    42         this.canvas = canvas;
    43     }
    44 
    45     @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
    46             body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
    47     @Override
    48     public native void arc(double centerX,
    49             double centerY,
    50             double startAngle,
    51             double radius,
    52             double endAngle,
    53             boolean ccw);
    54 
    55     @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
    56             body = "this._context().arcTo(x1,y1,x2,y2,r);")
    57     @Override
    58     public native void arcTo(double x1,
    59             double y1,
    60             double x2,
    61             double y2,
    62             double r);
    63 
    64     @JavaScriptBody(args = {"x", "y"},
    65             body = "return this._context().isPointInPath(x,y);")
    66     @Override
    67     public native boolean isPointInPath(double x, double y);
    68 
    69     @JavaScriptBody(args = {}, body = "this._context().fill();")
    70     @Override
    71     public native void fill();
    72 
    73     @JavaScriptBody(args = {}, body = "this._context().stroke();")
    74     @Override
    75     public native void stroke();
    76 
    77     @JavaScriptBody(args = {}, body = "this._context().beginPath();")
    78     @Override
    79     public native void beginPath();
    80 
    81     @JavaScriptBody(args = {}, body = "this._context().closePath();")
    82     @Override
    83     public native void closePath();
    84 
    85     @JavaScriptBody(args = {}, body = "this._context().clip();")
    86     @Override
    87     public native void clip();
    88 
    89     @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
    90     @Override
    91     public native void moveTo(double x, double y);
    92 
    93     @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
    94     @Override
    95     public native void lineTo(double x, double y);
    96 
    97     @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
    98     @Override
    99     public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
   100 
   101     @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
   102             body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
   103     @Override
   104     public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
   105 
   106     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
   107     @Override
   108     public native void fillRect(double x, double y, double width, double height);
   109 
   110     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
   111     @Override
   112     public native void strokeRect(double x, double y, double width, double height);
   113 
   114     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
   115     @Override
   116     public native void clearRect(double x, double y, double width, double height);
   117 
   118     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
   119     @Override
   120     public native void rect(double x, double y, double width, double height);
   121 
   122     @JavaScriptBody(args = {}, body = "this._context().save();")
   123     @Override
   124     public native void save();
   125 
   126     @JavaScriptBody(args = {}, body = "this._context().restore();")
   127     @Override
   128     public native void restore();
   129 
   130     @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
   131     @Override
   132     public native void rotate(double angle);
   133 
   134     @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
   135     @Override
   136     public native void transform(double a, double b, double c, double d, double e, double f);
   137 
   138     @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
   139     @Override
   140     public native void setTransform(double a, double b, double c, double d, double e, double f);
   141 
   142     @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
   143     @Override
   144     public native void translate(double x, double y);
   145 
   146     @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
   147     @Override
   148     public native void scale(double x, double y);
   149 
   150 ////    @Override
   151 ////    public void drawImage(Image image, double x, double y) {
   152 ////        drawImageImpl(context, Element.getElementById((Image) image), x, y);
   153 ////    }
   154 ////
   155 ////    @Override
   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);
   158 ////    }
   159 ////
   160 ////    @Override
   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);
   163 ////    }
   164 //
   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);
   167 ////
   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);
   170 ////
   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);
   176         }
   177         setFillStyleImpl(context, nativeStyle);
   178         return nativeStyle;
   179     }
   180 
   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));
   194             }
   195             return gradient;
   196 
   197 
   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));
   208             }
   209             return gradient;
   210         } else if (style instanceof Pattern) {
   211 //            return createPatternWrapper(((Pattern) style).getImage(), ((Pattern) style).getRepeat());
   212         }
   213         return null;
   214     }
   215 
   216     @JavaScriptBody(args = {"gradient", "position", "color"}, body =
   217             "gradient.addColorStop(position,color)")
   218     private static native void addColorStopImpl(Object gradient, double position, String color);
   219 
   220     @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
   221     private native void setFillStyleImpl(Object context, Object obj);
   222 
   223     @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
   224     public native void setStrokeStyle(String style);
   225 
   226     @Override
   227     public Object setStrokeStyle(Style style, Object nativeStyle) {
   228         if (nativeStyle == null){
   229             nativeStyle = createNativeStyle(style);
   230         }
   231         setStrokeStyleImpl(context, nativeStyle);
   232         return nativeStyle;
   233     }
   234 
   235     @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
   236     private native void setStrokeStyleImpl(Object context, Object obj);
   237 
   238     @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
   239     @Override
   240     public native void setShadowColor(String color);
   241 
   242     @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
   243     @Override
   244     public native void setShadowBlur(double blur);
   245 
   246     @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
   247     @Override
   248     public native void setShadowOffsetX(double x);
   249 
   250     @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
   251     @Override
   252     public native void setShadowOffsetY(double y);
   253 
   254     @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
   255     public native String getStrokeStyle();
   256 
   257     @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
   258     @Override
   259     public native String getShadowColor();
   260 
   261     @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
   262     @Override
   263     public native double getShadowBlur();
   264 
   265     @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
   266     @Override
   267     public native double getShadowOffsetX();
   268 
   269     @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
   270     @Override
   271     public native double getShadowOffsetY();
   272 
   273     @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
   274     @Override
   275     public native String getLineCap();
   276 
   277     @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
   278     @Override
   279     public native void setLineCap(String style);
   280 
   281     @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
   282     @Override
   283     public native String getLineJoin();
   284 
   285     @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
   286     @Override
   287     public native void setLineJoin(String style);
   288 
   289     @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
   290     @Override
   291     public native double getLineWidth();
   292 
   293     @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
   294     @Override
   295     public native void setLineWidth(double width);
   296 
   297     @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
   298     @Override
   299     public native double getMiterLimit();
   300 
   301     @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
   302     @Override
   303     public native void setMiterLimit(double limit);
   304 
   305     @JavaScriptBody(args = {}, body = "return this._context().font;")
   306     @Override
   307     public native String getFont();
   308 
   309     @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
   310     @Override
   311     public native void setFont(String font);
   312 
   313     @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
   314     @Override
   315     public native String getTextAlign();
   316 
   317     @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
   318     @Override
   319     public native void setTextAlign(String textAlign);
   320 
   321     @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
   322     @Override
   323     public native String getTextBaseline();
   324 
   325     @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
   326     @Override
   327     public native void setTextBaseline(String textbaseline);
   328 
   329     @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
   330 //    @JavaScriptBody(args = {"text", "x", "y"}, body = "console.log(text);")
   331     @Override
   332     public native void fillText(String text, double x, double y);
   333 
   334     @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
   335     @Override
   336     public void fillText(String text, double x, double y, double maxWidth) {
   337     }
   338 
   339     @Override
   340     public Dimension measureText(String text) {
   341         measureTextImpl(text);
   342         return new Dimension(1, 1);
   343     }
   344 
   345     @JavaScriptBody(args = {"text"},
   346             body = "return this._context().measureText(text);")
   347     private native Object measureTextImpl(String text);
   348 
   349     @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
   350     @Override
   351     public native void strokeText(String text, double x, double y);
   352 
   353     @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
   354     @Override
   355     public native void strokeText(String text, double x, double y, double maxWidth);
   356 
   357 ////    @Override
   358 ////    public ImageData createImageData(double x, double y) {
   359 ////        return new ImageDataWrapper(createImageDataImpl(x, y));
   360 ////    }
   361 ////
   362 ////    @JavaScriptBody(args = {"x", "y"},
   363 ////            body = "return this._context().createImageData(x,y);")
   364 ////    private native Object createImageDataImpl(double x, double y);
   365 ////
   366 ////    @Override
   367 ////    public ImageData createImageData(ImageData imageData) {
   368 ////        return new ImageDataWrapper(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
   369 ////    }
   370 ////
   371 ////    @Override
   372 ////    public ImageData getImageData(double x, double y, double width, double height) {
   373 ////        return new ImageDataWrapper(getImageDataImpl(x, y, width, height));
   374 ////    }
   375 //
   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);
   379 //
   380 ////    @Override
   381 ////    public void putImageData(ImageData imageData, double x, double y) {
   382 ////        putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y);
   383 ////    }
   384 ////
   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);
   388 ////
   389 ////    @Override
   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);
   392 ////    }
   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);
   396 
   397     @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
   398     @Override
   399     public native void setGlobalAlpha(double alpha);
   400 
   401     @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
   402     @Override
   403     public native double getGlobalAlpha();
   404 
   405     @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
   406     @Override
   407     public native void setGlobalCompositeOperation(String operation);
   408 
   409     @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
   410     @Override
   411     public native String getGlobalCompositeOperation();
   412 
   413     public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
   414         return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
   415     }
   416 
   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);
   419 
   420 //    public PatternWrapper createPatternWrapper(Image image, String repeat) {
   421 //        return new PatternWrapper(createPatternImpl(context, image, repeat));
   422 //    }
   423 //
   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));
   428     }
   429 
   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);
   432 
   433 ////  
   434 ////    @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
   435 ////    public native Image getImageForPath(String path);
   436     @Override
   437     public int getHeight() {
   438         return canvas.getHeight();
   439     }
   440 
   441     @Override
   442     public int getWidth() {
   443         return canvas.getWidth();
   444     }
   445 
   446     @Override
   447     public void setHeight(int height) {
   448         canvas.setHeight(height);
   449     }
   450 
   451     @Override
   452     public void setWidth(int width) {
   453         canvas.setWidth(width);
   454     }
   455 }