libcosmic/examples/text/src/font/layout.rs

64 lines
2 KiB
Rust
Raw Normal View History

2022-10-05 09:16:51 -06:00
use core::marker::PhantomData;
2022-10-05 11:28:05 -06:00
use super::FontLineIndex;
2022-10-05 09:16:51 -06:00
pub struct FontLayoutGlyph<'a, T: 'a> {
pub start: usize,
pub end: usize,
pub x: f32,
pub w: f32,
#[cfg(feature = "ab_glyph")]
pub inner: Option<ab_glyph::OutlinedGlyph>,
#[cfg(feature = "rusttype")]
pub inner: rusttype::PositionedGlyph<'a>,
2022-10-07 09:41:36 -06:00
#[cfg(feature = "swash")]
2022-10-07 10:31:49 -06:00
pub inner: (i32, i32, Option<swash::scale::image::Image>),
2022-10-05 09:16:51 -06:00
pub phantom: PhantomData<&'a T>,
}
pub struct FontLayoutLine<'a> {
2022-10-05 11:28:05 -06:00
pub line_i: FontLineIndex,
2022-10-05 09:16:51 -06:00
pub glyphs: Vec<FontLayoutGlyph<'a, ()>>,
}
impl<'a> FontLayoutLine<'a> {
2022-10-07 09:41:36 -06:00
pub fn draw<F: FnMut(i32, i32, u8)>(&self, mut f: F) {
2022-10-05 09:16:51 -06:00
for glyph in self.glyphs.iter() {
#[cfg(feature = "ab_glyph")]
if let Some(ref outline) = glyph.inner {
let bb = outline.px_bounds();
let x = bb.min.x as i32;
let y = bb.min.y as i32;
outline.draw(|off_x, off_y, v| {
2022-10-07 09:41:36 -06:00
f(x + off_x as i32, y + off_y as i32, (v * 255.0) as u8);
2022-10-05 09:16:51 -06:00
});
}
#[cfg(feature = "rusttype")]
if let Some(bb) = glyph.inner.pixel_bounding_box() {
let x = bb.min.x;
let y = bb.min.y;
glyph.inner.draw(|off_x, off_y, v| {
2022-10-07 09:41:36 -06:00
f(x + off_x as i32, y + off_y as i32, (v * 255.0) as u8);
2022-10-05 09:16:51 -06:00
});
}
2022-10-07 09:41:36 -06:00
#[cfg(feature = "swash")]
2022-10-07 10:31:49 -06:00
if let Some(ref image) = glyph.inner.2 {
2022-10-07 09:41:36 -06:00
assert_eq!(image.content, swash::scale::image::Content::Mask);
2022-10-07 10:31:49 -06:00
let x = glyph.inner.0 + image.placement.left;
let y = glyph.inner.1 - image.placement.top;
2022-10-07 09:41:36 -06:00
let mut i = 0;
for off_y in 0..image.placement.height as i32 {
for off_x in 0..image.placement.width as i32 {
f(x + off_x, y + off_y, image.data[i]);
i += 1;
}
}
}
2022-10-05 09:16:51 -06:00
}
}
}