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 */
13
14enum CanvasWindingRule { "nonzero", "evenodd" };
15
16dictionary ContextAttributes2D {
17  // whether or not we're planning to do a lot of readback operations
18  boolean willReadFrequently = false;
19  // signal if the canvas contains an alpha channel
20  boolean alpha = true;
21};
22
23dictionary HitRegionOptions {
24  Path2D? path = null;
25  DOMString id = "";
26  Element? control = null;
27};
28
29typedef (HTMLImageElement or
30         SVGImageElement) HTMLOrSVGImageElement;
31
32typedef (HTMLOrSVGImageElement or
33         HTMLCanvasElement or
34         HTMLVideoElement or
35         ImageBitmap) CanvasImageSource;
36
37interface CanvasRenderingContext2D {
38
39  // back-reference to the canvas.  Might be null if we're not
40  // associated with a canvas.
41  readonly attribute HTMLCanvasElement? canvas;
42
43  // Show the caret if appropriate when drawing
44  [Func="CanvasUtils::HasDrawWindowPrivilege"]
45  const unsigned long DRAWWINDOW_DRAW_CARET   = 0x01;
46  // Don't flush pending layout notifications that could otherwise
47  // be batched up
48  [Func="CanvasUtils::HasDrawWindowPrivilege"]
49  const unsigned long DRAWWINDOW_DO_NOT_FLUSH = 0x02;
50  // Draw scrollbars and scroll the viewport if they are present
51  [Func="CanvasUtils::HasDrawWindowPrivilege"]
52  const unsigned long DRAWWINDOW_DRAW_VIEW    = 0x04;
53  // Use the widget layer manager if available. This means hardware
54  // acceleration may be used, but it might actually be slower or
55  // lower quality than normal. It will however more accurately reflect
56  // the pixels rendered to the screen.
57  [Func="CanvasUtils::HasDrawWindowPrivilege"]
58  const unsigned long DRAWWINDOW_USE_WIDGET_LAYERS = 0x08;
59  // Don't synchronously decode images - draw what we have
60  [Func="CanvasUtils::HasDrawWindowPrivilege"]
61  const unsigned long DRAWWINDOW_ASYNC_DECODE_IMAGES = 0x10;
62
63  /**
64   * Renders a region of a window into the canvas.  The contents of
65   * the window's viewport are rendered, ignoring viewport clipping
66   * and scrolling.
67   *
68   * @param x
69   * @param y
70   * @param w
71   * @param h specify the area of the window to render, in CSS
72   * pixels.
73   *
74   * @param backgroundColor the canvas is filled with this color
75   * before we render the window into it. This color may be
76   * transparent/translucent. It is given as a CSS color string
77   * (e.g., rgb() or rgba()).
78   *
79   * @param flags Used to better control the drawWindow call.
80   * Flags can be ORed together.
81   *
82   * Of course, the rendering obeys the current scale, transform and
83   * globalAlpha values.
84   *
85   * Hints:
86   * -- If 'rgba(0,0,0,0)' is used for the background color, the
87   * drawing will be transparent wherever the window is transparent.
88   * -- Top-level browsed documents are usually not transparent
89   * because the user's background-color preference is applied,
90   * but IFRAMEs are transparent if the page doesn't set a background.
91   * -- If an opaque color is used for the background color, rendering
92   * will be faster because we won't have to compute the window's
93   * transparency.
94   *
95   * This API cannot currently be used by Web content. It is chrome
96   * and Web Extensions (with a permission) only.
97   */
98  [Throws, Func="CanvasUtils::HasDrawWindowPrivilege"]
99  void drawWindow(Window window, double x, double y, double w, double h,
100                  DOMString bgColor, optional unsigned long flags = 0);
101
102  /**
103   * This causes a context that is currently using a hardware-accelerated
104   * backend to fallback to a software one. All state should be preserved.
105   */
106  [ChromeOnly]
107  void demote();
108};
109
110CanvasRenderingContext2D includes CanvasState;
111CanvasRenderingContext2D includes CanvasTransform;
112CanvasRenderingContext2D includes CanvasCompositing;
113CanvasRenderingContext2D includes CanvasImageSmoothing;
114CanvasRenderingContext2D includes CanvasFillStrokeStyles;
115CanvasRenderingContext2D includes CanvasShadowStyles;
116CanvasRenderingContext2D includes CanvasFilters;
117CanvasRenderingContext2D includes CanvasRect;
118CanvasRenderingContext2D includes CanvasDrawPath;
119CanvasRenderingContext2D includes CanvasUserInterface;
120CanvasRenderingContext2D includes CanvasText;
121CanvasRenderingContext2D includes CanvasDrawImage;
122CanvasRenderingContext2D includes CanvasImageData;
123CanvasRenderingContext2D includes CanvasPathDrawingStyles;
124CanvasRenderingContext2D includes CanvasTextDrawingStyles;
125CanvasRenderingContext2D includes CanvasPathMethods;
126CanvasRenderingContext2D includes CanvasHitRegions;
127
128interface mixin CanvasState {
129  // state
130  void save(); // push state on state stack
131  void restore(); // pop state stack and restore state
132};
133
134interface mixin CanvasTransform {
135  // transformations (default transform is the identity matrix)
136// NOT IMPLEMENTED           attribute SVGMatrix currentTransform;
137  [Throws, LenientFloat]
138  void scale(double x, double y);
139  [Throws, LenientFloat]
140  void rotate(double angle);
141  [Throws, LenientFloat]
142  void translate(double x, double y);
143  [Throws, LenientFloat]
144  void transform(double a, double b, double c, double d, double e, double f);
145  [Throws, LenientFloat]
146  void setTransform(double a, double b, double c, double d, double e, double f);
147  [Throws]
148  void resetTransform();
149  [NewObject, Throws]
150  DOMMatrix getTransform();
151};
152
153[NoInterfaceObject]
154interface mixin CanvasCompositing {
155  attribute unrestricted double globalAlpha; // (default 1.0)
156  [Throws]
157  attribute DOMString globalCompositeOperation; // (default source-over)
158};
159
160interface mixin CanvasImageSmoothing {
161  // drawing images
162  attribute boolean imageSmoothingEnabled;
163};
164
165interface mixin CanvasFillStrokeStyles {
166  // colors and styles (see also the CanvasPathDrawingStyles interface)
167  attribute (DOMString or CanvasGradient or CanvasPattern) strokeStyle; // (default black)
168  attribute (DOMString or CanvasGradient or CanvasPattern) fillStyle; // (default black)
169  [NewObject]
170  CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1);
171  [NewObject, Throws]
172  CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1);
173  [NewObject, Throws]
174  CanvasPattern? createPattern(CanvasImageSource image, [TreatNullAs=EmptyString] DOMString repetition);
175};
176
177interface mixin CanvasShadowStyles {
178  [LenientFloat]
179  attribute double shadowOffsetX; // (default 0)
180  [LenientFloat]
181  attribute double shadowOffsetY; // (default 0)
182  [LenientFloat]
183  attribute double shadowBlur; // (default 0)
184  attribute DOMString shadowColor; // (default transparent black)
185};
186
187interface mixin CanvasFilters {
188  [Pref="canvas.filters.enabled", SetterThrows]
189  attribute DOMString filter; // (default empty string = no filter)
190};
191
192interface mixin CanvasRect {
193  [LenientFloat]
194  void clearRect(double x, double y, double w, double h);
195  [LenientFloat]
196  void fillRect(double x, double y, double w, double h);
197  [LenientFloat]
198  void strokeRect(double x, double y, double w, double h);
199};
200
201interface mixin CanvasDrawPath {
202  // path API (see also CanvasPathMethods)
203  void beginPath();
204  void fill(optional CanvasWindingRule winding = "nonzero");
205  void fill(Path2D path, optional CanvasWindingRule winding = "nonzero");
206  void stroke();
207  void stroke(Path2D path);
208  void clip(optional CanvasWindingRule winding = "nonzero");
209  void clip(Path2D path, optional CanvasWindingRule winding = "nonzero");
210// NOT IMPLEMENTED  void resetClip();
211  [NeedsSubjectPrincipal]
212  boolean isPointInPath(unrestricted double x, unrestricted double y, optional CanvasWindingRule winding = "nonzero");
213  [NeedsSubjectPrincipal] // Only required because overloads can't have different extended attributes.
214  boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y, optional CanvasWindingRule winding = "nonzero");
215  [NeedsSubjectPrincipal]
216  boolean isPointInStroke(double x, double y);
217  [NeedsSubjectPrincipal] // Only required because overloads can't have different extended attributes.
218  boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);
219};
220
221interface mixin CanvasUserInterface {
222  [Pref="canvas.focusring.enabled", Throws] void drawFocusIfNeeded(Element element);
223// NOT IMPLEMENTED  void drawSystemFocusRing(Path path, HTMLElement element);
224  [Pref="canvas.customfocusring.enabled"] boolean drawCustomFocusRing(Element element);
225// NOT IMPLEMENTED  boolean drawCustomFocusRing(Path path, HTMLElement element);
226// NOT IMPLEMENTED  void scrollPathIntoView();
227// NOT IMPLEMENTED  void scrollPathIntoView(Path path);
228};
229
230interface mixin CanvasText {
231  // text (see also the CanvasPathDrawingStyles interface)
232  [Throws, LenientFloat]
233  void fillText(DOMString text, double x, double y, optional double maxWidth);
234  [Throws, LenientFloat]
235  void strokeText(DOMString text, double x, double y, optional double maxWidth);
236  [NewObject, Throws]
237  TextMetrics measureText(DOMString text);
238};
239
240interface mixin CanvasDrawImage {
241  [Throws, LenientFloat]
242  void drawImage(CanvasImageSource image, double dx, double dy);
243  [Throws, LenientFloat]
244  void drawImage(CanvasImageSource image, double dx, double dy, double dw, double dh);
245  [Throws, LenientFloat]
246  void drawImage(CanvasImageSource image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh);
247};
248
249interface mixin CanvasImageData {
250  // pixel manipulation
251  [NewObject, Throws]
252  ImageData createImageData(double sw, double sh);
253  [NewObject, Throws]
254  ImageData createImageData(ImageData imagedata);
255  [NewObject, Throws, NeedsSubjectPrincipal]
256  ImageData getImageData(double sx, double sy, double sw, double sh);
257  [Throws]
258  void putImageData(ImageData imagedata, double dx, double dy);
259  [Throws]
260  void putImageData(ImageData imagedata, double dx, double dy, double dirtyX, double dirtyY, double dirtyWidth, double dirtyHeight);
261};
262
263interface mixin CanvasPathDrawingStyles {
264  // line caps/joins
265  [LenientFloat]
266  attribute double lineWidth; // (default 1)
267  attribute DOMString lineCap; // "butt", "round", "square" (default "butt")
268  [GetterThrows]
269  attribute DOMString lineJoin; // "round", "bevel", "miter" (default "miter")
270  [LenientFloat]
271  attribute double miterLimit; // (default 10)
272
273  // dashed lines
274  [LenientFloat, Throws] void setLineDash(sequence<double> segments); // default empty
275  sequence<double> getLineDash();
276  [LenientFloat] attribute double lineDashOffset;
277};
278
279interface mixin CanvasTextDrawingStyles {
280  // text
281  [SetterThrows]
282  attribute DOMString font; // (default 10px sans-serif)
283  attribute DOMString textAlign; // "start", "end", "left", "right", "center" (default: "start")
284  attribute DOMString textBaseline; // "top", "hanging", "middle", "alphabetic", "ideographic", "bottom" (default: "alphabetic")
285};
286
287interface mixin CanvasPathMethods {
288  // shared path API methods
289  void closePath();
290  [LenientFloat]
291  void moveTo(double x, double y);
292  [LenientFloat]
293  void lineTo(double x, double y);
294  [LenientFloat]
295  void quadraticCurveTo(double cpx, double cpy, double x, double y);
296
297  [LenientFloat]
298  void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
299
300  [Throws, LenientFloat]
301  void arcTo(double x1, double y1, double x2, double y2, double radius);
302// NOT IMPLEMENTED  [LenientFloat] void arcTo(double x1, double y1, double x2, double y2, double radiusX, double radiusY, double rotation);
303
304  [LenientFloat]
305  void rect(double x, double y, double w, double h);
306
307  [Throws, LenientFloat]
308  void arc(double x, double y, double radius, double startAngle, double endAngle, optional boolean anticlockwise = false);
309
310  [Throws, LenientFloat]
311  void ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, optional boolean anticlockwise = false);
312};
313
314interface mixin CanvasHitRegions {
315  // hit regions
316  [Pref="canvas.hitregions.enabled", Throws] void addHitRegion(optional HitRegionOptions options);
317  [Pref="canvas.hitregions.enabled"] void removeHitRegion(DOMString id);
318  [Pref="canvas.hitregions.enabled"] void clearHitRegions();
319};
320
321interface CanvasGradient {
322  // opaque object
323  [Throws]
324  // addColorStop should take a double
325  void addColorStop(float offset, DOMString color);
326};
327
328interface CanvasPattern {
329  // opaque object
330  // [Throws, LenientFloat] - could not do this overload because of bug 1020975
331  // void setTransform(double a, double b, double c, double d, double e, double f);
332
333  // No throw necessary here - SVGMatrix is always good.
334  void setTransform(SVGMatrix matrix);
335};
336
337interface TextMetrics {
338
339  // x-direction
340  readonly attribute double width; // advance width
341
342  /*
343   * NOT IMPLEMENTED YET
344
345  readonly attribute double actualBoundingBoxLeft;
346  readonly attribute double actualBoundingBoxRight;
347
348  // y-direction
349  readonly attribute double fontBoundingBoxAscent;
350  readonly attribute double fontBoundingBoxDescent;
351  readonly attribute double actualBoundingBoxAscent;
352  readonly attribute double actualBoundingBoxDescent;
353  readonly attribute double emHeightAscent;
354  readonly attribute double emHeightDescent;
355  readonly attribute double hangingBaseline;
356  readonly attribute double alphabeticBaseline;
357  readonly attribute double ideographicBaseline;
358  */
359
360};
361
362[Pref="canvas.path.enabled",
363 Constructor,
364 Constructor(Path2D other),
365 Constructor(DOMString pathString)]
366interface Path2D
367{
368  void addPath(Path2D path, optional SVGMatrix transformation);
369};
370Path2D includes CanvasPathMethods;
371