mirror of
https://github.com/penpot/penpot.git
synced 2025-02-14 19:19:09 -05: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
|
@ -6,10 +6,13 @@ use crate::render::BlendMode;
|
||||||
|
|
||||||
mod fills;
|
mod fills;
|
||||||
mod images;
|
mod images;
|
||||||
|
mod matrix;
|
||||||
mod paths;
|
mod paths;
|
||||||
mod renderable;
|
mod renderable;
|
||||||
|
|
||||||
pub use fills::*;
|
pub use fills::*;
|
||||||
pub use images::*;
|
pub use images::*;
|
||||||
|
use matrix::*;
|
||||||
pub use paths::*;
|
pub use paths::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -21,29 +24,6 @@ pub enum Kind {
|
||||||
|
|
||||||
pub type Color = skia::Color;
|
pub type Color = skia::Color;
|
||||||
|
|
||||||
#[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 identity() -> Self {
|
|
||||||
Self {
|
|
||||||
a: 1.,
|
|
||||||
b: 0.,
|
|
||||||
c: 0.,
|
|
||||||
d: 1.,
|
|
||||||
e: 0.,
|
|
||||||
f: 0.,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct Shape {
|
pub struct Shape {
|
||||||
|
@ -103,12 +83,7 @@ impl Shape {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_transform(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) {
|
pub fn set_transform(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) {
|
||||||
self.transform.a = a;
|
self.transform = Matrix::new(a, b, c, d, e, f);
|
||||||
self.transform.b = b;
|
|
||||||
self.transform.c = c;
|
|
||||||
self.transform.d = d;
|
|
||||||
self.transform.e = e;
|
|
||||||
self.transform.f = f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_opacity(&mut self, opacity: f32) {
|
pub fn set_opacity(&mut self, opacity: f32) {
|
||||||
|
@ -127,18 +102,6 @@ impl Shape {
|
||||||
self.children.clear();
|
self.children.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn translation(&self) -> (f32, f32) {
|
|
||||||
(self.transform.e, self.transform.f)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scale(&self) -> (f32, f32) {
|
|
||||||
(self.transform.a, self.transform.d)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn skew(&self) -> (f32, f32) {
|
|
||||||
(self.transform.c, self.transform.b)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn fills(&self) -> std::slice::Iter<Fill> {
|
pub fn fills(&self) -> std::slice::Iter<Fill> {
|
||||||
self.fills.iter()
|
self.fills.iter()
|
||||||
}
|
}
|
||||||
|
|
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 {
|
impl Renderable for Shape {
|
||||||
fn render(&self, surface: &mut skia_safe::Surface, images: &ImageStore) -> Result<(), String> {
|
fn render(&self, surface: &mut skia_safe::Surface, images: &ImageStore) -> Result<(), String> {
|
||||||
let mut transform = skia::Matrix::new_identity();
|
let transform = self.transform.to_skia_matrix();
|
||||||
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.,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check transform-matrix code from common/src/app/common/geom/shapes/transforms.cljc
|
// Check transform-matrix code from common/src/app/common/geom/shapes/transforms.cljc
|
||||||
let center = self.selrect.center();
|
let center = self.selrect.center();
|
||||||
|
|
Loading…
Add table
Reference in a new issue