Skip to content

Commit

Permalink
Simple command line interface
Browse files Browse the repository at this point in the history
Co-Authored-By: Martin <[email protected]>
  • Loading branch information
laurmaedje and reknih committed Jan 19, 2022
1 parent b850bbd commit c95fa32
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ categories = ["encoding", "graphics", "multimedia"]
keywords = ["svg", "pdf", "vector-graphics", "conversion"]

[features]
default = ["png", "jpeg", "text"]
default = ["png", "jpeg"]
png = ["image/png"]
jpeg = ["image/jpeg"]
cli = ["clap", "termcolor", "usvg/text", "fontdb"]

[dependencies]
miniz_oxide = "0.4"
pdf-writer = "0.4.1"
usvg = { version = "0.20", default-features = false }
clap = { version = "3", features = ["derive"], optional = true }
fontdb = { version = "0.7", optional = true }
image = { version = "0.23", default-features = false, optional = true }
termcolor = { version = "1", optional = true }

[[bin]]
name = "svg2pdf"
required-features = ["cli"]
68 changes: 68 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process;

use clap::Parser;
use termcolor::{ColorChoice, ColorSpec, StandardStream, WriteColor};

#[derive(Debug, Parser)]
#[clap(about, version)]
struct Args {
/// Path to read SVG file from.
input: PathBuf,
/// Path to write PDF file to.
output: Option<PathBuf>,
/// The number of SVG pixels per PDF points.
#[clap(long, default_value = "72.0")]
dpi: f64,
}

fn main() {
if let Err(msg) = run() {
print_error(&msg).unwrap();
process::exit(1);
}
}

fn run() -> Result<(), String> {
let args = Args::parse();

// Determine output path.
let name =
Path::new(args.input.file_name().ok_or("Input path does not point to a file")?);
let output = args.output.unwrap_or_else(|| name.with_extension("pdf"));

// Load source file.
let svg =
std::fs::read_to_string(&args.input).map_err(|_| "Failed to load SVG file")?;

// Convert string to SVG.
let mut options = usvg::Options::default();
options.fontdb = fontdb::Database::new();
options.fontdb.load_system_fonts();
let tree =
usvg::Tree::from_str(&svg, &options.to_ref()).map_err(|err| err.to_string())?;

// Convert SVG to PDF.
let mut options = svg2pdf::Options::default();
options.dpi = args.dpi;
let pdf = svg2pdf::convert_tree(&tree, options);

// Write output file.
std::fs::write(output, pdf).map_err(|_| "Failed to write PDF file")?;

Ok(())
}

fn print_error(msg: &str) -> io::Result<()> {
let mut w = StandardStream::stderr(ColorChoice::Always);

let mut color = ColorSpec::new();
color.set_fg(Some(termcolor::Color::Red));
color.set_bold(true);
w.set_color(&color)?;
write!(w, "error")?;

w.reset()?;
writeln!(w, ": {msg}.")
}

0 comments on commit c95fa32

Please sign in to comment.