-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to list the fonts detected
Adds a new CLI flag allowing the user to list the fonts which are detected on the system. Keep the pdf conversion as the default commandi. If no command is provided, an error message will be displayed. Signed-off-by: Rémy Greinhofer <[email protected]>
- Loading branch information
Showing
5 changed files
with
138 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::args::ConvertCommand; | ||
use std::path::{Path, PathBuf}; | ||
use svg2pdf::Options; | ||
use usvg::{TreeParsing, TreeTextToPath}; | ||
|
||
/// Execute a font listing command. | ||
pub fn _convert(command: ConvertCommand) -> Result<(), String> { | ||
convert_(&command.input, command.output, command.dpi) | ||
} | ||
|
||
pub fn convert_( | ||
input: &PathBuf, | ||
output: Option<PathBuf>, | ||
dpi: f32, | ||
) -> Result<(), String> { | ||
// Prepare the font database. | ||
let mut fontdb = fontdb::Database::new(); | ||
fontdb.load_system_fonts(); | ||
|
||
// Convert the file. | ||
let name = Path::new(input.file_name().ok_or("Input path does not point to a file")?); | ||
let output = output.unwrap_or_else(|| name.with_extension("pdf")); | ||
|
||
let svg = std::fs::read_to_string(input).map_err(|_| "Failed to load SVG file")?; | ||
|
||
let options = usvg::Options::default(); | ||
|
||
let mut tree = usvg::Tree::from_str(&svg, &options).map_err(|err| err.to_string())?; | ||
tree.convert_text(&fontdb); | ||
|
||
let pdf = svg2pdf::convert_tree(&tree, Options { dpi, ..Options::default() }); | ||
|
||
std::fs::write(output, pdf).map_err(|_| "Failed to write PDF file")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::args::FontsCommand; | ||
use std::collections::BTreeMap; | ||
|
||
/// Execute a font listing command. | ||
pub fn fonts(command: &FontsCommand) -> Result<(), String> { | ||
// Prepare the font database. | ||
let mut fontdb = fontdb::Database::new(); | ||
fontdb.load_system_fonts(); | ||
|
||
// Collect the font famillies. | ||
let mut font_families: BTreeMap<String, Vec<String>> = BTreeMap::new(); | ||
for face in fontdb.faces() { | ||
for family in &face.families { | ||
font_families | ||
.entry(family.0.clone()) | ||
.and_modify(|value| value.push(face.post_script_name.clone())) | ||
.or_insert(vec![face.post_script_name.clone()]); | ||
} | ||
} | ||
|
||
// Display the results. | ||
for (family, mut names) in font_families { | ||
names.sort(); | ||
let mut name_string = String::new(); | ||
name_string.push_str(&family); | ||
if command.all { | ||
for (idx, name) in names.iter().enumerate() { | ||
if idx == (names.len() - 1) { | ||
name_string.push_str("\n└ ") | ||
} else { | ||
name_string.push_str("\n├ ") | ||
} | ||
name_string.push_str(name); | ||
} | ||
} | ||
println!("{name_string}"); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters