[librsvg: 14/23] New API CairoRenderer::with_language
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 14/23] New API CairoRenderer::with_language
- Date: Mon, 24 May 2021 17:47:32 +0000 (UTC)
commit 71d2c1c5e4964df116b6302aa4dccc2e31deab3b
Author: Federico Mena Quintero <federico gnome org>
Date: Fri May 21 15:07:30 2021 -0500
New API CairoRenderer::with_language
I'm not 100% happy that all error handling in
locale_from_environment() and LangaugeTags::from_locale() is opaque.
However, I'm not yet sure how much it should be the business of the
caller to guarantee a valid LANG-like variable, anyway.
src/accept_language.rs | 6 +++---
src/api.rs | 50 +++++++++++++++++++++++++++++++++++++++++---------
src/drawing_ctx.rs | 7 ++++---
src/handle.rs | 21 +++++++++++++++++----
4 files changed, 65 insertions(+), 19 deletions(-)
---
diff --git a/src/accept_language.rs b/src/accept_language.rs
index 775d9a2c..b6e85c05 100644
--- a/src/accept_language.rs
+++ b/src/accept_language.rs
@@ -7,9 +7,9 @@ use std::str::FromStr;
/// Used to set the language for rendering.
///
-/// SVG documents can use the `<switch>` element whose children have a `systemLanguage`
-/// attribute; only the first child which has a `systemLanguage` that matches the user's
-/// preferences will be rendered.
+/// SVG documents can use the `<switch>` element, whose children have a `systemLanguage`
+/// attribute; only the first child which has a `systemLanguage` that matches the
+/// preferred languages will be rendered.
///
/// This enum, used with `CairoRenderer::with_language`, configures how to obtain the
/// user's prefererred languages.
diff --git a/src/api.rs b/src/api.rs
index 0b83601c..1500f732 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -5,6 +5,7 @@
#![warn(missing_docs)]
pub use crate::{
+ accept_language::{AcceptLanguage, Language, UserLanguage},
error::{ImplementationLimit, LoadingError, RenderingError},
length::{LengthUnit, RsvgLength as Length},
};
@@ -247,6 +248,7 @@ impl SvgHandle {
pub struct CairoRenderer<'a> {
handle: &'a SvgHandle,
dpi: Dpi,
+ user_language: UserLanguage,
is_testing: bool,
}
@@ -292,6 +294,7 @@ impl<'a> CairoRenderer<'a> {
CairoRenderer {
handle,
dpi: Dpi::new(DEFAULT_DPI_X, DEFAULT_DPI_Y),
+ user_language: UserLanguage::new(&Language::FromEnvironment),
is_testing: false,
}
}
@@ -311,6 +314,25 @@ impl<'a> CairoRenderer<'a> {
}
}
+ /// Configures the set of languages used for rendering.
+ ///
+ /// SVG documents can use the `<switch>` element, whose children have a
+ /// `systemLanguage` attribute; only the first child which has a `systemLanguage` that
+ /// matches the preferred languages will be rendered.
+ ///
+ /// This function sets the preferred languages. The default is
+ /// `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);
+
+ CairoRenderer {
+ user_language,
+ ..self
+ }
+ }
+
/// Queries the `width`, `height`, and `viewBox` attributes in an SVG document.
///
/// If you are calling this function to compute a scaling factor to render the SVG,
@@ -361,7 +383,7 @@ impl<'a> CairoRenderer<'a> {
) -> Result<(), RenderingError> {
self.handle
.0
- .render_document(cr, viewport, self.dpi, self.is_testing)
+ .render_document(cr, viewport, &self.user_language, self.dpi, self.is_testing)
}
/// Computes the (ink_rect, logical_rect) of an SVG element, as if
@@ -395,7 +417,7 @@ impl<'a> CairoRenderer<'a> {
) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
self.handle
.0
- .get_geometry_for_layer(id, viewport, self.dpi, self.is_testing)
+ .get_geometry_for_layer(id, viewport, &self.user_language, self.dpi, self.is_testing)
.map(|(i, l)| (i, l))
}
@@ -424,9 +446,14 @@ impl<'a> CairoRenderer<'a> {
id: Option<&str>,
viewport: &cairo::Rectangle,
) -> Result<(), RenderingError> {
- self.handle
- .0
- .render_layer(cr, id, viewport, self.dpi, self.is_testing)
+ self.handle.0.render_layer(
+ cr,
+ id,
+ viewport,
+ &self.user_language,
+ self.dpi,
+ self.is_testing,
+ )
}
/// Computes the (ink_rect, logical_rect) of a single SVG element
@@ -465,7 +492,7 @@ impl<'a> CairoRenderer<'a> {
) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
self.handle
.0
- .get_geometry_for_element(id, self.dpi, self.is_testing)
+ .get_geometry_for_element(id, &self.user_language, self.dpi, self.is_testing)
.map(|(i, l)| (i, l))
}
@@ -491,9 +518,14 @@ impl<'a> CairoRenderer<'a> {
id: Option<&str>,
element_viewport: &cairo::Rectangle,
) -> Result<(), RenderingError> {
- self.handle
- .0
- .render_element(cr, id, element_viewport, self.dpi, self.is_testing)
+ self.handle.0.render_element(
+ cr,
+ id,
+ element_viewport,
+ &self.user_language,
+ self.dpi,
+ self.is_testing,
+ )
}
/// Turns on test mode. Do not use this function; it is for librsvg's test suite only.
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 75003eca..6355511f 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -9,7 +9,7 @@ use std::cell::RefCell;
use std::convert::TryFrom;
use std::rc::{Rc, Weak};
-use crate::accept_language::{Language, UserLanguage};
+use crate::accept_language::UserLanguage;
use crate::aspect_ratio::AspectRatio;
use crate::bbox::BoundingBox;
use crate::coord_units::CoordUnits;
@@ -173,6 +173,7 @@ pub fn draw_tree(
mode: DrawingMode,
cr: &cairo::Context,
viewport: Rect,
+ user_language: &UserLanguage,
dpi: Dpi,
measuring: bool,
testing: bool,
@@ -215,6 +216,7 @@ pub fn draw_tree(
cr,
transform,
viewport,
+ user_language.clone(),
dpi,
measuring,
testing,
@@ -257,6 +259,7 @@ impl DrawingCtx {
cr: &cairo::Context,
transform: Transform,
viewport: Rect,
+ user_language: UserLanguage,
dpi: Dpi,
measuring: bool,
testing: bool,
@@ -267,8 +270,6 @@ impl DrawingCtx {
let viewport_stack = vec![initial_viewport];
- let user_language = UserLanguage::new(&Language::FromEnvironment);
-
DrawingCtx {
initial_viewport,
dpi,
diff --git a/src/handle.rs b/src/handle.rs
index 122c252e..9aea4c9f 100644
--- a/src/handle.rs
+++ b/src/handle.rs
@@ -2,6 +2,7 @@
//!
//! This module provides the primitives on which the public APIs are implemented.
+use crate::accept_language::UserLanguage;
use crate::bbox::BoundingBox;
use crate::css::{Origin, Stylesheet};
use crate::document::{AcquiredNodes, Document, NodeId};
@@ -153,6 +154,7 @@ impl Handle {
&self,
node: Node,
viewport: Rect,
+ user_language: &UserLanguage,
dpi: Dpi,
is_testing: bool,
) -> Result<(Rect, Rect), RenderingError> {
@@ -165,6 +167,7 @@ impl Handle {
DrawingMode::LimitToStack { node, root },
&cr,
viewport,
+ user_language,
dpi,
true,
is_testing,
@@ -181,13 +184,15 @@ impl Handle {
&self,
id: Option<&str>,
viewport: &cairo::Rectangle,
+ user_language: &UserLanguage,
dpi: Dpi,
is_testing: bool,
) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
let viewport = Rect::from(*viewport);
let node = self.get_node_or_root(id)?;
- let (ink_rect, logical_rect) = self.geometry_for_layer(node, viewport, dpi, is_testing)?;
+ let (ink_rect, logical_rect) =
+ self.geometry_for_layer(node, viewport, user_language, dpi, is_testing)?;
Ok((
cairo::Rectangle::from(ink_rect),
@@ -226,10 +231,11 @@ impl Handle {
&self,
cr: &cairo::Context,
viewport: &cairo::Rectangle,
+ user_language: &UserLanguage,
dpi: Dpi,
is_testing: bool,
) -> Result<(), RenderingError> {
- self.render_layer(cr, None, viewport, dpi, is_testing)
+ self.render_layer(cr, None, viewport, user_language, dpi, is_testing)
}
pub fn render_layer(
@@ -237,6 +243,7 @@ impl Handle {
cr: &cairo::Context,
id: Option<&str>,
viewport: &cairo::Rectangle,
+ user_language: &UserLanguage,
dpi: Dpi,
is_testing: bool,
) -> Result<(), RenderingError> {
@@ -253,6 +260,7 @@ impl Handle {
DrawingMode::LimitToStack { node, root },
cr,
viewport,
+ user_language,
dpi,
false,
is_testing,
@@ -267,6 +275,7 @@ impl Handle {
fn get_bbox_for_element(
&self,
node: &Node,
+ user_language: &UserLanguage,
dpi: Dpi,
is_testing: bool,
) -> Result<BoundingBox, RenderingError> {
@@ -279,6 +288,7 @@ impl Handle {
DrawingMode::OnlyNode(node),
&cr,
unit_rectangle(),
+ user_language,
dpi,
true,
is_testing,
@@ -290,12 +300,13 @@ impl Handle {
pub fn get_geometry_for_element(
&self,
id: Option<&str>,
+ user_language: &UserLanguage,
dpi: Dpi,
is_testing: bool,
) -> Result<(cairo::Rectangle, cairo::Rectangle), RenderingError> {
let node = self.get_node_or_root(id)?;
- let bbox = self.get_bbox_for_element(&node, dpi, is_testing)?;
+ let bbox = self.get_bbox_for_element(&node, user_language, dpi, is_testing)?;
let ink_rect = bbox.ink_rect.unwrap_or_default();
let logical_rect = bbox.rect.unwrap_or_default();
@@ -314,6 +325,7 @@ impl Handle {
cr: &cairo::Context,
id: Option<&str>,
element_viewport: &cairo::Rectangle,
+ user_language: &UserLanguage,
dpi: Dpi,
is_testing: bool,
) -> Result<(), RenderingError> {
@@ -321,7 +333,7 @@ impl Handle {
let node = self.get_node_or_root(id)?;
- let bbox = self.get_bbox_for_element(&node, dpi, is_testing)?;
+ let bbox = self.get_bbox_for_element(&node, user_language, dpi, is_testing)?;
if bbox.ink_rect.is_none() || bbox.rect.is_none() {
// Nothing to draw
@@ -349,6 +361,7 @@ impl Handle {
DrawingMode::OnlyNode(node),
&cr,
unit_rectangle(),
+ user_language,
dpi,
false,
is_testing,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]