Skip to content

Commit

Permalink
Revert usvg::Options::font_family removal.
Browse files Browse the repository at this point in the history
  • Loading branch information
RazrFalcon committed Dec 3, 2022
1 parent 11ec031 commit 36b0e0b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ This changelog also contains important changes in dependencies.
- `usvg::TransformFromBBox` trait. This is just a regular `usvg::Transform` method now.
- `usvg::OptionsRef`. `usvg::Options` is enough from now.
- `usvg::Options::fontdb`. Used only by `usvg-text-layout` now.
- `usvg::Options::font_family`. A font set via `fontdb::Database::set_serif_family`
will be used now. Which by default it is set to _Times New Roman_, so the default behavior
will not change.
- `--font-family` option from `resvg` and `usvg` CLIs. See above. Use `--serif-family` instead.
- (c-api) `resvg_options_set_font_family`. Use `resvg_options_set_serif_family` instead.
- `--dump-svg` from `resvg`.

## [0.27.0] - 2022-11-27
Expand Down
2 changes: 1 addition & 1 deletion c-api/ResvgQt.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ResvgOptions {

auto familyC = family.toUtf8();
familyC.append('\0');
resvg_options_set_serif_family(d, familyC.constData());
resvg_options_set_font_family(d, familyC.constData());
}

/**
Expand Down
12 changes: 12 additions & 0 deletions c-api/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ pub extern "C" fn resvg_options_set_dpi(opt: *mut resvg_options, dpi: f64) {
cast_opt(opt).dpi = dpi;
}

/// @brief Sets the default font family.
///
/// Will be used when no `font-family` attribute is set in the SVG.
///
/// Must be UTF-8. NULL is not allowed.
///
/// Default: Times New Roman
#[no_mangle]
pub extern "C" fn resvg_options_set_font_family(opt: *mut resvg_options, family: *const c_char) {
cast_opt(opt).font_family = cstr_to_str(family).unwrap().to_string();
}

/// @brief Sets the default font size.
///
/// Will be used when no `font-size` attribute is set in the SVG.
Expand Down
11 changes: 11 additions & 0 deletions c-api/resvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ void resvg_options_set_resources_dir(resvg_options *opt, const char *path);
*/
void resvg_options_set_dpi(resvg_options *opt, double dpi);

/**
* @brief Sets the default font family.
*
* Will be used when no `font-family` attribute is set in the SVG.
*
* Must be UTF-8. NULL is not allowed.
*
* Default: Times New Roman
*/
void resvg_options_set_font_family(resvg_options *opt, const char *family);

/**
* @brief Sets the default font size.
*
Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,14 @@ OPTIONS:
contains the SVG file, but can be set to any.
[default: input file directory]
--font-family FAMILY Sets the default font family that will be
used when no 'font-family' is present
[default: Times New Roman]
--font-size SIZE Sets the default font size that will be
used when no 'font-size' is present
[default: 12] [possible values: 1..192]
--serif-family FAMILY Sets the 'serif' font family.
Will be used when no 'font-family' is present
--serif-family FAMILY Sets the 'serif' font family
[default: Times New Roman]
--sans-serif-family FAMILY Sets the 'sans-serif' font family
[default: Arial]
--cursive-family FAMILY Sets the 'cursive' font family
Expand Down Expand Up @@ -207,6 +210,7 @@ struct CliArgs {
image_rendering: usvg::ImageRendering,
resources_dir: Option<path::PathBuf>,

font_family: Option<String>,
font_size: u32,
serif_family: Option<String>,
sans_serif_family: Option<String>,
Expand Down Expand Up @@ -267,6 +271,7 @@ fn collect_args() -> Result<CliArgs, pico_args::Error> {
.opt_value_from_str("--resources-dir")
.unwrap_or_default(),

font_family: input.opt_value_from_str("--font-family")?,
font_size: input
.opt_value_from_fn("--font-size", parse_font_size)?
.unwrap_or(12),
Expand Down Expand Up @@ -469,6 +474,10 @@ fn parse_args() -> Result<Args, String> {
let usvg = usvg::Options {
resources_dir,
dpi: args.dpi as f64,
font_family: args
.font_family
.take()
.unwrap_or_else(|| "Times New Roman".to_string()),
font_size: args.font_size as f64,
languages: args.languages,
shape_rendering: args.shape_rendering,
Expand Down
9 changes: 9 additions & 0 deletions usvg/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ pub struct Options {
/// Default: 96.0
pub dpi: f64,

/// A default font family.
///
/// Will be used when no `font-family` attribute is set in the SVG.
///
/// Default: Times New Roman
pub font_family: String,

/// A default font size.
///
/// Will be used when no `font-size` attribute is set in the SVG.
Expand Down Expand Up @@ -127,6 +134,8 @@ impl Default for Options {
Options {
resources_dir: None,
dpi: 96.0,
// Default font is user-agent dependent so we can use whichever we like.
font_family: "Times New Roman".to_owned(),
font_size: 12.0,
languages: vec!["en".to_string()],
shape_rendering: ShapeRendering::default(),
Expand Down
11 changes: 8 additions & 3 deletions usvg/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ impl Default for Style {
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct Font {
/// A list of family names.
/// Can be empty.
///
/// Never empty. Uses [`Options::font_family`](crate::Options::font_family) as fallback.
pub families: Vec<String>,
/// A font style.
pub style: Style,
Expand Down Expand Up @@ -501,7 +502,7 @@ fn collect_text_chunks_impl(
}
};

let font = convert_font(parent);
let font = convert_font(parent, state);

let raw_paint_order: svgtypes::PaintOrder = parent
.find_attribute(svgtree::AId::PaintOrder)
Expand Down Expand Up @@ -652,7 +653,7 @@ fn resolve_text_flow(node: svgtree::Node, state: &converter::State) -> Option<Te
Some(TextFlow::Path(Rc::new(TextPath { start_offset, path })))
}

fn convert_font(node: svgtree::Node) -> Font {
fn convert_font(node: svgtree::Node, state: &converter::State) -> Font {
let style: Style = node.find_attribute(AId::FontStyle).unwrap_or_default();
let stretch = conv_font_stretch(node);
let weight = resolve_font_weight(node);
Expand Down Expand Up @@ -680,6 +681,10 @@ fn convert_font(node: svgtree::Node) -> Font {
families.push(family.to_string());
}

if families.is_empty() {
families.push(state.opt.font_family.clone())
}

Font {
families,
style,
Expand Down

0 comments on commit 36b0e0b

Please sign in to comment.