use ab_glyph::{point, Font, FontRef, FontVec, Glyph, Point, PxScale, ScaleFont};
use image::{DynamicImage, Rgba};
const TEXT: &str = "This is ab_glyph rendered into a png!";
fn main() {
if let Some(font_path) = std::env::args().nth(1) {
let font_path = std::env::current_dir().unwrap().join(font_path);
let data = std::fs::read(&font_path).unwrap();
let font = FontVec::try_from_vec(data).unwrap_or_else(|_| {
panic!("error constructing a Font from data at {:?}", font_path);
});
if let Some(name) = font_path.file_name().and_then(|n| n.to_str()) {
eprintln!("Using font: {name}");
}
draw_image(font);
} else {
eprintln!("No font specified ... using OpenSans-Italic.ttf");
let font = FontRef::try_from_slice(include_bytes!("../fonts/DejaVuSansMono.ttf")).unwrap();
draw_image(font);
};
}
pub fn layout_paragraph<F, SF>(
font: SF,
position: Point,
max_width: f32,
text: &str,
target: &mut Vec<Glyph>,
) where
F: Font,
SF: ScaleFont<F>,
{
let v_advance = font.height() + font.line_gap();
let mut caret = position + point(0.0, font.ascent());
let mut last_glyph: Option<Glyph> = None;
for c in text.chars() {
if c.is_control() {
if c == '\n' {
caret = point(position.x, caret.y + v_advance);
last_glyph = None;
}
continue;
}
let mut glyph = font.scaled_glyph(c);
if let Some(previous) = last_glyph.take() {
caret.x += font.kern(previous.id, glyph.id);
}
glyph.position = caret;
last_glyph = Some(glyph.clone());
caret.x += font.h_advance(glyph.id);
if !c.is_whitespace() && caret.x > position.x + max_width {
caret = Point {
x: position.x,
y: caret.y + v_advance,
};
glyph.position = caret;
last_glyph = None;
}
target.push(glyph);
}
}
fn draw_image<F: Font>(font: F) {
let scale = PxScale::from(45.0);
let scaled_font = font.as_scaled(scale);
let mut glyphs = Vec::new();
layout_paragraph(scaled_font, point(20.0, 20.0), 9999.0, TEXT, &mut glyphs);
let colour = (255, 255, 255);
let glyphs_height = scaled_font.height().ceil() as u32;
let glyphs_width = {
let min_x = glyphs.first().unwrap().position.x;
let last_glyph = glyphs.last().unwrap();
let max_x = last_glyph.position.x + scaled_font.h_advance(last_glyph.id);
(max_x - min_x).ceil() as u32
};
let mut image = DynamicImage::new_rgba8(glyphs_width + 40, glyphs_height + 40).to_rgba8();
for glyph in glyphs {
if let Some(outlined) = scaled_font.outline_glyph(glyph) {
let bounds = outlined.px_bounds();
outlined.draw(|x, y, v| {
let px = image.get_pixel_mut(x + bounds.min.x as u32, y + bounds.min.y as u32);
*px = Rgba([
colour.0,
colour.1,
colour.2,
px.0[3].saturating_add((v * 255.0) as u8),
]);
});
}
}
image.save("image_example.png").unwrap();
println!("Generated: image_example.png");
}