Skip to main content

Length

Struct Length 

Source
pub struct Length { /* private fields */ }
Expand description

Length measurement stored as millipoints (1/1000 point)

Conversion factors:

  • 1 point = 1/72 inch
  • 1 inch = 72 points = 72000 millipoints
  • 1 mm = 2.834645669 points
  • 1 cm = 28.34645669 points

Font-relative units:

  • 1em = current font-size
  • 1ex = x-height of current font (typically ~0.5em)

Implementations§

Source§

impl Length

Source

pub const ZERO: Length

Zero length constant

Source

pub fn from_pt(pt: f64) -> Length

Create a length from points

Source

pub fn from_inch(inch: f64) -> Length

Create a length from inches

Source

pub fn from_mm(mm: f64) -> Length

Create a length from millimeters

Source

pub fn from_cm(cm: f64) -> Length

Create a length from centimeters

Source

pub const fn from_millipoints(millipoints: i32) -> Length

Create a length from millipoints directly

Source

pub fn from_em(em: f64) -> LengthUnit

Create a font-relative length in em units

1em equals the current font-size. The returned LengthUnit must be resolved with a FontContext to get an absolute length.

§Examples
use fop_types::{Length, FontContext, LengthUnit};

let em_unit = LengthUnit::Em(1.5);
let context = FontContext::new(Length::from_pt(12.0));
let resolved = Length::resolve_unit(&em_unit, &context);
assert_eq!(resolved, Length::from_pt(18.0)); // 1.5 * 12pt
Source

pub fn from_ex(ex: f64) -> LengthUnit

Create a font-relative length in ex units

1ex equals the x-height of the current font (typically ~0.5em). The returned LengthUnit must be resolved with a FontContext to get an absolute length.

§Examples
use fop_types::{Length, FontContext, LengthUnit};

let ex_unit = LengthUnit::Ex(2.0);
let context = FontContext::new(Length::from_pt(12.0));
let resolved = Length::resolve_unit(&ex_unit, &context);
// 2.0 * 6pt (x-height is 0.5 * font-size)
assert_eq!(resolved, Length::from_pt(12.0));
Source

pub fn resolve_unit(unit: &LengthUnit, context: &FontContext) -> Length

Resolve a LengthUnit to an absolute Length using a FontContext

For absolute units, returns the length directly. For em units, multiplies by the font size. For ex units, multiplies by the x-height.

§Examples
use fop_types::{Length, FontContext, LengthUnit};

let context = FontContext::new(Length::from_pt(12.0));

// Absolute unit
let abs = LengthUnit::Absolute(12000);
assert_eq!(Length::resolve_unit(&abs, &context), Length::from_pt(12.0));

// Em unit
let em = LengthUnit::Em(1.5);
assert_eq!(Length::resolve_unit(&em, &context), Length::from_pt(18.0));

// Ex unit
let ex = LengthUnit::Ex(2.0);
assert_eq!(Length::resolve_unit(&ex, &context), Length::from_pt(12.0));
Source

pub fn to_pt(self) -> f64

Get the value in points

Source

pub fn to_inch(self) -> f64

Get the value in inches

Source

pub fn to_mm(self) -> f64

Get the value in millimeters

Source

pub fn to_cm(self) -> f64

Get the value in centimeters

Source

pub const fn millipoints(self) -> i32

Get the raw millipoint value

Source

pub fn abs(self) -> Length

Get the absolute value

Trait Implementations§

Source§

impl Add for Length

Source§

type Output = Length

The resulting type after applying the + operator.
Source§

fn add(self, other: Length) -> Length

Performs the + operation. Read more
Source§

impl AddAssign for Length

Source§

fn add_assign(&mut self, other: Length)

Performs the += operation. Read more
Source§

impl Clone for Length

Source§

fn clone(&self) -> Length

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Length

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Length

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Div<i32> for Length

Source§

type Output = Length

The resulting type after applying the / operator.
Source§

fn div(self, scalar: i32) -> Length

Performs the / operation. Read more
Source§

impl Hash for Length

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul<i32> for Length

Source§

type Output = Length

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: i32) -> Length

Performs the * operation. Read more
Source§

impl Neg for Length

Source§

type Output = Length

The resulting type after applying the - operator.
Source§

fn neg(self) -> Length

Performs the unary - operation. Read more
Source§

impl Ord for Length

Source§

fn cmp(&self, other: &Length) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Length

Source§

fn eq(&self, other: &Length) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Length

Source§

fn partial_cmp(&self, other: &Length) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for Length

Source§

type Output = Length

The resulting type after applying the - operator.
Source§

fn sub(self, other: Length) -> Length

Performs the - operation. Read more
Source§

impl SubAssign for Length

Source§

fn sub_assign(&mut self, other: Length)

Performs the -= operation. Read more
Source§

impl Copy for Length

Source§

impl Eq for Length

Source§

impl StructuralPartialEq for Length

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.