javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/GraphicsContext.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Mar 2013 00:05:41 +0100
changeset 871 6168fb585ab4
parent 866 9b4751828ceb
child 1073 9321b4016d5c
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Alas, need to merge into default branch, but without any changes from the accidental commit on default branch
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage.api;
    19 
    20 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    21 
    22 /**
    23  *
    24  * @author Anton Epple <toni.epple@eppleton.de>
    25  */
    26 public class GraphicsContext {
    27 
    28     Object context;
    29 
    30     GraphicsContext(Object contextImpl) {
    31         this.context = contextImpl;
    32     }
    33 
    34     @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
    35             body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
    36     public native void arc(double centerX,
    37             double centerY,
    38             double startAngle,
    39             double radius,
    40             double endAngle,
    41             boolean ccw);
    42 
    43     @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
    44             body = "this._context().arcTo(x1,y1,x2,y2,r);")
    45     public native void arcTo(double x1,
    46             double y1,
    47             double x2,
    48             double y2,
    49             double r);
    50 
    51     @JavaScriptBody(args = {"x", "y"},
    52             body = "return this._context().isPointInPath(x,y);")
    53     public native boolean isPointInPath(double x, double y);
    54 
    55     @JavaScriptBody(args = {}, body = "this._context().fill();")
    56     public native void fill();
    57 
    58     @JavaScriptBody(args = {}, body = "this._context().stroke();")
    59     public native void stroke();
    60 
    61     @JavaScriptBody(args = {}, body = "this._context().beginPath();")
    62     public native void beginPath();
    63 
    64     @JavaScriptBody(args = {}, body = "this._context().closePath();")
    65     public native void closePath();
    66 
    67     @JavaScriptBody(args = {}, body = "this._context().clip();")
    68     public native void clip();
    69 
    70     @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
    71     public native void moveTo(double x, double y);
    72 
    73     @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
    74     public native void lineTo(double x, double y);
    75 
    76     @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
    77     public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
    78 
    79     @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
    80             body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
    81     public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
    82 
    83     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
    84     public native void fillRect(double x, double y, double width, double height);
    85 
    86     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
    87     public native void strokeRect(double x, double y, double width, double height);
    88 
    89     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
    90     public native void clearRect(double x, double y, double width, double height);
    91 
    92     @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
    93     public native void rect(double x, double y, double width, double height);
    94 
    95     @JavaScriptBody(args = {}, body = "this._context().save();")
    96     public native void save();
    97 
    98     @JavaScriptBody(args = {}, body = "this._context().restore();")
    99     public native void restore();
   100 
   101     @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
   102     public native void rotate(double angle);
   103 
   104     @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
   105     public native void transform(double a, double b, double c, double d, double e, double f);
   106 
   107     @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
   108     public native void setTransform(double a, double b, double c, double d, double e, double f);
   109 
   110     @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
   111     public native void translate(double x, double y);
   112 
   113     @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
   114     public native void scale(double x, double y);
   115 
   116     public void drawImage(Image image, double x, double y) {
   117         drawImageImpl(context, Element.getElementById(image), x, y);
   118     }
   119 
   120     public void drawImage(Image image, double x, double y, double width, double height) {
   121         drawImageImpl(context, Element.getElementById(image), x, y, width, height);
   122     }
   123 
   124     public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) {
   125         drawImageImpl(context, Element.getElementById(image), sx, sy, sWidth, sHeight, x, y, width, height);
   126     }
   127 
   128     @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height);")
   129     private native static void drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
   130 
   131     @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);")
   132     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);
   133 
   134     @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y);")
   135     private native static void drawImageImpl(Object ctx, Object img, double x, double y);
   136 
   137     @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style.valueOf();")
   138     public native void setFillStyle(String style);
   139 
   140     @JavaScriptBody(args = {}, body = "return this._context().fillStyle;")
   141     public native String getFillStyle();
   142 
   143     public void setFillStyle(LinearGradient style) {
   144         setFillStyleImpl(context, style.object());
   145     }
   146 
   147     public void setFillStyle(RadialGradient style) {
   148         setFillStyleImpl(context, style.object());
   149     }
   150 
   151     public void setFillStyle(Pattern style) {
   152         setFillStyleImpl(context, style.object());
   153     }
   154 
   155     @JavaScriptBody(args = {"context","obj"}, body = "context.fillStyle=obj;")
   156     private native void setFillStyleImpl(Object context, Object obj);
   157 
   158     @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
   159     public native void setStrokeStyle(String style);
   160 
   161     public void setStrokeStyle(LinearGradient style) {
   162         setStrokeStyleImpl(context, style.object());
   163     }
   164 
   165     public void setStrokeStyle(RadialGradient style) {
   166         setStrokeStyleImpl(context, style.object());
   167     }
   168 
   169     @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style;")
   170     public void setStrokeStyle(Pattern style) {
   171         setStrokeStyleImpl(context, style.object());
   172     }
   173 
   174     @JavaScriptBody(args = {"context","obj"}, body = "context.strokeStyle=obj;")
   175     private native void setStrokeStyleImpl(Object context, Object obj);
   176 
   177     @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
   178     public native void setShadowColor(String color);
   179 
   180     @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
   181     public native void setShadowBlur(double blur);
   182     
   183     @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
   184     public native void setShadowOffsetX(double x);
   185 
   186     @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
   187     public native void setShadowOffsetY(double y);
   188 
   189     @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
   190     public native String getStrokeStyle();
   191 
   192     @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
   193     public native String getShadowColor();
   194 
   195     @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
   196     public native double getShadowBlur();
   197 
   198     @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
   199     public native double getShadowOffsetX();
   200 
   201     @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
   202     public native double getShadowOffsetY();
   203 
   204     @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
   205     public native String getLineCap();
   206 
   207     @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
   208     public native void setLineCap(String style);
   209 
   210     @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
   211     public native String getLineJoin();
   212 
   213     @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
   214     public native void setLineJoin(String style) ;
   215 
   216     @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
   217     public native double getLineWidth();
   218 
   219     @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
   220     public native void setLineWidth(double width);
   221 
   222     @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
   223     public native double getMiterLimit();
   224 
   225     @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
   226     public native void setMiterLimit(double limit);
   227 
   228     @JavaScriptBody(args = {}, body = "return this._context().font;")
   229     public native String getFont();
   230 
   231     @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
   232     public native void setFont(String font);
   233 
   234     @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
   235     public native String getTextAlign();
   236 
   237     @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
   238     public native void setTextAlign(String textAlign);
   239 
   240     @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
   241     public native String getTextBaseline();
   242 
   243     @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
   244     public native void setTextBaseline(String textbaseline);
   245 
   246     @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
   247     public native void fillText(String text, double x, double y);
   248 
   249     @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
   250     public void fillText(String text, double x, double y, double maxWidth) {
   251     }
   252 
   253     public TextMetrics measureText(String text) {
   254         return new TextMetrics(measureTextImpl(text));
   255     }
   256 
   257     @JavaScriptBody(args = {"text"},
   258             body = "return this._context().measureText(text);")
   259     private native Object measureTextImpl(String text);
   260 
   261     @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
   262     public native void strokeText(String text, double x, double y);
   263 
   264     @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
   265     public native void strokeText(String text, double x, double y, double maxWidth) ;
   266 
   267     public ImageData createImageData(double x, double y) {
   268         return new ImageData(createImageDataImpl(x, y));
   269     }
   270 
   271     @JavaScriptBody(args = {"x", "y"},
   272             body = "return this._context().createImageData(x,y);")
   273     private native Object createImageDataImpl(double x, double y);
   274 
   275     public ImageData createImageData(ImageData imageData) {
   276         return new ImageData(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
   277     }
   278 
   279     public ImageData getImageData(double x, double y, double width, double height) {
   280         return new ImageData(getImageDataImpl(x, y, width, height));
   281     }
   282 
   283     @JavaScriptBody(args = {"x", "y", "width", "height"},
   284             body = "return this._context().getImageData(x,y,width,height);")
   285     private native Object getImageDataImpl(double x, double y, double width, double height);
   286 
   287     public void putImageData(ImageData imageData, double x, double y) {
   288         putImageDataImpl(imageData.object(), x, y);
   289     }
   290 
   291     @JavaScriptBody(args = {"imageData", "x", "y"},
   292             body = "this._context().putImageData(imageData,x,y);")
   293     private native void putImageDataImpl(Object imageData, double x, double y);
   294 
   295     public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
   296         putImageDataImpl(imageData.object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
   297     }
   298 
   299     @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
   300             body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
   301     private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
   302 
   303     @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
   304     public native void setGlobalAlpha(double alpha) ;
   305 
   306     @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
   307     public native double getGlobalAlpha();
   308 
   309     @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
   310     public native void setGlobalCompositeOperation(String operation);
   311 
   312     @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
   313     public native String getGlobalCompositeOperation();
   314 
   315     public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
   316         return new LinearGradient(createLinearGradientImpl(context, x0, y0, x1, y1));
   317     }
   318 
   319     @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
   320     private  native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
   321 
   322     public Pattern createPattern(Image image, String repeat) {
   323         return new Pattern(createPatternImpl(context, image, repeat));
   324     }
   325 
   326     @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
   327     private static native Object createPatternImpl(Object context, Image image, String repeat);
   328 
   329     public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
   330         return new RadialGradient(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
   331     }
   332 
   333     @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
   334     private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
   335 }