chromium/third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d.idl

/*
 * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// https://html.spec.whatwg.org/C/#canvasrenderingcontext2d

// The spec specifies:
//    typedef (HTMLImageElement or
//             SVGImageElement) HTMLOrSVGImageElement;
// but there's a problem with our IDL code generator for typedef-in-typedef,
// so we split this into two for simplicity. There's no difference from a user
// perspective.
// TODO(fserb): revisit this once union typedefs are finalized.

typedef (CSSImageValue or
         HTMLImageElement or
         SVGImageElement or
         HTMLVideoElement or
         HTMLCanvasElement or
         ImageBitmap or
         OffscreenCanvas or
         VideoFrame) CanvasImageSource;

enum CanvasFillRule { "nonzero", "evenodd" };
enum ImageSmoothingQuality {"low", "medium", "high"};
enum CanvasTextRendering {"auto", "optimizeSpeed", "optimizeLegibility", "geometricPrecision"};
enum CanvasFontStretch {"ultra-condensed", "extra-condensed", "condensed", "semi-condensed", "normal", "semi-expanded", "expanded", "extra-expanded", "ultra-expanded"};

dictionary BeginLayerOptions {
  CanvasFilterInput? filter = null;
};

[
    ActiveScriptWrappable,
    Exposed=Window
]
interface CanvasRenderingContext2D {
    // back-reference to the canvas
    readonly attribute HTMLCanvasElement canvas;

    // state
    void save(); // push state on state stack
    [NoAllocDirectCall, RaisesException] void restore(); // pop state stack if top state was pushed by save, and restore state
    [MeasureAs=Canvas2DLayers, RuntimeEnabled=Canvas2dLayers, CallWith=ScriptState] void beginLayer(); // push state on state stack and creates bitmap for subsequent draw ops
    [MeasureAs=Canvas2DLayers, RuntimeEnabled=Canvas2dLayersWithOptions, CallWith=ScriptState, RaisesException] void beginLayer(BeginLayerOptions options); // push state on state stack and creates bitmap for subsequent draw ops
    [NoAllocDirectCall, RuntimeEnabled=Canvas2dLayers, RaisesException] void endLayer(); // pop state stack if top state was pushed by beginLayer, restore state and draw the bitmap
    // Clear the canvas and reset the path
    void reset();

    // transformations (default transform is the identity matrix)
    [HighEntropy] void scale(unrestricted double x, unrestricted double y);
    [HighEntropy, NoAllocDirectCall] void rotate(unrestricted double angle);
    [HighEntropy] void translate(unrestricted double x, unrestricted double y);
    [HighEntropy] void transform(unrestricted double a, unrestricted double b, unrestricted double c, unrestricted double d, unrestricted double e, unrestricted double f);
    [HighEntropy] void setTransform(unrestricted double a, unrestricted double b, unrestricted double c, unrestricted double d, unrestricted double e, unrestricted double f);
    [RaisesException] void setTransform(optional DOMMatrixInit transform = {});
    DOMMatrix getTransform();
    [NoAllocDirectCall] void resetTransform();

    // compositing
    attribute unrestricted double globalAlpha; // (default 1.0)
    [HighEntropy] attribute DOMString globalCompositeOperation; // (default source-over)
    [MeasureAs=Canvas2DFilter, SetterCallWith=ScriptState] attribute (DOMString or CanvasFilter)? filter; // (default 'none')

    // image smoothing
    attribute boolean imageSmoothingEnabled; // (default True)
    [MeasureAs=Canvas2DImageSmoothingQuality]attribute ImageSmoothingQuality imageSmoothingQuality; // (default "low")

    // colors and styles (see also the CanvasDrawingStyles interface)
    [HighEntropy, SetterCallWith=Isolate, RaisesException=Setter, GetterCallWith=ScriptState] attribute any strokeStyle; // (default black)
    [HighEntropy, SetterCallWith=Isolate, RaisesException=Setter, GetterCallWith=ScriptState] attribute any fillStyle; // (default black)
    CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1);
    [RaisesException] CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1);
    CanvasGradient createConicGradient(double startAngle, double cx, double cy);
    [RaisesException] CanvasPattern? createPattern(CanvasImageSource image, [LegacyNullToEmptyString] DOMString repetitionType);

    // shadows
    [HighEntropy] attribute unrestricted double shadowOffsetX;
    [HighEntropy] attribute unrestricted double shadowOffsetY;
    [HighEntropy] attribute unrestricted double shadowBlur;
    [HighEntropy] attribute DOMString shadowColor;

    // rects
    [HighEntropy, NoAllocDirectCall] void clearRect(unrestricted double x, unrestricted double y, unrestricted double width, unrestricted double height);
    [HighEntropy, NoAllocDirectCall] void fillRect(unrestricted double x, unrestricted double y, unrestricted double width, unrestricted double height);
    [HighEntropy, NoAllocDirectCall] void strokeRect(unrestricted double x, unrestricted double y, unrestricted double width, unrestricted double height);

    // path API (see also CanvasPath)
    [HighEntropy, NoAllocDirectCall] void beginPath();
    [HighEntropy, NoAllocDirectCall] void fill();
    [HighEntropy] void fill(CanvasFillRule winding);
    [HighEntropy] void fill(Path2D path, optional CanvasFillRule winding);
    [HighEntropy, NoAllocDirectCall] void stroke();
    [HighEntropy] void stroke(Path2D path);
    // Focus rings
    void drawFocusIfNeeded(Element element);
    void drawFocusIfNeeded(Path2D path, Element element);

    void clip(optional CanvasFillRule winding);
    void clip(Path2D path, optional CanvasFillRule winding);
    [HighEntropy] boolean isPointInPath(unrestricted double x, unrestricted double y, optional CanvasFillRule winding);
    [HighEntropy] boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y, optional CanvasFillRule winding);
    [HighEntropy] boolean isPointInStroke(unrestricted double x, unrestricted double y);
    [HighEntropy] boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);

    // text (see also the CanvasDrawingStyles interface)
    [HighEntropy] void fillText(DOMString text, unrestricted double x, unrestricted double y, optional unrestricted double maxWidth);
    [HighEntropy] void strokeText(DOMString text, unrestricted double x, unrestricted double y, optional unrestricted double maxWidth);
    [HighEntropy] TextMetrics measureText(DOMString text);

    // drawing images
    [RaisesException] void drawImage(CanvasImageSource image, unrestricted double x, unrestricted double y);
    [RaisesException] void drawImage(CanvasImageSource image, unrestricted double x, unrestricted double y, unrestricted double width, unrestricted double height);
    [RaisesException] void drawImage(CanvasImageSource image, unrestricted double sx, unrestricted double sy, unrestricted double sw, unrestricted double sh, unrestricted double dx, unrestricted double dy, unrestricted double dw, unrestricted double dh);

    // placeElement: https://github.com/Igalia/explainers/blob/main/canvas-formatted-text/html-in-canvas.md
    [RuntimeEnabled=CanvasPlaceElement, RaisesException] void placeElement(Element element, unrestricted double x,
                                        unrestricted double y);

    // triangle mesh API
    [MeasureAs=Canvas2DMesh, RuntimeEnabled=Canvas2dMesh, RaisesException] Mesh2DVertexBuffer createMesh2DVertexBuffer(Float32Array buffer);
    [MeasureAs=Canvas2DMesh, RuntimeEnabled=Canvas2dMesh, RaisesException] Mesh2DUVBuffer createMesh2DUVBuffer(Float32Array buffer);
    [MeasureAs=Canvas2DMesh, RuntimeEnabled=Canvas2dMesh, RaisesException] Mesh2DIndexBuffer createMesh2DIndexBuffer(Uint16Array buffer);
    [MeasureAs=Canvas2DMesh, RuntimeEnabled=Canvas2dMesh, HighEntropy, RaisesException] void drawMesh(Mesh2DVertexBuffer vertex_buffer,
                                                                                                      Mesh2DUVBuffer uv_buffer,
                                                                                                      Mesh2DIndexBuffer index_buffer,
                                                                                                      CanvasImageSource image);

    // pixel manipulation
    [RaisesException] ImageData createImageData(ImageData imagedata);
    [RaisesException] ImageData createImageData([EnforceRange] long sw, [EnforceRange] long sh);
    [RaisesException] ImageData createImageData([EnforceRange] long sw, [EnforceRange] long sh, ImageDataSettings imageDataSettings);
    [HighEntropy, RaisesException] ImageData getImageData([EnforceRange] long sx, [EnforceRange] long sy, [EnforceRange] long sw, [EnforceRange] long sh);
    [HighEntropy, RaisesException] ImageData getImageData([EnforceRange] long sx, [EnforceRange] long sy, [EnforceRange] long sw, [EnforceRange] long sh, ImageDataSettings imageDataSettings);
    [RaisesException] void putImageData(ImageData imagedata, [EnforceRange] long dx, [EnforceRange] long dy);
    [RaisesException] void putImageData(ImageData imagedata, [EnforceRange] long dx, [EnforceRange] long dy, [EnforceRange] long dirtyX, [EnforceRange] long dirtyY, [EnforceRange] long dirtyWidth, [EnforceRange] long dirtyHeight);

    // Context state
    // Should be merged with WebGL counterpart in CanvasRenderingContext, once no-longer experimental
    boolean isContextLost();

    [MeasureAs=GetCanvas2DContextAttributes] CanvasRenderingContext2DSettings getContextAttributes();

    // FIXME: factor out to CanvasDrawingStyles
    // line caps/joins
    attribute unrestricted double lineWidth; // (default 1)
    attribute DOMString lineCap; // "butt", "round", "square" (default "butt")
    attribute DOMString lineJoin; // "round", "bevel", "miter" (default "miter")
    attribute unrestricted double miterLimit; // (default 10)

    // dashed lines
    void setLineDash(sequence<unrestricted double> dash);
    sequence<unrestricted double> getLineDash();
    attribute unrestricted double lineDashOffset;

    // text
    [HighEntropy] attribute DOMString font; // (default 10px sans-serif)
    attribute DOMString textAlign; // "start", "end", "left", "right", "center" (default: "start")
    attribute DOMString textBaseline; // "top", "hanging", "middle", "alphabetic", "ideographic", "bottom" (default: "alphabetic")
    attribute DOMString direction; // "inherit", "rtl", "ltr" (default: "inherit")
    attribute DOMString fontKerning; // "auto", "normal", "none" (default: "auto")
    attribute CanvasFontStretch fontStretch; // "ultra-condensed", "extra-condensed", "condensed", "semi-condensed", "normal", "semi-expanded", "expanded", "extra-expanded", "ultra-expanded" (default: normal)
    attribute DOMString fontVariantCaps; // "normal", "small-caps", "all-small-caps", "petite-caps", "all-petite-caps", "unicase", "titling-caps" (default: "normal")
    attribute DOMString letterSpacing; // length in pixel (default: "0px")
    attribute CanvasTextRendering textRendering; // "auto", "optimizeSpeed", "optimizeLegibility", "geometricPrecision" (default: "auto")
    attribute DOMString wordSpacing; // length in pixel (default: "0px")
};

CanvasRenderingContext2D includes CanvasPath;
CanvasRenderingContext2D includes Canvas2dGPUTransfer;