Struct Twips

Source
pub struct Twips(/* private fields */);
Expand description

A type-safe wrapper type documenting where “twips” are used in the SWF format.

A twip is 1/20th of a pixel. Most coordinates in an SWF file are represented in twips.

Use the from_pixels and to_pixels methods to convert to and from pixel values.

Please be careful when using twips for calculations to avoid overflows. As an example, since it takes 20 twips to get 1 pixel, 2,000 pixels are 40,000 twips, or 4*10^4 . If you then have two such numbers, multiplying them as part of calculations yields 16*10^8, which is relatively close to the upper limit of i32 at about 2*10^9.

Implementations§

Source§

impl Twips

Source

pub const TWIPS_PER_PIXEL: i32 = 20i32

There are 20 twips in a pixel.

Source

pub const ZERO: Self

The Twips object with a value of 0.

§Examples
assert_eq!(swf::Twips::ZERO.to_pixels(), 0.0);
Source

pub const ONE_PX: Self

The Twips object with a value of 1 pixel.

§Examples
assert_eq!(swf::Twips::ONE_PX.to_pixels(), 1.0);
Source

pub const HALF_PX: Self

The Twips object with a value of 0.5 pixels.

§Examples
assert_eq!(swf::Twips::HALF_PX.to_pixels(), 0.5);
Source

pub const fn new(twips: i32) -> Self

Creates a new Twips object. Note that the twips value is in twips, not pixels. Use the from_pixels method to convert from pixel units.

§Examples
use swf::Twips;

let twips = Twips::new(40);
Source

pub const fn get(self) -> i32

Returns the number of twips.

§Examples
use swf::Twips;

let twips = Twips::new(47);
assert_eq!(twips.get(), 47);
Source

pub fn from_pixels(pixels: f64) -> Self

Converts the given number of pixels into twips.

This may be a lossy conversion; any precision more than a twip (1/20 pixels) is truncated.

§Examples
use swf::Twips;

// 40 pixels is equivalent to 800 twips.
let twips = Twips::from_pixels(40.0);
assert_eq!(twips.get(), 800);

// Output is truncated if more precise than a twip (1/20 pixels).
let twips = Twips::from_pixels(40.018);
assert_eq!(twips.get(), 800);
Examples found in repository?
examples/writing.rs (line 9)
3fn main() {
4    let header = Header {
5        compression: Compression::Zlib,
6        version: 6,
7        stage_size: Rectangle {
8            x_min: Twips::ZERO,
9            x_max: Twips::from_pixels(400.0),
10            y_min: Twips::ZERO,
11            y_max: Twips::from_pixels(400.0),
12        },
13        frame_rate: Fixed8::from_f32(60.0),
14        num_frames: 1,
15    };
16    let tags = [
17        Tag::SetBackgroundColor(Color {
18            r: 255,
19            g: 0,
20            b: 0,
21            a: 255,
22        }),
23        Tag::ShowFrame,
24    ];
25    let file = std::fs::File::create("tests/swfs/SimpleRedBackground.swf").unwrap();
26    let writer = std::io::BufWriter::new(file);
27    swf::write_swf(&header, &tags, writer).unwrap();
28}
Source

pub const fn from_pixels_i32(pixels: i32) -> Self

Converts the given number of pixels into twips.

Source

pub fn to_pixels(self) -> f64

Converts this twips value into pixel units.

This is a lossless operation.

§Examples
use swf::Twips;

// 800 twips is equivalent to 40 pixels.
let twips = Twips::new(800);
assert_eq!(twips.to_pixels(), 40.0);

// Twips are sub-pixel: 713 twips represent 35.65 pixels.
let twips = Twips::new(713);
assert_eq!(twips.to_pixels(), 35.65);
Source

pub fn trunc_to_pixel(self) -> Self

Truncates this twips to a pixel.

§Examples
use swf::Twips;

assert_eq!(Twips::new(23).trunc_to_pixel(), Twips::new(20));
assert_eq!(Twips::new(439).trunc_to_pixel(), Twips::new(420));
assert_eq!(Twips::new(-47).trunc_to_pixel(), Twips::new(-40));
Source

pub fn round_to_pixel_ties_even(self) -> Self

Rounds this twips to the nearest pixel. Rounds half-way cases to the nearest even pixel.

§Examples
use swf::Twips;

assert_eq!(Twips::new(29).round_to_pixel_ties_even(), Twips::new(20));
assert_eq!(Twips::new(30).round_to_pixel_ties_even(), Twips::new(40));
assert_eq!(Twips::new(31).round_to_pixel_ties_even(), Twips::new(40));

Trait Implementations§

Source§

impl Add for Twips

Source§

type Output = Twips

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Twips

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl Clone for Twips

Source§

fn clone(&self) -> Twips

Returns a copy 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 Twips

Source§

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

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

impl Default for Twips

Source§

fn default() -> Twips

Returns the “default value” for a type. Read more
Source§

impl Display for Twips

Source§

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

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

impl Div<i32> for Twips

Source§

type Output = Twips

The resulting type after applying the / operator.
Source§

fn div(self, other: i32) -> Self

Performs the / operation. Read more
Source§

impl DivAssign<i32> for Twips

Source§

fn div_assign(&mut self, other: i32)

Performs the /= operation. Read more
Source§

impl Hash for Twips

Source§

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

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 Twips

Source§

type Output = Twips

The resulting type after applying the * operator.
Source§

fn mul(self, other: i32) -> Self

Performs the * operation. Read more
Source§

impl MulAssign<i32> for Twips

Source§

fn mul_assign(&mut self, other: i32)

Performs the *= operation. Read more
Source§

impl Neg for Twips

Source§

type Output = Twips

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for Twips

Source§

fn cmp(&self, other: &Twips) -> 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 Twips

Source§

fn eq(&self, other: &Twips) -> 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 Twips

Source§

fn partial_cmp(&self, other: &Twips) -> 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 Twips

Source§

type Output = Twips

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for Twips

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl Copy for Twips

Source§

impl Eq for Twips

Source§

impl StructuralPartialEq for Twips

Auto Trait Implementations§

§

impl Freeze for Twips

§

impl RefUnwindSafe for Twips

§

impl Send for Twips

§

impl Sync for Twips

§

impl Unpin for Twips

§

impl UnwindSafe for Twips

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.