#![deny(missing_docs)]
use fontdue::layout::{CoordinateSystem, Layout, TextStyle};
use raqote::DrawTarget;
const FONT_DATA: &[u8]= include_bytes!("../assets/Roboto-Regular.ttf") as &[u8];
#[must_use]
#[deprecated]
pub fn text2img_internal(text:String, weight:u8, font_size:u8) -> Result<DrawTarget,String> {
if text.is_empty() {
return Err("Content can not be empty".to_owned());
}
if weight==0 {
return Err("Weight can not be zero".to_owned());
}
let settings = fontdue::FontSettings::default();
let font=fontdue::Font::from_bytes(FONT_DATA, settings).unwrap();
let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
let fonts = &[font.clone()];
layout.append(fonts, &TextStyle::new(&text, font_size as f32, 0));
let height=layout.height() as i32;
let first=layout.glyphs().first().unwrap();
let last=layout.glyphs().last().unwrap();
let width=(last.x as usize + last.width + first.x as usize) as i32;
let mut dt = DrawTarget::new(width, height);
for glyph in layout.glyphs() {
let glyph=glyph.to_owned();
let index=glyph.key.glyph_index;
let px=glyph.key.px;
let (metrics, coverage) = font.rasterize_indexed(index, px);
let mut image_data = Vec::with_capacity(coverage.len());
for cov in coverage.iter() {
let pixel = if weight==100 || fastrand::u8(0..=100)<=weight {
rgb_to_u32(0, 0, 0, *cov)
} else { 0};
image_data.push(pixel);
}
dt.draw_image_at(
glyph.x,
glyph.y,
&raqote::Image {
width: metrics.width as i32,
height: metrics.height as i32,
data: &image_data,
},
&raqote::DrawOptions {
blend_mode: raqote::BlendMode::Darken,
alpha: 1.0,
antialias: raqote::AntialiasMode::Gray,
},
);
}
Ok(dt)
}
pub fn rgb_to_u32(red: u8, green: u8, blue: u8, alpha: u8) -> u32 {
(((alpha as u32) << 24) | ((red as u32) << 16) | ((green as u32) << 8) | (blue as u32)) as u32
}