use pltrs_core::Point;
use pltrs_font::ScanOutline;
use pltrs_raster::{GrayBitmap, RasterError};
use super::glyph::{GlyphTransform, freetype_contours, is_axis_aligned};
use super::mono::{self, MonoBitmap};
#[derive(Clone, Debug, PartialEq)]
pub enum GlyphShape {
Smooth(ScanOutline),
Mono(ScanOutline),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GlyphBitmap {
pub left: i32,
pub top: i32,
pub width: u32,
pub rows: u32,
pub buffer: Vec<u8>,
}
impl From<GrayBitmap> for GlyphBitmap {
fn from(bitmap: GrayBitmap) -> Self {
Self {
left: bitmap.left,
top: bitmap.top,
width: bitmap.coverage.width(),
rows: bitmap.coverage.height(),
buffer: bitmap.coverage.into_vec(),
}
}
}
impl From<MonoBitmap> for GlyphBitmap {
fn from(bitmap: MonoBitmap) -> Self {
let (left, top) = bitmap.origin();
let (width, rows) = bitmap.size();
Self {
left,
top,
width,
rows,
buffer: bitmap.pixels().map(u8::from).collect(),
}
}
}
pub(super) fn smooth_bitmap(
outline: &ScanOutline,
transform: &GlyphTransform,
) -> Result<Option<GrayBitmap>, RasterError> {
match freetype_contours(outline, transform) {
Some(contours) => pltrs_raster::render_glyph(&contours, false),
None => Ok(None),
}
}
pub fn glyph_axis_aligned(angle: f64) -> bool {
is_axis_aligned(angle)
}
pub fn render_glyph(
shape: &GlyphShape,
angle: f64,
pen: Point,
) -> Result<Option<GlyphBitmap>, RasterError> {
let transform = GlyphTransform::new(angle, pen);
Ok(match shape {
GlyphShape::Smooth(outline) => smooth_bitmap(outline, &transform)?.map(GlyphBitmap::from),
GlyphShape::Mono(outline) => mono::render(outline, &transform).map(GlyphBitmap::from),
})
}