Extract gsvg_renderer from gcore, remove gcore/vello feature (#2760)

Extract `gsvg_renderer` from `gcore`, remove `gcore/vello` feature
This commit is contained in:
Firestar99 2025-06-27 15:47:46 +02:00 committed by GitHub
parent ffc6c5532b
commit 9c4ab34a58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 546 additions and 368 deletions

21
Cargo.lock generated
View file

@ -2133,6 +2133,7 @@ dependencies = [
"graphene-application-io",
"graphene-core",
"graphene-path-bool",
"graphene-svg-renderer",
"iai-callgrind",
"js-sys",
"log",
@ -2210,8 +2211,6 @@ dependencies = [
"specta",
"tinyvec",
"tokio",
"usvg",
"vello",
"wgpu",
]
@ -2244,6 +2243,7 @@ dependencies = [
"graphene-application-io",
"graphene-core",
"graphene-path-bool",
"graphene-svg-renderer",
"image",
"log",
"ndarray",
@ -2259,6 +2259,22 @@ dependencies = [
"wgpu-executor",
]
[[package]]
name = "graphene-svg-renderer"
version = "0.1.0"
dependencies = [
"base64 0.22.1",
"bezier-rs",
"dyn-any",
"glam",
"graphene-core",
"log",
"num-traits",
"serde",
"usvg",
"vello",
]
[[package]]
name = "graphite-desktop"
version = "0.1.0"
@ -7427,6 +7443,7 @@ dependencies = [
"glam",
"graphene-application-io",
"graphene-core",
"graphene-svg-renderer",
"node-macro",
"vello",
"web-sys",

View file

@ -10,6 +10,7 @@ members = [
"node-graph/gpath-bool",
"node-graph/graph-craft",
"node-graph/graphene-cli",
"node-graph/gsvg-renderer",
"node-graph/interpreted-executor",
"node-graph/node-macro",
"node-graph/preprocessor",
@ -27,6 +28,7 @@ default-members = [
"node-graph/gpath-bool",
"node-graph/graph-craft",
"node-graph/graphene-cli",
"node-graph/gsvg-renderer",
"node-graph/interpreted-executor",
"node-graph/node-macro",
]
@ -44,6 +46,7 @@ graphene-core = { path = "node-graph/gcore" }
graphene-path-bool = { path = "node-graph/gpath-bool" }
graph-craft = { path = "node-graph/graph-craft" }
graphene-std = { path = "node-graph/gstd" }
graphene-svg-renderer = { path = "node-graph/gsvg-renderer" }
interpreted-executor = { path = "node-graph/interpreted-executor" }
node-macro = { path = "node-graph/node-macro" }
wgpu-executor = { path = "node-graph/wgpu-executor" }

View file

@ -11,7 +11,6 @@ default = ["serde"]
nightly = []
type_id_logging = []
wgpu = ["dep:wgpu"]
vello = ["dep:vello", "bezier-rs/kurbo", "wgpu"]
dealloc_nodes = []
[dependencies]
@ -23,7 +22,6 @@ bytemuck = { workspace = true }
node-macro = { workspace = true }
num-derive = { workspace = true }
num-traits = { workspace = true }
usvg = { workspace = true }
rand = { workspace = true }
glam = { workspace = true }
serde_json = { workspace = true }
@ -44,7 +42,6 @@ base64 = { workspace = true }
# Optional workspace dependencies
serde = { workspace = true, optional = true }
vello = { workspace = true, optional = true }
wgpu = { workspace = true, optional = true }
[dev-dependencies]

View file

@ -238,34 +238,3 @@ impl std::fmt::Display for BlendMode {
}
}
}
#[cfg(feature = "vello")]
impl From<BlendMode> for vello::peniko::Mix {
fn from(val: BlendMode) -> Self {
match val {
// Normal group
BlendMode::Normal => vello::peniko::Mix::Normal,
// Darken group
BlendMode::Darken => vello::peniko::Mix::Darken,
BlendMode::Multiply => vello::peniko::Mix::Multiply,
BlendMode::ColorBurn => vello::peniko::Mix::ColorBurn,
// Lighten group
BlendMode::Lighten => vello::peniko::Mix::Lighten,
BlendMode::Screen => vello::peniko::Mix::Screen,
BlendMode::ColorDodge => vello::peniko::Mix::ColorDodge,
// Contrast group
BlendMode::Overlay => vello::peniko::Mix::Overlay,
BlendMode::SoftLight => vello::peniko::Mix::SoftLight,
BlendMode::HardLight => vello::peniko::Mix::HardLight,
// Inversion group
BlendMode::Difference => vello::peniko::Mix::Difference,
BlendMode::Exclusion => vello::peniko::Mix::Exclusion,
// Component group
BlendMode::Hue => vello::peniko::Mix::Hue,
BlendMode::Saturation => vello::peniko::Mix::Saturation,
BlendMode::Color => vello::peniko::Mix::Color,
BlendMode::Luminosity => vello::peniko::Mix::Luminosity,
_ => todo!(),
}
}
}

View file

@ -0,0 +1,24 @@
use crate::Color;
use glam::{DAffine2, DVec2};
pub trait BoundingBox {
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]>;
}
macro_rules! none_impl {
($t:path) => {
impl BoundingBox for $t {
fn bounding_box(&self, _transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> {
None
}
}
};
}
none_impl!(String);
none_impl!(bool);
none_impl!(f32);
none_impl!(f64);
none_impl!(DVec2);
none_impl!(Option<Color>);
none_impl!(Vec<Color>);

View file

@ -1,5 +1,7 @@
use crate::blending::AlphaBlending;
use crate::bounds::BoundingBox;
use crate::instances::{Instance, Instances};
use crate::math::quad::Quad;
use crate::raster::image::Image;
use crate::raster_types::{CPU, GPU, Raster, RasterDataTable};
use crate::transform::TransformMut;
@ -7,11 +9,9 @@ use crate::uuid::NodeId;
use crate::vector::{VectorData, VectorDataTable};
use crate::{CloneVarArgs, Color, Context, Ctx, ExtractAll, OwnedContextImpl};
use dyn_any::DynAny;
use glam::{DAffine2, IVec2};
use glam::{DAffine2, DVec2, IVec2};
use std::hash::Hash;
pub mod renderer;
// TODO: Eventually remove this migration document upgrade code
pub fn migrate_graphic_group<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<GraphicGroupTable, D::Error> {
use serde::Deserialize;
@ -182,6 +182,25 @@ impl GraphicElement {
}
}
impl BoundingBox for GraphicElement {
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
match self {
GraphicElement::VectorData(vector_data) => vector_data.bounding_box(transform, include_stroke),
GraphicElement::RasterDataCPU(raster) => raster.bounding_box(transform, include_stroke),
GraphicElement::RasterDataGPU(raster) => raster.bounding_box(transform, include_stroke),
GraphicElement::GraphicGroup(graphic_group) => graphic_group.bounding_box(transform, include_stroke),
}
}
}
impl BoundingBox for GraphicGroupTable {
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.filter_map(|element| element.instance.bounding_box(transform * *element.transform, include_stroke))
.reduce(Quad::combine_bounds)
}
}
impl<'de> serde::Deserialize<'de> for Raster<CPU> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@ -247,6 +266,20 @@ impl Artboard {
}
}
impl BoundingBox for Artboard {
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
let artboard_bounds = (transform * Quad::from_box([self.location.as_dvec2(), self.location.as_dvec2() + self.dimensions.as_dvec2()])).bounding_box();
if self.clip {
Some(artboard_bounds)
} else {
[self.graphic_group.bounding_box(transform, include_stroke), Some(artboard_bounds)]
.into_iter()
.flatten()
.reduce(Quad::combine_bounds)
}
}
}
// TODO: Eventually remove this migration document upgrade code
pub fn migrate_artboard_group<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<ArtboardGroupTable, D::Error> {
use serde::Deserialize;
@ -282,6 +315,14 @@ pub fn migrate_artboard_group<'de, D: serde::Deserializer<'de>>(deserializer: D)
pub type ArtboardGroupTable = Instances<Artboard>;
impl BoundingBox for ArtboardGroupTable {
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.filter_map(|instance| instance.instance.bounding_box(transform, include_stroke))
.reduce(Quad::combine_bounds)
}
}
#[node_macro::node(category(""))]
async fn layer<I: 'n + Send + Clone>(
_: impl Ctx,
@ -506,3 +547,7 @@ impl From<GraphicGroupTable> for GraphicElement {
GraphicElement::GraphicGroup(graphic_group)
}
}
pub trait ToGraphicElement {
fn to_graphic_element(&self) -> GraphicElement;
}

View file

@ -4,6 +4,7 @@ extern crate log;
pub mod animation;
pub mod blending;
pub mod blending_nodes;
pub mod bounds;
pub mod color;
pub mod consts;
pub mod context;

View file

@ -1,8 +1,11 @@
use crate::Color;
use crate::bounds::BoundingBox;
use crate::instances::Instances;
use crate::math::quad::Quad;
use crate::raster::Image;
use core::ops::Deref;
use dyn_any::DynAny;
use glam::{DAffine2, DVec2};
#[cfg(feature = "wgpu")]
use std::sync::Arc;
@ -11,18 +14,18 @@ pub struct CPU;
#[derive(Clone, Debug, Hash, PartialEq, Eq, Copy)]
pub struct GPU;
trait Storage {}
trait Storage: 'static {}
impl Storage for CPU {}
impl Storage for GPU {}
#[derive(Clone, Debug, Hash, PartialEq)]
#[allow(private_bounds)]
pub struct Raster<T: 'static + Storage> {
pub struct Raster<T: Storage> {
data: RasterStorage,
storage: T,
}
unsafe impl<T: 'static + Storage> dyn_any::StaticType for Raster<T> {
unsafe impl<T: Storage> dyn_any::StaticType for Raster<T> {
type Static = Raster<T>;
}
#[derive(Clone, Debug, Hash, PartialEq, DynAny)]
@ -100,3 +103,14 @@ impl Deref for Raster<GPU> {
}
}
pub type RasterDataTable<Storage> = Instances<Raster<Storage>>;
impl<S: Storage> BoundingBox for RasterDataTable<S> {
fn bounding_box(&self, transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.flat_map(|instance| {
let transform = transform * *instance.transform;
(transform.matrix2.determinant() != 0.).then(|| (transform * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box())
})
.reduce(Quad::combine_bounds)
}
}

View file

@ -1,5 +1,5 @@
use crate::math::math_ext::QuadExt;
use crate::renderer::Quad;
use crate::math::quad::Quad;
use crate::vector::PointId;
use bezier_rs::Subpath;
use glam::{DAffine2, DMat2, DVec2};

View file

@ -1,70 +1,9 @@
//! Contains stylistic options for SVG elements.
use crate::Color;
use crate::consts::{LAYER_OUTLINE_STROKE_COLOR, LAYER_OUTLINE_STROKE_WEIGHT};
pub use crate::gradient::*;
use crate::renderer::{RenderParams, format_transform_matrix};
use dyn_any::DynAny;
use glam::{DAffine2, DVec2};
use std::fmt::Write;
impl Gradient {
/// Adds the gradient def through mutating the first argument, returning the gradient ID.
fn render_defs(&self, svg_defs: &mut String, element_transform: DAffine2, stroke_transform: DAffine2, bounds: [DVec2; 2], transformed_bounds: [DVec2; 2], _render_params: &RenderParams) -> u64 {
// TODO: Figure out how to use `self.transform` as part of the gradient transform, since that field (`Gradient::transform`) is currently never read from, it's only written to.
let bound_transform = DAffine2::from_scale_angle_translation(bounds[1] - bounds[0], 0., bounds[0]);
let transformed_bound_transform = element_transform * DAffine2::from_scale_angle_translation(transformed_bounds[1] - transformed_bounds[0], 0., transformed_bounds[0]);
let mut stop = String::new();
for (position, color) in self.stops.0.iter() {
stop.push_str("<stop");
if *position != 0. {
let _ = write!(stop, r#" offset="{}""#, (position * 1_000_000.).round() / 1_000_000.);
}
let _ = write!(stop, r##" stop-color="#{}""##, color.to_rgb_hex_srgb_from_gamma());
if color.a() < 1. {
let _ = write!(stop, r#" stop-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
}
stop.push_str(" />")
}
let mod_gradient = if transformed_bound_transform.matrix2.determinant() != 0. {
transformed_bound_transform.inverse()
} else {
DAffine2::IDENTITY // Ignore if the transform cannot be inverted (the bounds are zero). See issue #1944.
};
let mod_points = element_transform * stroke_transform * bound_transform;
let start = mod_points.transform_point2(self.start);
let end = mod_points.transform_point2(self.end);
let gradient_id = crate::uuid::generate_uuid();
let matrix = format_transform_matrix(mod_gradient);
let gradient_transform = if matrix.is_empty() { String::new() } else { format!(r#" gradientTransform="{}""#, matrix) };
match self.gradient_type {
GradientType::Linear => {
let _ = write!(
svg_defs,
r#"<linearGradient id="{}" x1="{}" x2="{}" y1="{}" y2="{}"{gradient_transform}>{}</linearGradient>"#,
gradient_id, start.x, end.x, start.y, end.y, stop
);
}
GradientType::Radial => {
let radius = (f64::powi(start.x - end.x, 2) + f64::powi(start.y - end.y, 2)).sqrt();
let _ = write!(
svg_defs,
r#"<radialGradient id="{}" cx="{}" cy="{}" r="{}"{gradient_transform}>{}</radialGradient>"#,
gradient_id, start.x, start.y, radius, stop
);
}
}
gradient_id
}
}
use glam::DAffine2;
/// Describes the fill of a layer.
///
@ -138,24 +77,6 @@ impl Fill {
}
}
/// Renders the fill, adding necessary defs through mutating the first argument.
pub fn render(&self, svg_defs: &mut String, element_transform: DAffine2, stroke_transform: DAffine2, bounds: [DVec2; 2], transformed_bounds: [DVec2; 2], render_params: &RenderParams) -> String {
match self {
Self::None => r#" fill="none""#.to_string(),
Self::Solid(color) => {
let mut result = format!(r##" fill="#{}""##, color.to_rgb_hex_srgb_from_gamma());
if color.a() < 1. {
let _ = write!(result, r#" fill-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
}
result
}
Self::Gradient(gradient) => {
let gradient_id = gradient.render_defs(svg_defs, element_transform, stroke_transform, bounds, transformed_bounds, render_params);
format!(r##" fill="url('#{gradient_id}')""##)
}
}
}
/// Extract a gradient from the fill
pub fn as_gradient(&self) -> Option<&Gradient> {
match self {
@ -279,7 +200,7 @@ pub enum StrokeCap {
}
impl StrokeCap {
fn svg_name(&self) -> &'static str {
pub fn svg_name(&self) -> &'static str {
match self {
StrokeCap::Butt => "butt",
StrokeCap::Round => "round",
@ -299,7 +220,7 @@ pub enum StrokeJoin {
}
impl StrokeJoin {
fn svg_name(&self) -> &'static str {
pub fn svg_name(&self) -> &'static str {
match self {
StrokeJoin::Bevel => "bevel",
StrokeJoin::Miter => "miter",
@ -469,60 +390,6 @@ impl Stroke {
self.join_miter_limit as f32
}
/// Provide the SVG attributes for the stroke.
pub fn render(&self, aligned_strokes: bool, override_paint_order: bool, _render_params: &RenderParams) -> String {
// Don't render a stroke at all if it would be invisible
let Some(color) = self.color else { return String::new() };
if !self.has_renderable_stroke() {
return String::new();
}
// Set to None if the value is the SVG default
let weight = (self.weight != 1.).then_some(self.weight);
let dash_array = (!self.dash_lengths.is_empty()).then_some(self.dash_lengths());
let dash_offset = (self.dash_offset != 0.).then_some(self.dash_offset);
let stroke_cap = (self.cap != StrokeCap::Butt).then_some(self.cap);
let stroke_join = (self.join != StrokeJoin::Miter).then_some(self.join);
let stroke_join_miter_limit = (self.join_miter_limit != 4.).then_some(self.join_miter_limit);
let stroke_align = (self.align != StrokeAlign::Center).then_some(self.align);
let paint_order = (self.paint_order != PaintOrder::StrokeAbove || override_paint_order).then_some(PaintOrder::StrokeBelow);
// Render the needed stroke attributes
let mut attributes = format!(r##" stroke="#{}""##, color.to_rgb_hex_srgb_from_gamma());
if color.a() < 1. {
let _ = write!(&mut attributes, r#" stroke-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
}
if let Some(mut weight) = weight {
if stroke_align.is_some() && aligned_strokes {
weight *= 2.;
}
let _ = write!(&mut attributes, r#" stroke-width="{}""#, weight);
}
if let Some(dash_array) = dash_array {
let _ = write!(&mut attributes, r#" stroke-dasharray="{}""#, dash_array);
}
if let Some(dash_offset) = dash_offset {
let _ = write!(&mut attributes, r#" stroke-dashoffset="{}""#, dash_offset);
}
if let Some(stroke_cap) = stroke_cap {
let _ = write!(&mut attributes, r#" stroke-linecap="{}""#, stroke_cap.svg_name());
}
if let Some(stroke_join) = stroke_join {
let _ = write!(&mut attributes, r#" stroke-linejoin="{}""#, stroke_join.svg_name());
}
if let Some(stroke_join_miter_limit) = stroke_join_miter_limit {
let _ = write!(&mut attributes, r#" stroke-miterlimit="{}""#, stroke_join_miter_limit);
}
// Add vector-effect attribute to make strokes non-scaling
if self.non_scaling {
let _ = write!(&mut attributes, r#" vector-effect="non-scaling-stroke""#);
}
if paint_order.is_some() {
let _ = write!(&mut attributes, r#" style="paint-order: stroke;" "#);
}
attributes
}
pub fn with_color(mut self, color: &Option<Color>) -> Option<Self> {
self.color = *color;
@ -604,8 +471,8 @@ impl Default for Stroke {
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize, DynAny, specta::Type)]
pub struct PathStyle {
stroke: Option<Stroke>,
fill: Fill,
pub stroke: Option<Stroke>,
pub fill: Fill,
}
impl std::hash::Hash for PathStyle {
@ -766,41 +633,6 @@ impl PathStyle {
pub fn clear_stroke(&mut self) {
self.stroke = None;
}
/// Renders the shape's fill and stroke attributes as a string with them concatenated together.
#[allow(clippy::too_many_arguments)]
pub fn render(
&self,
svg_defs: &mut String,
element_transform: DAffine2,
stroke_transform: DAffine2,
bounds: [DVec2; 2],
transformed_bounds: [DVec2; 2],
aligned_strokes: bool,
override_paint_order: bool,
render_params: &RenderParams,
) -> String {
let view_mode = render_params.view_mode;
match view_mode {
ViewMode::Outline => {
let fill_attribute = Fill::None.render(svg_defs, element_transform, stroke_transform, bounds, transformed_bounds, render_params);
let mut outline_stroke = Stroke::new(Some(LAYER_OUTLINE_STROKE_COLOR), LAYER_OUTLINE_STROKE_WEIGHT);
// Outline strokes should be non-scaling by default
outline_stroke.non_scaling = true;
let stroke_attribute = outline_stroke.render(aligned_strokes, override_paint_order, render_params);
format!("{fill_attribute}{stroke_attribute}")
}
_ => {
let fill_attribute = self.fill.render(svg_defs, element_transform, stroke_transform, bounds, transformed_bounds, render_params);
let stroke_attribute = self
.stroke
.as_ref()
.map(|stroke| stroke.render(aligned_strokes, override_paint_order, render_params))
.unwrap_or_default();
format!("{fill_attribute}{stroke_attribute}")
}
}
}
}
/// Represents different ways of rendering an object

View file

@ -4,7 +4,10 @@ mod modification;
use super::misc::{dvec2_to_point, point_to_dvec2};
use super::style::{PathStyle, Stroke};
use crate::bounds::BoundingBox;
use crate::instances::Instances;
use crate::math::quad::Quad;
use crate::transform::Transform;
use crate::vector::click_target::{ClickTargetType, FreePoint};
use crate::{AlphaBlending, Color, GraphicGroupTable};
pub use attributes::*;
@ -487,6 +490,29 @@ impl VectorData {
}
}
impl BoundingBox for VectorDataTable {
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.flat_map(|instance| {
if !include_stroke {
return instance.instance.bounding_box_with_transform(transform * *instance.transform);
}
let stroke_width = instance.instance.style.stroke().map(|s| s.weight()).unwrap_or_default();
let miter_limit = instance.instance.style.stroke().map(|s| s.join_miter_limit).unwrap_or(1.);
let scale = transform.decompose_scale();
// We use the full line width here to account for different styles of stroke caps
let offset = DVec2::splat(stroke_width * scale.x.max(scale.y) * miter_limit);
instance.instance.bounding_box_with_transform(transform * *instance.transform).map(|[a, b]| [a - offset, b + offset])
})
.reduce(Quad::combine_bounds)
}
}
/// A selectable part of a curve, either an anchor (start or end of a bézier) or a handle (doesn't necessarily go through the bézier but influences curvature).
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, DynAny, serde::Serialize, serde::Deserialize)]
pub enum ManipulatorPointId {

View file

@ -4,10 +4,10 @@ use super::algorithms::spline::{solve_spline_first_handle_closed, solve_spline_f
use super::misc::{CentroidType, point_to_dvec2};
use super::style::{Fill, Gradient, GradientStops, Stroke};
use super::{PointId, SegmentDomain, SegmentId, StrokeId, VectorData, VectorDataExt, VectorDataTable};
use crate::bounds::BoundingBox;
use crate::instances::{Instance, InstanceMut, Instances};
use crate::raster_types::{CPU, GPU, RasterDataTable};
use crate::registry::types::{Angle, Fraction, IntegerCount, Length, Multiplier, Percentage, PixelLength, PixelSize, SeedValue};
use crate::renderer::GraphicElementRendered;
use crate::transform::{Footprint, ReferencePoint, Transform};
use crate::vector::algorithms::merge_by_distance::MergeByDistanceExt;
use crate::vector::misc::{MergeByDistanceAlgorithm, PointSpacingType};
@ -221,10 +221,7 @@ async fn repeat<I: 'n + Send + Clone>(
direction: PixelSize,
angle: Angle,
#[default(4)] instances: IntegerCount,
) -> Instances<I>
where
Instances<I>: GraphicElementRendered,
{
) -> Instances<I> {
let angle = angle.to_radians();
let count = instances.max(1);
let total = (count - 1) as f64;
@ -258,10 +255,7 @@ async fn circular_repeat<I: 'n + Send + Clone>(
angle_offset: Angle,
#[default(5)] radius: f64,
#[default(5)] instances: IntegerCount,
) -> Instances<I>
where
Instances<I>: GraphicElementRendered,
{
) -> Instances<I> {
let count = instances.max(1);
let mut result_table = Instances::<I>::default();
@ -313,10 +307,7 @@ async fn copy_to_points<I: 'n + Send + Clone>(
random_rotation: Angle,
/// Seed to determine unique variations on all the randomized instance angles.
random_rotation_seed: SeedValue,
) -> Instances<I>
where
Instances<I>: GraphicElementRendered,
{
) -> Instances<I> {
let mut result_table = Instances::<I>::default();
let random_scale_difference = random_scale_max - random_scale_min;
@ -377,7 +368,7 @@ async fn mirror<I: 'n + Send + Clone>(
#[default(true)] keep_original: bool,
) -> Instances<I>
where
Instances<I>: GraphicElementRendered,
Instances<I>: BoundingBox,
{
let mut result_table = Instances::default();
@ -1090,7 +1081,7 @@ async fn solidify_stroke(_: impl Ctx, vector_data: VectorDataTable) -> VectorDat
#[node_macro::node(category("Vector"), path(graphene_core::vector))]
async fn flatten_path<I: 'n + Send>(_: impl Ctx, #[implementations(GraphicGroupTable, VectorDataTable)] graphic_group_input: Instances<I>) -> VectorDataTable
where
Instances<I>: GraphicElementRendered,
GraphicElement: From<Instances<I>>,
{
// A node based solution to support passing through vector data could be a network node with a cache node connected to
// a Flatten Path connected to an if else node, another connection from the cache directly
@ -1135,7 +1126,7 @@ where
};
// Flatten the graphic group input into the output VectorData instance
let base_graphic_group = GraphicGroupTable::new(graphic_group_input.to_graphic_element());
let base_graphic_group = GraphicGroupTable::new(GraphicElement::from(graphic_group_input));
flatten_group(&base_graphic_group, &mut output);
// Return the single-row VectorDataTable containing the flattened VectorData subpaths

View file

@ -18,6 +18,7 @@ dyn-any = { workspace = true }
graphene-core = { workspace = true }
graphene-path-bool = { workspace = true }
graphene-application-io = { workspace = true }
graphene-svg-renderer = { workspace = true }
# Workspace dependencies
log = { workspace = true }

View file

@ -8,11 +8,11 @@ use graphene_application_io::SurfaceFrame;
use graphene_core::raster::brush_cache::BrushCache;
use graphene_core::raster::{BlendMode, LuminanceCalculation};
use graphene_core::raster_types::CPU;
use graphene_core::renderer::RenderMetadata;
use graphene_core::transform::ReferencePoint;
use graphene_core::uuid::NodeId;
use graphene_core::vector::style::Fill;
use graphene_core::{Color, MemoHash, Node, Type};
use graphene_svg_renderer::RenderMetadata;
use std::fmt::Display;
use std::hash::Hash;
use std::marker::PhantomData;

View file

@ -18,7 +18,7 @@ wasm = [
"image/png",
]
image-compare = []
vello = ["dep:vello", "gpu", "graphene-core/vello"]
vello = ["dep:vello", "gpu"]
resvg = []
wayland = ["graph-craft/wayland"]
@ -29,6 +29,7 @@ graph-craft = { workspace = true }
wgpu-executor = { workspace = true }
graphene-core = { workspace = true }
graphene-path-bool = { workspace = true }
graphene-svg-renderer = { workspace = true }
graphene-application-io = { workspace = true }
# Workspace dependencies

View file

@ -2,6 +2,7 @@ use crate::raster::{empty_image, extend_image_to_bounds};
use glam::{DAffine2, DVec2};
use graph_craft::generic::FnNode;
use graph_craft::proto::FutureWrapperNode;
use graphene_core::bounds::BoundingBox;
use graphene_core::instances::Instance;
use graphene_core::math::bbox::{AxisAlignedBbox, Bbox};
use graphene_core::raster::adjustments::blend_colors;
@ -9,7 +10,6 @@ use graphene_core::raster::brush_cache::BrushCache;
use graphene_core::raster::image::Image;
use graphene_core::raster::{Alpha, BitmapMut, BlendMode, Color, Pixel, Sample};
use graphene_core::raster_types::{CPU, Raster, RasterDataTable};
use graphene_core::renderer::GraphicElementRendered;
use graphene_core::transform::Transform;
use graphene_core::value::ClonedNode;
use graphene_core::vector::brush_stroke::{BrushStroke, BrushStyle};

View file

@ -13,3 +13,10 @@ pub use graphene_application_io as application_io;
pub use graphene_core::vector;
pub use graphene_core::*;
pub use graphene_path_bool as path_bool;
/// stop gap solution until all `Quad` and `Rect` paths have been replaced with their absolute ones
pub mod renderer {
pub use graphene_core::math::quad::Quad;
pub use graphene_core::math::rect::Rect;
pub use graphene_svg_renderer::*;
}

View file

@ -8,11 +8,11 @@ use graphene_core::instances::Instances;
use graphene_core::math::bbox::Bbox;
use graphene_core::raster::image::Image;
use graphene_core::raster_types::{CPU, Raster, RasterDataTable};
use graphene_core::renderer::RenderMetadata;
use graphene_core::renderer::{GraphicElementRendered, RenderParams, RenderSvgSegmentList, SvgRender, format_transform_matrix};
use graphene_core::transform::Footprint;
use graphene_core::vector::VectorDataTable;
use graphene_core::{Color, Context, Ctx, ExtractFootprint, GraphicGroupTable, OwnedContextImpl, WasmNotSend};
use graphene_svg_renderer::RenderMetadata;
use graphene_svg_renderer::{GraphicElementRendered, RenderParams, RenderSvgSegmentList, SvgRender, format_transform_matrix};
#[cfg(target_arch = "wasm32")]
use base64::Engine;

View file

@ -0,0 +1,27 @@
[package]
name = "graphene-svg-renderer"
version = "0.1.0"
edition = "2024"
description = "graphene svg renderer"
authors = ["Graphite Authors <contact@graphite.rs>"]
license = "MIT OR Apache-2.0"
[features]
vello = ["dep:vello", "bezier-rs/kurbo"]
[dependencies]
# Local dependencies
dyn-any = { workspace = true }
graphene-core = { workspace = true }
bezier-rs = { workspace = true }
# Workspace dependencies
glam = { workspace = true }
serde = { workspace = true }
base64 = { workspace = true }
log = { workspace = true }
num-traits = { workspace = true }
usvg = { workspace = true }
# Optional workspace dependencies
vello = { workspace = true, optional = true }

View file

@ -1,6 +1,6 @@
use crate::vector::PointId;
use bezier_rs::{ManipulatorGroup, Subpath};
use glam::DVec2;
use graphene_core::vector::PointId;
pub fn convert_usvg_path(path: &usvg::Path) -> Vec<Subpath<PointId>> {
let mut subpaths = Vec::new();

View file

@ -0,0 +1,6 @@
pub mod convert_usvg_path;
pub mod render_ext;
mod renderer;
pub mod to_peniko;
pub use renderer::*;

View file

@ -0,0 +1,278 @@
use crate::renderer::{RenderParams, format_transform_matrix};
use glam::{DAffine2, DVec2};
use graphene_core::consts::{LAYER_OUTLINE_STROKE_COLOR, LAYER_OUTLINE_STROKE_WEIGHT};
use graphene_core::gradient::{Gradient, GradientType};
use graphene_core::uuid::generate_uuid;
use graphene_core::vector::style::{Fill, PaintOrder, PathStyle, Stroke, StrokeAlign, StrokeCap, StrokeJoin, ViewMode};
use std::fmt::Write;
pub trait RenderExt {
type Output;
fn render(
&self,
svg_defs: &mut String,
element_transform: DAffine2,
stroke_transform: DAffine2,
bounds: [DVec2; 2],
transformed_bounds: [DVec2; 2],
aligned_strokes: bool,
override_paint_order: bool,
render_params: &RenderParams,
) -> Self::Output;
}
impl RenderExt for Gradient {
type Output = u64;
// /// Adds the gradient def through mutating the first argument, returning the gradient ID.
fn render(
&self,
svg_defs: &mut String,
element_transform: DAffine2,
stroke_transform: DAffine2,
bounds: [DVec2; 2],
transformed_bounds: [DVec2; 2],
_aligned_strokes: bool,
_override_paint_order: bool,
_render_params: &RenderParams,
) -> Self::Output {
// TODO: Figure out how to use `self.transform` as part of the gradient transform, since that field (`Gradient::transform`) is currently never read from, it's only written to.
let bound_transform = DAffine2::from_scale_angle_translation(bounds[1] - bounds[0], 0., bounds[0]);
let transformed_bound_transform = element_transform * DAffine2::from_scale_angle_translation(transformed_bounds[1] - transformed_bounds[0], 0., transformed_bounds[0]);
let mut stop = String::new();
for (position, color) in self.stops.0.iter() {
stop.push_str("<stop");
if *position != 0. {
let _ = write!(stop, r#" offset="{}""#, (position * 1_000_000.).round() / 1_000_000.);
}
let _ = write!(stop, r##" stop-color="#{}""##, color.to_rgb_hex_srgb_from_gamma());
if color.a() < 1. {
let _ = write!(stop, r#" stop-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
}
stop.push_str(" />")
}
let mod_gradient = if transformed_bound_transform.matrix2.determinant() != 0. {
transformed_bound_transform.inverse()
} else {
DAffine2::IDENTITY // Ignore if the transform cannot be inverted (the bounds are zero). See issue #1944.
};
let mod_points = element_transform * stroke_transform * bound_transform;
let start = mod_points.transform_point2(self.start);
let end = mod_points.transform_point2(self.end);
let gradient_id = generate_uuid();
let matrix = format_transform_matrix(mod_gradient);
let gradient_transform = if matrix.is_empty() { String::new() } else { format!(r#" gradientTransform="{}""#, matrix) };
match self.gradient_type {
GradientType::Linear => {
let _ = write!(
svg_defs,
r#"<linearGradient id="{}" x1="{}" x2="{}" y1="{}" y2="{}"{gradient_transform}>{}</linearGradient>"#,
gradient_id, start.x, end.x, start.y, end.y, stop
);
}
GradientType::Radial => {
let radius = (f64::powi(start.x - end.x, 2) + f64::powi(start.y - end.y, 2)).sqrt();
let _ = write!(
svg_defs,
r#"<radialGradient id="{}" cx="{}" cy="{}" r="{}"{gradient_transform}>{}</radialGradient>"#,
gradient_id, start.x, start.y, radius, stop
);
}
}
gradient_id
}
}
impl RenderExt for Fill {
type Output = String;
/// Renders the fill, adding necessary defs through mutating the first argument.
fn render(
&self,
svg_defs: &mut String,
element_transform: DAffine2,
stroke_transform: DAffine2,
bounds: [DVec2; 2],
transformed_bounds: [DVec2; 2],
aligned_strokes: bool,
override_paint_order: bool,
render_params: &RenderParams,
) -> Self::Output {
match self {
Self::None => r#" fill="none""#.to_string(),
Self::Solid(color) => {
let mut result = format!(r##" fill="#{}""##, color.to_rgb_hex_srgb_from_gamma());
if color.a() < 1. {
let _ = write!(result, r#" fill-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
}
result
}
Self::Gradient(gradient) => {
let gradient_id = gradient.render(
svg_defs,
element_transform,
stroke_transform,
bounds,
transformed_bounds,
aligned_strokes,
override_paint_order,
render_params,
);
format!(r##" fill="url('#{gradient_id}')""##)
}
}
}
}
impl RenderExt for Stroke {
type Output = String;
/// Provide the SVG attributes for the stroke.
fn render(
&self,
_svg_defs: &mut String,
_element_transform: DAffine2,
_stroke_transform: DAffine2,
_bounds: [DVec2; 2],
_transformed_bounds: [DVec2; 2],
aligned_strokes: bool,
override_paint_order: bool,
_render_params: &RenderParams,
) -> Self::Output {
// Don't render a stroke at all if it would be invisible
let Some(color) = self.color else { return String::new() };
if !self.has_renderable_stroke() {
return String::new();
}
// Set to None if the value is the SVG default
let weight = (self.weight != 1.).then_some(self.weight);
let dash_array = (!self.dash_lengths.is_empty()).then_some(self.dash_lengths());
let dash_offset = (self.dash_offset != 0.).then_some(self.dash_offset);
let stroke_cap = (self.cap != StrokeCap::Butt).then_some(self.cap);
let stroke_join = (self.join != StrokeJoin::Miter).then_some(self.join);
let stroke_join_miter_limit = (self.join_miter_limit != 4.).then_some(self.join_miter_limit);
let stroke_align = (self.align != StrokeAlign::Center).then_some(self.align);
let paint_order = (self.paint_order != PaintOrder::StrokeAbove || override_paint_order).then_some(PaintOrder::StrokeBelow);
// Render the needed stroke attributes
let mut attributes = format!(r##" stroke="#{}""##, color.to_rgb_hex_srgb_from_gamma());
if color.a() < 1. {
let _ = write!(&mut attributes, r#" stroke-opacity="{}""#, (color.a() * 1000.).round() / 1000.);
}
if let Some(mut weight) = weight {
if stroke_align.is_some() && aligned_strokes {
weight *= 2.;
}
let _ = write!(&mut attributes, r#" stroke-width="{}""#, weight);
}
if let Some(dash_array) = dash_array {
let _ = write!(&mut attributes, r#" stroke-dasharray="{}""#, dash_array);
}
if let Some(dash_offset) = dash_offset {
let _ = write!(&mut attributes, r#" stroke-dashoffset="{}""#, dash_offset);
}
if let Some(stroke_cap) = stroke_cap {
let _ = write!(&mut attributes, r#" stroke-linecap="{}""#, stroke_cap.svg_name());
}
if let Some(stroke_join) = stroke_join {
let _ = write!(&mut attributes, r#" stroke-linejoin="{}""#, stroke_join.svg_name());
}
if let Some(stroke_join_miter_limit) = stroke_join_miter_limit {
let _ = write!(&mut attributes, r#" stroke-miterlimit="{}""#, stroke_join_miter_limit);
}
// Add vector-effect attribute to make strokes non-scaling
if self.non_scaling {
let _ = write!(&mut attributes, r#" vector-effect="non-scaling-stroke""#);
}
if paint_order.is_some() {
let _ = write!(&mut attributes, r#" style="paint-order: stroke;" "#);
}
attributes
}
}
impl RenderExt for PathStyle {
type Output = String;
/// Renders the shape's fill and stroke attributes as a string with them concatenated together.
#[allow(clippy::too_many_arguments)]
fn render(
&self,
svg_defs: &mut String,
element_transform: DAffine2,
stroke_transform: DAffine2,
bounds: [DVec2; 2],
transformed_bounds: [DVec2; 2],
aligned_strokes: bool,
override_paint_order: bool,
render_params: &RenderParams,
) -> String {
let view_mode = render_params.view_mode;
match view_mode {
ViewMode::Outline => {
let fill_attribute = Fill::None.render(
svg_defs,
element_transform,
stroke_transform,
bounds,
transformed_bounds,
aligned_strokes,
override_paint_order,
render_params,
);
let mut outline_stroke = Stroke::new(Some(LAYER_OUTLINE_STROKE_COLOR), LAYER_OUTLINE_STROKE_WEIGHT);
// Outline strokes should be non-scaling by default
outline_stroke.non_scaling = true;
let stroke_attribute = outline_stroke.render(
svg_defs,
element_transform,
stroke_transform,
bounds,
transformed_bounds,
aligned_strokes,
override_paint_order,
render_params,
);
format!("{fill_attribute}{stroke_attribute}")
}
_ => {
let fill_attribute = self.fill.render(
svg_defs,
element_transform,
stroke_transform,
bounds,
transformed_bounds,
aligned_strokes,
override_paint_order,
render_params,
);
let stroke_attribute = self
.stroke
.as_ref()
.map(|stroke| {
stroke.render(
svg_defs,
element_transform,
stroke_transform,
bounds,
transformed_bounds,
aligned_strokes,
override_paint_order,
render_params,
)
})
.unwrap_or_default();
format!("{fill_attribute}{stroke_attribute}")
}
}
}
}

View file

@ -1,19 +1,21 @@
pub mod convert_usvg_path;
use crate::instances::Instance;
pub use crate::math::quad::Quad;
pub use crate::math::rect::Rect;
use crate::raster::{BlendMode, Image};
use crate::raster_types::{CPU, GPU, RasterDataTable};
use crate::transform::{Footprint, Transform};
use crate::uuid::{NodeId, generate_uuid};
use crate::vector::VectorDataTable;
use crate::vector::click_target::{ClickTarget, FreePoint};
use crate::vector::style::{Fill, Stroke, StrokeAlign, ViewMode};
use crate::{Artboard, ArtboardGroupTable, Color, GraphicElement, GraphicGroupTable};
use crate::render_ext::RenderExt;
use crate::to_peniko::BlendModeExt;
use bezier_rs::Subpath;
use dyn_any::DynAny;
use glam::{DAffine2, DVec2};
use graphene_core::blending::BlendMode;
use graphene_core::bounds::BoundingBox;
use graphene_core::color::Color;
use graphene_core::instances::Instance;
use graphene_core::math::quad::Quad;
use graphene_core::raster::Image;
use graphene_core::raster_types::{CPU, GPU, RasterDataTable};
use graphene_core::transform::{Footprint, Transform};
use graphene_core::uuid::{NodeId, generate_uuid};
use graphene_core::vector::VectorDataTable;
use graphene_core::vector::click_target::{ClickTarget, FreePoint};
use graphene_core::vector::style::{Fill, Stroke, StrokeAlign, ViewMode};
use graphene_core::{AlphaBlending, Artboard, ArtboardGroupTable, GraphicElement, GraphicGroupTable};
use num_traits::Zero;
use std::collections::{HashMap, HashSet};
use std::fmt::Write;
@ -201,12 +203,11 @@ pub struct RenderMetadata {
}
// TODO: Rename to "Graphical"
pub trait GraphicElementRendered {
pub trait GraphicElementRendered: BoundingBox {
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams);
#[cfg(feature = "vello")]
fn render_to_vello(&self, scene: &mut Scene, transform: DAffine2, context: &mut RenderContext, _render_params: &RenderParams);
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]>;
/// The upstream click targets for each layer are collected during the render so that they do not have to be calculated for each click detection.
fn add_upstream_click_targets(&self, _click_targets: &mut Vec<ClickTarget>) {}
@ -222,10 +223,6 @@ pub trait GraphicElementRendered {
}
fn new_ids_from_hash(&mut self, _reference: Option<NodeId>) {}
fn to_graphic_element(&self) -> GraphicElement {
GraphicElement::default()
}
}
impl GraphicElementRendered for GraphicGroupTable {
@ -299,7 +296,7 @@ impl GraphicElementRendered for GraphicGroupTable {
if let Some(bounds) = bounds {
let blend_mode = match render_params.view_mode {
ViewMode::Outline => peniko::Mix::Normal,
_ => alpha_blending.blend_mode.into(),
_ => alpha_blending.blend_mode.to_peniko(),
};
let factor = if render_params.for_mask { 1. } else { alpha_blending.fill };
@ -326,7 +323,7 @@ impl GraphicElementRendered for GraphicGroupTable {
}
if let Some(bounds) = bounds {
let rect = vello::kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y);
let rect = kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y);
scene.push_layer(peniko::Mix::Normal, 1., kurbo::Affine::IDENTITY, &rect);
instance_mask.render_to_vello(scene, transform_mask, context, &render_params.for_clipper());
@ -349,12 +346,6 @@ impl GraphicElementRendered for GraphicGroupTable {
}
}
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.filter_map(|element| element.instance.bounding_box(transform * *element.transform, include_stroke))
.reduce(Quad::combine_bounds)
}
fn collect_metadata(&self, metadata: &mut RenderMetadata, footprint: Footprint, element_id: Option<NodeId>) {
for instance in self.instance_ref_iter() {
if let Some(element_id) = instance.source_node_id {
@ -406,10 +397,6 @@ impl GraphicElementRendered for GraphicGroupTable {
instance.instance.new_ids_from_hash(*instance.source_node_id);
}
}
fn to_graphic_element(&self) -> GraphicElement {
GraphicElement::GraphicGroup(self.clone())
}
}
impl GraphicElementRendered for VectorDataTable {
@ -519,8 +506,8 @@ impl GraphicElementRendered for VectorDataTable {
#[cfg(feature = "vello")]
fn render_to_vello(&self, scene: &mut Scene, parent_transform: DAffine2, _context: &mut RenderContext, render_params: &RenderParams) {
use crate::consts::{LAYER_OUTLINE_STROKE_COLOR, LAYER_OUTLINE_STROKE_WEIGHT};
use crate::vector::style::{GradientType, StrokeCap, StrokeJoin};
use graphene_core::consts::{LAYER_OUTLINE_STROKE_COLOR, LAYER_OUTLINE_STROKE_WEIGHT};
use graphene_core::vector::style::{GradientType, StrokeCap, StrokeJoin};
use vello::kurbo::{Cap, Join};
use vello::peniko;
@ -543,7 +530,7 @@ impl GraphicElementRendered for VectorDataTable {
// If we're using opacity or a blend mode, we need to push a layer
let blend_mode = match render_params.view_mode {
ViewMode::Outline => peniko::Mix::Normal,
_ => instance.alpha_blending.blend_mode.into(),
_ => instance.alpha_blending.blend_mode.to_peniko(),
};
let mut layer = false;
let factor = if render_params.for_mask { 1. } else { instance.alpha_blending.fill };
@ -582,7 +569,7 @@ impl GraphicElementRendered for VectorDataTable {
let weight = instance.instance.style.stroke().unwrap().weight;
let quad = Quad::from_box(layer_bounds).inflate(weight * element_transform.matrix2.determinant());
let rect = vello::kurbo::Rect::new(quad.top_left().x, quad.top_left().y, quad.bottom_right().x, quad.bottom_right().y);
let rect = kurbo::Rect::new(quad.top_left().x, quad.top_left().y, quad.bottom_right().x, quad.bottom_right().y);
let inside = instance.instance.style.stroke().unwrap().align == StrokeAlign::Inside;
let compose = if inside { peniko::Compose::SrcIn } else { peniko::Compose::SrcOut };
@ -725,27 +712,6 @@ impl GraphicElementRendered for VectorDataTable {
}
}
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.flat_map(|instance| {
if !include_stroke {
return instance.instance.bounding_box_with_transform(transform * *instance.transform);
}
let stroke_width = instance.instance.style.stroke().map(|s| s.weight()).unwrap_or_default();
let miter_limit = instance.instance.style.stroke().map(|s| s.join_miter_limit).unwrap_or(1.);
let scale = transform.decompose_scale();
// We use the full line width here to account for different styles of stroke caps
let offset = DVec2::splat(stroke_width * scale.x.max(scale.y) * miter_limit);
instance.instance.bounding_box_with_transform(transform * *instance.transform).map(|[a, b]| [a - offset, b + offset])
})
.reduce(Quad::combine_bounds)
}
fn collect_metadata(&self, metadata: &mut RenderMetadata, mut footprint: Footprint, element_id: Option<NodeId>) {
for instance in self.instance_ref_iter() {
let instance_transform = *instance.transform;
@ -828,10 +794,6 @@ impl GraphicElementRendered for VectorDataTable {
instance.instance.vector_new_ids_from_hash(reference.map(|id| id.0).unwrap_or_default());
}
}
fn to_graphic_element(&self) -> GraphicElement {
GraphicElement::VectorData(self.clone())
}
}
impl GraphicElementRendered for Artboard {
@ -906,18 +868,6 @@ impl GraphicElementRendered for Artboard {
}
}
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
let artboard_bounds = (transform * Quad::from_box([self.location.as_dvec2(), self.location.as_dvec2() + self.dimensions.as_dvec2()])).bounding_box();
if self.clip {
Some(artboard_bounds)
} else {
[self.graphic_group.bounding_box(transform, include_stroke), Some(artboard_bounds)]
.into_iter()
.flatten()
.reduce(Quad::combine_bounds)
}
}
fn collect_metadata(&self, metadata: &mut RenderMetadata, mut footprint: Footprint, element_id: Option<NodeId>) {
if let Some(element_id) = element_id {
let subpath = Subpath::new_rect(DVec2::ZERO, self.dimensions.as_dvec2());
@ -956,12 +906,6 @@ impl GraphicElementRendered for ArtboardGroupTable {
}
}
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.filter_map(|instance| instance.instance.bounding_box(transform, include_stroke))
.reduce(Quad::combine_bounds)
}
fn collect_metadata(&self, metadata: &mut RenderMetadata, footprint: Footprint, _element_id: Option<NodeId>) {
for instance in self.instance_ref_iter() {
instance.instance.collect_metadata(metadata, footprint, *instance.source_node_id);
@ -1036,15 +980,6 @@ impl GraphicElementRendered for RasterDataTable<CPU> {
}
}
fn bounding_box(&self, transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.flat_map(|instance| {
let transform = transform * *instance.transform;
(transform.matrix2.determinant() != 0.).then(|| (transform * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box())
})
.reduce(Quad::combine_bounds)
}
fn collect_metadata(&self, metadata: &mut RenderMetadata, footprint: Footprint, element_id: Option<NodeId>) {
let Some(element_id) = element_id else { return };
let subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
@ -1061,10 +996,6 @@ impl GraphicElementRendered for RasterDataTable<CPU> {
let subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
click_targets.push(ClickTarget::new_with_subpath(subpath, 0.));
}
fn to_graphic_element(&self) -> GraphicElement {
GraphicElement::RasterDataCPU(self.clone())
}
}
impl GraphicElementRendered for RasterDataTable<GPU> {
@ -1076,25 +1007,25 @@ impl GraphicElementRendered for RasterDataTable<GPU> {
fn render_to_vello(&self, scene: &mut Scene, transform: DAffine2, context: &mut RenderContext, _render_params: &RenderParams) {
use vello::peniko;
let mut render_stuff = |image: vello::peniko::Image, instance_transform: DAffine2, blend_mode: crate::AlphaBlending| {
let mut render_stuff = |image: peniko::Image, instance_transform: DAffine2, blend_mode: AlphaBlending| {
let image_transform = transform * instance_transform * DAffine2::from_scale(1. / DVec2::new(image.width as f64, image.height as f64));
let layer = blend_mode != Default::default();
let Some(bounds) = self.bounding_box(transform, true) else { return };
let blending = vello::peniko::BlendMode::new(blend_mode.blend_mode.into(), vello::peniko::Compose::SrcOver);
let blending = peniko::BlendMode::new(blend_mode.blend_mode.to_peniko(), peniko::Compose::SrcOver);
if layer {
let rect = vello::kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y);
let rect = kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y);
scene.push_layer(blending, blend_mode.opacity, kurbo::Affine::IDENTITY, &rect);
}
scene.draw_image(&image, vello::kurbo::Affine::new(image_transform.to_cols_array()));
scene.draw_image(&image, kurbo::Affine::new(image_transform.to_cols_array()));
if layer {
scene.pop_layer()
}
};
for instance in self.instance_ref_iter() {
let image = vello::peniko::Image::new(vec![].into(), peniko::Format::Rgba8, instance.instance.data().width(), instance.instance.data().height()).with_extend(peniko::Extend::Repeat);
let image = peniko::Image::new(vec![].into(), peniko::Format::Rgba8, instance.instance.data().width(), instance.instance.data().height()).with_extend(peniko::Extend::Repeat);
let id = image.data.id();
context.resource_overrides.insert(id, instance.instance.data_owned());
@ -1103,15 +1034,6 @@ impl GraphicElementRendered for RasterDataTable<GPU> {
}
}
fn bounding_box(&self, transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> {
self.instance_ref_iter()
.flat_map(|instance| {
let transform = transform * *instance.transform;
(transform.matrix2.determinant() != 0.).then(|| (transform * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box())
})
.reduce(Quad::combine_bounds)
}
fn collect_metadata(&self, metadata: &mut RenderMetadata, footprint: Footprint, element_id: Option<NodeId>) {
let Some(element_id) = element_id else { return };
let subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
@ -1150,15 +1072,6 @@ impl GraphicElementRendered for GraphicElement {
}
}
fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> {
match self {
GraphicElement::VectorData(vector_data) => vector_data.bounding_box(transform, include_stroke),
GraphicElement::RasterDataCPU(raster) => raster.bounding_box(transform, include_stroke),
GraphicElement::RasterDataGPU(raster) => raster.bounding_box(transform, include_stroke),
GraphicElement::GraphicGroup(graphic_group) => graphic_group.bounding_box(transform, include_stroke),
}
}
fn collect_metadata(&self, metadata: &mut RenderMetadata, footprint: Footprint, element_id: Option<NodeId>) {
if let Some(element_id) = element_id {
match self {
@ -1228,7 +1141,7 @@ impl GraphicElementRendered for GraphicElement {
}
/// Used to stop rust complaining about upstream traits adding display implementations to `Option<Color>`. This would not be an issue as we control that crate.
trait Primitive: std::fmt::Display {}
trait Primitive: std::fmt::Display + BoundingBox {}
impl Primitive for String {}
impl Primitive for bool {}
impl Primitive for f32 {}
@ -1246,10 +1159,6 @@ impl<P: Primitive> GraphicElementRendered for P {
render.parent_tag("text", text_attributes, |render| render.leaf_node(format!("{self}")));
}
fn bounding_box(&self, _transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> {
None
}
#[cfg(feature = "vello")]
fn render_to_vello(&self, _scene: &mut Scene, _transform: DAffine2, _context: &mut RenderContext, _render_params: &RenderParams) {}
}
@ -1274,10 +1183,6 @@ impl GraphicElementRendered for Option<Color> {
render.parent_tag("text", text_attributes, |render| render.leaf_node(color_info))
}
fn bounding_box(&self, _transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> {
None
}
#[cfg(feature = "vello")]
fn render_to_vello(&self, _scene: &mut Scene, _transform: DAffine2, _context: &mut RenderContext, _render_params: &RenderParams) {}
}
@ -1298,10 +1203,6 @@ impl GraphicElementRendered for Vec<Color> {
}
}
fn bounding_box(&self, _transform: DAffine2, _include_stroke: bool) -> Option<[DVec2; 2]> {
None
}
#[cfg(feature = "vello")]
fn render_to_vello(&self, _scene: &mut Scene, _transform: DAffine2, _context: &mut RenderContext, _render_params: &RenderParams) {}
}

View file

@ -0,0 +1,38 @@
use graphene_core::BlendMode;
use vello::peniko;
#[cfg(feature = "vello")]
pub trait BlendModeExt {
fn to_peniko(&self) -> peniko::Mix;
}
#[cfg(feature = "vello")]
impl BlendModeExt for BlendMode {
fn to_peniko(&self) -> peniko::Mix {
match self {
// Normal group
BlendMode::Normal => peniko::Mix::Normal,
// Darken group
BlendMode::Darken => peniko::Mix::Darken,
BlendMode::Multiply => peniko::Mix::Multiply,
BlendMode::ColorBurn => peniko::Mix::ColorBurn,
// Lighten group
BlendMode::Lighten => peniko::Mix::Lighten,
BlendMode::Screen => peniko::Mix::Screen,
BlendMode::ColorDodge => peniko::Mix::ColorDodge,
// Contrast group
BlendMode::Overlay => peniko::Mix::Overlay,
BlendMode::SoftLight => peniko::Mix::SoftLight,
BlendMode::HardLight => peniko::Mix::HardLight,
// Inversion group
BlendMode::Difference => peniko::Mix::Difference,
BlendMode::Exclusion => peniko::Mix::Exclusion,
// Component group
BlendMode::Hue => peniko::Mix::Hue,
BlendMode::Saturation => peniko::Mix::Saturation,
BlendMode::Color => peniko::Mix::Color,
BlendMode::Luminosity => peniko::Mix::Luminosity,
_ => todo!(),
}
}
}

View file

@ -11,8 +11,9 @@ passthrough = []
[dependencies]
# Local dependencies
graphene-core = { workspace = true, features = ["wgpu", "vello"] }
graphene-core = { workspace = true, features = ["wgpu"] }
graphene-application-io = { workspace = true, features = ["wgpu"] }
graphene-svg-renderer = { workspace = true, features = ["vello"] }
dyn-any = { workspace = true }
node-macro = { workspace = true }

View file

@ -6,6 +6,7 @@ use dyn_any::StaticType;
use glam::UVec2;
use graphene_application_io::{ApplicationIo, EditorApi, SurfaceHandle};
use graphene_core::{Color, Ctx};
pub use graphene_svg_renderer::RenderContext;
use std::sync::Arc;
use vello::{AaConfig, AaSupport, RenderParams, Renderer, RendererOptions, Scene};
use wgpu::{Origin3d, SurfaceConfiguration, TextureAspect};
@ -50,8 +51,6 @@ unsafe impl StaticType for Surface {
type Static = Surface;
}
pub use graphene_core::renderer::RenderContext;
impl WgpuExecutor {
pub async fn render_vello_scene(&self, scene: &Scene, surface: &WgpuSurface, width: u32, height: u32, context: &RenderContext, background: Color) -> Result<()> {
let surface = &surface.surface.inner;