Struct tiny_skia::Rect

source ·
pub struct Rect { /* private fields */ }
Expand description

A rectangle defined by left, top, right and bottom edges.

Can have zero width and/or height. But not a negative one.

§Guarantees

  • All values are finite.
  • Left edge is <= right.
  • Top edge is <= bottom.
  • Width and height are <= f32::MAX.

Implementations§

source§

impl Rect

source

pub fn from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> Option<Rect>

Creates new Rect.

Examples found in repository?
examples/image_on_image.rs (line 52)
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn create_triangle() -> Pixmap {
    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pb = PathBuilder::new();
    pb.move_to(0.0, 200.0);
    pb.line_to(200.0, 200.0);
    pb.line_to(100.0, 0.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(200, 200).unwrap();

    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );

    let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
    let stroke = Stroke::default();
    paint.set_color_rgba8(200, 0, 0, 220);

    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect

    pixmap
}
source

pub fn from_xywh(x: f32, y: f32, w: f32, h: f32) -> Option<Rect>

Creates new Rect.

Examples found in repository?
examples/mask.rs (line 24)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(250.0, 250.0, 200.0);
        pb.push_circle(250.0, 250.0, 100.0);
        pb.finish().unwrap()
    };

    let clip_path = clip_path
        .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
        .unwrap();

    let mut mask = Mask::new(500, 500).unwrap();
    mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());

    let mut paint = Paint::default();
    paint.anti_alias = false;
    paint.set_color_rgba8(50, 127, 150, 200);

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.fill_rect(
        Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
        &paint,
        Transform::identity(),
        Some(&mask),
    );
    pixmap.save_png("image.png").unwrap();
}
More examples
Hide additional examples
examples/large_image.rs (line 41)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
fn main() {
    let path1 = {
        let mut pb = PathBuilder::new();
        pb.move_to(1200.0, 1200.0);
        pb.line_to(3200.0, 18800.0);
        pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
        pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };

    let path2 = {
        let mut pb = PathBuilder::new();
        pb.move_to(18800.0, 1200.0);
        pb.line_to(16800.0, 18800.0);
        pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
        pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };

    let mut pixmap = Pixmap::new(20000, 20000).unwrap();

    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(10000.0, 10000.0, 7000.0);
        pb.finish().unwrap()
    };

    let mut mask = Mask::new(20000, 20000).unwrap();
    mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());

    let mut paint = Paint::default();
    paint.set_color_rgba8(90, 175, 100, 150);
    paint.anti_alias = true;
    let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
    pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);

    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;
    pixmap.fill_path(
        &path1,
        &paint,
        FillRule::Winding,
        Transform::default(),
        Some(&mask),
    );

    paint.set_color_rgba8(220, 140, 75, 180);
    paint.anti_alias = false;
    pixmap.fill_path(
        &path2,
        &paint,
        FillRule::Winding,
        Transform::default(),
        None,
    );

    paint.set_color_rgba8(255, 10, 15, 180);
    paint.anti_alias = true;
    let mut stroke = Stroke::default();
    stroke.width = 0.8; // hairline
    pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);

    pixmap.save_png("image.png").unwrap();
}
source

pub fn left(&self) -> f32

Returns the left edge.

source

pub fn top(&self) -> f32

Returns the top edge.

source

pub fn right(&self) -> f32

Returns the right edge.

source

pub fn bottom(&self) -> f32

Returns the bottom edge.

source

pub fn x(&self) -> f32

Returns rect’s X position.

source

pub fn y(&self) -> f32

Returns rect’s Y position.

source

pub fn width(&self) -> f32

Returns rect’s width.

source

pub fn height(&self) -> f32

Returns rect’s height.

source

pub fn round(&self) -> Option<IntRect>

Converts into an IntRect by adding 0.5 and discarding the fractional portion.

Width and height are guarantee to be >= 1.

source

pub fn round_out(&self) -> Option<IntRect>

Converts into an IntRect rounding outwards.

Width and height are guarantee to be >= 1.

source

pub fn intersect(&self, other: &Rect) -> Option<Rect>

Returns an intersection of two rectangles.

Returns None otherwise.

source

pub fn from_points(points: &[Point]) -> Option<Rect>

Creates a Rect from Point array.

Returns None if count is zero or if Point array contains an infinity or NaN.

source

pub fn inset(&self, dx: f32, dy: f32) -> Option<Rect>

Insets the rectangle by the specified offset.

source

pub fn outset(&self, dx: f32, dy: f32) -> Option<Rect>

Outsets the rectangle by the specified offset.

source

pub fn transform(&self, ts: Transform) -> Option<Rect>

Transforms the rect using the provided Transform.

This method is expensive.

source

pub fn bbox_transform(&self, bbox: NonZeroRect) -> Rect

Applies a bounding box transform.

source

pub fn to_non_zero_rect(&self) -> Option<NonZeroRect>

Converts into NonZeroRect.

Trait Implementations§

source§

impl Clone for Rect

source§

fn clone(&self) -> Rect

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 Rect

source§

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

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

impl PartialEq for Rect

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Rect

source§

impl StructuralPartialEq for Rect

Auto Trait Implementations§

§

impl RefUnwindSafe for Rect

§

impl Send for Rect

§

impl Sync for Rect

§

impl Unpin for Rect

§

impl UnwindSafe for Rect

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> 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,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.