Skip to content

Commit

Permalink
feat: introduce fill_svg to Frame
Browse files Browse the repository at this point in the history
  • Loading branch information
derezzedex committed Jan 16, 2024
1 parent 50c310f commit 1353ee4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
13 changes: 12 additions & 1 deletion renderer/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mod cache;

pub use cache::Cache;

use crate::core::{Point, Rectangle, Size, Vector};
use crate::core::svg;
use crate::core::{Color, Point, Rectangle, Size, Vector};
use crate::graphics::geometry::{Fill, Path, Stroke, Text};
use crate::Renderer;

Expand Down Expand Up @@ -82,6 +83,16 @@ impl Frame {
delegate!(self, frame, frame.fill_rectangle(top_left, size, fill));
}

/// TODO(derezzedex): document svg fill
pub fn fill_svg(
&mut self,
handle: &svg::Handle,
bounds: Rectangle<f32>,
color: Option<Color>,
) {
delegate!(self, frame, frame.fill_svg(handle, bounds, color));
}

/// Draws the stroke of the given [`Path`] on the [`Frame`] with the
/// provided style.
pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>) {
Expand Down
36 changes: 35 additions & 1 deletion tiny_skia/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::core::{Point, Rectangle, Size, Vector};
use crate::core::svg;
use crate::core::{Color, Point, Rectangle, Size, Vector};
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::stroke::{self, Stroke};
use crate::graphics::geometry::{Path, Style, Text};
Expand Down Expand Up @@ -76,6 +77,39 @@ impl Frame {
}));
}

/// TODO(derezzedex): document svg fill
pub fn fill_svg(
&mut self,
handle: &svg::Handle,
bounds: Rectangle<f32>,
color: Option<Color>,
) {
let position = if self.transform.is_identity() {
bounds.position()
} else {
let mut transformed = [tiny_skia::Point {
x: bounds.x,
y: bounds.y,
}];

self.transform.map_points(&mut transformed);

Point::new(transformed[0].x, transformed[0].y)
};

let bounds = Rectangle {
x: position.x,
y: position.y,
..bounds
};

self.primitives.push(Primitive::Svg {
handle: handle.clone(),
color,
bounds,
});
}

pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>) {
let Some(path) = convert_path(path) else {
return;
Expand Down
32 changes: 31 additions & 1 deletion wgpu/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Build and draw geometry.
use crate::core::{Point, Rectangle, Size, Vector};
use crate::core::svg;
use crate::core::{Color, Point, Rectangle, Size, Vector};
use crate::graphics::color;
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::{
Expand Down Expand Up @@ -263,6 +264,35 @@ impl Frame {
.expect("Fill rectangle");
}

/// TODO(derezzedex): document svg fill
pub fn fill_svg(
&mut self,
handle: &svg::Handle,
bounds: Rectangle<f32>,
color: Option<Color>,
) {
let position = self
.transforms
.current
.raw
.transform_point(lyon::math::Point::new(bounds.x, bounds.y));

let size = self.transforms.current.raw.transform_vector(
lyon::math::Vector::new(bounds.width, bounds.height),
);

let bounds = Rectangle::new(
Point::new(position.x, position.y),
Size::new(size.x, size.y),
);

self.primitives.push(Primitive::Svg {
handle: handle.clone(),
color,
bounds,
});
}

/// Draws the stroke of the given [`Path`] on the [`Frame`] with the
/// provided style.
pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>) {
Expand Down

0 comments on commit 1353ee4

Please sign in to comment.