Skip to content

Commit

Permalink
v0.3.1: Fix origin override broken by 8f9db32
Browse files Browse the repository at this point in the history
This now needs to be converted from mm into user units
  • Loading branch information
sameer committed Aug 6, 2024
1 parent 53c7c29 commit e50e42b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
16 changes: 8 additions & 8 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 cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "svg2gcode-cli"
version = "0.0.13"
version = "0.0.14"
authors = ["Sameer Puri <crates@purisa.me>"]
edition = "2021"
description = "Command line interface for svg2gcode"
repository = "https://github.com/sameer/svg2gcode"
license = "MIT"

[dependencies]
svg2gcode = { version = "0.3.0", features = ["serde"] }
svg2gcode = { version = "0.3.1", features = ["serde"] }
env_logger = { version = "0", default-features = false, features = [
"atty",
"termcolor",
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "svg2gcode"
version = "0.3.0"
version = "0.3.1"
authors = ["Sameer Puri <crates@purisa.me>"]
edition = "2021"
description = "Convert paths in SVG files to GCode for a pen plotter, laser engraver, or other machine."
Expand Down
13 changes: 11 additions & 2 deletions lib/src/converter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use roxmltree::{Document, Node};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use svgtypes::Length;
use uom::si::f64::Length as UomLength;
use uom::si::length::{inch, millimeter};

use crate::{turtle::*, Machine};
use self::units::CSS_DEFAULT_DPI;

#[cfg(feature = "serde")]
mod length_serde;
Expand All @@ -26,7 +29,7 @@ pub struct ConversionConfig {
pub feedrate: f64,
/// Dots per inch for pixels, picas, points, etc.
pub dpi: f64,
/// Set the origin point for this conversion
/// Set the origin point in millimeters for this conversion
#[cfg_attr(feature = "serde", serde(default = "zero_origin"))]
pub origin: [Option<f64>; 2],
}
Expand Down Expand Up @@ -119,7 +122,13 @@ pub fn svg2program<'a, 'input: 'a>(

visitor.terrarium.turtle.inner.bounding_box
};
let origin_transform = match config.origin {

// Convert from millimeters to user units
let origin = config
.origin
.map(|dim| dim.map(|d| UomLength::new::<millimeter>(d).get::<inch>() * CSS_DEFAULT_DPI));

let origin_transform = match origin {
[None, Some(origin_y)] => {
let bb = bounding_box_generator();
Transform2D::translation(0., origin_y - bb.min.y)
Expand Down
7 changes: 5 additions & 2 deletions lib/src/converter/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use crate::Turtle;

use super::ConversionVisitor;

/// The DPI assumed by CSS is 96.
///
/// <https://www.w3.org/TR/css3-values/#absolute-lengths>
pub const CSS_DEFAULT_DPI: f64 = 96.;

/// Used to compute percentages correctly
///
/// <https://www.w3.org/TR/SVG/coords.html#Units>
Expand Down Expand Up @@ -47,8 +52,6 @@ impl<'a, T: Turtle> ConversionVisitor<'a, T> {
use uom::si::f64::Length;
use uom::si::length::*;

const CSS_DEFAULT_DPI: f64 = 96.;

match l.unit {
Cm => Length::new::<centimeter>(l.number).get::<inch>() * CSS_DEFAULT_DPI,
Mm => Length::new::<millimeter>(l.number).get::<inch>() * CSS_DEFAULT_DPI,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Settings {
///
/// - Settings version is [`Version::Unknown`].
/// - There are breaking changes requiring manual intervention. In which case this does a partial update to that point.
pub fn try_upgrade(&mut self) -> Result<(), ()> {
pub fn try_upgrade(&mut self) -> Result<(), &'static str> {
loop {
match self.version {
// Compatibility for M2 by default
Expand All @@ -46,7 +46,7 @@ impl Settings {
self.version = Version::V5;
}
Version::V5 => break Ok(()),
Version::Unknown(_) => break Err(()),
Version::Unknown(_) => break Err("cannot upgrade unknown version"),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "svg2gcode-web"
version = "0.0.14"
version = "0.0.15"
authors = ["Sameer Puri <crates@purisa.me>"]
edition = "2021"
description = "Convert vector graphics to g-code for pen plotters, laser engravers, and other CNC machines"
Expand All @@ -10,7 +10,7 @@ license = "MIT"

[dependencies]
wasm-bindgen = "0.2"
svg2gcode = { version = "0.3.0", features = ["serde"] }
svg2gcode = { version = "0.3.1", features = ["serde"] }
roxmltree = "0.19"
g-code = "0.4.2"
codespan-reporting = "0.11"
Expand Down

0 comments on commit e50e42b

Please # to comment.