pltrs_render/raster/
bitmap.rs1use pltrs_core::Point;
6use pltrs_font::ScanOutline;
7use pltrs_raster::{GrayBitmap, RasterError};
8
9use super::glyph::{GlyphTransform, freetype_contours, is_axis_aligned};
10use super::mono::{self, MonoBitmap};
11
12#[derive(Clone, Debug, PartialEq)]
15pub enum GlyphShape {
16 Smooth(ScanOutline),
18 Mono(ScanOutline),
20}
21
22#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct GlyphBitmap {
25 pub left: i32,
27 pub top: i32,
29 pub width: u32,
31 pub rows: u32,
33 pub buffer: Vec<u8>,
36}
37
38impl From<GrayBitmap> for GlyphBitmap {
39 fn from(bitmap: GrayBitmap) -> Self {
40 Self {
41 left: bitmap.left,
42 top: bitmap.top,
43 width: bitmap.coverage.width(),
44 rows: bitmap.coverage.height(),
45 buffer: bitmap.coverage.into_vec(),
46 }
47 }
48}
49
50impl From<MonoBitmap> for GlyphBitmap {
51 fn from(bitmap: MonoBitmap) -> Self {
52 let (left, top) = bitmap.origin();
53 let (width, rows) = bitmap.size();
54 Self {
55 left,
56 top,
57 width,
58 rows,
59 buffer: bitmap.pixels().map(u8::from).collect(),
60 }
61 }
62}
63
64pub(super) fn smooth_bitmap(
68 outline: &ScanOutline,
69 transform: &GlyphTransform,
70) -> Result<Option<GrayBitmap>, RasterError> {
71 match freetype_contours(outline, transform) {
72 Some(contours) => pltrs_raster::render_glyph(&contours, false),
73 None => Ok(None),
74 }
75}
76
77pub fn glyph_axis_aligned(angle: f64) -> bool {
81 is_axis_aligned(angle)
82}
83
84pub fn render_glyph(
94 shape: &GlyphShape,
95 angle: f64,
96 pen: Point,
97) -> Result<Option<GlyphBitmap>, RasterError> {
98 let transform = GlyphTransform::new(angle, pen);
99 Ok(match shape {
100 GlyphShape::Smooth(outline) => smooth_bitmap(outline, &transform)?.map(GlyphBitmap::from),
101 GlyphShape::Mono(outline) => mono::render(outline, &transform).map(GlyphBitmap::from),
102 })
103}