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
5 changes: 3 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ paste = "1"
roxmltree = "0.21"
serde = { version = "1", default-features = false }
serde_json = "1"
smallvec = "1.16"
svgtypes = "0.16"
uom = "0.38"
uom = "0.38"
25 changes: 8 additions & 17 deletions g_code/src/turtle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, fmt::Debug};

use ::g_code::{command, emit::Token};
use lyon_geom::{Point, SvgArc};
use lyon_geom::{LineSegment, Point, SvgArc};
use rust_decimal::{Decimal, prelude::*};
use svg2star::turtle::{
Turtle,
Expand Down Expand Up @@ -140,18 +140,15 @@ impl Turtle for GCodeTurtle<'_> {

fn stroke(&mut self, stroke: Stroke) {
let start = stroke.start_point();
let mut commands = stroke.into_commands().peekable();

self.tool_off();

// Comments should be inline after tool_off but before a rapid move.
while matches!(commands.peek(), Some(DrawCommand::Comment(_))) {
if let Some(DrawCommand::Comment(comment)) = commands.next() {
self.program.push(Token::Comment {
is_inline: false,
inner: Cow::Owned(comment),
});
}
if let Some(comment) = stroke.comment() {
self.program.push(Token::Comment {
is_inline: false,
inner: Cow::Owned(comment.to_owned()),
});
}

self.program.append(
Expand All @@ -163,9 +160,9 @@ impl Turtle for GCodeTurtle<'_> {
);
self.tool_on();

for command in commands {
for command in stroke.into_commands() {
match command {
DrawCommand::LineTo { from: _, to } => {
DrawCommand::LineTo(LineSegment { from: _, to }) => {
self.program.append(
&mut command!(LinearInterpolation {
X: self.round(to.x),
Expand Down Expand Up @@ -248,12 +245,6 @@ impl Turtle for GCodeTurtle<'_> {
}
}
}
DrawCommand::Comment(comment) => {
self.program.push(Token::Comment {
is_inline: false,
inner: Cow::Owned(comment),
});
}
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions g_code/tests/shapes.gcode
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,6 @@ G1 X133.707 Y112.443 F300
G1 X133.716 Y112.143 F300
G1 X133.717 Y112.053 F300;svg#svg1 > g#layer1 > polygon#polygon1
G0 X12.575 Y173.877
G1 X12.575 Y173.877 F300
G1 X8.009 Y179.602 F300
G1 X9.639 Y186.741 F300
G1 X16.236 Y189.919 F300
Expand All @@ -939,7 +938,6 @@ G1 X24.463 Y179.602 F300
G1 X42.162 Y169.979 F300
G1 X12.575 Y173.877 F300;svg#svg1 > g#layer1 > polyline#polyline1
G0 X76.339 Y112.397
G1 X76.339 Y112.397 F300
G1 X71.773 Y118.122 F300
G1 X73.402 Y125.261 F300
G1 X80 Y128.439 F300
Expand Down
2 changes: 1 addition & 1 deletion g_code/tests/smooth_curves.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion g_code/tests/transform_origin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion g_code/tests/transform_origin_equivalent.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion g_code/tests/use_defs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion g_code/tests/use_symbol.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion g_code/tests/use_xlink_href.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions star/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ log.workspace = true
uom.workspace = true
roxmltree.workspace = true
svgtypes.workspace = true
smallvec.workspace = true

[dependencies.serde]
workspace = true
Expand Down
22 changes: 18 additions & 4 deletions star/src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,18 @@ pub struct ConversionOptions {
pub dimensions: [Option<Length>; 2],
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TemplatedExpansion {
Use,
Pattern,
Marker,
}

/// Maps SVG [`Node`]s and their attributes into operations on a [`Terrarium`]
#[derive(Debug)]
struct ConversionVisitor<'a, 'input, T: Turtle> {
terrarium: Terrarium<T>,
/// Whether to flip the Y axis to convert from SVG (Y-down) to the output coordinate system.
/// Whether to flip the Y axis to convert from SVG (Y-down) to the output coordinate system (Y-up?).
coordinate_system: CoordinateSystem,
name_stack: Vec<String>,
/// Used to convert percentage values
Expand All @@ -94,6 +101,8 @@ struct ConversionVisitor<'a, 'input, T: Turtle> {
selector_filter: Option<selector::SelectorList>,
/// Resolved CSS class rules
css_rules: HashMap<&'input str, HashMap<&'input str, &'input str>>,
/// Stack of active template expansion scopes.
expansion_stack: Vec<TemplatedExpansion>,
}

impl<T: Turtle> ConversionVisitor<'_, '_, T> {
Expand All @@ -115,9 +124,11 @@ impl<T: Turtle> ConversionVisitor<'_, '_, T> {
}

fn should_draw_node(&self, node: Node) -> bool {
self.selector_filter
.as_ref()
.is_none_or(|s| s.matches(node))
!self.expansion_stack.is_empty()
|| self
.selector_filter
.as_ref()
.is_none_or(|s| s.matches(node))
}

fn begin(&mut self) {
Expand Down Expand Up @@ -169,6 +180,7 @@ pub fn svg_to_turtle<T: Turtle>(
viewport_dim_stack: vec![],
selector_filter: selector_filter.clone(),
css_rules: visit::parse_css(doc),
expansion_stack: vec![],
};

visitor.begin();
Expand Down Expand Up @@ -213,6 +225,7 @@ pub fn svg_to_turtle<T: Turtle>(
viewport_dim_stack: vec![],
selector_filter: selector_filter.clone(),
css_rules: visit::parse_css(doc),
expansion_stack: vec![],
};

conversion_visitor
Expand Down Expand Up @@ -259,6 +272,7 @@ fn svg_to_optimized_strokes(
viewport_dim_stack: vec![],
selector_filter,
css_rules: visit::parse_css(doc),
expansion_stack: vec![],
};
collect_visitor.terrarium.push_transform(origin_transform);
collect_visitor.begin();
Expand Down
Loading
Loading