1
  2
  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
 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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

/*!
[resvg](https://github.com/RazrFalcon/resvg) is an SVG rendering library.
*/

#![doc(html_root_url = "https://docs.rs/resvg/0.12.0")]

#![warn(missing_docs)]

pub use rgb::RGBA8;
pub use usvg::ScreenSize;

use usvg::NodeExt;
use log::warn;

#[macro_use] mod macros;
mod clip;
mod filter;
mod image;
mod layers;
mod mask;
mod paint_server;
mod path;
mod render;


/// A raster image that contains rendering results.
///
/// Unpremultiplied RGBA color type is used.
#[derive(Clone)]
pub struct Image {
    data: Vec<u8>,
    width: u32,
    height: u32,
}

impl Image {
    fn from_canvas(canvas: tiny_skia::Canvas) -> Self {
        use rgb::FromSlice;

        let width = canvas.pixmap.width();
        let height = canvas.pixmap.height();

        let mut data = canvas.pixmap.take();
        svgfilters::demultiply_alpha(data.as_rgba_mut());

        Image {
            data,
            width,
            height,
        }
    }

    /// Returns the image width.
    ///
    /// Newer zero.
    pub fn width(&self) -> u32 {
        self.width
    }

    /// Returns the image height.
    ///
    /// Newer zero.
    pub fn height(&self) -> u32 {
        self.height
    }

    /// Returns the image size.
    pub fn size(&self) -> ScreenSize {
        ScreenSize::new(self.width(), self.height()).unwrap()
    }

    /// Returns the image content as `u8` slice.
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    /// Returns the underlying data.
    pub fn take(self) -> Vec<u8> {
        self.data
    }

    /// Save the image as PNG at a provided path.
    pub fn save_png<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), png::EncodingError> {
        let file = std::fs::File::create(path)?;
        let ref mut w = std::io::BufWriter::new(file);

        let mut encoder = png::Encoder::new(w, self.width(), self.height());
        encoder.set_color(png::ColorType::RGBA);
        encoder.set_depth(png::BitDepth::Eight);

        let mut writer = encoder.write_header()?;
        writer.write_image_data(&self.data())
    }
}


/// Renders an SVG to image.
pub fn render(
    tree: &usvg::Tree,
    fit_to: usvg::FitTo,
    background: Option<usvg::Color>,
) -> Option<Image> {
    let (mut canvas, size)
        = render::create_root_image(tree.svg_node().size.to_screen_size(), fit_to, background)?;
    render::render_to_canvas(tree, size, &mut canvas);
    Some(Image::from_canvas(canvas))
}

/// Renders an SVG node to image.
pub fn render_node(
    node: &usvg::Node,
    fit_to: usvg::FitTo,
    background: Option<usvg::Color>,
) -> Option<Image> {
    let node_bbox = if let Some(bbox) = node.calculate_bbox() {
        bbox
    } else {
        warn!("Node '{}' has zero size.", node.id());
        return None;
    };

    let vbox = usvg::ViewBox {
        rect: node_bbox,
        aspect: usvg::AspectRatio::default(),
    };

    let (mut img, img_size)
        = render::create_root_image(node_bbox.size().to_screen_size(), fit_to, background)?;

    render::render_node_to_canvas(node, vbox, img_size, &mut render::RenderState::Ok, &mut img);
    Some(Image::from_canvas(img))
}