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
impl Twips
Sourcepub const TWIPS_PER_PIXEL: i32 = 20i32
pub const TWIPS_PER_PIXEL: i32 = 20i32
There are 20 twips in a pixel.
Sourcepub const ONE_PX: Self
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);
Sourcepub const HALF_PX: Self
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);
Sourcepub const fn new(twips: i32) -> Self
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);
Sourcepub const fn get(self) -> i32
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);
Sourcepub fn from_pixels(pixels: f64) -> Self
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?
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}
Sourcepub const fn from_pixels_i32(pixels: i32) -> Self
pub const fn from_pixels_i32(pixels: i32) -> Self
Converts the given number of pixels
into twips.
Sourcepub fn to_pixels(self) -> f64
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);
Sourcepub fn trunc_to_pixel(self) -> Self
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));
Sourcepub fn round_to_pixel_ties_even(self) -> Self
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 AddAssign for Twips
impl AddAssign for Twips
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+=
operation. Read moreSource§impl DivAssign<i32> for Twips
impl DivAssign<i32> for Twips
Source§fn div_assign(&mut self, other: i32)
fn div_assign(&mut self, other: i32)
/=
operation. Read moreSource§impl MulAssign<i32> for Twips
impl MulAssign<i32> for Twips
Source§fn mul_assign(&mut self, other: i32)
fn mul_assign(&mut self, other: i32)
*=
operation. Read moreSource§impl Ord for Twips
impl Ord for Twips
Source§impl PartialOrd for Twips
impl PartialOrd for Twips
Source§impl SubAssign for Twips
impl SubAssign for Twips
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
-=
operation. Read more