primitives/prelude/
canvas.rs

1#![allow(clippy::too_many_arguments)]
2
3use crate::{
4    prelude::TextAlign,
5    foundation::{colorspace::Color, BaseLine, FontStyle, FontWeight, Gradient}
6};
7
8/// Specifies the current text direction used to draw text
9///
10#[derive(Copy, Clone, Debug)]
11pub enum Direction {
12    /// Left to right text direction
13    Ltr,
14    /// Right to left text direction
15    Rtl,
16    /// Inherited text direction
17    Inherit,
18}
19
20/// Represents the dimensions of a piece of text in the canvas
21///
22#[derive(Copy, Clone, Debug)]
23pub struct TextMetrics {
24    /// Text width
25    pub width: f64,
26    /// Text height
27    pub height: f64,
28}
29
30/// Specifies how the ends of the drawn lines will look.
31///
32#[derive(Clone, Copy, Debug)]
33pub enum LineCap {
34    /// Butt line cap
35    Butt,
36    /// Round line cap
37    Round,
38    /// Square line cap
39    Square,
40}
41
42impl Default for LineCap {
43    fn default() -> Self {
44        LineCap::Butt
45    }
46}
47
48/// Specifies the shape of the vertices at which the lines converge
49///
50#[derive(Clone, Copy, Debug)]
51pub enum LineJoin {
52    /// Miter line join
53    Miter,
54    /// Bevel line join
55    Bevel,
56    /// Round line join
57    Round,
58}
59
60impl Default for LineJoin {
61    fn default() -> Self {
62        LineJoin::Miter
63    }
64}
65
66/// Specifies how the pattern is laid out
67///
68#[allow(dead_code)]
69#[derive(Clone, Copy, Debug)]
70pub enum PatternExtend {
71    /// None pattern extend
72    None,
73    /// Repeat pattern extend
74    Repeat,
75    /// Reflect pattern extend
76    Reflect,
77    /// Pad pattern extend
78    Pad,
79}
80
81impl Default for PatternExtend {
82    fn default() -> Self {
83        PatternExtend::Repeat
84    }
85}
86
87/// Provides for interacting with the canvas.
88///
89pub trait CanvasContext {
90    /// Represents pattern for canvas
91    type Pattern;
92    // /// Get current global transformation matrix
93    // fn get_current_transform(&self) -> Matrix<f64>;
94
95    // /// Set global transformation matrix
96    // fn set_current_transform(&self, value: Matrix<f64>);
97
98    /// Get direction
99    fn get_direction(&self) -> Direction;
100
101    /// Set direction
102    fn set_direction(&self, value: Direction) -> String;
103
104    /// Set fill color
105    fn set_fill_color(&self, value: Color);
106
107    /// Set fill gradient
108    fn set_fill_gradient(&self, value: &Gradient);
109
110    /// Set fill pattern
111    fn set_fill_pattern(&self, value: &Self::Pattern);
112
113    /// Get filter
114    fn get_filter(&self) -> String;
115
116    /// Set filter
117    fn set_filter(&self, value: &str);
118
119    /// Get font
120    fn get_font(&self) -> String;
121
122    /// Set direction
123    fn set_font(&self, family: &str, style: FontStyle, weight: FontWeight, size: f64);
124
125    /// Get global alpha
126    fn get_global_alpha(&self) -> f64;
127
128    /// Set global alpha
129    fn set_global_alpha(&self, value: f64);
130
131    /// Get global composite operation
132    fn get_global_composite_operation(&self) -> String;
133
134    /// Set global composite operation
135    fn set_global_composite_operation(&self, value: &str);
136
137    /// Whether images and patterns on this canvas will be smoothed when
138    /// this canvas is scaled.
139    fn is_image_smoothing_enabled(&self) -> bool;
140
141    /// Set image smoothing
142    fn set_image_smoothing(&self, value: bool);
143
144    // /// Get image smoothing quality
145    // fn get_image_smoothing_quality(&self) -> String;
146
147    // /// Set image smoothing quality
148    // fn set_image_smoothing_quality(&self, value: String);
149
150    /// Get line cap
151    fn get_line_cap(&self) -> LineCap;
152
153    /// Set line cap
154    fn set_line_cap(&self, value: LineCap);
155
156    /// Get line dash offset
157    fn get_line_dash_offset(&self) -> f64;
158
159    /// Set line dash offset
160    fn set_line_dash_offset(&self, value: f64);
161
162    /// Get line join
163    fn get_line_join(&self) -> LineJoin;
164
165    /// Set line dash
166    fn set_line_join(&self, value: LineJoin);
167
168    /// Get line width
169    fn get_line_width(&self) -> f64;
170
171    /// Set line width
172    fn set_line_width(&self, value: f64);
173
174    /// Get miter limit
175    fn get_miter_limit(&self) -> f64;
176
177    /// Set miter limit
178    fn set_miter_limit(&self, value: f64);
179
180    /// Get shadow blur
181    fn get_shadow_blur(&self) -> f64;
182
183    /// Set shadow blur
184    fn set_shadow_blur(&self, value: f64);
185
186    /// Get shadow color
187    fn get_shadow_color(&self) -> Color;
188
189    /// Set shadow color
190    fn set_shadow_color(&self, value: Color);
191
192    /// Get shadow offset x
193    fn get_shadow_offset_x(&self) -> f64;
194
195    /// Set shadow offset x
196    fn set_shadow_offset_x(&self, value: f64);
197
198    /// Get shadow offset y
199    fn get_shadow_offset_y(&self) -> f64;
200
201    /// Set shadow offset y
202    fn set_shadow_offset_y(&self, value: f64);
203
204    /// Set stroke color
205    fn set_stroke_color(&self, value: Color);
206
207    /// Set stroke gradient
208    fn set_stroke_gradient(&self, value: &Gradient);
209
210    /// Set stroke pattern
211    fn set_stroke_pattern(&self, value: &Self::Pattern);
212
213    /// Get text align
214    fn get_text_align(&self) -> TextAlign;
215
216    /// Set text align
217    fn set_text_align(&self, value: TextAlign);
218
219    /// Get text baseline
220    fn get_text_baseline(&self) -> BaseLine;
221
222    /// Set text baseline
223    fn set_text_baseline(&self, value: BaseLine);
224
225    /// Add arc to current path with anticlockwise param
226    fn arc(
227        &self,
228        x: f64,
229        y: f64,
230        radius: f64,
231        start_angle: f64,
232        end_angle: f64,
233        anticlockwise: bool,
234    );
235
236    /// Add arc to current path
237    fn arc_to(&self, x1: f64, y1: f64, x2: f64, y2: f64, radius: f64);
238
239    /// Begin the path
240    fn begin_path(&self);
241
242    /// Add bezier curve to current path
243    fn bezier_curve_to(&self, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64);
244
245    /// Clear rectangle on current canvas
246    fn clear_rect(&self, x: f64, y: f64, width: f64, height: f64);
247
248    // [path_OR_winding: dynamic, winding: String]
249
250    // /// Set clip to current operation
251    // fn clip(path_OR_winding: dynamic, winding: String); // TODO:
252
253    /// Close the current path
254    fn close_path(&self);
255
256    // [int? sh_OR_sw, dynamic imageDataColorSettings_OR_sh, Map? imageDataColorSettings]
257
258    // /// Create the image data from slice
259    // fn createImageData(data_OR_imagedata_OR_sw: dynamic, sh_OR_sw: int, imageDataColorSettings_OR_sh: dynamic, imageDataColorSettings: Map) -> ImageData; // TODO:
260
261    // /// Create the image data from other
262    // fn createImageDataFromImageData(imagedata: ImageData) -> ImageData; // TODO:
263
264    // [Element? element]
265    // fn drawFocusIfNeeded(element_OR_path: dynamic, element: Element); // TODO:
266
267    // /// Draws an image from a CanvasImageSource to this canvas.
268    // fn drawImage(source: CanvasImageSource, destX: f64, destY: f64); // TODO:
269
270    // /// Draws an image from a CanvasImageSource to an area of this canvas.
271    // fn drawImageScaled(source: CanvasImageSource, destX: f64, destY: f64, destWidth: f64, destHeight: f64); // TODO:
272
273    // /// Draws an image from a CanvasImageSource to an area of this canvas.
274    // fn drawImageScaledFromSource(source: CanvasImageSource, sourceX: f64, sourceY: f64, sourceWidth: f64, sourceHeight: f64, destX: f64, destY: f64, destWidth: f64, destHeight: f64); // TODO:
275
276    // /// Draws an image from a CanvasImageSource to an area of this canvas.
277    // fn drawImageToRect(source: CanvasImageSource, destRect: Rectangle<f64>, sourceRect: Rectangle<f64>); // TODO:
278
279    /// Add ellipse to current path
280    fn ellipse(
281        &self,
282        x: f64,
283        y: f64,
284        radius_x: f64,
285        radius_y: f64,
286        rotation: f64,
287        start_angle: f64,
288        end_angle: f64,
289        anticlockwise: bool,
290    );
291
292    // [dynamic path_OR_winding, String? winding]
293
294    // /// Fill current path
295    // fn fill(path_OR_winding: dynamic, winding: String); // TODO:
296
297    /// Fill current path
298    fn fill(&self);
299
300    /// Fill rectangle
301    fn fill_rect(&self, x: f64, y: f64, width: f64, height: f64);
302
303    /// Draws text to the canvas.
304    fn fill_text(&self, text: &str, x: f64, y: f64);
305
306    // fn getContextAttributes() -> Map; // TODO:
307
308    // fn getImageData(sx: i64, sy: i64, sw: i64, sh: i64) -> ImageData; // TODO:
309
310    /// Get line dash
311    fn get_line_dash(&self) -> Vec<f64>;
312
313    // [dynamic winding_OR_y, String? winding]
314    // fn isPointInPath(path_OR_x: dynamic, x_OR_y: f64, winding_OR_y: dynamic, winding: String) -> bool; // TODO:
315
316    // [f64? y]
317    // fn isPointInStroke(path_OR_x: dynamic, x_OR_y: f64, y: f64) -> bool; // TODO:
318
319    /// Add line to path from current position
320    fn line_to(&self, x: f64, y: f64);
321
322    /// Measure text using current font face and font size
323    fn measure_text(&self, text: &str) -> TextMetrics;
324
325    /// Move cursor to position
326    fn move_to(&self, x: f64, y: f64);
327
328    // [int? dirtyX, int? dirtyY, int? dirtyWidth, int? dirtyHeight]
329    // fn putImageData(imagedata: ImageData, dx: i64, dy: i64, dirtyX: i64, dirtyY: i64, dirtyWidth: i64, dirtyHeight: i64); // TODO:
330
331    /// Add quadratic curve to current path
332    fn quadratic_curve_to(&self, cpx: f64, cpy: f64, x: f64, y: f64);
333
334    /// Add rectangle to current path
335    fn rect(&self, x: f64, y: f64, width: f64, height: f64);
336
337    /// Reset current transformations
338    fn reset_transform(&self);
339
340    /// Restore transformations
341    fn restore(&self);
342
343    /// Add rotate to current transformations
344    fn rotate(&self, angle: f64);
345
346    /// Save current transformations
347    fn save(&self);
348
349    /// Add scle to current transformations
350    fn scale(&self, x: f64, y: f64);
351
352    // [Path2D? path]
353    // fn scrollPathIntoView(path: Path2D); // TODO:
354
355    /// Set line dash
356    fn set_line_dash(&self, dash: &[f64]);
357
358    /// Set transform matrix
359    fn set_transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64);
360
361    /// Stroke current path
362    fn stroke(&self);
363
364    /// Stroke rectangle
365    fn stroke_rect(&self, x: f64, y: f64, width: f64, height: f64);
366
367    /// Stroke text
368    fn stroke_text(&self, text: &str, x: f64, y: f64);
369
370    /// Add transform matrix to current transformations
371    fn transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64);
372
373    /// Add translate to current transformations
374    fn translate(&self, x: f64, y: f64);
375}