Files
rusty-path-of-building/src/fonts/rasterizer.rs
T

276 lines
8.8 KiB
Rust

use crate::{
color::Srgba,
dpi::{LogicalVector, PhysicalPoint, PhysicalRect, PhysicalVector, ScaleFactor},
fonts::{atlas::FontAtlas, glyph_key::GlyphKey, layout::LayoutRect},
math::Size,
uv::UvRect,
};
use ahash::HashMap;
use image::GenericImage;
use ordered_float::OrderedFloat;
use parley::{FontData, GlyphRun};
use std::borrow::Cow;
use swash::zeno;
type FontBlobId = u64;
type FontIndex = u32;
type SwashFontOffset = u32;
pub type StyleId = u32;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct StyleKey<'a> {
font_blob_id: FontBlobId,
font_size: OrderedFloat<f32>,
normalized_coords: Cow<'a, [i16]>,
skew: i8,
}
impl<'a> StyleKey<'a> {
fn new(
font_blob_id: FontBlobId,
font_size: f32,
normalized_coords: &'a [i16],
skew: i8,
) -> Self {
Self {
font_blob_id,
font_size: font_size.into(),
normalized_coords: Cow::Borrowed(normalized_coords),
skew,
}
}
fn to_static(&self) -> StyleKey<'static> {
StyleKey {
normalized_coords: self.normalized_coords.clone().into_owned().into(),
..*self
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CachedGlyph {
pub rect: PhysicalRect<u32>,
pub uv: UvRect,
pub texture_layer_idx: u32,
// offset from top/left to baseline
pub baseline_offset: PhysicalVector<i32>,
}
pub struct RasterizedGlyph {
pub rect: LayoutRect<f32>,
pub uv: UvRect,
pub texture_layer_idx: u32,
pub color: Srgba,
}
impl RasterizedGlyph {
fn from_cached(
cached: CachedGlyph,
position: PhysicalPoint<i32>,
color: Srgba,
scale_factor: ScaleFactor<f32>,
) -> Self {
let glyph_rect = PhysicalRect::from_origin_and_size(
position + cached.baseline_offset,
cached.rect.cast().size(),
);
RasterizedGlyph {
// TODO: store sizes instead of rects
rect: (glyph_rect.cast() / scale_factor).cast_unit(),
uv: cached.uv,
texture_layer_idx: cached.texture_layer_idx,
color,
}
}
}
pub struct GlyphRasterizer {
scale_context: swash::scale::ScaleContext,
swash_keys: HashMap<(FontBlobId, FontIndex), (SwashFontOffset, swash::CacheKey)>,
// Style properties (font, size, etc.) are the same for each glyph run and don't
// need to be part of each glyph key. Instead, associate each style with its own
// ID and include that in the glyph key.
style_ids: HashMap<StyleKey<'static>, StyleId>,
next_style_id: StyleId,
cached_glyphs: HashMap<GlyphKey, Option<CachedGlyph>>,
// scratch image buffer used to write bitmap data into
scratch: swash::scale::image::Image,
}
impl GlyphRasterizer {
pub fn new() -> Self {
Self {
scale_context: swash::scale::ScaleContext::new(),
swash_keys: Default::default(),
style_ids: Default::default(),
next_style_id: 0,
cached_glyphs: Default::default(),
scratch: Default::default(),
}
}
/// Gets a [`swash::FontRef`] from [`FontData`]
fn get_font_ref<'a>(&mut self, font: &'a FontData) -> swash::FontRef<'a> {
let font_blob_id = font.data.id();
let (swash_offset, swash_key) = *self
.swash_keys
.entry((font_blob_id, font.index))
.or_insert_with(|| {
let font_ref =
swash::FontRef::from_index(font.data.data(), font.index as usize).unwrap();
(font_ref.offset, font_ref.key)
});
swash::FontRef {
data: font.data.data(),
offset: swash_offset,
key: swash_key,
}
}
fn get_style_id(
&mut self,
font_data: &FontData,
font_size: f32,
norm_coords: &[i16],
skew: i8,
) -> StyleId {
let style_key = StyleKey::new(font_data.data.id(), font_size, norm_coords, skew);
match self.style_ids.get(&style_key) {
Some(key) => *key,
None => *self
.style_ids
.entry(style_key.to_static())
.or_insert_with(|| {
let id = self.next_style_id;
self.next_style_id += 1;
id
}),
}
}
/// Rasterizes glyph run and returns the placement and UV for each glyph.
/// Can return `None` if glyph doesn't take up any space (e.g. whitespace).
pub fn rasterize_glyph_run<'slf: 'run, 'run, 'atlas>(
&'slf mut self,
atlas: &'atlas mut FontAtlas,
glyph_run: &'run GlyphRun<'_, Srgba>,
// additional offset relative to layout origin
glyph_offset: LogicalVector<f32>,
scale_factor: ScaleFactor<f32>,
) -> impl Iterator<Item = Option<RasterizedGlyph>> + use<'slf, 'run, 'atlas> {
let run = glyph_run.run();
let color = glyph_run.style().brush;
let font_size = run.font_size() * scale_factor.get();
let normalized_coords = run.normalized_coords();
let skew = run.synthesis().skew(); // skew angle for faux italic
let font_ref = self.get_font_ref(run.font());
let style_id = self.get_style_id(
run.font(),
font_size,
normalized_coords,
// parley stores skew as i8 internally so this conversion is ok
skew.unwrap_or_default() as i8,
);
let mut scaler = self
.scale_context
.builder(font_ref)
.size(font_size)
.normalized_coords(normalized_coords)
.hint(true)
.build();
let image = &mut self.scratch;
let cached_glyphs = &mut self.cached_glyphs;
glyph_run.positioned_glyphs().map(move |mut glyph| {
glyph.x += glyph_offset.x;
glyph.y += glyph_offset.y;
let (glyph_key, glyph_pos) = GlyphKey::from_glyph(&glyph, style_id, scale_factor.get());
if let Some(cached_glyph) = cached_glyphs.get(&glyph_key) {
return cached_glyph.map(|cached| {
RasterizedGlyph::from_cached(cached, glyph_pos, color, scale_factor)
});
}
let fract_offset = glyph_key.get_fractional_offset();
image.clear();
let did_render = swash::scale::Render::new(&[
swash::scale::Source::ColorOutline(0),
swash::scale::Source::ColorBitmap(swash::scale::StrikeWith::BestFit),
swash::scale::Source::Outline,
])
.format(zeno::Format::Alpha)
.transform(skew.map(|skew| {
zeno::Transform::skew(zeno::Angle::from_degrees(skew), zeno::Angle::ZERO)
}))
.offset(fract_offset)
.render_into(&mut scaler, glyph.id as u16, image);
if !did_render || image.placement.width == 0 || image.placement.height == 0 {
cached_glyphs.insert(glyph_key, None);
return None;
};
let (glyph_rect, atlas_uv, atlas_layer_idx) = write_to_atlas(image, atlas);
let cached_glyph = CachedGlyph {
rect: glyph_rect,
uv: atlas_uv,
texture_layer_idx: atlas_layer_idx,
baseline_offset: PhysicalVector::new(image.placement.left, -image.placement.top),
};
cached_glyphs.insert(glyph_key, Some(cached_glyph));
Some(RasterizedGlyph::from_cached(
cached_glyph,
glyph_pos,
color,
scale_factor,
))
})
}
}
/// Writes rasterized glyph to atlas and returns the UV coordinates of it
fn write_to_atlas(
image: &swash::scale::image::Image,
atlas: &mut FontAtlas,
) -> (PhysicalRect<u32>, UvRect, u32) {
let mut allocated_glyph =
atlas.allocate(Size::new(image.placement.width, image.placement.height));
match image.content {
swash::scale::image::Content::Mask => {
let mut i = 0;
for y in 0..image.placement.height {
for x in 0..image.placement.width {
let a = image.data[i];
// SAFETY: allocated atlas region and swash image have the same size
unsafe {
allocated_glyph.sub_image.unsafe_put_pixel(
x,
y,
Srgba::new(255, 255, 255, a).into(),
)
};
i += 1;
}
}
}
_ => unreachable!(),
};
(
PhysicalRect::from_size(Size::new(image.placement.width, image.placement.height)),
allocated_glyph.uv,
allocated_glyph.layer_idx,
)
}