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
28 changes: 28 additions & 0 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use clap::{ArgAction, Parser, Subcommand};
use std::path::PathBuf;

/// The character typically used to separate path components
/// in environment variables.
const ENV_PATH_SEP: char = if cfg!(windows) { ';' } else { ':' };

#[derive(Debug, Parser)]
#[clap(about, version)]
pub struct CliArguments {
Expand All @@ -26,6 +30,10 @@ pub struct CliArguments {
/// How much raster images of rasterized effects should be scaled up.
#[clap(long, default_value = "1.5")]
pub raster_scale: f32,

/// Common font arguments.
#[command(flatten)]
pub font: FontsArgs,
}

// What to do.
Expand All @@ -42,4 +50,24 @@ pub struct FontsCommand {
/// Also lists style variants of each font family
#[arg(long)]
pub all: bool,

/// Common font arguments.
#[command(flatten)]
pub font: FontsArgs,
}

/// Common arguments to customize available fonts.
#[derive(Debug, Clone, Parser)]
pub struct FontsArgs {
/// Adds additional directories to search for fonts.
///
/// If multiple paths are specified, they are separated by the system's path
/// separator (`:` on Unix-like systems and `;` on Windows).
#[arg(long = "font-path", value_name = "DIR", value_delimiter = ENV_PATH_SEP)]
pub font_paths: Vec<PathBuf>,

/// Ensures system fonts won't be used, unless explicitly included via
/// `--font-path`.
#[arg(long)]
pub ignore_system_fonts: bool,
}
12 changes: 11 additions & 1 deletion cli/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use svg2pdf::{ConversionOptions, PageOptions};

use crate::args::FontsArgs;

pub fn convert_(
input: &PathBuf,
output: Option<PathBuf>,
conversion_options: ConversionOptions,
page_options: PageOptions,
font_options: FontsArgs,
) -> Result<(), String> {
if let Ok(()) = log::set_logger(&LOGGER) {
log::set_max_level(log::LevelFilter::Warn);
}

let mut fontdb = fontdb::Database::new();
fontdb.load_system_fonts();

if !font_options.ignore_system_fonts {
fontdb.load_system_fonts();
}

for font_path in &font_options.font_paths {
fontdb.load_fonts_dir(font_path);
}

fontdb.set_serif_family("Times New Roman");
fontdb.set_sans_serif_family("Arial");
Expand Down
9 changes: 8 additions & 1 deletion cli/src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ use std::collections::BTreeMap;
pub fn fonts(command: &FontsCommand) -> Result<(), String> {
// Prepare the font database.
let mut fontdb = fontdb::Database::new();
fontdb.load_system_fonts();

if !command.font.ignore_system_fonts {
fontdb.load_system_fonts();
}

for font_path in &command.font.font_paths {
fontdb.load_fonts_dir(font_path);
}

// Collect the font famillies.
let mut font_families: BTreeMap<String, Vec<String>> = BTreeMap::new();
Expand Down
8 changes: 7 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ fn run() -> Result<(), String> {

let page_options = PageOptions { dpi: args.dpi };

return convert::convert_(&input, args.output, conversion_options, page_options);
return convert::convert_(
&input,
args.output,
conversion_options,
page_options,
args.font,
);
};

// Otherwise execute the command provided if any.
Expand Down