1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.platform.canvas.rendering;
16
17 import java.io.IOException;
18 import java.nio.ByteBuffer;
19
20 import org.htmlunit.javascript.host.canvas.Path2D;
21
22 /**
23 * Interface to the rendering context used by
24 * {@link org.htmlunit.javascript.host.canvas.CanvasRenderingContext2D}.
25 *
26 * @author Ronald Brill
27 */
28 public interface RenderingBackend {
29
30 /**
31 * WindingRule to be used while rendering.
32 */
33 enum WindingRule {
34 /** WindingRule.NON_ZERO. */
35 NON_ZERO,
36 /** WindingRule.EVEN_ODD. */
37 EVEN_ODD
38 }
39
40 /**
41 * LineJoin to be used while rendering.
42 */
43 enum LineJoin {
44 /** LineJoin.MITER. */
45 MITER,
46 /** LineJoin.ROUND. */
47 ROUND,
48 /** LineJoin.BEVEL. */
49 BEVEL
50 }
51
52 /**
53 * LineCap to be used while rendering.
54 */
55 enum LineCap {
56 /** LineCap.BUTT. */
57 BUTT,
58 /** LineCap.ROUND. */
59 ROUND,
60 /** LineCap.SQUARE. */
61 SQUARE
62 }
63
64 /**
65 * Starts a new path by emptying the list of sub-paths.
66 */
67 void beginPath();
68
69 /**
70 * Adds a cubic Bézier curve to the current sub-path. It requires
71 * three points: the first two are control points and the third one
72 * is the end point. The starting point is the latest point in the
73 * current path, which can be changed using moveTo() before
74 * creating the Bézier curve.
75 * @param cp1x the cp1x
76 * @param cp1y the cp1y
77 * @param cp2x the cp2x
78 * @param cp2y the cp2y
79 * @param x the x
80 * @param y the y
81 */
82 void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y,
83 double x, double y);
84
85 /**
86 * Adds a circular arc to the current sub-path.
87 * @param x the x
88 * @param y the y
89 * @param radius the radius
90 * @param startAngle the start angle
91 * @param endAngle the end angle
92 * @param anticlockwise is anti-clockwise
93 */
94 void arc(double x, double y, double radius, double startAngle,
95 double endAngle, boolean anticlockwise);
96
97 /**
98 * Paints the specified rectangular area.
99 * @param x the x
100 * @param y the y
101 * @param w the width
102 * @param h the height
103 */
104 void clearRect(double x, double y, double w, double h);
105
106 /**
107 * Draws images onto the context.
108 *
109 * @param imageData the reader to read the image from 8the first one)
110 * @param sx the X coordinate of the top left corner of the sub-rectangle of the source image
111 * to draw into the destination context
112 * @param sy the Y coordinate of the top left corner of the sub-rectangle of the source image
113 * to draw into the destination context
114 * @param sWidth the width of the sub-rectangle of the source image to draw into the destination context
115 * @param sHeight the height of the sub-rectangle of the source image to draw into the destination context
116 * @param dx the X coordinate in the destination canvas at which to place the top-left corner of the source image
117 * @param dy the Y coordinate in the destination canvas at which to place the top-left corner of the source image
118 * @param dWidth the width to draw the image in the destination canvas. This allows scaling of the drawn image
119 * @param dHeight the height to draw the image in the destination canvas. This allows scaling of the drawn image
120 * @throws IOException in case o problems
121 */
122 void drawImage(org.htmlunit.platform.image.ImageData imageData,
123 int sx, int sy, Integer sWidth, Integer sHeight,
124 int dx, int dy, Integer dWidth, Integer dHeight) throws IOException;
125
126 /**
127 * Constructs a base64 encoded string out of the image data.
128 *
129 * @param type the name of the image format
130 * @return the base64 encoded string
131 * @throws IOException in case o problems
132 */
133 String encodeToString(String type) throws IOException;
134
135 /**
136 * Creates an elliptical arc centered at (x, y) with the radii radiusX and radiusY.
137 * The path starts at startAngle and ends at endAngle, and travels in the direction
138 * given by anticlockwise (defaulting to clockwise).
139 * @param x the x
140 * @param y the y
141 * @param radiusX the radiusX
142 * @param radiusY the radiusY
143 * @param rotation the rotation
144 * @param startAngle the start angle
145 * @param endAngle the end angle
146 * @param anticlockwise is anti-clockwise
147 */
148 void ellipse(double x, double y,
149 double radiusX, double radiusY,
150 double rotation, double startAngle, double endAngle,
151 boolean anticlockwise);
152
153 /**
154 * Fills the current or given path with the current fillStyle.
155 * @param windingRule the {@link WindingRule}
156 */
157 void fill(RenderingBackend.WindingRule windingRule);
158
159 /**
160 * Paints the specified rectangular area.
161 * @param x the x
162 * @param y the y
163 * @param w the width
164 * @param h the height
165 */
166 void fillRect(double x, double y, double w, double h);
167
168 /**
169 * Fills a given text at the given (x, y) position.
170 * @param text the text
171 * @param x the x
172 * @param y the y
173 */
174 void fillText(String text, double x, double y);
175
176 /**
177 * Creates a byte array containing the (4) color values of all pixels.
178 *
179 * @param width the width
180 * @param height the height
181 * @param sx start point x
182 * @param sy start point y
183 * @return the bytes
184 */
185 byte[] getBytes(int width, int height, int sx, int sy);
186
187 /**
188 * Adds a straight line to the current sub-path by connecting the
189 * sub-path's last point to the specified (x, y) coordinates.
190 * @param x the x
191 * @param y the y
192 */
193 void lineTo(double x, double y);
194
195 /**
196 * Begins a new sub-path at the point specified
197 * by the given (x, y) coordinates.
198 * @param x the x
199 * @param y the y
200 */
201 void moveTo(double x, double y);
202
203 /**
204 * Paints data from the given ImageData object onto the canvas.
205 * @param imageDataBytes an array of pixel values
206 * @param imageDataWidth the width of the imageData
207 * @param imageDataHeight the height of the imageData
208 * @param dx horizontal position (x coordinate) at which to place the image data in the destination canvas
209 * @param dy vertical position (y coordinate) at which to place the image data in the destination canvas
210 * @param dirtyX horizontal position (x coordinate) of the top-left corner
211 * from which the image data will be extracted. Defaults to 0.
212 * @param dirtyY vertical position (y coordinate) of the top-left corner
213 * from which the image data will be extracted. Defaults to 0.
214 * @param dirtyWidth width of the rectangle to be painted.
215 * Defaults to the width of the image data.
216 * @param dirtyHeight height of the rectangle to be painted.
217 * Defaults to the height of the image data.
218 */
219 void putImageData(ByteBuffer imageDataBytes, int imageDataWidth, int imageDataHeight,
220 int dx, int dy, int dirtyX, int dirtyY, int dirtyWidth, int dirtyHeight);
221
222 /**
223 * Adds a quadratic Bézier curve to the current sub-path. It requires
224 * two points: the first one is a control point and the second one is
225 * the end point. The starting point is the latest point in the
226 * current path, which can be changed using moveTo() before
227 * creating the quadratic Bézier curve.
228 * @param cpx the cpx
229 * @param cpy the cpy
230 * @param x the x
231 * @param y the y
232 */
233 void quadraticCurveTo(double cpx, double cpy, double x, double y);
234
235 /**
236 * Adds a rectangle to the current path.
237 * @param x the x
238 * @param y the y
239 * @param w the width
240 * @param h the height
241 */
242 void rect(double x, double y, double w, double h);
243
244 /**
245 * Restores the most recently saved canvas state by popping the top
246 * entry in the drawing state stack. If there is no saved state,
247 * this method does nothing.
248 */
249 void restore();
250
251 /**
252 * Adds a rotation to the transformation matrix.
253 * @param angle the angle
254 */
255 void rotate(double angle);
256
257 /**
258 * Saves the entire state of the canvas by pushing
259 * the current state onto a stack.
260 */
261 void save();
262
263 /**
264 * Sets the {@code fillStyle} property.
265 * @param fillStyle the {@code fillStyle} property
266 */
267 void setFillStyle(String fillStyle);
268
269 /**
270 * Sets the {@code strokeStyle} property.
271 * @param strokeStyle the {@code strokeStyle} property
272 */
273 void setStrokeStyle(String strokeStyle);
274
275 /**
276 * Returns the {@code lineWidth} property.
277 * @return the {@code lineWidth} property
278 */
279 float getLineWidth();
280
281 /**
282 * Sets the {@code lineWidth} property.
283 * @param lineWidth the {@code lineWidth} property
284 */
285 void setLineWidth(float lineWidth);
286
287 /**
288 * Resets (overrides) the current transformation to the identity matrix,
289 * and then invokes a transformation described by the arguments of this method.
290 * This lets you scale, rotate, translate (move), and skew the context.
291 * @param m11 Horizontal scaling. A value of 1 results in no scaling
292 * @param m12 Vertical skewing
293 * @param m21 Horizontal skewing
294 * @param m22 Vertical scaling. A value of 1 results in no scaling
295 * @param dx Horizontal translation (moving)
296 * @param dy Vertical translation (moving).
297 */
298 void setTransform(double m11, double m12, double m21, double m22, double dx, double dy);
299
300 /**
301 * Strokes (outlines) the current or given path with the current stroke style.
302 */
303 void stroke();
304
305 /**
306 * Paints the specified rectangular area.
307 * @param x the x
308 * @param y the y
309 * @param w the width
310 * @param h the height
311 */
312 void strokeRect(double x, double y, double w, double h);
313
314 /**
315 * Multiplies the current transformation with the matrix described by the
316 * arguments of this method. This lets you scale, rotate, translate (move),
317 * and skew the context.
318 * @param m11 Horizontal scaling. A value of 1 results in no scaling
319 * @param m12 Vertical skewing
320 * @param m21 Horizontal skewing
321 * @param m22 Vertical scaling. A value of 1 results in no scaling
322 * @param dx Horizontal translation (moving)
323 * @param dy Vertical translation (moving).
324 */
325 void transform(double m11, double m12, double m21, double m22, double dx, double dy);
326
327 /**
328 * Adds a translation transformation to the current matrix.
329 * @param x the x
330 * @param y the y
331 */
332 void translate(double x, double y);
333
334 /**
335 * Turns the current or given path into the current clipping region.
336 * It replaces any previous clipping region.
337 * @param windingRule the RenderingBackend.WindingRule {@link WindingRule}
338 * to be used
339 * @param path the path or null if the current path should be used
340 */
341 void clip(RenderingBackend.WindingRule windingRule, Path2D path);
342
343 /**
344 * Attempts to add a straight line from the current point to the start of the current sub-path.
345 * If the shape has already been closed or has only one point, this function does nothing.
346 */
347 void closePath();
348
349 /**
350 * Returns the alpha (transparency) value that is applied to shapes and images
351 * before they are drawn onto the canvas.
352 *
353 * @return the alpha (transparency) value that is applied to shapes and images
354 * before they are drawn onto the canvas.
355 */
356 double getGlobalAlpha();
357
358 /**
359 * Specifies the alpha (transparency) value that is applied to shapes and images
360 * before they are drawn onto the canvas.
361 * @param globalAlpha the new alpha
362 */
363 void setGlobalAlpha(double globalAlpha);
364
365 /**
366 * Returns the the shape used to join two line segments where they meet.
367 * There are three possible values for this property: "round", "bevel",
368 * and "miter". The default is "miter".
369 *
370 * @return the the shape used to join two line segments
371 */
372 LineJoin getLineJoin();
373
374 /**
375 * Sets the {@code lineJoin} property.
376 * @param lineJoin the {@code lineJoin} property value
377 */
378 void setLineJoin(LineJoin lineJoin);
379
380 /**
381 * Returns the shape used to draw the end points of lines.
382 *
383 * @return the shape used to draw the end points of lines.
384 */
385 LineCap getLineCap();
386
387 /**
388 * Sets the {@code lineCap} property.
389 * @param lineCap the {@code lineCap} property value
390 */
391 void setLineCap(LineCap lineCap);
392 }