@@ -14,7 +14,8 @@ use self::units::CSS_DEFAULT_DPI;
1414use crate :: {
1515 lower:: selector:: SelectorList ,
1616 turtle:: {
17- DpiConvertingTurtle , PreprocessTurtle , StrokeCollectingTurtle , Terrarium , Turtle ,
17+ CoordinateSystem , DpiConvertingTurtle , PreprocessTurtle , StrokeCollectingTurtle , Terrarium ,
18+ Turtle ,
1819 elements:: { Stroke , minimize_travel_time} ,
1920 } ,
2021} ;
@@ -83,6 +84,8 @@ pub struct ConversionOptions {
8384#[ derive( Debug ) ]
8485struct ConversionVisitor < ' a , T : Turtle > {
8586 terrarium : Terrarium < T > ,
87+ /// Whether to flip the Y axis to convert from SVG (Y-down) to the output coordinate system.
88+ coordinate_system : CoordinateSystem ,
8689 name_stack : Vec < String > ,
8790 /// Used to convert percentage values
8891 viewport_dim_stack : Vec < [ f64 ; 2 ] > ,
@@ -111,14 +114,18 @@ impl<'a, T: Turtle> ConversionVisitor<'a, T> {
111114 }
112115
113116 fn begin ( & mut self ) {
114- // Part 1 of converting from SVG to GCode coordinates
115- self . terrarium . push_transform ( Transform2D :: scale ( 1. , -1. ) ) ;
117+ if self . coordinate_system == CoordinateSystem :: YUp {
118+ // Part 1 of converting from SVG (Y-down) to output (Y-up) coordinates
119+ self . terrarium . push_transform ( Transform2D :: scale ( 1. , -1. ) ) ;
120+ }
116121 self . terrarium . turtle . begin ( ) ;
117122 }
118123
119124 fn end ( & mut self ) {
120125 self . terrarium . turtle . end ( ) ;
121- self . terrarium . pop_transform ( ) ;
126+ if self . coordinate_system == CoordinateSystem :: YUp {
127+ self . terrarium . pop_transform ( ) ;
128+ }
122129 }
123130}
124131
@@ -137,6 +144,7 @@ pub fn svg_to_turtle<T: Turtle>(
137144 config : & ConversionConfig ,
138145 options : ConversionOptions ,
139146 turtle : T ,
147+ coordinate_system : CoordinateSystem ,
140148) -> T {
141149 let selector_filter = config
142150 . selector_filter
@@ -149,6 +157,7 @@ pub fn svg_to_turtle<T: Turtle>(
149157 inner : PreprocessTurtle :: default ( ) ,
150158 dpi : config. dpi ,
151159 } ) ,
160+ coordinate_system,
152161 _config : config,
153162 options : options. clone ( ) ,
154163 name_stack : vec ! [ ] ,
@@ -189,6 +198,7 @@ pub fn svg_to_turtle<T: Turtle>(
189198 inner : turtle,
190199 dpi : config. dpi ,
191200 } ) ,
201+ coordinate_system,
192202 _config : config,
193203 options : options. clone ( ) ,
194204 name_stack : vec ! [ ] ,
@@ -202,8 +212,14 @@ pub fn svg_to_turtle<T: Turtle>(
202212 conversion_visitor. begin ( ) ;
203213
204214 if config. optimize_path_order {
205- let strokes =
206- svg_to_optimized_strokes ( doc, config, options, origin_transform, selector_filter) ;
215+ let strokes = svg_to_optimized_strokes (
216+ doc,
217+ config,
218+ options,
219+ origin_transform,
220+ selector_filter,
221+ coordinate_system,
222+ ) ;
207223 let turtle = & mut conversion_visitor. terrarium . turtle ;
208224 for stroke in strokes {
209225 turtle. move_to ( stroke. start_point ( ) ) ;
@@ -227,9 +243,11 @@ fn svg_to_optimized_strokes(
227243 options : ConversionOptions ,
228244 origin_transform : Transform2D < f64 > ,
229245 selector_filter : Option < SelectorList > ,
246+ coordinate_system : CoordinateSystem ,
230247) -> Vec < Stroke > {
231248 let mut collect_visitor = ConversionVisitor {
232249 terrarium : Terrarium :: new ( StrokeCollectingTurtle :: default ( ) ) ,
250+ coordinate_system,
233251 _config : config,
234252 options,
235253 name_stack : vec ! [ ] ,
0 commit comments