-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//! Draws text into `image_example.png`. | ||
//! | ||
//! Use a custom font file: `cargo run --example image /path/to/font.otf` | ||
//! https://github.com/alexheretic/ab-glyph/blob/main/dev/examples/image.rs | ||
use ab_glyph::{point, Point, Font, Glyph, FontRef, FontVec, 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) { | ||
// The font size to use | ||
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); | ||
|
||
// Use a dark red colour | ||
let colour = (255, 255, 255); | ||
|
||
// work out the layout size | ||
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 | ||
}; | ||
|
||
// Create a new rgba image with some padding | ||
let mut image = DynamicImage::new_rgba8(glyphs_width + 40, glyphs_height + 40).to_rgba8(); | ||
|
||
// Loop through the glyphs in the text, positing each one on a line | ||
for glyph in glyphs { | ||
if let Some(outlined) = scaled_font.outline_glyph(glyph) { | ||
let bounds = outlined.px_bounds(); | ||
// Draw the glyph into the image per-pixel by using the draw closure | ||
outlined.draw(|x, y, v| { | ||
// Offset the position by the glyph bounding box | ||
let px = image.get_pixel_mut(x + bounds.min.x as u32, y + bounds.min.y as u32); | ||
// Turn the coverage into an alpha value (blended with any previous) | ||
*px = Rgba([ | ||
colour.0, | ||
colour.1, | ||
colour.2, | ||
px.0[3].saturating_add((v * 255.0) as u8), | ||
]); | ||
}); | ||
} | ||
} | ||
|
||
// Save the image to a png file | ||
image.save("image_example.png").unwrap(); | ||
println!("Generated: image_example.png"); | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters