Bug 1704628 Part 4: Avoid use of ESC to close context menu in browser_toolbox_content...
[gecko.git] / dom / webidl / CanvasRenderingContext2D.webidl
blob0294cd32b8024b5f3c483d27bf0930ad1e679826
1 /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4  * You can obtain one at http://mozilla.org/MPL/2.0/.
5  *
6  * The origin of this IDL file is
7  * http://www.whatwg.org/specs/web-apps/current-work/
8  *
9  * © Copyright 2004-2011 Apple Computer, Inc., Mozilla Foundation, and
10  * Opera Software ASA. You are granted a license to use, reproduce
11  * and create derivative works of this document.
12  */
14 enum CanvasWindingRule { "nonzero", "evenodd" };
16 [GenerateInit]
17 dictionary ContextAttributes2D {
18   // whether or not we're planning to do a lot of readback operations
19   boolean willReadFrequently = false;
20   // signal if the canvas contains an alpha channel
21   boolean alpha = true;
24 dictionary HitRegionOptions {
25   Path2D? path = null;
26   DOMString id = "";
27   Element? control = null;
30 typedef (HTMLImageElement or
31          SVGImageElement) HTMLOrSVGImageElement;
33 typedef (HTMLOrSVGImageElement or
34          HTMLCanvasElement or
35          HTMLVideoElement or
36          ImageBitmap) CanvasImageSource;
38 [Exposed=Window]
39 interface CanvasRenderingContext2D {
41   // back-reference to the canvas.  Might be null if we're not
42   // associated with a canvas.
43   readonly attribute HTMLCanvasElement? canvas;
45   // Mozilla-specific stuff
46   // FIXME Bug 768048 mozCurrentTransform/mozCurrentTransformInverse should return a WebIDL array.
47   [Throws]
48   attribute object mozCurrentTransform; // [ m11, m12, m21, m22, dx, dy ], i.e. row major
49   [Throws]
50   attribute object mozCurrentTransformInverse;
52   [SetterThrows]
53   attribute UTF8String mozTextStyle;
55   // image smoothing mode -- if disabled, images won't be smoothed
56   // if scaled.
57   [Deprecated="PrefixedImageSmoothingEnabled",
58    BinaryName="imageSmoothingEnabled"]
59   attribute boolean mozImageSmoothingEnabled;
61   // Show the caret if appropriate when drawing
62   [Func="CanvasUtils::HasDrawWindowPrivilege"]
63   const unsigned long DRAWWINDOW_DRAW_CARET   = 0x01;
64   // Don't flush pending layout notifications that could otherwise
65   // be batched up
66   [Func="CanvasUtils::HasDrawWindowPrivilege"]
67   const unsigned long DRAWWINDOW_DO_NOT_FLUSH = 0x02;
68   // Draw scrollbars and scroll the viewport if they are present
69   [Func="CanvasUtils::HasDrawWindowPrivilege"]
70   const unsigned long DRAWWINDOW_DRAW_VIEW    = 0x04;
71   // Use the widget layer manager if available. This means hardware
72   // acceleration may be used, but it might actually be slower or
73   // lower quality than normal. It will however more accurately reflect
74   // the pixels rendered to the screen.
75   [Func="CanvasUtils::HasDrawWindowPrivilege"]
76   const unsigned long DRAWWINDOW_USE_WIDGET_LAYERS = 0x08;
77   // Don't synchronously decode images - draw what we have
78   [Func="CanvasUtils::HasDrawWindowPrivilege"]
79   const unsigned long DRAWWINDOW_ASYNC_DECODE_IMAGES = 0x10;
81   /**
82    * Renders a region of a window into the canvas.  The contents of
83    * the window's viewport are rendered, ignoring viewport clipping
84    * and scrolling.
85    *
86    * @param x
87    * @param y
88    * @param w
89    * @param h specify the area of the window to render, in CSS
90    * pixels.
91    *
92    * @param backgroundColor the canvas is filled with this color
93    * before we render the window into it. This color may be
94    * transparent/translucent. It is given as a CSS color string
95    * (e.g., rgb() or rgba()).
96    *
97    * @param flags Used to better control the drawWindow call.
98    * Flags can be ORed together.
99    *
100    * Of course, the rendering obeys the current scale, transform and
101    * globalAlpha values.
102    *
103    * Hints:
104    * -- If 'rgba(0,0,0,0)' is used for the background color, the
105    * drawing will be transparent wherever the window is transparent.
106    * -- Top-level browsed documents are usually not transparent
107    * because the user's background-color preference is applied,
108    * but IFRAMEs are transparent if the page doesn't set a background.
109    * -- If an opaque color is used for the background color, rendering
110    * will be faster because we won't have to compute the window's
111    * transparency.
112    *
113    * This API cannot currently be used by Web content. It is chrome
114    * and Web Extensions (with a permission) only.
115    */
116   [Throws, NeedsSubjectPrincipal, Func="CanvasUtils::HasDrawWindowPrivilege"]
117   void drawWindow(Window window, double x, double y, double w, double h,
118                   UTF8String bgColor, optional unsigned long flags = 0);
120   /**
121    * This causes a context that is currently using a hardware-accelerated
122    * backend to fallback to a software one. All state should be preserved.
123    */
124   [ChromeOnly]
125   void demote();
128 CanvasRenderingContext2D includes CanvasState;
129 CanvasRenderingContext2D includes CanvasTransform;
130 CanvasRenderingContext2D includes CanvasCompositing;
131 CanvasRenderingContext2D includes CanvasImageSmoothing;
132 CanvasRenderingContext2D includes CanvasFillStrokeStyles;
133 CanvasRenderingContext2D includes CanvasShadowStyles;
134 CanvasRenderingContext2D includes CanvasFilters;
135 CanvasRenderingContext2D includes CanvasRect;
136 CanvasRenderingContext2D includes CanvasDrawPath;
137 CanvasRenderingContext2D includes CanvasUserInterface;
138 CanvasRenderingContext2D includes CanvasText;
139 CanvasRenderingContext2D includes CanvasDrawImage;
140 CanvasRenderingContext2D includes CanvasImageData;
141 CanvasRenderingContext2D includes CanvasPathDrawingStyles;
142 CanvasRenderingContext2D includes CanvasTextDrawingStyles;
143 CanvasRenderingContext2D includes CanvasPathMethods;
144 CanvasRenderingContext2D includes CanvasHitRegions;
147 interface mixin CanvasState {
148   // state
149   void save(); // push state on state stack
150   void restore(); // pop state stack and restore state
153 interface mixin CanvasTransform {
154   // transformations (default transform is the identity matrix)
155   [Throws, LenientFloat]
156   void scale(double x, double y);
157   [Throws, LenientFloat]
158   void rotate(double angle);
159   [Throws, LenientFloat]
160   void translate(double x, double y);
161   [Throws, LenientFloat]
162   void transform(double a, double b, double c, double d, double e, double f);
164   [NewObject, Throws] DOMMatrix getTransform();
165   [Throws, LenientFloat]
166   void setTransform(double a, double b, double c, double d, double e, double f);
167   [Throws]
168   void setTransform(optional DOMMatrix2DInit transform = {});
169   [Throws]
170   void resetTransform();
173 interface mixin CanvasCompositing {
174   attribute unrestricted double globalAlpha; // (default 1.0)
175   [Throws]
176   attribute DOMString globalCompositeOperation; // (default source-over)
179 interface mixin CanvasImageSmoothing {
180   // drawing images
181   attribute boolean imageSmoothingEnabled;
184 interface mixin CanvasFillStrokeStyles {
185   // colors and styles (see also the CanvasPathDrawingStyles interface)
186   attribute (UTF8String or CanvasGradient or CanvasPattern) strokeStyle; // (default black)
187   attribute (UTF8String or CanvasGradient or CanvasPattern) fillStyle; // (default black)
188   [NewObject]
189   CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1);
190   [NewObject, Throws]
191   CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1);
192   [Pref="canvas.createConicGradient.enabled", NewObject]
193   CanvasGradient createConicGradient(double angle, double cx, double cy);
194   [NewObject, Throws]
195   CanvasPattern? createPattern(CanvasImageSource image, [LegacyNullToEmptyString] DOMString repetition);
198 interface mixin CanvasShadowStyles {
199   [LenientFloat]
200   attribute double shadowOffsetX; // (default 0)
201   [LenientFloat]
202   attribute double shadowOffsetY; // (default 0)
203   [LenientFloat]
204   attribute double shadowBlur; // (default 0)
205   attribute UTF8String shadowColor; // (default transparent black)
208 interface mixin CanvasFilters {
209   [Pref="canvas.filters.enabled", SetterThrows]
210   attribute UTF8String filter; // (default empty string = no filter)
213 interface mixin CanvasRect {
214   [LenientFloat]
215   void clearRect(double x, double y, double w, double h);
216   [LenientFloat]
217   void fillRect(double x, double y, double w, double h);
218   [LenientFloat]
219   void strokeRect(double x, double y, double w, double h);
222 interface mixin CanvasDrawPath {
223   // path API (see also CanvasPathMethods)
224   void beginPath();
225   void fill(optional CanvasWindingRule winding = "nonzero");
226   void fill(Path2D path, optional CanvasWindingRule winding = "nonzero");
227   void stroke();
228   void stroke(Path2D path);
229   void clip(optional CanvasWindingRule winding = "nonzero");
230   void clip(Path2D path, optional CanvasWindingRule winding = "nonzero");
231 // NOT IMPLEMENTED  void resetClip();
232   [NeedsSubjectPrincipal]
233   boolean isPointInPath(unrestricted double x, unrestricted double y, optional CanvasWindingRule winding = "nonzero");
234   [NeedsSubjectPrincipal] // Only required because overloads can't have different extended attributes.
235   boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y, optional CanvasWindingRule winding = "nonzero");
236   [NeedsSubjectPrincipal]
237   boolean isPointInStroke(double x, double y);
238   [NeedsSubjectPrincipal] // Only required because overloads can't have different extended attributes.
239   boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);
242 interface mixin CanvasUserInterface {
243   [Pref="canvas.focusring.enabled", Throws] void drawFocusIfNeeded(Element element);
244 // NOT IMPLEMENTED  void scrollPathIntoView();
245 // NOT IMPLEMENTED  void scrollPathIntoView(Path path);
248 interface mixin CanvasText {
249   // text (see also the CanvasPathDrawingStyles interface)
250   [Throws, LenientFloat]
251   void fillText(DOMString text, double x, double y, optional double maxWidth);
252   [Throws, LenientFloat]
253   void strokeText(DOMString text, double x, double y, optional double maxWidth);
254   [NewObject, Throws]
255   TextMetrics measureText(DOMString text);
258 interface mixin CanvasDrawImage {
259   [Throws, LenientFloat]
260   void drawImage(CanvasImageSource image, double dx, double dy);
261   [Throws, LenientFloat]
262   void drawImage(CanvasImageSource image, double dx, double dy, double dw, double dh);
263   [Throws, LenientFloat]
264   void drawImage(CanvasImageSource image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh);
267 // See https://github.com/whatwg/html/issues/6262 for [EnforceRange] usage.
268 interface mixin CanvasImageData {
269   // pixel manipulation
270   [NewObject, Throws]
271   ImageData createImageData([EnforceRange] long sw, [EnforceRange] long sh);
272   [NewObject, Throws]
273   ImageData createImageData(ImageData imagedata);
274   [NewObject, Throws, NeedsSubjectPrincipal]
275   ImageData getImageData([EnforceRange] long sx, [EnforceRange] long sy, [EnforceRange] long sw, [EnforceRange] long sh);
276   [Throws]
277   void putImageData(ImageData imagedata, [EnforceRange] long dx, [EnforceRange] long dy);
278   [Throws]
279   void putImageData(ImageData imagedata, [EnforceRange] long dx, [EnforceRange] long dy, [EnforceRange] long dirtyX, [EnforceRange] long dirtyY, [EnforceRange] long dirtyWidth, [EnforceRange] long dirtyHeight);
282 interface mixin CanvasPathDrawingStyles {
283   // line caps/joins
284   [LenientFloat]
285   attribute double lineWidth; // (default 1)
286   attribute DOMString lineCap; // "butt", "round", "square" (default "butt")
287   [GetterThrows]
288   attribute DOMString lineJoin; // "round", "bevel", "miter" (default "miter")
289   [LenientFloat]
290   attribute double miterLimit; // (default 10)
292   // dashed lines
293   [LenientFloat, Throws] void setLineDash(sequence<double> segments); // default empty
294   sequence<double> getLineDash();
295   [LenientFloat] attribute double lineDashOffset;
298 interface mixin CanvasTextDrawingStyles {
299   // text
300   [SetterThrows]
301   attribute UTF8String font; // (default 10px sans-serif)
302   attribute DOMString textAlign; // "start", "end", "left", "right", "center" (default: "start")
303   attribute DOMString textBaseline; // "top", "hanging", "middle", "alphabetic", "ideographic", "bottom" (default: "alphabetic")
306 interface mixin CanvasPathMethods {
307   // shared path API methods
308   void closePath();
309   [LenientFloat]
310   void moveTo(double x, double y);
311   [LenientFloat]
312   void lineTo(double x, double y);
313   [LenientFloat]
314   void quadraticCurveTo(double cpx, double cpy, double x, double y);
316   [LenientFloat]
317   void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
319   [Throws, LenientFloat]
320   void arcTo(double x1, double y1, double x2, double y2, double radius);
321 // NOT IMPLEMENTED  [LenientFloat] void arcTo(double x1, double y1, double x2, double y2, double radiusX, double radiusY, double rotation);
323   [LenientFloat]
324   void rect(double x, double y, double w, double h);
326   [Throws, LenientFloat]
327   void arc(double x, double y, double radius, double startAngle, double endAngle, optional boolean anticlockwise = false);
329   [Throws, LenientFloat]
330   void ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, optional boolean anticlockwise = false);
333 interface mixin CanvasHitRegions {
334   // hit regions
335   [Pref="canvas.hitregions.enabled", Throws] void addHitRegion(optional HitRegionOptions options = {});
336   [Pref="canvas.hitregions.enabled"] void removeHitRegion(DOMString id);
337   [Pref="canvas.hitregions.enabled"] void clearHitRegions();
340 [Exposed=Window]
341 interface CanvasGradient {
342   // opaque object
343   [Throws]
344   // addColorStop should take a double
345   void addColorStop(float offset, UTF8String color);
348 [Exposed=Window]
349 interface CanvasPattern {
350   // opaque object
351   // [Throws, LenientFloat] - could not do this overload because of bug 1020975
352   // void setTransform(double a, double b, double c, double d, double e, double f);
354   [Throws]
355   void setTransform(optional DOMMatrix2DInit matrix = {});
358 [Exposed=Window]
359 interface TextMetrics {
361   // x-direction
362   readonly attribute double width; // advance width
364   // [experimental] actualBoundingBox* attributes
365   [Pref="dom.textMetrics.actualBoundingBox.enabled"]
366   readonly attribute double actualBoundingBoxLeft;
367   [Pref="dom.textMetrics.actualBoundingBox.enabled"]
368   readonly attribute double actualBoundingBoxRight;
370   // y-direction
371   // [experimental] fontBoundingBox* attributes
372   [Pref="dom.textMetrics.fontBoundingBox.enabled"]
373   readonly attribute double fontBoundingBoxAscent;
374   [Pref="dom.textMetrics.fontBoundingBox.enabled"]
375   readonly attribute double fontBoundingBoxDescent;
377   // [experimental] actualBoundingBox* attributes
378   [Pref="dom.textMetrics.actualBoundingBox.enabled"]
379   readonly attribute double actualBoundingBoxAscent;
380   [Pref="dom.textMetrics.actualBoundingBox.enabled"]
381   readonly attribute double actualBoundingBoxDescent;
383   // [experimental] emHeight* attributes
384   [Pref="dom.textMetrics.emHeight.enabled"]
385   readonly attribute double emHeightAscent;
386   [Pref="dom.textMetrics.emHeight.enabled"]
387   readonly attribute double emHeightDescent;
389   // [experimental] *Baseline attributes
390   [Pref="dom.textMetrics.baselines.enabled"]
391   readonly attribute double hangingBaseline;
392   [Pref="dom.textMetrics.baselines.enabled"]
393   readonly attribute double alphabeticBaseline;
394   [Pref="dom.textMetrics.baselines.enabled"]
395   readonly attribute double ideographicBaseline;
398 [Pref="canvas.path.enabled",
399  Exposed=Window]
400 interface Path2D
402   constructor();
403   constructor(Path2D other);
404   constructor(DOMString pathString);
406   [Throws] void addPath(Path2D path, optional DOMMatrix2DInit transform = {});
408 Path2D includes CanvasPathMethods;