[librsvg: 1/3] FontSizeSpec: move to a font_props.rs file
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 1/3] FontSizeSpec: move to a font_props.rs file
- Date: Thu, 9 Aug 2018 00:06:13 +0000 (UTC)
commit 7ae2bb3053fbbe65e032f688d3c7c1aef58f5702
Author: Federico Mena Quintero <federico gnome org>
Date: Wed Aug 8 17:55:46 2018 -0500
FontSizeSpec: move to a font_props.rs file
We'll use this file for various types/properties that need their own
custom parsers.
Makefile.am | 1 +
rsvg_internals/src/font_props.rs | 123 +++++++++++++++++++++++++++++++++++++++
rsvg_internals/src/length.rs | 112 +----------------------------------
rsvg_internals/src/lib.rs | 1 +
rsvg_internals/src/state.rs | 3 +-
5 files changed, 128 insertions(+), 112 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 69bf4bc3..f22aef00 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -80,6 +80,7 @@ RUST_SRC = \
rsvg_internals/src/filters/turbulence.rs \
rsvg_internals/src/error.rs \
rsvg_internals/src/float_eq_cairo.rs \
+ rsvg_internals/src/font_props.rs \
rsvg_internals/src/gradient.rs \
rsvg_internals/src/handle.rs \
rsvg_internals/src/image.rs \
diff --git a/rsvg_internals/src/font_props.rs b/rsvg_internals/src/font_props.rs
new file mode 100644
index 00000000..32963869
--- /dev/null
+++ b/rsvg_internals/src/font_props.rs
@@ -0,0 +1,123 @@
+use cssparser::{Parser, Token};
+
+use drawing_ctx::DrawingCtx;
+use error::*;
+use length::{Length, LengthDir, LengthUnit, POINTS_PER_INCH};
+use parsers::Parse;
+use state::ComputedValues;
+
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub enum FontSizeSpec {
+ Smaller,
+ Larger,
+ XXSmall,
+ XSmall,
+ Small,
+ Medium,
+ Large,
+ XLarge,
+ XXLarge,
+ Value(Length),
+}
+
+impl FontSizeSpec {
+ pub fn value(&self) -> Length {
+ match self {
+ FontSizeSpec::Value(s) => s.clone(),
+ _ => unreachable!(),
+ }
+ }
+
+ pub fn compute(&self, v: &ComputedValues) -> Self {
+ let compute_points = |p| 12.0 * 1.2f64.powf(p) / POINTS_PER_INCH;
+
+ let size = v.font_size.0.value();
+
+ let new_size = match self {
+ FontSizeSpec::Smaller => Length::new(size.length / 1.2, size.unit, LengthDir::Both),
+ FontSizeSpec::Larger => Length::new(size.length * 1.2, size.unit, LengthDir::Both),
+ FontSizeSpec::XXSmall => {
+ Length::new(compute_points(-3.0), LengthUnit::Inch, LengthDir::Both)
+ }
+ FontSizeSpec::XSmall => {
+ Length::new(compute_points(-2.0), LengthUnit::Inch, LengthDir::Both)
+ }
+ FontSizeSpec::Small => {
+ Length::new(compute_points(-1.0), LengthUnit::Inch, LengthDir::Both)
+ }
+ FontSizeSpec::Medium => {
+ Length::new(compute_points(0.0), LengthUnit::Inch, LengthDir::Both)
+ }
+ FontSizeSpec::Large => {
+ Length::new(compute_points(1.0), LengthUnit::Inch, LengthDir::Both)
+ }
+ FontSizeSpec::XLarge => {
+ Length::new(compute_points(2.0), LengthUnit::Inch, LengthDir::Both)
+ }
+ FontSizeSpec::XXLarge => {
+ Length::new(compute_points(3.0), LengthUnit::Inch, LengthDir::Both)
+ }
+ FontSizeSpec::Value(s) if s.unit == LengthUnit::Percent => {
+ Length::new(size.length * s.length, size.unit, LengthDir::Both)
+ }
+ FontSizeSpec::Value(s) => s.clone(),
+ };
+
+ FontSizeSpec::Value(new_size)
+ }
+
+ pub fn normalize(&self, values: &ComputedValues, draw_ctx: &DrawingCtx) -> f64 {
+ self.value().normalize(values, draw_ctx)
+ }
+}
+
+impl Parse for FontSizeSpec {
+ type Data = ();
+ type Err = AttributeError;
+
+ fn parse(parser: &mut Parser, _: Self::Data) -> Result<FontSizeSpec, ::error::AttributeError> {
+ let parser_state = parser.state();
+
+ Length::parse(parser, LengthDir::Both)
+ .and_then(|s| Ok(FontSizeSpec::Value(s)))
+ .or_else(|e| {
+ parser.reset(&parser_state);
+
+ {
+ let token = parser.next().map_err(|_| {
+ ::error::AttributeError::Parse(::parsers::ParseError::new("expected token"))
+ })?;
+
+ if let Token::Ident(ref cow) = token {
+ match cow.as_ref() {
+ "smaller" => return Ok(FontSizeSpec::Smaller),
+ "larger" => return Ok(FontSizeSpec::Larger),
+ "xx-small" => return Ok(FontSizeSpec::XXSmall),
+ "x-small" => return Ok(FontSizeSpec::XSmall),
+ "small" => return Ok(FontSizeSpec::Small),
+ "medium" => return Ok(FontSizeSpec::Medium),
+ "large" => return Ok(FontSizeSpec::Large),
+ "x-large" => return Ok(FontSizeSpec::XLarge),
+ "xx-large" => return Ok(FontSizeSpec::XXLarge),
+ _ => (),
+ };
+ }
+ }
+
+ parser.reset(&parser_state);
+
+ Err(e)
+ })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn invalid_font_size_yields_error() {
+ assert!(is_parse_error(&FontSizeSpec::parse_str("furlong", ())));
+ }
+
+}
diff --git a/rsvg_internals/src/length.rs b/rsvg_internals/src/length.rs
index 85c3d96e..7fe2bcf4 100644
--- a/rsvg_internals/src/length.rs
+++ b/rsvg_internals/src/length.rs
@@ -40,7 +40,7 @@ impl Default for Length {
}
}
-const POINTS_PER_INCH: f64 = 72.0;
+pub const POINTS_PER_INCH: f64 = 72.0;
const CM_PER_INCH: f64 = 2.54;
const MM_PER_INCH: f64 = 25.4;
const PICA_PER_INCH: f64 = 6.0;
@@ -257,111 +257,6 @@ fn viewport_percentage(x: f64, y: f64) -> f64 {
(x * x + y * y).sqrt() / SQRT_2
}
-#[derive(Debug, Copy, Clone, PartialEq)]
-pub enum FontSizeSpec {
- Smaller,
- Larger,
- XXSmall,
- XSmall,
- Small,
- Medium,
- Large,
- XLarge,
- XXLarge,
- Value(Length),
-}
-
-impl FontSizeSpec {
- pub fn value(&self) -> Length {
- match self {
- FontSizeSpec::Value(s) => s.clone(),
- _ => unreachable!(),
- }
- }
-
- pub fn compute(&self, v: &ComputedValues) -> Self {
- let compute_points = |p| 12.0 * 1.2f64.powf(p) / POINTS_PER_INCH;
-
- let size = v.font_size.0.value();
-
- let new_size = match self {
- FontSizeSpec::Smaller => Length::new(size.length / 1.2, size.unit, LengthDir::Both),
- FontSizeSpec::Larger => Length::new(size.length * 1.2, size.unit, LengthDir::Both),
- FontSizeSpec::XXSmall => {
- Length::new(compute_points(-3.0), LengthUnit::Inch, LengthDir::Both)
- }
- FontSizeSpec::XSmall => {
- Length::new(compute_points(-2.0), LengthUnit::Inch, LengthDir::Both)
- }
- FontSizeSpec::Small => {
- Length::new(compute_points(-1.0), LengthUnit::Inch, LengthDir::Both)
- }
- FontSizeSpec::Medium => {
- Length::new(compute_points(0.0), LengthUnit::Inch, LengthDir::Both)
- }
- FontSizeSpec::Large => {
- Length::new(compute_points(1.0), LengthUnit::Inch, LengthDir::Both)
- }
- FontSizeSpec::XLarge => {
- Length::new(compute_points(2.0), LengthUnit::Inch, LengthDir::Both)
- }
- FontSizeSpec::XXLarge => {
- Length::new(compute_points(3.0), LengthUnit::Inch, LengthDir::Both)
- }
- FontSizeSpec::Value(s) if s.unit == LengthUnit::Percent => {
- Length::new(size.length * s.length, size.unit, LengthDir::Both)
- }
- FontSizeSpec::Value(s) => s.clone(),
- };
-
- FontSizeSpec::Value(new_size)
- }
-
- pub fn normalize(&self, values: &ComputedValues, draw_ctx: &DrawingCtx) -> f64 {
- self.value().normalize(values, draw_ctx)
- }
-}
-
-impl Parse for FontSizeSpec {
- type Data = ();
- type Err = AttributeError;
-
- fn parse(parser: &mut Parser, _: Self::Data) -> Result<FontSizeSpec, ::error::AttributeError> {
- let parser_state = parser.state();
-
- Length::parse(parser, LengthDir::Both)
- .and_then(|s| Ok(FontSizeSpec::Value(s)))
- .or_else(|e| {
- parser.reset(&parser_state);
-
- {
- let token = parser.next().map_err(|_| {
- ::error::AttributeError::Parse(::parsers::ParseError::new("expected token"))
- })?;
-
- if let Token::Ident(ref cow) = token {
- match cow.as_ref() {
- "smaller" => return Ok(FontSizeSpec::Smaller),
- "larger" => return Ok(FontSizeSpec::Larger),
- "xx-small" => return Ok(FontSizeSpec::XXSmall),
- "x-small" => return Ok(FontSizeSpec::XSmall),
- "small" => return Ok(FontSizeSpec::Small),
- "medium" => return Ok(FontSizeSpec::Medium),
- "large" => return Ok(FontSizeSpec::Large),
- "x-large" => return Ok(FontSizeSpec::XLarge),
- "xx-large" => return Ok(FontSizeSpec::XXLarge),
- _ => (),
- };
- }
- }
-
- parser.reset(&parser_state);
-
- Err(e)
- })
- }
-}
-
#[derive(Debug, PartialEq, Clone)]
pub enum Dasharray {
None,
@@ -510,11 +405,6 @@ mod tests {
);
}
- #[test]
- fn invalid_font_size_yields_error() {
- assert!(is_parse_error(&FontSizeSpec::parse_str("furlong", ())));
- }
-
fn parse_dash_array_str(s: &str) -> Result<Dasharray, AttributeError> {
Dasharray::parse_str(s, ())
}
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 62e9c2dd..b2a8610c 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -91,6 +91,7 @@ mod defs;
mod drawing_ctx;
mod error;
pub mod filters;
+mod font_props;
mod gradient;
mod handle;
mod image;
diff --git a/rsvg_internals/src/state.rs b/rsvg_internals/src/state.rs
index 767628bd..78c40fb8 100644
--- a/rsvg_internals/src/state.rs
+++ b/rsvg_internals/src/state.rs
@@ -8,9 +8,10 @@ use std::str::FromStr;
use attributes::Attribute;
use error::*;
+use font_props::FontSizeSpec;
use handle::RsvgHandle;
use iri::IRI;
-use length::{Dasharray, FontSizeSpec, Length, LengthDir, LengthUnit};
+use length::{Dasharray, Length, LengthDir, LengthUnit};
use node::RsvgNode;
use paint_server::PaintServer;
use parsers::{Parse, ParseError};
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]