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 kurbo conversions to scene API #171

Merged
merged 8 commits into from
May 19, 2022
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
26 changes: 20 additions & 6 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions piet-scene/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"
[dependencies]
bytemuck = { version = "1.7.2", features = ["derive"] }
smallvec = "1.8.0"
pinot = "0.1.5"
moscato = "0.1.2"
moscato = { git = "https://github.com/dfrg/pinot" }
kurbo = { version = "0.8.3", optional = true }
2 changes: 1 addition & 1 deletion piet-scene/src/glyph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//
// Also licensed under MIT license, at your choice.

pub use pinot;
pub use moscato::pinot;

use crate::brush::{Brush, Color};
use crate::geometry::Affine;
Expand Down
100 changes: 100 additions & 0 deletions piet-scene/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,103 @@ pub mod glyph;
pub mod path;
pub mod resource;
pub mod scene;

/// Implement conversions to and from Kurbo types when the `kurbo` feature is
/// enabled.
#[cfg(feature = "kurbo")]
mod kurbo_conv {
use super::geometry::{Affine, Point, Rect};
use super::path::Element;

impl Point {
/// Creates a new point from the equivalent kurbo type.
pub fn from_kurbo(point: kurbo::Point) -> Self {
Self::new(point.x as f32, point.y as f32)
}
}

impl From<Point> for kurbo::Point {
fn from(p: Point) -> kurbo::Point {
Self::new(p.x as f64, p.y as f64)
}
}

impl Affine {
/// Creates a new affine transformation from the equivalent kurbo type.
pub fn from_kurbo(affine: kurbo::Affine) -> Self {
let c = affine.as_coeffs();
Self {
xx: c[0] as f32,
yx: c[1] as f32,
xy: c[2] as f32,
yy: c[3] as f32,
dx: c[4] as f32,
dy: c[5] as f32,
}
}
}

impl From<Affine> for kurbo::Affine {
fn from(a: Affine) -> Self {
Self::new([
a.xx as f64,
a.yx as f64,
a.yx as f64,
a.yy as f64,
a.dx as f64,
a.dy as f64,
])
}
}

impl Rect {
/// Creates a new rectangle from the equivalent kurbo type.
pub fn from_kurbo(rect: kurbo::Rect) -> Self {
Self {
min: Point::new(rect.x0 as f32, rect.y0 as f32),
max: Point::new(rect.x1 as f32, rect.y1 as f32),
}
}
}

impl From<Rect> for kurbo::Rect {
fn from(r: Rect) -> Self {
Self {
x0: r.min.x as f64,
y0: r.min.y as f64,
x1: r.max.x as f64,
y1: r.max.y as f64,
}
}
}

impl Element {
/// Creates a new path element from the equivalent kurbo type.
pub fn from_kurbo(el: kurbo::PathEl) -> Self {
use kurbo::PathEl::*;
use Point::from_kurbo;
match e {
MoveTo(p0) => Self::MoveTo(from_kurbo(p0)),
LineTo(p0) => Self::LineTo(from_kurbo(p0)),
QuadTo(p0, p1) => Self::QuadTo(from_kurbo(p0), from_kurbo(p1)),
CurveTo(p0, p1, p2) => {
Self::CurveTo(from_kurbo(p0), from_kurbo(p1), from_kurbo(p2))
}
ClosePath => Self::Close,
}
}
}

impl From<Element> for kurbo::PathEl {
fn from(e: Element) -> Self {
use Element::*;
match e {
MoveTo(p0) => Self::MoveTo(p0.into()),
LineTo(p0) => Self::LineTo(p0.into()),
QuadTo(p0, p1) => Self::QuadTo(p0.into(), p1.into()),
CurveTo(p0, p1, p2) => Self::CurveTo(p0.into(), p1.into(), p2.into()),
Close => Self::ClosePath,
}
}
}
}
7 changes: 4 additions & 3 deletions piet-scene/src/scene/blend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum Mix {
Saturation = 13,
Color = 14,
Luminosity = 15,
// Clip is the same as normal, but doesn't always push a blend group.
Clip = 128,
}

/// Defines the layer composition function for a blend operation.
Expand All @@ -53,8 +55,7 @@ pub enum Compose {
DestAtop = 10,
Xor = 11,
Plus = 12,
PlusDarker = 13,
PlusLighter = 14,
PlusLighter = 13,
dfrg marked this conversation as resolved.
Show resolved Hide resolved
}

/// Blend mode consisting of mixing and composition functions.
Expand All @@ -77,7 +78,7 @@ impl Blend {
impl Default for Blend {
fn default() -> Self {
Self {
mix: Mix::Normal,
mix: Mix::Clip,
compose: Compose::SrcOver,
}
}
Expand Down
10 changes: 6 additions & 4 deletions piet-scene/src/scene/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ use core::borrow::Borrow;

const MAX_BLEND_STACK: usize = 256;

/// Creates a new builder for constructing a scene.
pub fn build_scene<'a>(scene: &'a mut Scene, resources: &'a mut ResourceContext) -> Builder<'a> {
Builder::new(&mut scene.data, ResourceData::Scene(resources))
/// Creates a new builder for filling a scene. Any current content in the scene
/// will be cleared.
pub fn build_scene<'a>(scene: &'a mut Scene, rcx: &'a mut ResourceContext) -> Builder<'a> {
Builder::new(&mut scene.data, ResourceData::Scene(rcx))
}

/// Creates a new builder for construction a scene fragment.
/// Creates a new builder for filling a scene fragment. Any current content in
/// the fragment will be cleared.
pub fn build_fragment<'a>(fragment: &'a mut Fragment) -> Builder<'a> {
Builder::new(
&mut fragment.data,
Expand Down
6 changes: 5 additions & 1 deletion piet-scene/src/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ pub use builder::{build_fragment, build_scene, Builder};
pub use style::*;

use super::brush::*;
use super::geometry::{Affine, Point, Rect};
use super::geometry::{Affine, Point};
use super::path::Element;

use core::ops::Range;

/// Raw data streams describing an encoded scene.
#[derive(Default)]
pub struct SceneData {
pub transform_stream: Vec<Affine>,
Expand Down Expand Up @@ -83,6 +84,7 @@ pub struct Scene {
}

impl Scene {
/// Returns the raw encoded scene data streams.
pub fn data(&self) -> &SceneData {
&self.data
}
Expand All @@ -96,6 +98,8 @@ pub struct Fragment {
}

impl Fragment {
/// Returns the underlying stream of points that defined all encoded path
/// segments.
pub fn points(&self) -> &[Point] {
bytemuck::cast_slice(&self.data.pathseg_stream)
}
Expand Down