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