javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/HTML5GraphicsEnvironment.java
author Anton Epple <toni.epple@eppleton.de>
Thu, 23 May 2013 15:33:14 +0200
branchcanvas
changeset 1135 836bc1845c65
parent 1134 javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/HTML5GraphicsEnvironment.java@0a2190f2a210
child 1138 94bd7330ff58
permissions -rw-r--r--
moved some non-api classes out of api package
     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.awt.Dimension;
    21 import java.util.HashMap;
    22 import java.util.Set;
    23 import net.java.html.canvas.GraphicsEnvironment;
    24 import net.java.html.canvas.ImageData;
    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;
    30 import org.apidesign.bck2brwsr.htmlpage.api.Canvas;
    31 import org.apidesign.bck2brwsr.htmlpage.api.Element;
    32 import org.apidesign.bck2brwsr.htmlpage.api.Image;
    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    public  HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
    43         this.context = contextImpl;
    44         this.canvas = canvas;
    45     }
    46 
    47     @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
    48             body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
    49     @Override
    50     public native void arc(double centerX,
    51             double centerY,
    52             double startAngle,
    53             double radius,
    54             double endAngle,
    55             boolean ccw);
    56 
    57     @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
    58             body = "this._context().arcTo(x1,y1,x2,y2,r);")
    59     @Override
    60     public native void arcTo(double x1,
    61             double y1,
    62             double x2,
    63             double y2,
    64             double r);
    65 
    66     @JavaScriptBody(args = {"x", "y"},
    67             body = "return this._context().isPointInPath(x,y);")
    68     @Override
    69     public native boolean isPointInPath(double x, double y);
    70 
    71     @JavaScriptBody(args = {}, body = "this._context().fill();")
    72     @Override
    73     public native void fill();
    74 
    75     @JavaScriptBody(args = {}, body = "this._context().stroke();")
    76     @Override
    77     public native void stroke();
    78 
    79     @JavaScriptBody(args = {}, body = "this._context().beginPath();")
    80     @Override
    81     public native void beginPath();
    82 
    83     @JavaScriptBody(args = {}, body = "this._context().closePath();")
    84     @Override
    85     public native void closePath();
    86 
    87     @JavaScriptBody(args = {}, body = "this._context().clip();")
    88     @Override
    89     public native void clip();
    90 
    91     @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
    92     @Override
    93     public native void moveTo(double x, double y);
    94 
    95     @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
    96     @Override
    97     public native void lineTo(double x, double y);
    98 
    99     @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
   100     @Override
   101     public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
   102 
   103     @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
   104             body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
   105     @Override
   106     public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
   107 
   108     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
   109     @Override
   110     public native void fillRect(double x, double y, double width, double height);
   111 
   112     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
   113     @Override
   114     public native void strokeRect(double x, double y, double width, double height);
   115 
   116     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
   117     @Override
   118     public native void clearRect(double x, double y, double width, double height);
   119 
   120     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
   121     @Override
   122     public native void rect(double x, double y, double width, double height);
   123 
   124     @JavaScriptBody(args = {}, body = "this._context().save();")
   125     @Override
   126     public native void save();
   127 
   128     @JavaScriptBody(args = {}, body = "this._context().restore();")
   129     @Override
   130     public native void restore();
   131 
   132     @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
   133     @Override
   134     public native void rotate(double angle);
   135 
   136     @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
   137     @Override
   138     public native void transform(double a, double b, double c, double d, double e, double f);
   139 
   140     @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
   141     @Override
   142     public native void setTransform(double a, double b, double c, double d, double e, double f);
   143 
   144     @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
   145     @Override
   146     public native void translate(double x, double y);
   147 
   148     @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
   149     @Override
   150     public native void scale(double x, double y);
   151 
   152     @Override
   153     public void drawImage(ImageData image, double x, double y) {
   154         drawImageImpl(context, Element.getElementById((Image) image), x, y);
   155     }
   156 
   157     @Override
   158     public void drawImage(ImageData image, double x, double y, double width, double height) {
   159         drawImageImpl(context, Element.getElementById((Image) image), x, y, width, height);
   160     }
   161 
   162     @Override
   163     public void drawImage(ImageData image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) {
   164         drawImageImpl(context, Element.getElementById((Image) image), sx, sy, sWidth, sHeight, x, y, width, height);
   165     }
   166 
   167     @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height);")
   168     private native static void drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
   169 
   170     @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);")
   171     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);
   172 
   173     @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y);")
   174     private native static void drawImageImpl(Object ctx, Object img, double x, double y);
   175 
   176     @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style.valueOf();")
   177     @Override
   178     public native void setFillStyle(String style);
   179 
   180     @JavaScriptBody(args = {}, body = "return this._context().fillStyle;")
   181     @Override
   182     public native String getFillStyle();
   183 
   184     @Override
   185     public void setFillStyle(Style style) {
   186         setFillStyleImpl(context, createFillStyle(style));
   187     }
   188 
   189     private Object createFillStyle(Style style) {
   190         if (style instanceof RadialGradient) {
   191             RadialGradientWrapper gradient = createRadialGradientWrapper(
   192                     ((RadialGradient) style).getX0(),
   193                     ((RadialGradient) style).getY0(),
   194                     ((RadialGradient) style).getR0(),
   195                     ((RadialGradient) style).getX1(),
   196                     ((RadialGradient) style).getY1(),
   197                     ((RadialGradient) style).getR1());
   198             HashMap<Double, String> stops = ((LinearGradient) style).getStops();
   199             Set<Double> keySet = stops.keySet();
   200             for (Double double1 : keySet) {
   201                 addColorStopImpl(style, double1, stops.get(double1));
   202             }
   203             return gradient;
   204 
   205 
   206         } else if (style instanceof LinearGradient) {
   207             LinearGradientWrapper gradient = createLinearGradientWrapper(
   208                     ((LinearGradient) style).getX0(),
   209                     ((LinearGradient) style).getY0(),
   210                     ((LinearGradient) style).getX1(),
   211                     ((LinearGradient) style).getY1());
   212             HashMap<Double, String> stops = ((LinearGradient) style).getStops();
   213             Set<Double> keySet = stops.keySet();
   214             for (Double double1 : keySet) {
   215                 addColorStopImpl(style, double1, stops.get(double1));
   216             }
   217             return gradient;
   218         } else if (style instanceof Pattern) {
   219             return createPatternWrapper(((Pattern) style).getImageData(), ((Pattern) style).getRepeat());
   220         }
   221         return null;
   222     }
   223 
   224 
   225     @JavaScriptBody(args = {"gradient", "position", "color"}, body =
   226             "gradient.addColorStop(position,color)")
   227     private static native void addColorStopImpl(Object gradient, double position, String color);
   228 
   229     @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
   230     private native void setFillStyleImpl(Object context, Object obj);
   231 
   232     @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
   233     @Override
   234     public native void setStrokeStyle(String style);
   235 
   236     @Override
   237     public void setStrokeStyle(Style style) {
   238         setStrokeStyleImpl(context, createFillStyle(style));
   239     }
   240 
   241     @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
   242     private native void setStrokeStyleImpl(Object context, Object obj);
   243 
   244     @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
   245     @Override
   246     public native void setShadowColor(String color);
   247 
   248     @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
   249     @Override
   250     public native void setShadowBlur(double blur);
   251 
   252     @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
   253     @Override
   254     public native void setShadowOffsetX(double x);
   255 
   256     @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
   257     @Override
   258     public native void setShadowOffsetY(double y);
   259 
   260     @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
   261     @Override
   262     public native String getStrokeStyle();
   263 
   264     @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
   265     @Override
   266     public native String getShadowColor();
   267 
   268     @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
   269     @Override
   270     public native double getShadowBlur();
   271 
   272     @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
   273     @Override
   274     public native double getShadowOffsetX();
   275 
   276     @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
   277     @Override
   278     public native double getShadowOffsetY();
   279 
   280     @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
   281     @Override
   282     public native String getLineCap();
   283 
   284     @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
   285     @Override
   286     public native void setLineCap(String style);
   287 
   288     @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
   289     @Override
   290     public native String getLineJoin();
   291 
   292     @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
   293     @Override
   294     public native void setLineJoin(String style);
   295 
   296     @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
   297     @Override
   298     public native double getLineWidth();
   299 
   300     @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
   301     @Override
   302     public native void setLineWidth(double width);
   303 
   304     @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
   305     @Override
   306     public native double getMiterLimit();
   307 
   308     @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
   309     @Override
   310     public native void setMiterLimit(double limit);
   311 
   312     @JavaScriptBody(args = {}, body = "return this._context().font;")
   313     @Override
   314     public native String getFont();
   315 
   316     @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
   317     @Override
   318     public native void setFont(String font);
   319 
   320     @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
   321     @Override
   322     public native String getTextAlign();
   323 
   324     @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
   325     @Override
   326     public native void setTextAlign(String textAlign);
   327 
   328     @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
   329     @Override
   330     public native String getTextBaseline();
   331 
   332     @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
   333     @Override
   334     public native void setTextBaseline(String textbaseline);
   335 
   336     @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
   337     @Override
   338     public native void fillText(String text, double x, double y);
   339 
   340     @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
   341     @Override
   342     public void fillText(String text, double x, double y, double maxWidth) {
   343     }
   344 
   345     @Override
   346     public Dimension measureText(String text) {
   347         TextMetrics textMetrics = new TextMetrics(measureTextImpl(text));
   348         return new Dimension((int) textMetrics.getWidth(), (int) textMetrics.getHeight());
   349     }
   350 
   351     @JavaScriptBody(args = {"text"},
   352             body = "return this._context().measureText(text);")
   353     private native Object measureTextImpl(String text);
   354 
   355     @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
   356     @Override
   357     public native void strokeText(String text, double x, double y);
   358 
   359     @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
   360     @Override
   361     public native void strokeText(String text, double x, double y, double maxWidth);
   362 
   363 //    @Override
   364 //    public ImageData createImageData(double x, double y) {
   365 //        return new ImageDataWrapper(createImageDataImpl(x, y));
   366 //    }
   367 //
   368 //    @JavaScriptBody(args = {"x", "y"},
   369 //            body = "return this._context().createImageData(x,y);")
   370 //    private native Object createImageDataImpl(double x, double y);
   371 //
   372 //    @Override
   373 //    public ImageData createImageData(ImageData imageData) {
   374 //        return new ImageDataWrapper(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
   375 //    }
   376 //
   377 //    @Override
   378 //    public ImageData getImageData(double x, double y, double width, double height) {
   379 //        return new ImageDataWrapper(getImageDataImpl(x, y, width, height));
   380 //    }
   381 
   382     @JavaScriptBody(args = {"x", "y", "width", "height"},
   383             body = "return this._context().getImageData(x,y,width,height);")
   384     private native Object getImageDataImpl(double x, double y, double width, double height);
   385 
   386 //    @Override
   387 //    public void putImageData(ImageData imageData, double x, double y) {
   388 //        putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y);
   389 //    }
   390 //
   391 //    @JavaScriptBody(args = {"imageData", "x", "y"},
   392 //            body = "this._context().putImageData(imageData,x,y);")
   393 //    private native void putImageDataImpl(Object imageData, double x, double y);
   394 //
   395 //    @Override
   396 //    public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
   397 //        putImageDataImpl(((ImageDataWrapper) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
   398 //    }
   399 
   400     @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
   401             body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
   402     private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
   403 
   404     @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
   405     @Override
   406     public native void setGlobalAlpha(double alpha);
   407 
   408     @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
   409     @Override
   410     public native double getGlobalAlpha();
   411 
   412     @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
   413     @Override
   414     public native void setGlobalCompositeOperation(String operation);
   415 
   416     @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
   417     @Override
   418     public native String getGlobalCompositeOperation();
   419 
   420     public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
   421         return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
   422     }
   423 
   424     @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
   425     private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
   426 
   427     public PatternWrapper createPatternWrapper(ImageData image, String repeat) {
   428         return new PatternWrapper(createPatternImpl(context, image, repeat));
   429     }
   430 
   431     @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
   432     private static native Object createPatternImpl(Object context, ImageData image, String repeat);
   433 
   434     public RadialGradientWrapper createRadialGradientWrapper(double x0, double y0, double r0, double x1, double y1, double r1) {
   435         return new RadialGradientWrapper(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
   436     }
   437 
   438     @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
   439     private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
   440 
   441 //  
   442 //    @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
   443 //    public native Image getImageForPath(String path);
   444 
   445     
   446     
   447     @Override
   448     public int getHeight() {
   449        return canvas.getHeight();
   450     }
   451 
   452     @Override
   453     public int getWidth() {
   454        return canvas.getWidth();
   455     }
   456 
   457     @Override
   458     public void setHeight(int height) {
   459        canvas.setHeight(height);
   460     }
   461 
   462     @Override
   463     public void setWidth(int width) {
   464        canvas.setWidth(width);
   465     }
   466 
   467 }