RsvgLength: make ::parse() a constructor, not a standalone parse_length() function

parent deb3e8eb
......@@ -68,7 +68,7 @@ fn compute_named_size (name: &str) -> f64 {
pub extern fn rsvg_length_parse (string: *const libc::c_char, dir: LengthDir) -> RsvgLength {
let my_string = unsafe { &String::from_glib_none (string) };
parse_length (my_string, dir)
RsvgLength::parse (my_string, dir)
}
/* https://www.w3.org/TR/SVG/types.html#DataTypeLength
......@@ -82,77 +82,79 @@ pub extern fn rsvg_length_parse (string: *const libc::c_char, dir: LengthDir) ->
* when _rsvg_css_normalize_length() needs to know to what the
* length refers.
*/
pub fn parse_length (string: &str, dir: LengthDir) -> RsvgLength {
let unit: LengthUnit;
let (mut value, rest) = strtod (string);
match rest.as_ref () {
"%" => {
value *= 0.01; // normalize to [0, 1]
unit = LengthUnit::Percent;
},
"em" => {
unit = LengthUnit::FontEm;
},
"ex" => {
unit = LengthUnit::FontEx;
},
"pt" => {
value /= POINTS_PER_INCH;
unit = LengthUnit::Inch;
},
"in" => {
unit = LengthUnit::Inch;
},
"cm" => {
value /= CM_PER_INCH;
unit = LengthUnit::Inch;
},
"mm" => {
value /= MM_PER_INCH;
unit = LengthUnit::Inch;
},
"pc" => {
value /= PICA_PER_INCH;
unit = LengthUnit::Inch;
},
"larger" => {
unit = LengthUnit::RelativeLarger;
},
"smaller" => {
unit = LengthUnit::RelativeSmaller;
},
"xx-small" |
"x-small" |
"small" |
"medium" |
"large" |
"x-large" |
"xx-large" => {
value = compute_named_size (rest);
unit = LengthUnit::Inch;
},
_ => {
unit = LengthUnit::Default;
impl RsvgLength {
pub fn parse (string: &str, dir: LengthDir) -> RsvgLength {
let unit: LengthUnit;
let (mut value, rest) = strtod (string);
match rest.as_ref () {
"%" => {
value *= 0.01; // normalize to [0, 1]
unit = LengthUnit::Percent;
},
"em" => {
unit = LengthUnit::FontEm;
},
"ex" => {
unit = LengthUnit::FontEx;
},
"pt" => {
value /= POINTS_PER_INCH;
unit = LengthUnit::Inch;
},
"in" => {
unit = LengthUnit::Inch;
},
"cm" => {
value /= CM_PER_INCH;
unit = LengthUnit::Inch;
},
"mm" => {
value /= MM_PER_INCH;
unit = LengthUnit::Inch;
},
"pc" => {
value /= PICA_PER_INCH;
unit = LengthUnit::Inch;
},
"larger" => {
unit = LengthUnit::RelativeLarger;
},
"smaller" => {
unit = LengthUnit::RelativeSmaller;
},
"xx-small" |
"x-small" |
"small" |
"medium" |
"large" |
"x-large" |
"xx-large" => {
value = compute_named_size (rest);
unit = LengthUnit::Inch;
},
_ => {
unit = LengthUnit::Default;
}
}
}
RsvgLength {
length: value,
unit: unit,
dir: dir
RsvgLength {
length: value,
unit: unit,
dir: dir
}
}
}
......@@ -175,13 +177,13 @@ mod tests {
#[test]
fn parses_default () {
assert_eq! (parse_length ("42", LengthDir::Horizontal),
assert_eq! (RsvgLength::parse ("42", LengthDir::Horizontal),
RsvgLength {
length: 42.0,
unit: LengthUnit::Default,
dir: LengthDir::Horizontal});
assert_eq! (parse_length ("-42px", LengthDir::Horizontal),
assert_eq! (RsvgLength::parse ("-42px", LengthDir::Horizontal),
RsvgLength {
length: -42.0,
unit: LengthUnit::Default,
......@@ -190,7 +192,7 @@ mod tests {
#[test]
fn parses_percent () {
assert_eq! (parse_length ("50.0%", LengthDir::Horizontal),
assert_eq! (RsvgLength::parse ("50.0%", LengthDir::Horizontal),
RsvgLength {
length: 0.5,
unit: LengthUnit::Percent,
......@@ -199,7 +201,7 @@ mod tests {
#[test]
fn parses_font_em () {
assert_eq! (parse_length ("22.5em", LengthDir::Vertical),
assert_eq! (RsvgLength::parse ("22.5em", LengthDir::Vertical),
RsvgLength {
length: 22.5,
unit: LengthUnit::FontEm,
......@@ -208,7 +210,7 @@ mod tests {
#[test]
fn parses_font_ex () {
assert_eq! (parse_length ("22.5ex", LengthDir::Vertical),
assert_eq! (RsvgLength::parse ("22.5ex", LengthDir::Vertical),
RsvgLength {
length: 22.5,
unit: LengthUnit::FontEx,
......@@ -217,31 +219,31 @@ mod tests {
#[test]
fn parses_physical_units () {
assert_eq! (parse_length ("72pt", LengthDir::Both),
assert_eq! (RsvgLength::parse ("72pt", LengthDir::Both),
RsvgLength {
length: 1.0,
unit: LengthUnit::Inch,
dir: LengthDir::Both });
assert_eq! (parse_length ("-22.5in", LengthDir::Both),
assert_eq! (RsvgLength::parse ("-22.5in", LengthDir::Both),
RsvgLength {
length: -22.5,
unit: LengthUnit::Inch,
dir: LengthDir::Both });
assert_eq! (parse_length ("-25.4cm", LengthDir::Both),
assert_eq! (RsvgLength::parse ("-25.4cm", LengthDir::Both),
RsvgLength {
length: -10.0,
unit: LengthUnit::Inch,
dir: LengthDir::Both });
assert_eq! (parse_length ("254mm", LengthDir::Both),
assert_eq! (RsvgLength::parse ("254mm", LengthDir::Both),
RsvgLength {
length: 10.0,
unit: LengthUnit::Inch,
dir: LengthDir::Both });
assert_eq! (parse_length ("60pc", LengthDir::Both),
assert_eq! (RsvgLength::parse ("60pc", LengthDir::Both),
RsvgLength {
length: 10.0,
unit: LengthUnit::Inch,
......@@ -250,7 +252,7 @@ mod tests {
#[test]
fn parses_relative_larger () {
assert_eq! (parse_length ("larger", LengthDir::Vertical),
assert_eq! (RsvgLength::parse ("larger", LengthDir::Vertical),
RsvgLength {
length: 0.0,
unit: LengthUnit::RelativeLarger,
......@@ -259,7 +261,7 @@ mod tests {
#[test]
fn parses_relative_smaller () {
assert_eq! (parse_length ("smaller", LengthDir::Vertical),
assert_eq! (RsvgLength::parse ("smaller", LengthDir::Vertical),
RsvgLength {
length: 0.0,
unit: LengthUnit::RelativeSmaller,
......@@ -282,7 +284,7 @@ mod tests {
// enforce a particular sequence.
for name in names {
let length = parse_length (name, LengthDir::Both);
let length = RsvgLength::parse (name, LengthDir::Both);
assert_eq! (length.unit, LengthUnit::Inch);
assert_eq! (length.dir, LengthDir::Both);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment