1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use super::*;
/// Inherent implementation of [`Canvas2dChartBackend`].
impl Canvas2dChartBackend {
/// Creates a backend around the given canvas.
///
/// Returns `None` when the canvas cannot give out a 2D context — in
/// practice, when the canvas already has a WebGPU or WebGL context.
///
/// # Arguments
///
/// - `&HtmlCanvasElement` - Shared reference to a `HtmlCanvasElement`.
///
/// # Returns
///
/// - `Option<Self>` - `Some(...)` on success, `None` otherwise.
pub fn create(canvas: &HtmlCanvasElement) -> Option<Self> {
let context: CanvasRenderingContext2d = canvas
.get_context("2d")
.ok()
.flatten()?
.dyn_into::<CanvasRenderingContext2d>()
.ok()?;
Some(Self {
canvas: canvas.clone(),
context,
dpr: 1.0,
width: 0.0,
height: 0.0,
})
}
/// Returns the canvas the backend draws into.
///
/// # Returns
///
/// - `&HtmlCanvasElement` - A `HtmlCanvasElement` value.
pub fn canvas(&self) -> &HtmlCanvasElement {
self.get_canvas()
}
/// Returns the device pixel ratio the backend renders at.
///
/// # Returns
///
/// - `f64` - A 64-bit float.
pub fn dpr(&self) -> f64 {
self.get_dpr()
}
/// Traces a path into the 2D context.
///
/// # Arguments
///
/// - `&Path` - Shared reference to a `Path`.
pub(crate) fn trace_path(&self, path: &Path) {
self.get_context().begin_path();
for command in path.commands() {
match *command {
PathCommand::MoveTo(x, y) => self.get_context().move_to(x as f64, y as f64),
PathCommand::LineTo(x, y) => self.get_context().line_to(x as f64, y as f64),
PathCommand::QuadTo(cx, cy, x, y) => self
.get_context()
.quadratic_curve_to(cx as f64, cy as f64, x as f64, y as f64),
PathCommand::CubicTo(c1x, c1y, c2x, c2y, x, y) => {
self.get_context().bezier_curve_to(
c1x as f64, c1y as f64, c2x as f64, c2y as f64, x as f64, y as f64,
)
}
PathCommand::Close => self.get_context().close_path(),
}
}
}
/// Traces a triangle strip into the 2D context.
///
/// # Arguments
///
/// - `&[Point]` - Shared reference to a `[Point]`.
/// - `f32` - A 32-bit float (`f32`).
pub(crate) fn trace_strip(&self, points: &[Point], baseline: f32) {
self.get_context().begin_path();
if let Some(first) = points.first() {
self.get_context().move_to(first.x as f64, first.y as f64);
for point in &points[1..] {
self.get_context().line_to(point.x as f64, point.y as f64);
}
self.get_context()
.line_to(points[points.len() - 1].x as f64, baseline as f64);
self.get_context()
.line_to(points[0].x as f64, baseline as f64);
self.get_context().close_path();
}
}
/// Applies a fill paint to the context.
///
/// # Arguments
///
/// - `&Fill` - Shared reference to a `Fill`.
pub(crate) fn apply_fill(&self, paint: &Fill) {
match paint {
Fill::Solid(color) => {
self.get_context().set_fill_style_str(&color.to_css());
}
Fill::Linear(gradient) => {
if let Some(canvas) = self.create_gradient(gradient) {
self.get_context().set_fill_style_canvas_gradient(&canvas);
}
}
Fill::Radial(gradient) => {
if let Some(canvas) = self.create_radial_gradient(gradient) {
self.get_context().set_fill_style_canvas_gradient(&canvas);
}
}
Fill::None => {}
}
}
/// Applies a stroke paint to the context.
///
/// # Arguments
///
/// - `&Stroke` - Shared reference to a `Stroke`.
pub(crate) fn apply_stroke(&self, paint: &Stroke) {
match paint {
Stroke::Solid(color) => {
self.get_context().set_stroke_style_str(&color.to_css());
}
Stroke::Linear(gradient) => {
if let Some(canvas) = self.create_gradient(gradient) {
self.get_context().set_stroke_style_canvas_gradient(&canvas);
}
}
Stroke::Radial(gradient) => {
if let Some(canvas) = self.create_radial_gradient(gradient) {
self.get_context().set_stroke_style_canvas_gradient(&canvas);
}
}
Stroke::None => {}
}
}
/// Builds a Canvas2D linear gradient.
///
/// # Arguments
///
/// - `&LinearGradient` - Shared reference to a `LinearGradient`.
///
/// # Returns
///
/// - `Option<CanvasGradient>` - `Some(...)` on success, `None` otherwise.
fn create_gradient(&self, gradient: &LinearGradient) -> Option<CanvasGradient> {
let canvas: CanvasGradient = self.get_context().create_linear_gradient(
gradient.start.0,
gradient.start.1,
gradient.end.0,
gradient.end.1,
);
for stop in &gradient.stops {
let _: Result<(), JsValue> =
canvas.add_color_stop(stop.offset as f32, &stop.color.to_css());
}
Some(canvas)
}
/// Builds a Canvas2D radial gradient.
///
/// # Arguments
///
/// - `&RadialGradient` - Shared reference to a `RadialGradient`.
///
/// # Returns
///
/// - `Option<CanvasGradient>` - `Some(...)` on success, `None` otherwise.
fn create_radial_gradient(&self, gradient: &RadialGradient) -> Option<CanvasGradient> {
let canvas: CanvasGradient = self
.get_context()
.create_radial_gradient(
gradient.center.0,
gradient.center.1,
0.0,
gradient.center.0,
gradient.center.1,
gradient.radius,
)
.ok()?;
for stop in &gradient.stops {
let _: Result<(), JsValue> =
canvas.add_color_stop(stop.offset as f32, &stop.color.to_css());
}
Some(canvas)
}
}
/// Implements [`ChartBackend`] for [`Canvas2dChartBackend`].
impl ChartBackend for Canvas2dChartBackend {
/// Implements the `kind` operation on [`Canvas2dChartBackend`].
///
/// # Returns
///
/// - `BackendKind` - A `BackendKind` value.
fn kind(&self) -> BackendKind {
BackendKind::Canvas2D
}
/// Implements the `resize` operation on [`Canvas2dChartBackend`].
///
/// # Arguments
///
/// - `f64` - A 64-bit float (`f64`).
fn resize(&mut self, width: f64, height: f64, dpr: f64) {
self.set_dpr(dpr);
self.set_width(width);
self.set_height(height);
let physical_width: u32 = (width * dpr).round().max(1.0) as u32;
let physical_height: u32 = (height * dpr).round().max(1.0) as u32;
if self.get_canvas().width() != physical_width {
self.get_canvas().set_width(physical_width);
}
if self.get_canvas().height() != physical_height {
self.get_canvas().set_height(physical_height);
}
// Setting the width or the height of a canvas resets its context
// state, so the transform has to be re-applied afterwards.
let _: Result<(), JsValue> = self
.get_context()
.set_transform(dpr, 0.0, 0.0, dpr, 0.0, 0.0);
self.get_context().set_line_join("miter");
}
/// Implements the `render` operation on [`Canvas2dChartBackend`].
///
/// # Arguments
///
/// - `&DisplayList` - Shared reference to a `DisplayList`.
/// - `&mut TextShaper` - Mutable reference to a `TextShaper` (mutated in place).
fn render(&mut self, list: &DisplayList, shaper: &mut TextShaper) {
let _: Result<(), JsValue> =
self.get_context()
.set_transform(self.get_dpr(), 0.0, 0.0, self.get_dpr(), 0.0, 0.0);
self.get_context()
.clear_rect(0.0, 0.0, self.get_width(), self.get_height());
for item in list.items() {
match &item.cmd {
DrawCmd::Fill {
path,
paint,
opacity,
} => {
if matches!(paint, Fill::None) {
continue;
}
self.get_context().save();
self.get_context().set_global_alpha(*opacity);
self.apply_fill(paint);
self.trace_path(path);
self.get_context().fill();
self.get_context().restore();
}
DrawCmd::Stroke {
path,
paint,
style,
opacity,
} => {
if matches!(paint, Stroke::None) || style.width <= 0.0 {
continue;
}
self.get_context().save();
self.get_context()
.set_global_alpha(*opacity * style.opacity);
self.apply_stroke(paint);
self.get_context().set_line_width(style.width);
self.get_context().set_line_cap(line_cap_name(style.cap));
self.get_context().set_line_join(line_join_name(style.join));
if !style.dash.is_solid() {
let dash: Array = Array::new();
for length in &style.dash.pattern {
dash.push(&JsValue::from_f64(*length as f64));
}
let _: Result<(), JsValue> =
self.get_context().set_line_dash(dash.as_ref());
self.get_context()
.set_line_dash_offset(style.dash.offset as f64);
} else {
let _: Result<(), JsValue> =
self.get_context().set_line_dash(Array::new().as_ref());
}
self.trace_path(path);
self.get_context().stroke();
self.get_context().restore();
}
DrawCmd::Strip {
points,
baseline,
paint,
opacity,
} => {
if matches!(paint, Fill::None) || points.len() < 2 {
continue;
}
self.get_context().save();
self.get_context().set_global_alpha(*opacity);
self.apply_fill(paint);
self.trace_strip(points, *baseline);
self.get_context().fill();
self.get_context().restore();
}
DrawCmd::Text { text, x, y, style } => {
// The run is shaped with the same shaper the GPU
// backends use, so the baseline lands on the same
// pixel whichever backend is in use.
let run: TextRun = shaper.shape(text, style, *x, *y);
self.get_context().save();
self.get_context().set_font(&style.font());
self.get_context().set_text_align("left");
self.get_context().set_text_baseline("alphabetic");
for line in &run.lines {
if let Some(background) = style.background {
self.get_context().set_fill_style_str(&background.to_css());
self.get_context().fill_rect(
line.left - style.padding.3,
line.baseline - run.ascent - style.padding.0,
line.width + style.padding.1 + style.padding.3,
run.ascent + run.descent + style.padding.0 + style.padding.2,
);
}
self.get_context().set_fill_style_str(&style.color.to_css());
let _: Result<(), JsValue> =
self.get_context()
.fill_text(&line.text, line.left, line.baseline);
}
self.get_context().restore();
}
DrawCmd::Image {
image,
x,
y,
width,
height,
} => {
let _: Result<(), JsValue> = self
.get_context()
.draw_image_with_html_image_element_and_dw_and_dh(
image, *x, *y, *width, *height,
);
}
DrawCmd::Clip { path } => {
self.get_context().save();
self.trace_path(path);
self.get_context().clip();
}
DrawCmd::PopClip => {
self.get_context().restore();
}
}
}
}
/// Implements the `dispose` operation on [`Canvas2dChartBackend`].
fn dispose(&mut self) {
let _: Result<(), JsValue> = self
.get_context()
.set_transform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
self.get_context()
.clear_rect(0.0, 0.0, self.get_width(), self.get_height());
}
}