pub struct Image<'a> { /* private fields */ }
Expand description
This is the structure of the image that will be created.
Use the
new
function to get started.
It is important to remember that the order in which elements are added to the image defines which element goes on top of which. For example, adding text that starts at point 0,0 and then adding a rectangle that also starts at the same point will cause the rectangle to cover the text. However, by reversing the order and adding the rectangle first, it will be placed underneath the text. It is essential to keep this order in mind when creating images with multiple elements to ensure that the elements are in the desired order.
§Examples
let mut image = Image::new(500, 500, colors::WHITE);
image.add_text(Text::new("Image Builder"));
image.add_rect(Rect::new().size(200, 200)); // This rectangle covers the text.
let mut image = Image::new(500, 500, colors::WHITE);
image.add_rect(Rect::new().size(200, 200)); // This rectangle is in the background of the text.
image.add_text(Text::new("Image Builder"));
Implementations§
Source§impl<'a> Image<'a>
impl<'a> Image<'a>
Sourcepub fn new(width: u32, height: u32, background: Color) -> Image<'a>
pub fn new(width: u32, height: u32, background: Color) -> Image<'a>
This method creates a new instance of an image, setting the background color, and size in pixels, and allocating memory to add fonts and elements to be drawn.
§Example
use image_builder::{colors, Image};
let mut image = Image::new(400, 300, colors::GRAY);
Sourcepub fn add_custom_font(&mut self, name: &'a str, font: Vec<u8>)
pub fn add_custom_font(&mut self, name: &'a str, font: Vec<u8>)
The add_custom_font method requires that a .ttf font file (not provided) be loaded using fs.read,
and internally linked to the provided name in a HashMap. This will allow you to use this font in
your text by simply passing the font name as a parameter. Trying to use a font that has not been
loaded cause the application to panic. Additionally, providing an invalid Vec
§Example
use image_builder::Image;
use std::fs;
use image_builder::colors;
let mut image = Image::new(500, 500, colors::WHITE);
let roboto_bold = fs::read("fonts/Roboto/Roboto-Bold.ttf").unwrap();
image.add_custom_font("Roboto bold", roboto_bold);
Sourcepub fn add_picture(&mut self, picture: Picture)
pub fn add_picture(&mut self, picture: Picture)
With this method, it is possible to add an image on top of the image being built, taking into account
transparent backgrounds. This means that transparent areas of the added image will not overlap areas
already drawn in the main image. Please refer to the Picture
for more details.
Sourcepub fn add_text(&mut self, text: Text)
pub fn add_text(&mut self, text: Text)
This method allows for adding formatted text to the image being built. Refer to the Text
for more details.
Sourcepub fn text_size(&mut self, text: &Text) -> (i32, i32)
pub fn text_size(&mut self, text: &Text) -> (i32, i32)
This method can be used before add_text
to reqeust the expected width and height of a
text element.
Sourcepub fn add_rect(&mut self, rect: Rect)
pub fn add_rect(&mut self, rect: Rect)
This method allows for adding rectangular shapes to the image being built. Refer to the Rect
for more details.
Sourcepub fn save(&mut self, file_name: &str)
pub fn save(&mut self, file_name: &str)
The save method is responsible for the entire rendering process of the library. It creates the image buffer and renders the list of elements added in the order they were inserted by the user. Then, it creates the image file, adds the generated buffer, and encodes the content to save it to the disk.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Image<'a>
impl<'a> RefUnwindSafe for Image<'a>
impl<'a> Send for Image<'a>
impl<'a> Sync for Image<'a>
impl<'a> Unpin for Image<'a>
impl<'a> UnwindSafe for Image<'a>
Blanket Implementations§
Source§impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
Source§fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
Source§impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
Source§type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
Source§fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, Dst> ConvAsUtil<Dst> for T
impl<T, Dst> ConvAsUtil<Dst> for T
Source§impl<T> ConvUtil for T
impl<T> ConvUtil for T
Source§fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
Source§fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.