Skip to content

Implement clipping masks, stroke align, and stroke paint order #2644

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

Merged
merged 47 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
743b8fc
refactor: opacity + blend_mode -> blend_style
mTvare6 May 13, 2025
af72397
Add code for clipping
mTvare6 May 13, 2025
df33d70
Add alt-click masking
mTvare6 May 16, 2025
a9b53bb
Clip to all colors. Fill option
mTvare6 May 16, 2025
15d00f5
Fix undo not working. Fix strokes not being white
mTvare6 May 16, 2025
e90fd26
Allow clipped to be grouped or raster
mTvare6 May 16, 2025
f7e946e
Switch to alpha mode in mask-type
mTvare6 May 16, 2025
79cc9aa
add plumbing to know if clipped in frontend and add fill slider
mTvare6 May 19, 2025
39cb75e
Merge branch 'master' into clipping
Keavon May 20, 2025
a680c7f
Attempt at document upgrade code
mTvare6 May 20, 2025
d9e4079
Merge branch 'master' into foo
Keavon May 21, 2025
209a69d
Fix fill slider
Keavon May 21, 2025
9f2e436
Add clipped styling and Alt-click layer border
Keavon May 21, 2025
bf132e5
Use mask attr judiciously by using clip when possible
mTvare6 May 22, 2025
ced2788
Fix breaking documents and upgrade code
mTvare6 May 23, 2025
2c9c299
Fix fixes
mTvare6 May 23, 2025
51d1575
Merge branch 'master' into clipping
mTvare6 May 23, 2025
4d211d8
No-op toggle if last child of parent and don't show clip UI if last e…
mTvare6 May 23, 2025
c26e6dd
Fix mouse styles by plumbing clippable to frontend
mTvare6 May 23, 2025
86ddcae
Fix Clip detection by disallowed groups as clipPath according to SVG …
mTvare6 May 24, 2025
4de5bab
Add opacity to clippers can_use_clip check
mTvare6 May 24, 2025
4727a11
Fix issue with clipping not working nicely with strokes by using masks
mTvare6 May 25, 2025
72bef1f
Add vello code
mTvare6 May 27, 2025
1ea4cd1
cleanup
mTvare6 May 28, 2025
6a5f783
Add stroke alignment hacks to SVG renderer
mTvare6 May 29, 2025
7202523
svg: Fix mask bounds in vector data
mTvare6 May 29, 2025
21eb4c1
vello: Implement mask hacks to support stroke alignment
mTvare6 May 30, 2025
738ad48
Move around alignment and doc upgrade code
mTvare6 May 30, 2025
8da395c
rename Line X -> X
mTvare6 May 30, 2025
f28ced1
An attempt at fixing names not updating
mTvare6 May 30, 2025
6a8e59f
svg: add stroke order with svg
mTvare6 May 30, 2025
3a7a8af
vello: add stroke order with by calling one before the other explicitly
mTvare6 May 30, 2025
460bd36
Merge branch 'master' into clipping
mTvare6 May 31, 2025
44345c6
fix merge
mTvare6 May 31, 2025
e124de0
fix svg renderer messing up transform det
mTvare6 Jun 4, 2025
89c88d0
Merge branch 'master' into clipping
Keavon Jun 7, 2025
d426e45
Merge branch 'master' into clipping
Keavon Jun 9, 2025
5abbc5e
Code review; reorder and rename parameters (TODO: fix tools)
Keavon Jun 9, 2025
f3e3ead
Fixes to previous
Keavon Jun 9, 2025
e2dad94
Formatting
Keavon Jun 9, 2025
2743982
fix bug 3
mTvare6 Jun 10, 2025
2dd9165
some moving around (not fixed)
mTvare6 Jun 11, 2025
4567b37
fix issue 1
mTvare6 Jun 12, 2025
f2b91fb
Merge branch 'master' into clipping
mTvare6 Jun 12, 2025
77937db
fix vello
mTvare6 Jun 13, 2025
0349a8a
Merge branch 'master' into clipping
mTvare6 Jun 17, 2025
6f4e196
Final code review
Keavon Jun 18, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ impl PreferencesDialogMessageHandler {
];

let mut checkbox_id = CheckboxId::default();
let vector_mesh_tooltip = "Allow tools to produce vector meshes, where more than two segments can connect to an anchor point.\n\nCurrently this does not properly handle line joins and fills.";
let vector_mesh_tooltip =
"Allow tools to produce vector meshes, where more than two segments can connect to an anchor point.\n\nCurrently this does not properly handle stroke joins and fills.";
let vector_meshes = vec![
Separator::new(SeparatorType::Unrelated).widget_holder(),
Separator::new(SeparatorType::Unrelated).widget_holder(),
Expand Down
6 changes: 6 additions & 0 deletions editor/src/messages/portfolio/document/document_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ pub enum DocumentMessage {
SelectedLayersReorder {
relative_index_offset: isize,
},
ClipLayer {
id: NodeId,
},
SelectLayer {
id: NodeId,
ctrl: bool,
Expand All @@ -142,6 +145,9 @@ pub enum DocumentMessage {
SetOpacityForSelectedLayers {
opacity: f64,
},
SetFillForSelectedLayers {
fill: f64,
},
SetOverlaysVisibility {
visible: bool,
overlays_type: Option<OverlaysType>,
Expand Down
63 changes: 53 additions & 10 deletions editor/src/messages/portfolio/document/document_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::messages::portfolio::document::utility_types::network_interface::{Flo
use crate::messages::portfolio::document::utility_types::nodes::RawBuffer;
use crate::messages::portfolio::utility_types::PersistentData;
use crate::messages::prelude::*;
use crate::messages::tool::common_functionality::graph_modification_utils::{self, get_blend_mode, get_opacity};
use crate::messages::tool::common_functionality::graph_modification_utils::{self, get_blend_mode, get_fill, get_opacity};
use crate::messages::tool::tool_messages::select_tool::SelectToolPointerKeys;
use crate::messages::tool::tool_messages::tool_prelude::Key;
use crate::messages::tool::utility_types::ToolType;
Expand Down Expand Up @@ -1082,6 +1082,12 @@ impl MessageHandler<DocumentMessage, DocumentMessageData<'_>> for DocumentMessag
DocumentMessage::SelectedLayersReorder { relative_index_offset } => {
self.selected_layers_reorder(relative_index_offset, responses);
}
DocumentMessage::ClipLayer { id } => {
let layer = LayerNodeIdentifier::new(id, &self.network_interface, &[]);

responses.add(DocumentMessage::AddTransaction);
responses.add(GraphOperationMessage::ClipModeToggle { layer });
}
DocumentMessage::SelectLayer { id, ctrl, shift } => {
let layer = LayerNodeIdentifier::new(id, &self.network_interface, &[]);

Expand Down Expand Up @@ -1176,6 +1182,12 @@ impl MessageHandler<DocumentMessage, DocumentMessageData<'_>> for DocumentMessag
responses.add(GraphOperationMessage::OpacitySet { layer, opacity });
}
}
DocumentMessage::SetFillForSelectedLayers { fill } => {
let fill = fill.clamp(0., 1.);
for layer in self.network_interface.selected_nodes().selected_layers_except_artboards(&self.network_interface) {
responses.add(GraphOperationMessage::BlendingFillSet { layer, fill });
}
}
DocumentMessage::SetOverlaysVisibility { visible, overlays_type } => {
let visibility_settings = &mut self.overlays_visibility_settings;
let overlays_type = match overlays_type {
Expand Down Expand Up @@ -2532,38 +2544,47 @@ impl DocumentMessageHandler {
let selected_layers_except_artboards = selected_nodes.selected_layers_except_artboards(&self.network_interface);

// Look up the current opacity and blend mode of the selected layers (if any), and split the iterator into the first tuple and the rest.
let mut opacity_and_blend_mode = selected_layers_except_artboards.map(|layer| {
let mut blending_options = selected_layers_except_artboards.map(|layer| {
(
get_opacity(layer, &self.network_interface).unwrap_or(100.),
get_fill(layer, &self.network_interface).unwrap_or(100.),
get_blend_mode(layer, &self.network_interface).unwrap_or_default(),
)
});
let first_opacity_and_blend_mode = opacity_and_blend_mode.next();
let result_opacity_and_blend_mode = opacity_and_blend_mode;
let first_blending_options = blending_options.next();
let result_blending_options = blending_options;

// If there are no selected layers, disable the opacity and blend mode widgets.
let disabled = first_opacity_and_blend_mode.is_none();
let disabled = first_blending_options.is_none();

// Amongst the selected layers, check if the opacities and blend modes are identical across all layers.
// The result is setting `option` and `blend_mode` to Some value if all their values are identical, or None if they are not.
// If identical, we display the value in the widget. If not, we display a dash indicating dissimilarity.
let (opacity, blend_mode) = first_opacity_and_blend_mode
.map(|(first_opacity, first_blend_mode)| {
let (opacity, fill, blend_mode) = first_blending_options
.map(|(first_opacity, first_fill, first_blend_mode)| {
let mut opacity_identical = true;
let mut fill_identical = true;
let mut blend_mode_identical = true;

for (opacity, blend_mode) in result_opacity_and_blend_mode {
for (opacity, fill, blend_mode) in result_blending_options {
if (opacity - first_opacity).abs() > (f64::EPSILON * 100.) {
opacity_identical = false;
}
if (fill - first_fill).abs() > (f64::EPSILON * 100.) {
fill_identical = false;
}
if blend_mode != first_blend_mode {
blend_mode_identical = false;
}
}

(opacity_identical.then_some(first_opacity), blend_mode_identical.then_some(first_blend_mode))
(
opacity_identical.then_some(first_opacity),
fill_identical.then_some(first_fill),
blend_mode_identical.then_some(first_blend_mode),
)
})
.unwrap_or((None, None));
.unwrap_or((None, None, None));

let blend_mode_menu_entries = BlendMode::list_svg_subset()
.iter()
Expand Down Expand Up @@ -2622,6 +2643,28 @@ impl DocumentMessageHandler {
.max_width(100)
.tooltip("Opacity")
.widget_holder(),
Separator::new(SeparatorType::Related).widget_holder(),
NumberInput::new(fill)
.label("Fill")
.unit("%")
.display_decimal_places(0)
.disabled(disabled)
.min(0.)
.max(100.)
.range_min(Some(0.))
.range_max(Some(100.))
.mode_range()
.on_update(|number_input: &NumberInput| {
if let Some(value) = number_input.value {
DocumentMessage::SetFillForSelectedLayers { fill: value / 100. }.into()
} else {
Message::NoOp
}
})
.on_commit(|_| DocumentMessage::AddTransaction.into())
.max_width(100)
.tooltip("Fill")
.widget_holder(),
];
let layers_panel_control_bar_left = WidgetLayout::new(vec![LayoutGroup::Row { widgets }]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub enum GraphOperationMessage {
layer: LayerNodeIdentifier,
fill: Fill,
},
BlendingFillSet {
layer: LayerNodeIdentifier,
fill: f64,
},
OpacitySet {
layer: LayerNodeIdentifier,
opacity: f64,
Expand All @@ -29,6 +33,9 @@ pub enum GraphOperationMessage {
layer: LayerNodeIdentifier,
blend_mode: BlendMode,
},
ClipModeToggle {
layer: LayerNodeIdentifier,
},
StrokeSet {
layer: LayerNodeIdentifier,
stroke: Stroke,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use crate::messages::portfolio::document::utility_types::document_metadata::Laye
use crate::messages::portfolio::document::utility_types::network_interface::{InputConnector, NodeNetworkInterface, OutputConnector};
use crate::messages::portfolio::document::utility_types::nodes::CollapsedLayers;
use crate::messages::prelude::*;
use crate::messages::tool::common_functionality::graph_modification_utils::get_clip_mode;
use glam::{DAffine2, DVec2, IVec2};
use graph_craft::document::{NodeId, NodeInput};
use graphene_core::Color;
use graphene_core::renderer::Quad;
use graphene_core::text::{Font, TypesettingConfig};
use graphene_core::vector::style::{Fill, Gradient, GradientStops, GradientType, LineCap, LineJoin, Stroke};
use graphene_core::vector::style::{Fill, Gradient, GradientStops, GradientType, PaintOrder, Stroke, StrokeAlign, StrokeCap, StrokeJoin};
use graphene_std::vector::convert_usvg_path;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -41,6 +42,11 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
modify_inputs.fill_set(fill);
}
}
GraphOperationMessage::BlendingFillSet { layer, fill } => {
if let Some(mut modify_inputs) = ModifyInputsContext::new_with_layer(layer, network_interface, responses) {
modify_inputs.blending_fill_set(fill);
}
}
GraphOperationMessage::OpacitySet { layer, opacity } => {
if let Some(mut modify_inputs) = ModifyInputsContext::new_with_layer(layer, network_interface, responses) {
modify_inputs.opacity_set(opacity);
Expand All @@ -51,6 +57,12 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
modify_inputs.blend_mode_set(blend_mode);
}
}
GraphOperationMessage::ClipModeToggle { layer } => {
let clip_mode = get_clip_mode(layer, network_interface);
if let Some(mut modify_inputs) = ModifyInputsContext::new_with_layer(layer, network_interface, responses) {
modify_inputs.clip_mode_toggle(clip_mode);
}
}
GraphOperationMessage::StrokeSet { layer, stroke } => {
if let Some(mut modify_inputs) = ModifyInputsContext::new_with_layer(layer, network_interface, responses) {
modify_inputs.stroke_set(stroke);
Expand Down Expand Up @@ -376,18 +388,20 @@ fn apply_usvg_stroke(stroke: &usvg::Stroke, modify_inputs: &mut ModifyInputsCont
weight: stroke.width().get() as f64,
dash_lengths: stroke.dasharray().as_ref().map(|lengths| lengths.iter().map(|&length| length as f64).collect()).unwrap_or_default(),
dash_offset: stroke.dashoffset() as f64,
line_cap: match stroke.linecap() {
usvg::LineCap::Butt => LineCap::Butt,
usvg::LineCap::Round => LineCap::Round,
usvg::LineCap::Square => LineCap::Square,
cap: match stroke.linecap() {
usvg::LineCap::Butt => StrokeCap::Butt,
usvg::LineCap::Round => StrokeCap::Round,
usvg::LineCap::Square => StrokeCap::Square,
},
line_join: match stroke.linejoin() {
usvg::LineJoin::Miter => LineJoin::Miter,
usvg::LineJoin::MiterClip => LineJoin::Miter,
usvg::LineJoin::Round => LineJoin::Round,
usvg::LineJoin::Bevel => LineJoin::Bevel,
join: match stroke.linejoin() {
usvg::LineJoin::Miter => StrokeJoin::Miter,
usvg::LineJoin::MiterClip => StrokeJoin::Miter,
usvg::LineJoin::Round => StrokeJoin::Round,
usvg::LineJoin::Bevel => StrokeJoin::Bevel,
},
line_join_miter_limit: stroke.miterlimit().get() as f64,
join_miter_limit: stroke.miterlimit().get() as f64,
align: StrokeAlign::Center,
paint_order: PaintOrder::StrokeAbove,
transform,
non_scaling: false,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use graphene_core::vector::brush_stroke::BrushStroke;
use graphene_core::vector::style::{Fill, Stroke};
use graphene_core::vector::{PointId, VectorModificationType};
use graphene_core::{Artboard, Color};
use graphene_std::GraphicGroupTable;
use graphene_std::vector::{VectorData, VectorDataTable};
use graphene_std::{GraphicGroupTable, NodeInputDecleration};

#[derive(PartialEq, Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]
pub enum TransformIn {
Expand Down Expand Up @@ -58,13 +58,13 @@ impl<'a> ModifyInputsContext<'a> {
/// Non layer nodes directly upstream of a layer are treated as part of that layer. See insert_index == 2 in the diagram
/// -----> Post node
/// | if insert_index == 0, return (Post node, Some(Layer1))
/// -> Layer1
/// -> Layer1
/// ↑ if insert_index == 1, return (Layer1, Some(Layer2))
/// -> Layer2
/// -> Layer2
/// ↑
/// -> NonLayerNode
/// ↑ if insert_index == 2, return (NonLayerNode, Some(Layer3))
/// -> Layer3
/// -> Layer3
/// if insert_index == 3, return (Layer3, None)
pub fn get_post_node_with_index(network_interface: &NodeNetworkInterface, parent: LayerNodeIdentifier, insert_index: usize) -> InputConnector {
let mut post_node_input_connector = if parent == LayerNodeIdentifier::ROOT_PARENT {
Expand Down Expand Up @@ -333,37 +333,52 @@ impl<'a> ModifyInputsContext<'a> {
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Fill(fill), false), false);
}

pub fn blend_mode_set(&mut self, blend_mode: BlendMode) {
let Some(blend_node_id) = self.existing_node_id("Blending", true) else { return };
let input_connector = InputConnector::node(blend_node_id, 1);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::BlendMode(blend_mode), false), false);
}

pub fn opacity_set(&mut self, opacity: f64) {
let Some(opacity_node_id) = self.existing_node_id("Opacity", true) else { return };
let input_connector = InputConnector::node(opacity_node_id, 1);
let Some(blend_node_id) = self.existing_node_id("Blending", true) else { return };
let input_connector = InputConnector::node(blend_node_id, 2);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::F64(opacity * 100.), false), false);
}

pub fn blend_mode_set(&mut self, blend_mode: BlendMode) {
let Some(blend_mode_node_id) = self.existing_node_id("Blend Mode", true) else {
return;
};
let input_connector = InputConnector::node(blend_mode_node_id, 1);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::BlendMode(blend_mode), false), false);
pub fn blending_fill_set(&mut self, fill: f64) {
let Some(blend_node_id) = self.existing_node_id("Blending", true) else { return };
let input_connector = InputConnector::node(blend_node_id, 3);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::F64(fill * 100.), false), false);
}

pub fn clip_mode_toggle(&mut self, clip_mode: Option<bool>) {
let clip = !clip_mode.unwrap_or(false);
let Some(clip_node_id) = self.existing_node_id("Blending", true) else { return };
let input_connector = InputConnector::node(clip_node_id, 4);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::Bool(clip), false), false);
}

pub fn stroke_set(&mut self, stroke: Stroke) {
let Some(stroke_node_id) = self.existing_node_id("Stroke", true) else { return };

let input_connector = InputConnector::node(stroke_node_id, 1);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::ColorInput::<Option<Color>>::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::OptionalColor(stroke.color), false), true);
let input_connector = InputConnector::node(stroke_node_id, 2);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::WeightInput::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::F64(stroke.weight), false), true);
let input_connector = InputConnector::node(stroke_node_id, 3);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::AlignInput::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::StrokeAlign(stroke.align), false), false);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::CapInput::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::StrokeCap(stroke.cap), false), true);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::JoinInput::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::StrokeJoin(stroke.join), false), true);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::MiterLimitInput::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::F64(stroke.join_miter_limit), false), false);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::PaintOrderInput::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::PaintOrder(stroke.paint_order), false), false);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::DashLengthsInput::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::VecF64(stroke.dash_lengths), false), true);
let input_connector = InputConnector::node(stroke_node_id, 4);
let input_connector = InputConnector::node(stroke_node_id, graphene_std::vector::stroke::DashOffsetInput::INDEX);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::F64(stroke.dash_offset), false), true);
let input_connector = InputConnector::node(stroke_node_id, 5);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::LineCap(stroke.line_cap), false), true);
let input_connector = InputConnector::node(stroke_node_id, 6);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::LineJoin(stroke.line_join), false), true);
let input_connector = InputConnector::node(stroke_node_id, 7);
self.set_input_with_refresh(input_connector, NodeInput::value(TaggedValue::F64(stroke.line_join_miter_limit), false), false);
}

/// Update the transform value of the upstream Transform node based a change to its existing value and the given parent transform.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::messages::portfolio::document::utility_types::network_interface::{
use crate::messages::portfolio::document::utility_types::nodes::{CollapsedLayers, LayerPanelEntry};
use crate::messages::prelude::*;
use crate::messages::tool::common_functionality::auto_panning::AutoPanning;
use crate::messages::tool::common_functionality::graph_modification_utils::get_clip_mode;
use crate::messages::tool::tool_messages::tool_prelude::{Key, MouseMotion};
use crate::messages::tool::utility_types::{HintData, HintGroup, HintInfo};
use glam::{DAffine2, DVec2, IVec2};
Expand Down Expand Up @@ -2442,6 +2443,7 @@ impl NodeGraphMessageHandler {
}
});

let clippable = layer.can_be_clipped(network_interface.document_metadata());
let data = LayerPanelEntry {
id: node_id,
alias: network_interface.display_name(&node_id, &[]),
Expand All @@ -2461,6 +2463,8 @@ impl NodeGraphMessageHandler {
selected: selected_layers.contains(&node_id),
ancestor_of_selected: ancestors_of_selected.contains(&node_id),
descendant_of_selected: descendants_of_selected.contains(&node_id),
clipped: get_clip_mode(layer, network_interface).unwrap_or(false) && clippable,
clippable,
};
responses.add(FrontendMessage::UpdateDocumentLayerDetails { data });
}
Expand Down
Loading
Loading