fn render_text()

in waterfall/src/lib.rs [229:261]


fn render_text(string: &str, size: f32, x_pos: usize, y_pos: usize, buf: &mut RgbImage) {
    // load font
    let font_data = dejavu::sans_mono::regular();
    let font = Font::try_from_bytes(font_data as &[u8]).unwrap();

    // size and scaling
    let height: f32 = size;
    let scale = TypeScale {
        x: height * 1.0,
        y: height,
    };

    let v_metrics = font.v_metrics(scale);
    let offset = point(0.0, v_metrics.ascent);

    let glyphs: Vec<PositionedGlyph> = font.layout(string, scale, offset).collect();

    for g in glyphs {
        if let Some(bb) = g.pixel_bounding_box() {
            g.draw(|x, y, v| {
                let x = (x as i32 + bb.min.x) as usize;
                let y = (y as i32 + bb.min.y) as usize;
                if v > 0.25 {
                    let x = (x + x_pos).try_into().unwrap();
                    let y = (y + y_pos).try_into().unwrap();
                    if x < buf.width() && y < buf.height() {
                        buf.put_pixel(x, y, Rgb([255, 255, 255]));
                    }
                }
            })
        }
    }
}