Skip to content
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
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "area example",
"cargo": {
"args": [
"build", "--example", "area",
]
},
"args": ["png", "svg"]
},
{
"type": "lldb",
"request": "launch",
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ noto-serif-italic = ["plotive-text/noto-serif-italic"]
time = []
utils = []

[[example]]
name = "area"

[[example]]
name = "bars"

Expand Down
280 changes: 280 additions & 0 deletions base/src/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Y low coordinates are at the top.
*/

use std::marker::PhantomData;

use strict_num::{FiniteF32, PositiveF32};
pub use tiny_skia_path::{Path, PathBuilder, PathSegment, PathVerb, Point, Transform};

Expand Down Expand Up @@ -604,3 +606,281 @@ impl From<(f32, f32, f32, f32)> for Margin {
Margin::Custom { t, r, b, l }
}
}

pub fn path_segments_rev_iter<'a>(path: &'a Path) -> PathSegmentsRevIter<'a> {
PathSegmentsRevIter::new(path)
}

pub fn reverse_path(path: &Path) -> Path {
let mut pb = PathBuilder::new();
for seg in path_segments_rev_iter(path) {
match seg {
PathSegment::MoveTo(p) => pb.move_to(p.x, p.y),
PathSegment::LineTo(p) => pb.line_to(p.x, p.y),
PathSegment::QuadTo(ctrl, to) => pb.quad_to(ctrl.x, ctrl.y, to.x, to.y),
PathSegment::CubicTo(ctrl1, ctrl2, to) => {
pb.cubic_to(ctrl1.x, ctrl1.y, ctrl2.x, ctrl2.y, to.x, to.y)
}
PathSegment::Close => pb.close(),
}
}
pb.finish()
.expect("Reversing a valid path should yield a valid path")
}

pub struct PathSegmentsRevIter<'a> {
segments: Vec<PathSegment>,
segment_index: usize,
_path: PhantomData<&'a Path>,
}

impl PathSegmentsRevIter<'_> {
fn reverse_subpath(subpath: &SubPath, out: &mut Vec<PathSegment>) {
let start = subpath.start;
let last = subpath
.segments
.last()
.map(|segment| segment.end())
.unwrap_or(start);

out.push(PathSegment::MoveTo(last));

for segment in subpath.segments.iter().rev() {
out.push(segment.rev_segment());
}

if subpath.closed {
out.push(PathSegment::Close);
}
}

fn new(path: &Path) -> Self {
let mut subpaths: Vec<SubPath> = Vec::new();
let mut curr_subpath: Option<SubPath> = None;
let mut points_index = 0;

for verb in path.verbs() {
match *verb {
PathVerb::Move => {
if let Some(subpath) = curr_subpath.take() {
subpaths.push(subpath);
}
let start = path.points()[points_index];
points_index += 1;
curr_subpath = Some(SubPath::new(start));
}
PathVerb::Line => {
let to = path.points()[points_index];
points_index += 1;
if let Some(subpath) = curr_subpath.as_mut() {
subpath.push_line(to);
}
}
PathVerb::Quad => {
let ctrl = path.points()[points_index];
let to = path.points()[points_index + 1];
points_index += 2;
if let Some(subpath) = curr_subpath.as_mut() {
subpath.push_quad(ctrl, to);
}
}
PathVerb::Cubic => {
let ctrl1 = path.points()[points_index];
let ctrl2 = path.points()[points_index + 1];
let to = path.points()[points_index + 2];
points_index += 3;
if let Some(subpath) = curr_subpath.as_mut() {
subpath.push_cubic(ctrl1, ctrl2, to);
}
}
PathVerb::Close => {
if let Some(subpath) = curr_subpath.as_mut() {
subpath.closed = true;
}
}
}
}

if let Some(subpath) = curr_subpath.take() {
subpaths.push(subpath);
}

let mut segments: Vec<PathSegment> = Vec::new();
for subpath in subpaths.iter().rev() {
Self::reverse_subpath(subpath, &mut segments);
}

PathSegmentsRevIter {
segment_index: 0,
segments,
_path: PhantomData,
}
}
}

impl<'a> Iterator for PathSegmentsRevIter<'a> {
type Item = PathSegment;

fn next(&mut self) -> Option<Self::Item> {
if self.segment_index >= self.segments.len() {
return None;
}

let segment = self.segments[self.segment_index];
self.segment_index += 1;
Some(segment)
}
}

#[derive(Clone, Copy)]
enum SubPathSegment {
Line {
from: Point,
to: Point,
},
Quad {
from: Point,
ctrl: Point,
to: Point,
},
Cubic {
from: Point,
ctrl1: Point,
ctrl2: Point,
to: Point,
},
}

impl SubPathSegment {
fn end(&self) -> Point {
match self {
SubPathSegment::Line { to, .. } => *to,
SubPathSegment::Quad { to, .. } => *to,
SubPathSegment::Cubic { to, .. } => *to,
}
}

fn rev_segment(&self) -> PathSegment {
match self {
SubPathSegment::Line { from, .. } => PathSegment::LineTo(*from),
SubPathSegment::Quad { from, ctrl, .. } => PathSegment::QuadTo(*ctrl, *from),
SubPathSegment::Cubic {
from, ctrl1, ctrl2, ..
} => PathSegment::CubicTo(*ctrl2, *ctrl1, *from),
}
}
}

struct SubPath {
start: Point,
curr: Point,
closed: bool,
segments: Vec<SubPathSegment>,
}

impl SubPath {
fn new(start: Point) -> Self {
Self {
start,
curr: start,
closed: false,
segments: Vec::new(),
}
}

fn push_line(&mut self, to: Point) {
self.segments.push(SubPathSegment::Line {
from: self.curr,
to,
});
self.curr = to;
}

fn push_quad(&mut self, ctrl: Point, to: Point) {
self.segments.push(SubPathSegment::Quad {
from: self.curr,
ctrl,
to,
});
self.curr = to;
}

fn push_cubic(&mut self, ctrl1: Point, ctrl2: Point, to: Point) {
self.segments.push(SubPathSegment::Cubic {
from: self.curr,
ctrl1,
ctrl2,
to,
});
self.curr = to;
}
}

#[cfg(test)]
mod tests {
use super::*;

fn p(x: f32, y: f32) -> Point {
Point { x, y }
}

#[test]
fn reverse_open_path_segments() {
let mut pb = PathBuilder::new();
pb.move_to(0.0, 0.0);
pb.line_to(1.0, 1.0);
pb.quad_to(2.0, 3.0, 4.0, 5.0);
pb.cubic_to(6.0, 7.0, 8.0, 9.0, 10.0, 11.0);
let path = pb.finish().unwrap();

let rev: Vec<PathSegment> = path_segments_rev_iter(&path).collect();
let expected = vec![
PathSegment::MoveTo(p(10.0, 11.0)),
PathSegment::CubicTo(p(8.0, 9.0), p(6.0, 7.0), p(4.0, 5.0)),
PathSegment::QuadTo(p(2.0, 3.0), p(1.0, 1.0)),
PathSegment::LineTo(p(0.0, 0.0)),
];

assert_eq!(rev, expected);
}

#[test]
fn reverse_closed_path_segments() {
let mut pb = PathBuilder::new();
pb.move_to(0.0, 0.0);
pb.line_to(1.0, 0.0);
pb.line_to(1.0, 1.0);
pb.close();
let path = pb.finish().unwrap();

let rev: Vec<PathSegment> = path_segments_rev_iter(&path).collect();
let expected = vec![
PathSegment::MoveTo(p(1.0, 1.0)),
PathSegment::LineTo(p(1.0, 0.0)),
PathSegment::LineTo(p(0.0, 0.0)),
PathSegment::Close,
];

assert_eq!(rev, expected);
}

#[test]
fn reverse_multiple_subpaths() {
let mut pb = PathBuilder::new();
pb.move_to(0.0, 0.0);
pb.line_to(1.0, 0.0);
pb.move_to(5.0, 5.0);
pb.line_to(6.0, 6.0);
let path = pb.finish().unwrap();

let rev: Vec<PathSegment> = path_segments_rev_iter(&path).collect();
let expected = vec![
PathSegment::MoveTo(p(6.0, 6.0)),
PathSegment::LineTo(p(5.0, 5.0)),
PathSegment::MoveTo(p(1.0, 0.0)),
PathSegment::LineTo(p(0.0, 0.0)),
];

assert_eq!(rev, expected);
}
}
32 changes: 32 additions & 0 deletions examples/area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use plotive::{data, des};

mod common;

fn main() {
let x = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0];
let y1 = vec![10.0, 15.0, 8.0, 6.0, 12.0, 10.0];
let y2 = vec![4.0, 9.0, 2.0, 0.0, 6.0, 4.0];

let fig = des::Plot::new(vec![
des::series::Area::new(
des::data_src_ref("x"),
des::data_src_ref("y1"),
des::data_src_ref("y2").into(),
)
.into(),
des::series::Area::new(
des::data_src_ref("x"),
des::data_src_ref("y2"),
Default::default(),
)
.into(),
])
.into_figure();

let data_source = data::TableSource::new()
.with_f64_column("x", x)
.with_f64_column("y1", y1)
.with_f64_column("y2", y2);

common::save_figure(&fig, &data_source, Default::default(), "area");
}
Loading
Loading