javaquery/canvas/src/main/java/net/java/html/canvas/spi/GraphicsEnvironment.java
author Anton Epple <toni.epple@eppleton.de>
Wed, 12 Feb 2014 14:43:38 +0100
branchcanvas
changeset 1450 0726c9779524
parent 1447 3e3fb431d2b7
permissions -rw-r--r--
(Y05) Reintroduced the methods for using ImageData, since they?re already implemented and I have a very good usecase:
It?s useful for the tricks we need on mobile for fast application launch: Take a snapshot of the UI at application shutdown and display it on application start.
     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 net.java.html.canvas.spi;
    19 
    20 import net.java.html.canvas.Dimension;
    21 import net.java.html.canvas.Image;
    22 import net.java.html.canvas.ImageData;
    23 import net.java.html.canvas.Style;
    24 
    25 /**
    26  * Provider API for Canvas. Implement this to add support for your platform.
    27  *
    28  * @author antonepple
    29  */
    30 public interface GraphicsEnvironment {
    31 
    32     /**
    33      * Adds path elements to the current path to make an arc.
    34      *
    35      * @param centerX the center x position of the arc.
    36      * @param centerY the center y position of the arc.
    37      * @param startAngle the startAngle of the arc
    38      * @param radius the radius of the arc.
    39      * @param endAngle the endAngle of the arc
    40      * @param ccw the direction of the arc (counterclockwise)
    41      */
    42     public void arc(double centerX,
    43             double centerY,
    44             double startAngle,
    45             double radius,
    46             double endAngle,
    47             boolean ccw);
    48 
    49     /**
    50      * Adds segments to the current path to make an arc.
    51      *
    52      * @param x1 the X coordinate of the first point of the arc.
    53      * @param y1 the Y coordinate of the first point of the arc.
    54      * @param x2 the X coordinate of the second point of the arc.
    55      * @param y2 the Y coordinate of the second point of the arc.
    56      * @param radius the radius of the arc in the range {0.0-positive infinity}.
    57      */
    58     public void arcTo(double x1,
    59             double y1,
    60             double x2,
    61             double y2,
    62             double radius);
    63 
    64     /**
    65      * Returns true if the the given x,y point is inside the path.
    66      *
    67      * @param x the X coordinate to use for the check.
    68      * @param y the Y coordinate to use for the check.
    69      * @return true if the point given is inside the path, false otherwise.
    70      */
    71     public boolean isPointInPath(double x, double y);
    72 
    73     /**
    74      * Fills the path with the current fill paint.
    75      */
    76     public void fill();
    77 
    78     /**
    79      * Strokes the path with the current stroke paint.
    80      */
    81     public void stroke();
    82 
    83     /**
    84      * Starts a Path
    85      */
    86     public void beginPath();
    87 
    88     /**
    89      * Closes the path.
    90      */
    91     public void closePath();
    92 
    93     /**
    94      * Clips using the current path
    95      */
    96     public void clip();
    97 
    98     /**
    99      * Issues a move command for the current path to the given x,y coordinate.
   100      *
   101      * @param x the X position for the move to command.
   102      * @param y the Y position for the move to command.
   103      */
   104     public void moveTo(double x, double y);
   105 
   106     /**
   107      * Adds segments to the current path to make a line at the given x,y
   108      * coordinate.
   109      *
   110      * @param x the X coordinate of the ending point of the line.
   111      * @param y the Y coordinate of the ending point of the line.
   112      */
   113     public void lineTo(double x, double y);
   114 
   115     /**
   116      * Adds segments to the current path to make a quadratic curve.
   117      *
   118      * @param cpx the X coordinate of the control point
   119      * @param cpy the Y coordinate of the control point
   120      * @param x the X coordinate of the end point
   121      * @param y the Y coordinate of the end point
   122      */
   123     public void quadraticCurveTo(double cpx, double cpy, double x, double y);
   124 
   125     /**
   126      * Adds segments to the current path to make a cubic bezier curve.
   127      *
   128      * @param cp1x the X coordinate of first bezier control point.
   129      * @param cp1y the Y coordinate of the first bezier control point.
   130      * @param cp2x the X coordinate of the second bezier control point.
   131      * @param cp2y the Y coordinate of the second bezier control point.
   132      * @param x the X coordinate of the end point.
   133      * @param y the Y coordinate of the end point.
   134      */
   135     public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
   136 
   137     /**
   138      * Fills a rectangle using the current fill paint.
   139      *
   140      * @param x the X position of the upper left corner of the rectangle.
   141      * @param y the Y position of the upper left corner of the rectangle.
   142      * @param width the width of the rectangle.
   143      * @param height the height of the rectangle.
   144      */
   145     public void fillRect(double x, double y, double width, double height);
   146 
   147     /**
   148      * Strokes a rectangle using the current stroke paint.
   149      *
   150      * @param x the X position of the upper left corner of the rectangle.
   151      * @param y the Y position of the upper left corner of the rectangle.
   152      * @param width the width of the rectangle.
   153      * @param height the height of the rectangle.
   154      */
   155     public void strokeRect(double x, double y, double width, double height);
   156 
   157     /**
   158      * Clears a portion of the canvas with a transparent color value.
   159      *
   160      * @param x X position of the upper left corner of the rectangle.
   161      * @param y Y position of the upper left corner of the rectangle.
   162      * @param width width of the rectangle.
   163      * @param height height of the rectangle.
   164      */
   165     public void clearRect(double x, double y, double width, double height);
   166 
   167     /**
   168      * Clears a portion of the canvas with a transparent color value.
   169      *
   170      * @param x X position of the upper left corner of the rectangle.
   171      * @param y Y position of the upper left corner of the rectangle.
   172      * @param width width of the rectangle.
   173      * @param height height of the rectangle.
   174      */
   175     public void rect(double x, double y, double width, double height);
   176 
   177     /**
   178      * Saves the following attributes onto a stack.
   179      * <ul>
   180      * <li>Global Alpha</li>
   181      * <li>Global Blend Operation</li>
   182      * <li>Transform</li>
   183      * <li>Fill Paint</li>
   184      * <li>Stroke Paint</li>
   185      * <li>Line Width</li>
   186      * <li>Line Cap</li>
   187      * <li>Line Join</li>
   188      * <li>Miter Limit</li>
   189      * <li>Number of Clip Paths</li>
   190      * <li>Font</li>
   191      * <li>Text Align</li>
   192      * <li>Text Baseline</li>
   193      * <li>Effect</li>
   194      * <li>Fill Rule</li>
   195      * </ul>
   196      * This method does NOT alter the current state in any way. Also, not that
   197      * the current path is not saved.
   198      */
   199     public void save();
   200 
   201     /**
   202      * Pops the state off of the stack, setting the following attributes to
   203      * their value at the time when that state was pushed onto the stack. If the
   204      * stack is empty then nothing is changed.
   205      *
   206      * <ul>
   207      * <li>Global Alpha</li>
   208      * <li>Global Blend Operation</li>
   209      * <li>Transform</li>
   210      * <li>Fill Paint</li>
   211      * <li>Stroke Paint</li>
   212      * <li>Line Width</li>
   213      * <li>Line Cap</li>
   214      * <li>Line Join</li>
   215      * <li>Miter Limit</li>
   216      * <li>Number of Clip Paths</li>
   217      * <li>Font</li>
   218      * <li>Text Align</li>
   219      * <li>Text Baseline</li>
   220      * <li>Effect</li>
   221      * <li>Fill Rule</li>
   222      * </ul>
   223      */
   224     public void restore();
   225 
   226     /**
   227      * Rotates the current transform in degrees.
   228      *
   229      * @param angle value in degrees to rotate the current transform.
   230      */
   231     public void rotate(double angle);
   232 
   233     /**
   234      * Concatenates the input with the current transform.
   235      *
   236      * @param a - the X coordinate scaling element of the 3x4 matrix
   237      * @param b - the Y coordinate shearing element of the 3x4 matrix
   238      * @param c - the X coordinate shearing element of the 3x4 matrix
   239      * @param d - the Y coordinate scaling element of the 3x4 matrix
   240      * @param e - the X coordinate translation element of the 3x4 matrix
   241      * @param f - the Y coordinate translation element of the 3x4 matrix
   242      */
   243     public void transform(double a, double b, double c, double d, double e, double f);
   244 
   245     /**
   246      * Concatenates the input with the current transform.
   247      *
   248      * @param a - the X coordinate scaling element of the 3x4 matrix
   249      * @param b - the Y coordinate shearing element of the 3x4 matrix
   250      * @param c - the X coordinate shearing element of the 3x4 matrix
   251      * @param d - the Y coordinate scaling element of the 3x4 matrix
   252      * @param e - the X coordinate translation element of the 3x4 matrix
   253      * @param f - the Y coordinate translation element of the 3x4 matrix
   254      */
   255     public void setTransform(double a, double b, double c, double d, double e, double f);
   256 
   257     /**
   258      * Translates the current transform by x, y.
   259      *
   260      * @param x value to translate along the x axis.
   261      * @param y value to translate along the y axis.
   262      */
   263     public void translate(double x, double y);
   264 
   265     /**
   266      * Scales the current transform by x, y.
   267      *
   268      * @param x value to scale in the x axis.
   269      * @param y value to scale in the y axis.
   270      */
   271     public void scale(double x, double y);
   272 
   273     /**
   274      * Draws an image at the given x, y position using the width and height of
   275      * the given image.
   276      *
   277      * @param image the image to be drawn.
   278      * @param x the X coordinate on the destination for the upper left of the
   279      * image.
   280      * @param y the Y coordinate on the destination for the upper left of the
   281      * image.
   282      * @return the native Image for caching.
   283      */
   284     public Object drawImage(Image image, double x, double y, Object nativeImage);
   285 
   286     /**
   287      * Draws an image into the given destination rectangle of the canvas. The
   288      * Image is scaled to fit into the destination rectagnle.
   289      *
   290      * @param image the image to be drawn.
   291      * @param x the X coordinate on the destination for the upper left of the
   292      * image.
   293      * @param y the Y coordinate on the destination for the upper left of the
   294      * image.
   295      * @param width the width of the destination rectangle.
   296      * @param height the height of the destination rectangle.
   297      * @return the native Image for caching.
   298      *
   299      */
   300     public Object drawImage(Image image, double x, double y, double width, double height, Object nativeImage);
   301 
   302     /**
   303      * Draws the current source rectangle of the given image to the given
   304      * destination rectangle of the Canvas.
   305      *
   306      * @param image the image to be drawn.
   307      * @param sx the source rectangle's X coordinate position.
   308      * @param sy the source rectangle's Y coordinate position.
   309      * @param sWidth the source rectangle's width.
   310      * @param sHeight the source rectangle's height.
   311      * @param x the destination rectangle's X coordinate position.
   312      * @param y the destination rectangle's Y coordinate position.
   313      * @param width the destination rectangle's width.
   314      * @param height the destination rectangle's height.
   315      * @return the native Image for caching.
   316      */
   317     public Object drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height, Object nativeImage);
   318 
   319     /**
   320      * Get the width of this Image
   321      *
   322      * @param image the image to measure
   323      * @param nativeImage the cached native Image or null
   324      * @return the width of the image
   325      */
   326     public int getWidth(Image image, Object nativeImage);
   327 
   328     /**
   329      * Get the height of this Image
   330      *
   331      * @param image the image to measure
   332      * @param nativeImage the cached native Image or null
   333      * @return the height of the image
   334      */
   335     public int getHeight(Image image, Object nativeImage);
   336 
   337     /**
   338      * When implementing you can return an Object of your choice to enable
   339      * caching. Returning null means no caching. When caching is enabled, and
   340      * the cache hasn't been invalidated, the Object you returned will be passed
   341      * as a parameter.
   342      *
   343      * @param style The style object you should use to create your native style
   344      * @param nativeStyle your native object if cached, null otherwise
   345      * @return return native Object for caching
   346      *
   347      */
   348     public Object setFillStyle(Style style, Object nativeStyle);
   349 
   350     /**
   351      * When implementing you can return an Object of your choice to enable
   352      * caching. Returning null means no caching. When caching is enabled, and
   353      * the cache hasn't been invalidated, the Object you returned will be passed
   354      * as a parameter.
   355      *
   356      * @param style The style object you should use to create your native style
   357      * @param nativeStyle your native object if cached, null otherwise
   358      * @return return native Object for caching
   359      *
   360      */
   361     public Object setStrokeStyle(Style style, Object nativeStyle);
   362 
   363 //    public void setShadowColor(String color);
   364 //
   365 //    public void setShadowBlur(double blur);
   366 //
   367 //    public void setShadowOffsetX(double x);
   368 //
   369 //    public void setShadowOffsetY(double y);
   370 //    
   371 //    public String getShadowColor();
   372 //
   373 //    public double getShadowBlur();
   374 //
   375 //    public double getShadowOffsetX();
   376 //
   377 //    public double getShadowOffsetY();
   378     /**
   379      * Gets the current stroke line cap.
   380      *
   381      * @return {@code StrokeLineCap} with a value of Butt, Round, or Square.
   382      */
   383     public String getLineCap();
   384 
   385     /**
   386      * Sets the current stroke line cap.
   387      *
   388      * @param style a value of Butt, Round, or Square.
   389      */
   390     public void setLineCap(String style);
   391 
   392     /**
   393      * Gets the current stroke line join.
   394      *
   395      * @return a value of Miter, Bevel, or Round.
   396      */
   397     public String getLineJoin();
   398 
   399     /**
   400      * Sets the current stroke line join.
   401      *
   402      * @param style with a value of Miter, Bevel, or Round.
   403      */
   404     public void setLineJoin(String style);
   405 
   406     /**
   407      * Gets the current line width.
   408      *
   409      * @return value between 0 and infinity.
   410      */
   411     public double getLineWidth();
   412 
   413     /**
   414      * Sets the current line width.
   415      *
   416      * @param width value in the range {0-positive infinity}, with any other
   417      * value being ignored and leaving the value unchanged.
   418      */
   419     public void setLineWidth(double width);
   420 
   421     /**
   422      * Gets the current miter limit. v
   423      *
   424      * @return the miter limit value in the range {@code 0.0-positive infinity}
   425      */
   426     public double getMiterLimit();
   427 
   428     /**
   429      * Sets the current miter limit.
   430      *
   431      * @param limit miter limit value between 0 and positive infinity with any
   432      * other value being ignored and leaving the value unchanged.
   433      */
   434     public void setMiterLimit(double limit);
   435 
   436     /**
   437      * Gets the current Font.
   438      *
   439      * @return the Font
   440      */
   441     public String getFont();
   442 
   443     /**
   444      * Sets the current Font.
   445      *
   446      */
   447     public void setFont(String font);
   448 
   449     /**
   450      * Gets the current {@code TextAlignment}.
   451      *
   452      * @return TextAlignment with values of Left, Center, Right, or Justify.
   453      */
   454     public String getTextAlign();
   455 
   456     /**
   457      * Defines horizontal text alignment, relative to the text origin.
   458      *
   459      * @param textAlign with values of Left, Center, Right.
   460      */
   461     public void setTextAlign(String textAlign);
   462 
   463     /**
   464      * Sets the current Text Baseline.
   465      *
   466      * @param baseline with values of Top, Center, Baseline, or Bottom
   467      */
   468     public String getTextBaseline();
   469 
   470     /**
   471      * Sets the current Text Baseline.
   472      *
   473      * @param baseline with values of Top, Center, Baseline, or Bottom
   474      */
   475     public void setTextBaseline(String baseline);
   476 
   477     /**
   478      * Fills the given string of text at position x, y (0,0 at top left) with
   479      * the current fill paint attribute.
   480      *
   481      * @param text the string of text.
   482      * @param x position on the x axis.
   483      * @param y position on the y axis.
   484      */
   485     public void fillText(String text, double x, double y);
   486 
   487     /**
   488      * Fills text and includes a maximum width of the string.
   489      *
   490      * If the width of the text extends past max width, then it will be sized to
   491      * fit.
   492      *
   493      * @param text the string of text.
   494      * @param x position on the x axis.
   495      * @param y position on the y axis.
   496      * @param maxWidth maximum width the text string can have.
   497      */
   498     public void fillText(String text, double x, double y, double maxWidth);
   499 
   500     /**
   501      * The Dimension of this text using the current Font settings
   502      *
   503      * @param text
   504      * @return the Dimension of this text using the current Font settings
   505      */
   506     public Dimension measureText(String text);
   507 
   508     /**
   509      * draws the given string of text at position x, y (0,0 at top left) with
   510      * the current stroke paint attribute.
   511      *
   512      * @param text the string of text.
   513      * @param x position on the x axis.
   514      * @param y position on the y axis.
   515      */
   516     public void strokeText(String text, double x, double y);
   517 
   518     /**
   519      * Draws text with stroke paint and includes a maximum width of the string.
   520      *
   521      * If the width of the text extends past max width, then it will be sized to
   522      * fit.
   523      *
   524      * @param text the string of text.
   525      * @param x position on the x axis.
   526      * @param y position on the y axis.
   527      * @param maxWidth maximum width the text string can have.
   528      */
   529     public void strokeText(String text, double x, double y, double maxWidth);
   530 
   531     /**
   532      * Get a pixel array that you can manipulate, e.g. apply effects / transparency
   533      * @param x width
   534      * @param y height
   535      * @return a PixelMap
   536      */
   537     public ImageData createPixelMap(double x, double y);
   538 
   539     /**
   540      * Create a new ImageData object with the same dimensions as the 
   541      * object specified by imageData (this does not copy the image data)
   542      * @param imageData
   543      * @return 
   544      */
   545     public ImageData createPixelMap(ImageData imageData);
   546 
   547     /**
   548      * Get the pixels for a region of your GraphicsContext
   549      * @param x start x coordinate
   550      * @param y start y coordinate
   551      * @param width width
   552      * @param height height
   553      * @return 
   554      */
   555     public ImageData getPixelMap(double x, double y, double width, double height);
   556 
   557     /**
   558      * Render an ImageData Object at the specified position
   559      * @param imageData the Pixel array
   560       * @param x start x coordinate
   561      * @param y start y coordinate
   562      */
   563    public void putPixelMap(ImageData imageData, double x, double y);
   564 
   565     /**
   566      * Render an ImageData Object at the specified position
   567      * @param imageData the Pixel array to draw
   568      * @param x start x coordinate
   569      * @param y start y coordinate
   570      * @param dirtyx The horizontal (x) value, in pixels, where to place the image on the canvas
   571      * @param dirtyy The vertical (y) value, in pixels, where to place the image on the canvas
   572      * @param dirtywidth The width to use to draw the image on the canvas
   573      * @param dirtyheight The height to use to draw the image on the canvas
   574      */
   575     public void putPixelMap(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
   576     /**
   577      * Sets the global alpha of the current state.
   578      *
   579      * @param alpha value in the range {@code 0.0-1.0}. The value is clamped if
   580      * it is out of range.
   581      */
   582     public void setGlobalAlpha(double alpha);
   583 
   584     /**
   585      * Get the global alpha of the current state.
   586      *
   587      * @return alpha value in the range {@code 0.0-1.0}.
   588      */
   589     public double getGlobalAlpha();
   590 
   591     /**
   592      * Sets the global blend mode.
   593      *
   594      * @param operation the BlendMode that will be set.
   595      */
   596     public void setGlobalCompositeOperation(String operation);
   597 
   598     /**
   599      * Gets the global blend mode.
   600      *
   601      * @return the global BlendMode of the current state.
   602      */
   603     public String getGlobalCompositeOperation();
   604 
   605     /**
   606      * Get the height of this GraphicsContext (which should be the same as the
   607      * enclosing canvas height)
   608      *
   609      * @return the height of this GraphicsContext
   610      */
   611     public int getHeight();
   612 
   613     /**
   614      * Get the width of this GraphicsContext (which should be the same as the
   615      * enclosing canvas height)
   616      *
   617      * @return the width of this GraphicsContext
   618      */
   619     public int getWidth();
   620 
   621 //    public void setHeight(int height);
   622 //
   623 //    public void setWidth(int width);
   624      /**
   625      * Merges two images drawing one on top of the other and returning the
   626      * result.
   627      *
   628      * @param a the lower Image
   629      * @param b the upper Image
   630      * @param cachedA the native cached Image, if available, or null.
   631      * @param cachedB the native cached Image, if available, or null.
   632      * @return
   633      */  
   634     public Object mergeImages(Image a, Image b, Object cachedA, Object cachedB);
   635 }