mirror of
https://github.com/penpot/penpot.git
synced 2025-05-13 03:26:38 +02:00
♻️ Refactor custom matrix code into submodule
This commit is contained in:
parent
9f7428d44a
commit
c9355a257a
3 changed files with 66 additions and 56 deletions
61
render-wasm/src/shapes/matrix.rs
Normal file
61
render-wasm/src/shapes/matrix.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
use skia_safe as skia;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Matrix {
|
||||
pub a: f32,
|
||||
pub b: f32,
|
||||
pub c: f32,
|
||||
pub d: f32,
|
||||
pub e: f32,
|
||||
pub f: f32,
|
||||
}
|
||||
|
||||
impl Matrix {
|
||||
pub fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
|
||||
Self { a, b, c, d, e, f }
|
||||
}
|
||||
|
||||
pub fn identity() -> Self {
|
||||
Self {
|
||||
a: 1.,
|
||||
b: 0.,
|
||||
c: 0.,
|
||||
d: 1.,
|
||||
e: 0.,
|
||||
f: 0.,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_skia_matrix(&self) -> skia::Matrix {
|
||||
let mut res = skia::Matrix::new_identity();
|
||||
|
||||
let (translate_x, translate_y) = self.translation();
|
||||
let (scale_x, scale_y) = self.scale();
|
||||
let (skew_x, skew_y) = self.skew();
|
||||
res.set_all(
|
||||
scale_x,
|
||||
skew_x,
|
||||
translate_x,
|
||||
skew_y,
|
||||
scale_y,
|
||||
translate_y,
|
||||
0.,
|
||||
0.,
|
||||
1.,
|
||||
);
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
fn translation(&self) -> (f32, f32) {
|
||||
(self.e, self.f)
|
||||
}
|
||||
|
||||
fn scale(&self) -> (f32, f32) {
|
||||
(self.a, self.d)
|
||||
}
|
||||
|
||||
fn skew(&self) -> (f32, f32) {
|
||||
(self.c, self.b)
|
||||
}
|
||||
}
|
|
@ -7,21 +7,7 @@ use crate::render::{ImageStore, Renderable};
|
|||
|
||||
impl Renderable for Shape {
|
||||
fn render(&self, surface: &mut skia_safe::Surface, images: &ImageStore) -> Result<(), String> {
|
||||
let mut transform = skia::Matrix::new_identity();
|
||||
let (translate_x, translate_y) = self.translation();
|
||||
let (scale_x, scale_y) = self.scale();
|
||||
let (skew_x, skew_y) = self.skew();
|
||||
transform.set_all(
|
||||
scale_x,
|
||||
skew_x,
|
||||
translate_x,
|
||||
skew_y,
|
||||
scale_y,
|
||||
translate_y,
|
||||
0.,
|
||||
0.,
|
||||
1.,
|
||||
);
|
||||
let transform = self.transform.to_skia_matrix();
|
||||
|
||||
// Check transform-matrix code from common/src/app/common/geom/shapes/transforms.cljc
|
||||
let center = self.selrect.center();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue