[librsvg: 17/23] (#356): Add --accept-language option to rsvg-convert
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 17/23] (#356): Add --accept-language option to rsvg-convert
- Date: Mon, 24 May 2021 17:47:32 +0000 (UTC)
commit d1658a7ab3c5427986cbe8f5d0be7a40e351f0a1
Author: Federico Mena Quintero <federico gnome org>
Date: Fri May 21 19:57:14 2021 -0500
(#356): Add --accept-language option to rsvg-convert
This takes a string formatted like an HTTP Accept-Language header:
rsvg-convert --accept-language=es-MX,en,fr foo.svg
Fixes https://gitlab.gnome.org/GNOME/librsvg/-/issues/356
src/api.rs | 4 +--
src/bin/rsvg-convert.rs | 29 ++++++++++++++++++---
tests/fixtures/cmdline/accept-language-de.png | Bin 0 -> 173 bytes
tests/fixtures/cmdline/accept-language-es.png | Bin 0 -> 172 bytes
.../fixtures/cmdline/accept-language-fallback.png | Bin 0 -> 173 bytes
tests/fixtures/cmdline/accept-language.svg | 7 +++++
tests/src/cmdline/rsvg_convert.rs | 26 ++++++++++++++++++
7 files changed, 61 insertions(+), 5 deletions(-)
---
diff --git a/src/api.rs b/src/api.rs
index 1500f732..31199aef 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -324,8 +324,8 @@ impl<'a> CairoRenderer<'a> {
/// `Language::FromEnvironment`, which means that the set of preferred languages will
/// be obtained from the program's environment. To set an explicit list of languages,
/// you can use `Language::AcceptLanguage` instead.
- pub fn with_language(self, language: Language) -> Self {
- let user_language = UserLanguage::new(&language);
+ pub fn with_language(self, language: &Language) -> Self {
+ let user_language = UserLanguage::new(language);
CairoRenderer {
user_language,
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index 78a6e22e..8d76a1c5 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -21,7 +21,7 @@ mod windows_imports {
use self::windows_imports::*;
use librsvg::rsvg_convert_only::{LegacySize, PathOrUrl};
-use librsvg::{CairoRenderer, Color, Loader, Parse, RenderingError};
+use librsvg::{AcceptLanguage, CairoRenderer, Color, Language, Loader, Parse, RenderingError};
use once_cell::unsync::OnceCell;
use std::ops::Deref;
use std::path::PathBuf;
@@ -435,7 +435,6 @@ arg_enum! {
}
}
-#[derive(Debug)]
struct Converter {
pub dpi: (f64, f64),
pub zoom: Scale,
@@ -446,6 +445,7 @@ struct Converter {
pub keep_aspect_ratio: bool,
pub background_color: Option<Color>,
pub stylesheet: Option<PathBuf>,
+ pub language: Language,
pub unlimited: bool,
pub keep_image_data: bool,
pub input: Vec<Input>,
@@ -487,7 +487,9 @@ impl Converter {
.map_err(|e| error!("Error applying stylesheet: {}", e))?;
}
- let renderer = CairoRenderer::new(&handle).with_dpi(self.dpi.0, self.dpi.1);
+ let renderer = CairoRenderer::new(&handle)
+ .with_dpi(self.dpi.0, self.dpi.1)
+ .with_language(&self.language);
let geometry = self.natural_geometry(&renderer, input)?;
let natural_size = Size::new(geometry.width, geometry.height);
@@ -689,6 +691,14 @@ fn parse_args() -> Result<Converter, Error> {
.value_name("object id")
.help("SVG id of object to export [default is to export all objects]"),
)
+ .arg(
+ clap::Arg::with_name("accept-language")
+ .short("l")
+ .long("accept-language")
+ .empty_values(false)
+ .value_name("languages")
+ .help("Languages to accept, for example \"es-MX,de,en\" [default uses language from the
environment]"),
+ )
.arg(
clap::Arg::with_name("keep_aspect")
.short("a")
@@ -741,6 +751,18 @@ fn parse_args() -> Result<Converter, Error> {
_ => matches.is_present("keep_image_data"),
};
+ let language = value_t!(matches, "accept-language", String)
+ .or_none()
+ .and_then(|lang_str| match lang_str {
+ None => Ok(Language::FromEnvironment),
+ Some(s) => AcceptLanguage::parse(&s)
+ .map(Language::AcceptLanguage)
+ .map_err(|e| {
+ let desc = format!("{}", e);
+ clap::Error::with_description(&desc, clap::ErrorKind::InvalidValue)
+ }),
+ });
+
let background_color = value_t!(matches, "background", String).and_then(parse_color_string);
// librsvg expects ids starting with '#', so it can lookup ids in externs like "subfile.svg#subid".
@@ -793,6 +815,7 @@ fn parse_args() -> Result<Converter, Error> {
stylesheet: matches.value_of_os("stylesheet").map(PathBuf::from),
unlimited: matches.is_present("unlimited"),
keep_image_data,
+ language: language?,
input,
output: matches
.value_of_os("output")
diff --git a/tests/fixtures/cmdline/accept-language-de.png b/tests/fixtures/cmdline/accept-language-de.png
new file mode 100644
index 00000000..cc797dc2
Binary files /dev/null and b/tests/fixtures/cmdline/accept-language-de.png differ
diff --git a/tests/fixtures/cmdline/accept-language-es.png b/tests/fixtures/cmdline/accept-language-es.png
new file mode 100644
index 00000000..4cf3a21f
Binary files /dev/null and b/tests/fixtures/cmdline/accept-language-es.png differ
diff --git a/tests/fixtures/cmdline/accept-language-fallback.png
b/tests/fixtures/cmdline/accept-language-fallback.png
new file mode 100644
index 00000000..43b20f01
Binary files /dev/null and b/tests/fixtures/cmdline/accept-language-fallback.png differ
diff --git a/tests/fixtures/cmdline/accept-language.svg b/tests/fixtures/cmdline/accept-language.svg
new file mode 100644
index 00000000..c132b65d
--- /dev/null
+++ b/tests/fixtures/cmdline/accept-language.svg
@@ -0,0 +1,7 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
+ <switch allowReorder="yes">
+ <rect systemLanguage="de" fill="red" width="10" height="10" />
+ <rect systemLanguage="es" fill="lime" width="10" height="10" />
+ <rect fill="yellow" id="rect3" width="10" height="10" />
+ </switch>
+</svg>
diff --git a/tests/src/cmdline/rsvg_convert.rs b/tests/src/cmdline/rsvg_convert.rs
index 64c79510..5dbdb65c 100644
--- a/tests/src/cmdline/rsvg_convert.rs
+++ b/tests/src/cmdline/rsvg_convert.rs
@@ -708,6 +708,32 @@ fn overflowing_size_is_detected() {
));
}
+#[test]
+fn accept_language_given() {
+ RsvgConvert::new_with_input("tests/fixtures/cmdline/accept-language.svg")
+ .arg("--accept-language=es-MX")
+ .assert()
+ .success()
+ .stdout(file::is_png().with_contents("tests/fixtures/cmdline/accept-language-es.png"));
+
+ RsvgConvert::new_with_input("tests/fixtures/cmdline/accept-language.svg")
+ .arg("--accept-language=de")
+ .assert()
+ .success()
+ .stdout(file::is_png().with_contents("tests/fixtures/cmdline/accept-language-de.png"));
+}
+
+#[test]
+fn accept_language_fallback() {
+ RsvgConvert::new_with_input("tests/fixtures/cmdline/accept-language.svg")
+ .arg("--accept-language=fr")
+ .assert()
+ .success()
+ .stdout(
+ file::is_png().with_contents("tests/fixtures/cmdline/accept-language-fallback.png"),
+ );
+}
+
#[test]
fn keep_image_data_option() {
RsvgConvert::accepts_arg("--keep-image-data");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]