pub struct SvgImage { /* private fields */ }
Expand description

Creates a struct holding an SVG image

Implementations

Loads the image from a filesystem path, doesn’t check for the validity of the data

Errors

Errors on non-existent path or invalid format

Examples found in repository?
examples/hello.rs (line 9)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let app = app::App::default().with_scheme(app::Scheme::Gleam);
    let mut wind = Window::new(100, 100, 400, 300, "Hello from rust");

    let mut frame = Frame::default().with_size(360, 260).center_of(&wind);
    frame.set_frame(FrameType::EngravedBox);
    let mut image = SvgImage::load("screenshots/RustLogo.svg").unwrap();
    image.scale(200, 200, true, true);
    frame.set_image(Some(image));

    wind.make_resizable(true);
    wind.end();
    wind.show();

    app.run().unwrap();
}

Loads the image from data/memory

Errors

Errors on invalid format

Examples found in repository?
examples/rounded_images.rs (line 39)
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
    pub fn new(radius: i32, mut image: image::RgbImage) -> Self {
        let mut frame = frame::Frame::new(0, 0, radius * 2, radius * 2, None);
        frame.set_frame(enums::FrameType::FlatBox);
        frame.draw(move |f| {
            image.scale(f.w(), f.h(), false, true);
            image.draw(f.x(), f.y(), f.w(), f.h());
            let color = f.color().to_rgb();
            let s = format!(
                "<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n
              <svg width='{}' height='{}'>\n
                <rect x='{}' 
                    y='{}' 
                    rx='{}' 
                    ry='{}' 
                    width='{}' 
                    height='{}' 
                    fill='none' 
                    stroke='rgb({}, {}, {})' 
                    stroke-width='{}' />\n
              </svg>\n",
                f.w(),
                f.h(),
                -f.w() / 2,
                -f.w() / 2,
                f.w(),
                f.w(),
                f.w() + f.w(),
                f.h() + f.w(),
                color.0,
                color.1,
                color.2,
                f.w()
            );
            let mut s = image::SvgImage::from_data(&s).unwrap();
            s.draw(f.x(), f.y(), f.w(), f.h());
        });
        Self
    }
More examples
Hide additional examples
examples/custom_widgets.rs (line 134)
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    pub fn new(x: i32, y: i32, w: i32, h: i32) -> Self {
        let mut frm = Frame::new(x, y, w, h, "");
        frm.set_frame(FrameType::FlatBox);
        frm.set_color(Color::Black);
        let on = Rc::from(RefCell::from(false));
        frm.draw({
            // storing two almost identical images here, in a real application this could be optimized
            let on = Rc::clone(&on);
            let mut on_svg =
                SvgImage::from_data(&POWER.to_string().replace("red", "green")).unwrap();
            on_svg.scale(frm.width(), frm.height(), true, true);
            let mut off_svg = SvgImage::from_data(POWER).unwrap();
            off_svg.scale(frm.width(), frm.height(), true, true);
            move |f| {
                if *on.borrow() {
                    on_svg.draw(f.x(), f.y(), f.width(), f.height());
                } else {
                    off_svg.draw(f.x(), f.y(), f.width(), f.height());
                };
            }
        });
        frm.handle({
            let on = on.clone();
            move |f, ev| match ev {
                Event::Push => {
                    let prev = *on.borrow();
                    *on.borrow_mut() = !prev;
                    f.do_callback();
                    f.redraw();
                    true
                }
                _ => false,
            }
        });
        Self { frm, on }
    }

Rasterize an SvgImage

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Performs a deep copy of the image

Draws the image at the presupplied coordinates and size

Draws the image at the presupplied coordinates and size and offset cx, cy

Return the width of the image

Return the height of the image

Return the width of the image

Return the height of the image

Returns a pointer of the image Read more

Transforms a raw image pointer to an image Read more

Returns the underlying raw rgb image data

Returns the underlying raw image data

Transforms the image into an RgbImage Read more

Transforms the image into an RgbImage Read more

Scales the image

Return the count of pointers in an image (Pixmaps have more than 1, bitmaps have 0, Rgb based images have 1)

Gets the image’s data width

Gets the image’s data height

Gets the image’s depth

Gets the image’s line data size

Greys the image

Deletes the image Read more

Checks if the image was deleted

Transforms an Image base into another Image Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.