Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stroke_rectangle to canvas::Frame #2473

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions graphics/src/geometry/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ where
self.raw.stroke(path, stroke);
}

/// Draws the stroke of an axis-aligned rectangle with the provided style
/// given its top-left corner coordinate and its `Size` on the [`Frame`] .
pub fn stroke_rectangle<'a>(
&mut self,
top_left: Point,
size: Size,
stroke: impl Into<Stroke<'a>>,
) {
self.raw.stroke_rectangle(top_left, size, stroke);
}

/// Draws the characters of the given [`Text`] on the [`Frame`], filling
/// them with the given color.
///
Expand Down Expand Up @@ -200,6 +211,12 @@ pub trait Backend: Sized {
fn paste(&mut self, frame: Self);

fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>);
fn stroke_rectangle<'a>(
&mut self,
top_left: Point,
size: Size,
stroke: impl Into<Stroke<'a>>,
);

fn fill(&mut self, path: &Path, fill: impl Into<Fill>);
fn fill_text(&mut self, text: impl Into<Text>);
Expand Down Expand Up @@ -248,6 +265,13 @@ impl Backend for () {
fn paste(&mut self, _frame: Self) {}

fn stroke<'a>(&mut self, _path: &Path, _stroke: impl Into<Stroke<'a>>) {}
fn stroke_rectangle<'a>(
&mut self,
_top_left: Point,
_size: Size,
_stroke: impl Into<Stroke<'a>>,
) {
}

fn fill(&mut self, _path: &Path, _fill: impl Into<Fill>) {}
fn fill_text(&mut self, _text: impl Into<Text>) {}
Expand Down
13 changes: 13 additions & 0 deletions renderer/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,19 @@ mod geometry {
delegate!(self, frame, frame.stroke(path, stroke));
}

fn stroke_rectangle<'a>(
&mut self,
top_left: Point,
size: Size,
stroke: impl Into<Stroke<'a>>,
) {
delegate!(
self,
frame,
frame.stroke_rectangle(top_left, size, stroke)
);
}

fn fill_text(&mut self, text: impl Into<Text>) {
delegate!(self, frame, frame.fill_text(text));
}
Expand Down
9 changes: 9 additions & 0 deletions tiny_skia/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ impl geometry::frame::Backend for Frame {
});
}

fn stroke_rectangle<'a>(
&mut self,
top_left: Point,
size: Size,
stroke: impl Into<Stroke<'a>>,
) {
self.stroke(&Path::rectangle(top_left, size), stroke);
}

fn fill_text(&mut self, text: impl Into<geometry::Text>) {
let text = text.into();

Expand Down
38 changes: 38 additions & 0 deletions wgpu/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,44 @@ impl geometry::frame::Backend for Frame {
.expect("Stroke path");
}

fn stroke_rectangle<'a>(
&mut self,
top_left: Point,
size: Size,
stroke: impl Into<Stroke<'a>>,
) {
let stroke = stroke.into();

let mut buffer = self
.buffers
.get_stroke(&self.transforms.current.transform_style(stroke.style));

let top_left = self
.transforms
.current
.0
.transform_point(lyon::math::Point::new(top_left.x, top_left.y));

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

let mut options = tessellation::StrokeOptions::default();
options.line_width = stroke.width;
options.start_cap = into_line_cap(stroke.line_cap);
options.end_cap = into_line_cap(stroke.line_cap);
options.line_join = into_line_join(stroke.line_join);

self.stroke_tessellator
.tessellate_rectangle(
&lyon::math::Box2D::new(top_left, top_left + size),
&options,
buffer.as_mut(),
)
.expect("Stroke rectangle");
}

fn fill_text(&mut self, text: impl Into<geometry::Text>) {
let text = text.into();

Expand Down
Loading