From 405da83bacde9afd93a4590662616b59a67a28eb Mon Sep 17 00:00:00 2001 From: bendn Date: Fri, 8 Dec 2023 09:46:56 +0700 Subject: [PATCH] add Image::text for packed --- src/drawing/text.rs | 72 ++++++++++++++++++++++++++------------------- src/show.rs | 2 +- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/drawing/text.rs b/src/drawing/text.rs index b0b8da2..7c03101 100644 --- a/src/drawing/text.rs +++ b/src/drawing/text.rs @@ -7,16 +7,13 @@ use crate::{ use fontdue::{layout::TextStyle, Font}; use umath::{generic_float::Constructors, FF32}; -impl Image<&mut [u32], 1> { - pub(crate) fn text_u32( - &mut self, - x: u32, - y: u32, - size: f32, - font: &Font, - text: &str, - color: [u8; 4], - ) { +/// note: `N` may != channels +pub trait Text { + fn text(&mut self, x: u32, y: u32, size: f32, font: &Font, text: &str, color: [u8; N]); +} + +impl + AsRef<[u32]>> Text<4> for Image { + fn text(&mut self, x: u32, y: u32, size: f32, font: &Font, text: &str, color: [u8; 4]) { let mut lay = fontdue::layout::Layout::new(fontdue::layout::CoordinateSystem::PositiveYDown); lay.append(&[font], &TextStyle::new(text, size, 0)); @@ -36,10 +33,9 @@ impl Image<&mut [u32], 1> { // SAFETY: the rasterizer kinda promises that metrics width and height are in bounds let fill = unsafe { float(*bitmap.get_unchecked(j * metrics.width + i)) }; // SAFETY: we clampin - let bg: [u8; 4] = - unsafe { Pack::unpack(*self.buffer.get_unchecked(self.at(x, y))) }; + let bg: [u8; 4] = unsafe { Pack::unpack(self.pixel(x, y)[0]) }; // SAFETY: see above - *unsafe { self.buffer.get_unchecked_mut(self.at(x, y)) } = + *(&mut unsafe { self.pixel_mut(x, y) }[0]) = // SAFETY: fill is 0..=1 Pack::pack(unsafe { &bg.wam(color, FF32::one() - fill, fill) }); } @@ -48,23 +44,8 @@ impl Image<&mut [u32], 1> { } } -impl + AsRef<[u8]>> Image { - /// Draw text. - /// - /// ``` - /// # use fimg::Image; - /// let font = fontdue::Font::from_bytes( - /// &include_bytes!("../../data/CascadiaCode.ttf")[..], - /// fontdue::FontSettings { - /// scale: 200.0, - /// ..Default::default() - /// }, - /// ).unwrap(); - /// let mut i: Image<_, 4> = Image::alloc(750, 250).boxed(); - /// i.text(50, 10, 200.0, &font, "hello", [0, 0, 0, 255]); - /// # assert_eq!(&**i.buffer(), include_bytes!("../../tdata/text.imgbuf")); - /// ``` - pub fn text(&mut self, x: u32, y: u32, size: f32, font: &Font, text: &str, color: [u8; N]) { +impl + AsRef<[u8]>> Text for Image { + fn text(&mut self, x: u32, y: u32, size: f32, font: &Font, text: &str, color: [u8; N]) { let mut lay = fontdue::layout::Layout::new(fontdue::layout::CoordinateSystem::PositiveYDown); lay.append(&[font], &TextStyle::new(text, size, 0)); @@ -92,3 +73,34 @@ impl + AsRef<[u8]>> Image { } } } + +impl Image { + /// Draw text. + /// + /// ``` + /// # use fimg::Image; + /// let font = fontdue::Font::from_bytes( + /// &include_bytes!("../../data/CascadiaCode.ttf")[..], + /// fontdue::FontSettings { + /// scale: 200.0, + /// ..Default::default() + /// }, + /// ).unwrap(); + /// let mut i: Image<_, 4> = Image::alloc(750, 250).boxed(); + /// i.text(50, 10, 200.0, &font, "hello", [0, 0, 0, 255]); + /// # assert_eq!(&**i.buffer(), include_bytes!("../../tdata/text.imgbuf")); + /// ``` + pub fn text( + &mut self, + x: u32, + y: u32, + size: f32, + font: &Font, + text: &str, + color: [u8; P], + ) where + Image: Text

, + { + Text::text(self, x, y, size, font, text, color) + } +} diff --git a/src/show.rs b/src/show.rs index f38dbc3..2a220f4 100644 --- a/src/show.rs +++ b/src/show.rs @@ -34,7 +34,7 @@ mod real { .map(|(x, y)| (x.min(i.width()), y.min(i.height()))) { // SAFETY: ctor - unsafe { Image::new(buf.width, buf.height, &mut *buf.buffer) }.text_u32( + unsafe { Image::new(buf.width, buf.height, &mut *buf.buffer) }.text( 5, i.height() - 20, 12.0,