extern crate core;
pub mod clipping;
pub mod drawable;
pub mod drawing;
pub mod image;
#[cfg(feature = "image_loading")]
pub mod image_loading;
pub mod indexed;
pub mod integration;
pub mod renderable_image;
pub mod renderable_macros;
pub mod scaling;
pub mod shapes;
pub mod text;
use crate::clipping::Clip;
use crate::GraphicsError::InvalidBufferLength;
use fnv::FnvHashMap;
use graphics_shapes::coord::Coord;
use ici_files::errors::IndexedImageError;
use thiserror::Error;
pub mod prelude {
pub use crate::drawable::*;
pub use crate::drawing::*;
pub use crate::image::*;
#[cfg(feature = "image_loading")]
pub use crate::image_loading::*;
pub use crate::indexed::*;
#[allow(unused_imports)]
pub use crate::integration::*;
pub use crate::shapes::collection::*;
pub use crate::shapes::polyline::*;
pub use crate::shapes::*;
pub use crate::text::format::*;
pub use crate::text::pos::*;
pub use crate::text::wrapping::*;
pub use crate::text::*;
pub use crate::Graphics;
pub use crate::GraphicsError;
pub use graphics_shapes::prelude::*;
pub use ici_files::prelude::*;
#[cfg(feature = "image_loading")]
pub use image_lib::ImageError;
#[cfg(feature = "image_loading")]
pub use image_lib::ImageFormat;
}
#[derive(Error, Debug)]
pub enum GraphicsError {
#[error("Invalid buffer length, expected: {0}, found: {1}")]
InvalidBufferLength(usize, usize),
#[error("Invalid pixel array length, expected: {0}, found: {1}")]
ImageInitSize(usize, usize),
#[error("Both images must be the same size, expected: {0}x{1}, found: {2}x{3}")]
ImageBlendSize(usize, usize, usize, usize),
#[error("Over 255 colours have been drawn")]
TooManyColors,
#[error("Size is greater than 255x255: {0}x{1}")]
TooBig(usize, usize),
#[error("Creating image")]
ImageError(IndexedImageError),
}
pub struct Graphics<'buffer> {
buffer: &'buffer mut [u8],
width: usize,
height: usize,
translate: Coord,
clip: Clip,
pub custom_font: FnvHashMap<u8, CustomLetter>,
}
impl Graphics<'_> {
#[inline]
pub fn create_buffer(width: usize, height: usize) -> Vec<u8> {
vec![0; width * height * 4]
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct CustomLetter {
pub _4x4: [bool; text::font::standard_4x4::LETTER_PX_COUNT],
pub _4x5: [bool; text::font::standard_4x5::LETTER_PX_COUNT],
pub _6x7: [bool; text::font::standard_6x7::LETTER_PX_COUNT],
pub _7x9: [bool; text::font::outline_7x9::LETTER_PX_COUNT],
pub _8x8: [bool; text::font::script_8x8::LETTER_PX_COUNT],
pub _8x10: [bool; text::font::standard_8x10::LETTER_PX_COUNT],
pub _3x5: [bool; text::font::limited_3x5::LETTER_PX_COUNT],
}
impl Default for CustomLetter {
fn default() -> Self {
Self {
_4x4: [false; text::font::standard_4x4::LETTER_PX_COUNT],
_4x5: [false; text::font::standard_4x5::LETTER_PX_COUNT],
_6x7: [false; text::font::standard_6x7::LETTER_PX_COUNT],
_7x9: [false; text::font::outline_7x9::LETTER_PX_COUNT],
_8x8: [false; text::font::script_8x8::LETTER_PX_COUNT],
_8x10: [false; text::font::standard_8x10::LETTER_PX_COUNT],
_3x5: [false; text::font::limited_3x5::LETTER_PX_COUNT],
}
}
}
impl<'buffer> Graphics<'_> {
pub fn new(
buffer: &'buffer mut [u8],
width: usize,
height: usize,
) -> Result<Graphics<'buffer>, GraphicsError> {
let count = width * height * 4;
if count != buffer.len() {
return Err(InvalidBufferLength(count, buffer.len()));
}
Ok(Graphics {
buffer,
width,
height,
translate: Coord::default(),
clip: Clip::new(width, height),
custom_font: FnvHashMap::default(),
})
}
pub fn new_unchecked(
buffer: &'buffer mut [u8],
width: usize,
height: usize,
) -> Graphics<'buffer> {
if cfg!(debug) {
let count = width * height * 4;
debug_assert_eq!(count, buffer.len());
}
Graphics {
buffer,
width,
height,
translate: Coord::default(),
clip: Clip::new(width, height),
custom_font: FnvHashMap::default(),
}
}
}
impl Graphics<'_> {
pub fn set_clip(&mut self, clip: Clip) {
self.clip = clip;
}
pub fn clip(&self) -> &Clip {
&self.clip
}
pub fn clip_mut(&mut self) -> &mut Clip {
&mut self.clip
}
}