summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Mena Quintero <federico@gnome.org>2023-03-22 17:08:33 -0600
committerMarge Bot <marge-bot@gnome.org>2023-03-23 01:41:14 +0000
commite9cd2617738273ad09af78638209706bd4b28cbc (patch)
tree7dc667055c460afa0b4d93e20c2dcecbd96d8f02
parentfe55161d826e8e674dd54b9c16d7fa31ac8b4e14 (diff)
downloadlibrsvg-e9cd2617738273ad09af78638209706bd4b28cbc.tar.gz
NormalizeValues: Hacky (?) way to extract what is needed from ComputedValues for normalization
I want to remove the use of ComputedValues from DrawingCtx::with_discrete_layer(), but it is needed for NormalizeParams::new(). So, provide a temporary type NormalizeValues that just extracts the information needed from ComputedValues - in this case, just the font size. Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/816>
-rw-r--r--src/length.rs32
1 files changed, 25 insertions, 7 deletions
diff --git a/src/length.rs b/src/length.rs
index 50d6addf..f5656cbe 100644
--- a/src/length.rs
+++ b/src/length.rs
@@ -51,7 +51,7 @@ use crate::dpi::Dpi;
use crate::drawing_ctx::ViewParams;
use crate::error::*;
use crate::parsers::{finite_f32, Parse};
-use crate::properties::ComputedValues;
+use crate::properties::{ComputedValues, FontSize};
use crate::rect::Rect;
use crate::viewbox::ViewBox;
@@ -302,6 +302,22 @@ impl<N: Normalize, V: Validate> Parse for CssLength<N, V> {
}
}
+/// Parameters for length normalization extractedfrom [`ComputedValues`].
+///
+/// This is a precursor to [`NormalizeParams::from_values`], for cases where it is inconvenient
+/// to keep a [`ComputedValues`] around.
+pub struct NormalizeValues {
+ font_size: FontSize,
+}
+
+impl NormalizeValues {
+ pub fn new(values: &ComputedValues) -> NormalizeValues {
+ NormalizeValues {
+ font_size: values.font_size(),
+ }
+ }
+}
+
/// Parameters to normalize [`Length`] values to user-space distances.
pub struct NormalizeParams {
vbox: ViewBox,
@@ -312,13 +328,15 @@ pub struct NormalizeParams {
impl NormalizeParams {
/// Extracts the information needed to normalize [`Length`] values from a set of
/// [`ComputedValues`] and the viewport size in [`ViewParams`].
- // TODO: It is awkward to pass a `ComputedValues` everywhere just to be able to get
- // the font size in the end. Can we instead have a `ComputedFontSize(FontSize)`
- // newtype, extracted from the `ComputedValues`?
pub fn new(values: &ComputedValues, params: &ViewParams) -> NormalizeParams {
+ let v = NormalizeValues::new(values);
+ NormalizeParams::from_values(&v, params)
+ }
+
+ pub fn from_values(v: &NormalizeValues, params: &ViewParams) -> NormalizeParams {
NormalizeParams {
vbox: params.vbox,
- font_size: font_size_from_values(values, params.dpi),
+ font_size: font_size_from_values(v, params.dpi),
dpi: params.dpi,
}
}
@@ -451,8 +469,8 @@ impl<N: Normalize, V: Validate> CssLength<N, V> {
}
}
-fn font_size_from_values(values: &ComputedValues, dpi: Dpi) -> f64 {
- let v = &values.font_size().value();
+fn font_size_from_values(values: &NormalizeValues, dpi: Dpi) -> f64 {
+ let v = values.font_size.value();
match v.unit {
LengthUnit::Percent => unreachable!("ComputedValues can't have a relative font size"),