javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/HTML5GraphicsContext.java
author toni.epple@eppleton.de
Fri, 17 May 2013 11:00:38 +0200
branchcanvas
changeset 1110 ca05c9890031
parent 871 javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/GraphicsContext.java@6168fb585ab4
permissions -rw-r--r--
Started extracting GraphicsContext interface from JavaQuery API. First extracting methods with primitive parameters and return type.
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@1110
    20
import net.java.html.canvas.GraphicsContext;
toni@521
    21
import org.apidesign.bck2brwsr.core.JavaScriptBody;
toni@521
    22
toni@521
    23
/**
toni@521
    24
 *
toni@521
    25
 * @author Anton Epple <toni.epple@eppleton.de>
toni@521
    26
 */
toni@1110
    27
public class HTML5GraphicsContext implements GraphicsContext {
toni@521
    28
toni@521
    29
    Object context;
toni@521
    30
toni@1110
    31
    HTML5GraphicsContext(Object contextImpl) {
toni@521
    32
        this.context = contextImpl;
toni@521
    33
    }
toni@521
    34
toni@521
    35
    @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
jaroslav@592
    36
            body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
toni@1110
    37
    @Override
toni@521
    38
    public native void arc(double centerX,
toni@521
    39
            double centerY,
toni@521
    40
            double startAngle,
toni@521
    41
            double radius,
toni@521
    42
            double endAngle,
toni@521
    43
            boolean ccw);
toni@521
    44
toni@521
    45
    @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
jaroslav@592
    46
            body = "this._context().arcTo(x1,y1,x2,y2,r);")
toni@1110
    47
    @Override
toni@521
    48
    public native void arcTo(double x1,
toni@521
    49
            double y1,
toni@521
    50
            double x2,
toni@521
    51
            double y2,
toni@521
    52
            double r);
toni@521
    53
toni@521
    54
    @JavaScriptBody(args = {"x", "y"},
jaroslav@592
    55
            body = "return this._context().isPointInPath(x,y);")
toni@1110
    56
    @Override
toni@521
    57
    public native boolean isPointInPath(double x, double y);
toni@521
    58
jaroslav@592
    59
    @JavaScriptBody(args = {}, body = "this._context().fill();")
toni@1110
    60
    @Override
toni@521
    61
    public native void fill();
toni@521
    62
jaroslav@592
    63
    @JavaScriptBody(args = {}, body = "this._context().stroke();")
toni@1110
    64
    @Override
toni@521
    65
    public native void stroke();
toni@521
    66
jaroslav@592
    67
    @JavaScriptBody(args = {}, body = "this._context().beginPath();")
toni@1110
    68
    @Override
toni@521
    69
    public native void beginPath();
toni@521
    70
jaroslav@592
    71
    @JavaScriptBody(args = {}, body = "this._context().closePath();")
toni@1110
    72
    @Override
toni@521
    73
    public native void closePath();
toni@521
    74
jaroslav@592
    75
    @JavaScriptBody(args = {}, body = "this._context().clip();")
toni@1110
    76
    @Override
toni@521
    77
    public native void clip();
toni@521
    78
jaroslav@592
    79
    @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
toni@1110
    80
    @Override
toni@521
    81
    public native void moveTo(double x, double y);
toni@521
    82
jaroslav@592
    83
    @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
toni@1110
    84
    @Override
toni@521
    85
    public native void lineTo(double x, double y);
toni@521
    86
jaroslav@592
    87
    @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
toni@1110
    88
    @Override
toni@521
    89
    public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
toni@521
    90
toni@521
    91
    @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
jaroslav@592
    92
            body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
toni@1110
    93
    @Override
toni@521
    94
    public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
toni@521
    95
jaroslav@592
    96
    @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
toni@1110
    97
    @Override
toni@521
    98
    public native void fillRect(double x, double y, double width, double height);
toni@521
    99
jaroslav@592
   100
    @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
toni@1110
   101
    @Override
toni@521
   102
    public native void strokeRect(double x, double y, double width, double height);
toni@521
   103
jaroslav@592
   104
    @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
toni@1110
   105
    @Override
toni@521
   106
    public native void clearRect(double x, double y, double width, double height);
toni@521
   107
jaroslav@592
   108
    @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rectect(x,y,width,height);")
toni@1110
   109
    @Override
toni@521
   110
    public native void rect(double x, double y, double width, double height);
toni@521
   111
jaroslav@592
   112
    @JavaScriptBody(args = {}, body = "this._context().save();")
toni@1110
   113
    @Override
toni@521
   114
    public native void save();
toni@521
   115
jaroslav@592
   116
    @JavaScriptBody(args = {}, body = "this._context().restore();")
toni@1110
   117
    @Override
toni@521
   118
    public native void restore();
toni@521
   119
jaroslav@592
   120
    @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
toni@1110
   121
    @Override
toni@521
   122
    public native void rotate(double angle);
toni@521
   123
jaroslav@592
   124
    @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
toni@1110
   125
    @Override
toni@521
   126
    public native void transform(double a, double b, double c, double d, double e, double f);
toni@521
   127
jaroslav@592
   128
    @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
toni@1110
   129
    @Override
toni@521
   130
    public native void setTransform(double a, double b, double c, double d, double e, double f);
toni@521
   131
jaroslav@592
   132
    @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
toni@1110
   133
    @Override
toni@521
   134
    public native void translate(double x, double y);
toni@521
   135
jaroslav@592
   136
    @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
toni@1110
   137
    @Override
toni@521
   138
    public native void scale(double x, double y);
toni@521
   139
jaroslav@871
   140
    public void drawImage(Image image, double x, double y) {
toni@521
   141
        drawImageImpl(context, Element.getElementById(image), x, y);
toni@521
   142
    }
toni@521
   143
jaroslav@871
   144
    public void drawImage(Image image, double x, double y, double width, double height) {
toni@521
   145
        drawImageImpl(context, Element.getElementById(image), x, y, width, height);
toni@521
   146
    }
toni@521
   147
jaroslav@871
   148
    public void drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height) {
toni@521
   149
        drawImageImpl(context, Element.getElementById(image), sx, sy, sWidth, sHeight, x, y, width, height);
toni@521
   150
    }
toni@521
   151
toni@521
   152
    @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "ctx.drawImage(img,x,y,width,height);")
toni@521
   153
    private native static void drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
toni@521
   154
toni@521
   155
    @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
   156
    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
   157
toni@521
   158
    @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "ctx.drawImage(img,x,y);")
toni@521
   159
    private native static void drawImageImpl(Object ctx, Object img, double x, double y);
toni@521
   160
jaroslav@776
   161
    @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style.valueOf();")
toni@1110
   162
    @Override
toni@521
   163
    public native void setFillStyle(String style);
toni@521
   164
jaroslav@592
   165
    @JavaScriptBody(args = {}, body = "return this._context().fillStyle;")
toni@1110
   166
    @Override
toni@521
   167
    public native String getFillStyle();
toni@521
   168
toni@521
   169
    public void setFillStyle(LinearGradient style) {
toni@521
   170
        setFillStyleImpl(context, style.object());
toni@521
   171
    }
toni@521
   172
toni@521
   173
    public void setFillStyle(RadialGradient style) {
toni@521
   174
        setFillStyleImpl(context, style.object());
toni@521
   175
    }
toni@521
   176
toni@521
   177
    public void setFillStyle(Pattern style) {
toni@521
   178
        setFillStyleImpl(context, style.object());
toni@521
   179
    }
toni@521
   180
toni@521
   181
    @JavaScriptBody(args = {"context","obj"}, body = "context.fillStyle=obj;")
toni@521
   182
    private native void setFillStyleImpl(Object context, Object obj);
toni@521
   183
jaroslav@776
   184
    @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
toni@1110
   185
    @Override
toni@521
   186
    public native void setStrokeStyle(String style);
toni@521
   187
toni@521
   188
    public void setStrokeStyle(LinearGradient style) {
toni@521
   189
        setStrokeStyleImpl(context, style.object());
toni@521
   190
    }
toni@521
   191
toni@521
   192
    public void setStrokeStyle(RadialGradient style) {
toni@521
   193
        setStrokeStyleImpl(context, style.object());
toni@521
   194
    }
toni@521
   195
jaroslav@592
   196
    @JavaScriptBody(args = {"style"}, body = "this._context().fillStyle=style;")
toni@521
   197
    public void setStrokeStyle(Pattern style) {
toni@521
   198
        setStrokeStyleImpl(context, style.object());
toni@521
   199
    }
toni@521
   200
toni@521
   201
    @JavaScriptBody(args = {"context","obj"}, body = "context.strokeStyle=obj;")
toni@521
   202
    private native void setStrokeStyleImpl(Object context, Object obj);
toni@521
   203
jaroslav@776
   204
    @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
toni@1110
   205
    @Override
toni@521
   206
    public native void setShadowColor(String color);
toni@521
   207
jaroslav@592
   208
    @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
toni@1110
   209
    @Override
toni@521
   210
    public native void setShadowBlur(double blur);
toni@521
   211
    
jaroslav@592
   212
    @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
toni@1110
   213
    @Override
toni@521
   214
    public native void setShadowOffsetX(double x);
toni@521
   215
jaroslav@592
   216
    @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
toni@1110
   217
    @Override
toni@521
   218
    public native void setShadowOffsetY(double y);
toni@521
   219
jaroslav@592
   220
    @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
toni@1110
   221
    @Override
toni@521
   222
    public native String getStrokeStyle();
toni@521
   223
jaroslav@592
   224
    @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
toni@1110
   225
    @Override
toni@521
   226
    public native String getShadowColor();
toni@521
   227
jaroslav@592
   228
    @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
toni@1110
   229
    @Override
toni@521
   230
    public native double getShadowBlur();
toni@521
   231
jaroslav@592
   232
    @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
toni@1110
   233
    @Override
toni@521
   234
    public native double getShadowOffsetX();
toni@521
   235
jaroslav@592
   236
    @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
toni@1110
   237
    @Override
toni@521
   238
    public native double getShadowOffsetY();
toni@521
   239
jaroslav@592
   240
    @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
toni@1110
   241
    @Override
toni@521
   242
    public native String getLineCap();
toni@521
   243
jaroslav@776
   244
    @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
toni@1110
   245
    @Override
toni@521
   246
    public native void setLineCap(String style);
toni@521
   247
jaroslav@592
   248
    @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
toni@1110
   249
    @Override
toni@521
   250
    public native String getLineJoin();
toni@521
   251
jaroslav@776
   252
    @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
toni@1110
   253
    @Override
toni@1110
   254
    public native void setLineJoin(String style);
toni@521
   255
jaroslav@592
   256
    @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
toni@1110
   257
    @Override
toni@521
   258
    public native double getLineWidth();
toni@521
   259
jaroslav@776
   260
    @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
toni@1110
   261
    @Override
toni@521
   262
    public native void setLineWidth(double width);
toni@521
   263
jaroslav@592
   264
    @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
toni@1110
   265
    @Override
toni@521
   266
    public native double getMiterLimit();
toni@521
   267
jaroslav@592
   268
    @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
toni@1110
   269
    @Override
toni@521
   270
    public native void setMiterLimit(double limit);
toni@521
   271
jaroslav@592
   272
    @JavaScriptBody(args = {}, body = "return this._context().font;")
toni@1110
   273
    @Override
toni@521
   274
    public native String getFont();
toni@521
   275
jaroslav@776
   276
    @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
toni@1110
   277
    @Override
toni@521
   278
    public native void setFont(String font);
toni@521
   279
jaroslav@592
   280
    @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
toni@1110
   281
    @Override
toni@521
   282
    public native String getTextAlign();
toni@521
   283
jaroslav@776
   284
    @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
toni@1110
   285
    @Override
toni@521
   286
    public native void setTextAlign(String textAlign);
toni@521
   287
jaroslav@592
   288
    @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
toni@1110
   289
    @Override
toni@521
   290
    public native String getTextBaseline();
toni@521
   291
jaroslav@776
   292
    @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
toni@1110
   293
    @Override
toni@521
   294
    public native void setTextBaseline(String textbaseline);
toni@521
   295
jaroslav@592
   296
    @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
toni@1110
   297
    @Override
toni@521
   298
    public native void fillText(String text, double x, double y);
toni@521
   299
jaroslav@592
   300
    @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
toni@1110
   301
    @Override
toni@521
   302
    public void fillText(String text, double x, double y, double maxWidth) {
toni@521
   303
    }
toni@521
   304
toni@521
   305
    public TextMetrics measureText(String text) {
toni@521
   306
        return new TextMetrics(measureTextImpl(text));
toni@521
   307
    }
toni@521
   308
toni@521
   309
    @JavaScriptBody(args = {"text"},
jaroslav@592
   310
            body = "return this._context().measureText(text);")
toni@521
   311
    private native Object measureTextImpl(String text);
toni@521
   312
jaroslav@592
   313
    @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
toni@1110
   314
    @Override
toni@521
   315
    public native void strokeText(String text, double x, double y);
toni@521
   316
jaroslav@592
   317
    @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
toni@1110
   318
    @Override
toni@1110
   319
    public native void strokeText(String text, double x, double y, double maxWidth);
toni@521
   320
toni@521
   321
    public ImageData createImageData(double x, double y) {
toni@521
   322
        return new ImageData(createImageDataImpl(x, y));
toni@521
   323
    }
toni@521
   324
toni@521
   325
    @JavaScriptBody(args = {"x", "y"},
jaroslav@592
   326
            body = "return this._context().createImageData(x,y);")
toni@521
   327
    private native Object createImageDataImpl(double x, double y);
toni@521
   328
toni@521
   329
    public ImageData createImageData(ImageData imageData) {
toni@521
   330
        return new ImageData(createImageDataImpl(imageData.getWidth(), imageData.getHeight()));
toni@521
   331
    }
toni@521
   332
toni@521
   333
    public ImageData getImageData(double x, double y, double width, double height) {
toni@521
   334
        return new ImageData(getImageDataImpl(x, y, width, height));
toni@521
   335
    }
toni@521
   336
toni@521
   337
    @JavaScriptBody(args = {"x", "y", "width", "height"},
jaroslav@592
   338
            body = "return this._context().getImageData(x,y,width,height);")
toni@521
   339
    private native Object getImageDataImpl(double x, double y, double width, double height);
toni@521
   340
toni@521
   341
    public void putImageData(ImageData imageData, double x, double y) {
toni@521
   342
        putImageDataImpl(imageData.object(), x, y);
toni@521
   343
    }
toni@521
   344
toni@521
   345
    @JavaScriptBody(args = {"imageData", "x", "y"},
jaroslav@592
   346
            body = "this._context().putImageData(imageData,x,y);")
toni@521
   347
    private native void putImageDataImpl(Object imageData, double x, double y);
toni@521
   348
toni@521
   349
    public void putImageData(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
toni@521
   350
        putImageDataImpl(imageData.object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
toni@521
   351
    }
toni@521
   352
toni@521
   353
    @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
jaroslav@592
   354
            body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
toni@521
   355
    private native void putImageDataImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
toni@521
   356
jaroslav@592
   357
    @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
toni@1110
   358
    @Override
toni@1110
   359
    public native void setGlobalAlpha(double alpha);
toni@521
   360
jaroslav@592
   361
    @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
toni@1110
   362
    @Override
toni@521
   363
    public native double getGlobalAlpha();
toni@521
   364
jaroslav@776
   365
    @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
toni@1110
   366
    @Override
david@731
   367
    public native void setGlobalCompositeOperation(String operation);
toni@521
   368
jaroslav@592
   369
    @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
toni@1110
   370
    @Override
david@731
   371
    public native String getGlobalCompositeOperation();
toni@521
   372
toni@521
   373
    public LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
toni@521
   374
        return new LinearGradient(createLinearGradientImpl(context, x0, y0, x1, y1));
toni@521
   375
    }
toni@521
   376
toni@521
   377
    @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
toni@521
   378
    private  native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
toni@521
   379
jaroslav@871
   380
    public Pattern createPattern(Image image, String repeat) {
toni@521
   381
        return new Pattern(createPatternImpl(context, image, repeat));
toni@521
   382
    }
toni@521
   383
toni@521
   384
    @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
jaroslav@871
   385
    private static native Object createPatternImpl(Object context, Image image, String repeat);
toni@521
   386
toni@521
   387
    public RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
toni@521
   388
        return new RadialGradient(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
toni@521
   389
    }
toni@521
   390
toni@521
   391
    @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
toni@521
   392
    private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
toni@521
   393
}