Skip to content

Commit

Permalink
Apply tiny-skia to painting example
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jan 8, 2025
1 parent 5992932 commit 5c8e9ce
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/gpui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ rand.workspace = true
util = { workspace = true, features = ["test-support"] }
http_client = { workspace = true, features = ["test-support"] }
unicode-segmentation.workspace = true
tiny-skia = "0.11.4"
tiny-skia-path = "0.11.4"

[build-dependencies]
embed-resource = "3.0"
Expand Down
43 changes: 32 additions & 11 deletions crates/gpui/examples/painting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,48 @@ impl Render for PaintingViewer {
canvas(
move |_, _| {},
move |_, _, cx| {
const STROKE_WIDTH: Pixels = px(2.0);

for path in default_lines {
cx.paint_path(path, gpui::black());
}

let stroke =tiny_skia::Stroke {
width: 1.0,
..Default::default()
};

for points in lines {
let mut path = Path::new(points[0]);
if points.len() < 2 {
continue;
}

let mut builder = tiny_skia::PathBuilder::new();
let first_p = points.first().unwrap();
builder.move_to(first_p.x.0, first_p.y.0);
for p in points.iter().skip(1) {
path.line_to(*p);
builder.line_to(p.x.0, p.y.0);
}

let mut last = points.last().unwrap();
for p in points.iter().rev() {
let mut offset_x = px(0.);
if last.x == p.x {
offset_x = STROKE_WIDTH;
let path = builder.finish().unwrap();
let stroke_path = tiny_skia::PathStroker::new().stroke(&path, &stroke, cx.scale_factor());
if let Some(stroke_path) = stroke_path {
let Some(first_p) = stroke_path.points().first() else {
break;
};

let mut path = Path::new(point(px(first_p.x), px(first_p.y)));
for i in 1..stroke_path.len() - 1 {
let p = stroke_path.points()[i];
let verb = stroke_path.verbs()[i];
match verb {
tiny_skia_path::PathVerb::Move => path.move_to(point(px(p.x), px(p.y))),
tiny_skia_path::PathVerb::Line => path.line_to(point(px(p.x), px(p.y))),
_ => {}
}
}
path.line_to(point(p.x + offset_x, p.y + STROKE_WIDTH));
last = p;
cx.paint_path(path, gpui::black());
}

cx.paint_path(path, gpui::black());
}
},
)
Expand Down
9 changes: 0 additions & 9 deletions crates/gpui/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,15 +722,6 @@ impl Path<Pixels> {
self.current = to;
}

/// Close the current contour.
pub fn close(&mut self) {
self.contour_count += 1;
self.push_triangle(
(self.current, self.start, self.start),
(point(0., 1.), point(0., 1.), point(0., 1.)),
);
}

/// Draw a straight line from the current point to the given point.
pub fn line_to(&mut self, to: Point<Pixels>) {
self.contour_count += 1;
Expand Down

0 comments on commit 5c8e9ce

Please sign in to comment.