pub struct Length {
pub amount: I48F16,
pub unit: LengthUnit,
}Expand description
A dimension: an exact amount of a LengthUnit.
A length is written and projected as its decimal amount followed directly
by its unit — 1tab, 1.5em, 10pt — through its Display, and read
back from that text by Length::parse. Two equal lengths compare equal
and hash alike; a length is never ordered against another, since 1in and
2pt have no order a derived comparison would get right. The default is
the zero length, 0tab.
See the module documentation for why the amount is exact and what each unit measures.
§Examples
use flm_core::stdlib::backends::pres::{Length, LengthUnit};
let indent = Length::parse("1.5em")?;
assert_eq!(indent.unit, LengthUnit::Ems);
assert_eq!(indent.to_string(), "1.5em");
assert_eq!(Length::tabs(2).to_string(), "2tab");
assert!(Length::parse("1.5").is_err());Fields§
§amount: I48F16The amount, exact to one scaled point (1/65536 of the unit).
unit: LengthUnitThe unit the amount counts.
Implementations§
Source§impl Length
impl Length
Sourcepub fn new(amount: I48F16, unit: LengthUnit) -> Length
pub fn new(amount: I48F16, unit: LengthUnit) -> Length
The length of amount units of unit.
Sourcepub fn tabs(count: i32) -> Length
pub fn tabs(count: i32) -> Length
A whole number of tabs: count units of LengthUnit::Tabs, how a
line indented by count \indent commands is indented.
Sourcepub fn parse(text: &str) -> Result<Length, LengthError>
pub fn parse(text: &str) -> Result<Length, LengthError>
The length written as text: a decimal amount followed directly by a
unit’s stable name, such as 1tab, 1.5em, -2pt or .5in.
The amount is an optional sign, digits, and optionally a full stop followed by digits, with at least one digit in all; it is rounded to the nearest scaled point (1/65536 of the unit). No space may be written between the amount and the unit, nor around the length.
§Errors
A LengthError saying what is wrong: an empty text, a text with no
unit (1.5), an unknown unit (2xx), no amount before the unit
(pt), an amount that is not a decimal number (1.2.3pt, 1e3pt),
or an amount too large to hold.
Source§impl Length
impl Length
Sourcepub fn convert_to(
&self,
unit: LengthUnit,
) -> Result<Length, LengthConversionError>
pub fn convert_to( &self, unit: LengthUnit, ) -> Result<Length, LengthConversionError>
The same length written in unit: 2.54cm in inches is 1in.
The conversion is exact but for one rounding of the result to the
nearest scaled point of unit, a half rounded away from zero (see the
module documentation).
§Errors
LengthConversionError::NotPhysical where the length’s unit or
unit is a tab or an em, and LengthConversionError::Overflow
where the result is too large to hold.
§Examples
use flm_core::stdlib::backends::pres::{Length, LengthUnit};
let inch = Length::parse("2.54cm")?.convert_to(LengthUnit::Inches)?;
assert_eq!(inch.to_string(), "1in");
assert!(Length::parse("1em")?.convert_to(LengthUnit::Points).is_err());Sourcepub fn scaled(
&self,
numerator: &Length,
denominator: &Length,
unit: LengthUnit,
) -> Result<Length, LengthConversionError>
pub fn scaled( &self, numerator: &Length, denominator: &Length, unit: LengthUnit, ) -> Result<Length, LengthConversionError>
This length multiplied by the ratio numerator ÷ denominator of two
lengths, written in unit.
This is how one dimension of a picture follows from the other while
keeping its proportions: a picture 340pt wide and 170pt high shown
5cm wide is 170pt scaled by 5cm ÷ 340pt, written in cm, a height
of 2.5cm. The three lengths may be in any physical units; the result
is computed exactly and rounded once, to the nearest scaled point of
unit, a half rounded away from zero.
§Errors
LengthConversionError::NotPhysical where any of the three lengths
or unit is a tab or an em; LengthConversionError::ZeroDenominator
where denominator is zero; LengthConversionError::Overflow where
the result is too large to hold.
§Examples
use flm_core::stdlib::backends::pres::{Length, LengthUnit};
let height = Length::parse("170pt")?;
let given_width = Length::parse("5cm")?;
let width = Length::parse("340pt")?;
let shown = height.scaled(&given_width, &width, given_width.unit)?;
assert_eq!(shown.to_string(), "2.5cm");Sourcepub fn times(&self, factor: I48F16) -> Result<Length, LengthConversionError>
pub fn times(&self, factor: I48F16) -> Result<Length, LengthConversionError>
This length multiplied by factor, in the same unit.
This is how an image is magnified for one output. The product is computed exactly and rounded once, to the nearest scaled point of the unit, a half rounded away from zero. Any unit is accepted, since the unit does not change.
§Errors
LengthConversionError::Overflow where the result is too large to
hold.
§Examples
use flm_core::stdlib::backends::pres::{I48F16, Length};
let width = Length::parse("4cm")?;
assert_eq!(width.times(I48F16::from_num(1.25))?.to_string(), "5cm");Sourcepub fn from_dots(
dots: u64,
dots_per_inch: I48F16,
) -> Result<Length, LengthConversionError>
pub fn from_dots( dots: u64, dots_per_inch: I48F16, ) -> Result<Length, LengthConversionError>
The size, in points, of dots dots printed at dots_per_inch dots
per inch: dots × 72 ÷ dots_per_inch points, a point being 1/72 inch.
This is the physical size of a picture of dots pixels at a stated
resolution. It is computed exactly and rounded once, to the nearest
scaled point (1/65536 pt), a half rounded away from zero.
§Errors
LengthConversionError::ZeroDenominator where dots_per_inch is
zero, LengthConversionError::Overflow where the result is too
large to hold. A negative resolution gives a negative length.
§Examples
use flm_core::stdlib::backends::pres::{I48F16, Length};
assert_eq!(Length::from_dots(300, I48F16::from_num(150))?.to_string(), "144pt");Trait Implementations§
impl Copy for Length
impl Eq for Length
impl StructuralPartialEq for Length
Auto Trait Implementations§
impl Forget for Length
impl Freeze for Length
impl Move for Length
impl RefUnwindSafe for Length
impl Send for Length
impl Sync for Length
impl Unpin for Length
impl UnsafeUnpin for Length
impl UnwindSafe for Length
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more