Skip to content

Commit e981165

Browse files
committed
Expose current_transform() for contexts
1 parent 2f87198 commit e981165

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

piet-cairo/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ impl<'a> RenderContext for CairoRenderContext<'a> {
255255
self.ctx.transform(affine_to_matrix(transform));
256256
}
257257

258+
fn current_transform(&self) -> Affine {
259+
matrix_to_affine(self.ctx.get_matrix())
260+
}
261+
258262
fn make_image(
259263
&mut self,
260264
width: usize,
@@ -492,6 +496,12 @@ fn affine_to_matrix(affine: Affine) -> Matrix {
492496
}
493497
}
494498

499+
fn matrix_to_affine(matrix: Matrix) -> Affine {
500+
Affine::new([
501+
matrix.xx, matrix.yx, matrix.xy, matrix.yy, matrix.x0, matrix.y0,
502+
])
503+
}
504+
495505
fn scale_matrix(scale: f64) -> Matrix {
496506
Matrix {
497507
xx: scale,

piet-direct2d/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'b, 'a: 'b> D2DRenderContext<'a> {
109109
}
110110
}
111111

112-
fn current_transform(&self) -> Affine {
112+
pub fn current_transform(&self) -> Affine {
113113
// This is an unwrap because we protect the invariant.
114114
self.ctx_stack.last().unwrap().transform
115115
}
@@ -425,6 +425,10 @@ impl<'a> RenderContext for D2DRenderContext<'a> {
425425
.set_transform(&affine_to_matrix3x2f(self.current_transform()));
426426
}
427427

428+
fn current_transform(&self) -> Affine {
429+
self.current_transform()
430+
}
431+
428432
fn make_image(
429433
&mut self,
430434
width: usize,

piet-web/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ impl<'a> RenderContext for WebRenderContext<'a> {
262262
let _ = self.ctx.transform(a[0], a[1], a[2], a[3], a[4], a[5]);
263263
}
264264

265+
fn current_transform(&self) -> Affine {
266+
// todo
267+
// current_transform() and get_transform() currently not implemented:
268+
// https://github.com/rustwasm/wasm-bindgen/blob/f8354b3a88de013845a304ea77d8b9b9286a0d7b/crates/web-sys/webidls/enabled/CanvasRenderingContext2D.webidl#L136
269+
Affine::default()
270+
}
271+
265272
fn make_image(
266273
&mut self,
267274
width: usize,

piet/src/null_renderer.rs

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ impl RenderContext for NullRenderContext {
117117
_interp: InterpolationMode,
118118
) {
119119
}
120+
121+
fn current_transform(&self) -> Affine {
122+
Affine::default()
123+
}
120124
}
121125

122126
impl Text for NullText {

piet/src/render_context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where
8080
///
8181
/// TODO: figure out how to document lifetime and rebuilding requirements. Should
8282
/// that be the responsibility of the client, or should the back-end take
83-
/// responsiblity? We could have a cache that is flushed when the Direct2D
83+
/// responsibility? We could have a cache that is flushed when the Direct2D
8484
/// render target is rebuilt. Solid brushes are super lightweight, but
8585
/// other potentially retained objects will be heavier.
8686
fn solid_brush(&mut self, color: Color) -> Self::Brush;
@@ -185,6 +185,9 @@ where
185185
/// The image is scaled to the provided `rect`. It will be squashed if
186186
/// aspect ratios don't match.
187187
fn draw_image(&mut self, image: &Self::Image, rect: impl Into<Rect>, interp: InterpolationMode);
188+
189+
/// Returns the transformations currently applied to the context.
190+
fn current_transform(&self) -> Affine;
188191
}
189192

190193
/// A trait for various types that can be used as brushes. These include

0 commit comments

Comments
 (0)