use crcbl_image::resize::{nearest_size, resize};
use crcbl_image::{Limits, png};
use skrifa::FontRef;
use skrifa::bitmap::{BitmapData, BitmapFormat, BitmapGlyph, BitmapStrikes, Origin};
use super::{ColorBitmap, MAX_COLOR_GLYPH_SIDE};
const LIMITS: Limits = Limits {
max_width: MAX_COLOR_GLYPH_SIDE,
max_height: MAX_COLOR_GLYPH_SIDE,
max_bytes: MAX_COLOR_GLYPH_SIDE as usize * MAX_COLOR_GLYPH_SIDE as usize * 4,
};
pub(super) fn rasterize(
font: &FontRef<'_>,
glyph: skrifa::GlyphId,
size: f32,
units_per_em: u16,
) -> Option<ColorBitmap> {
let bitmap = [BitmapFormat::Cbdt, BitmapFormat::Sbix]
.into_iter()
.filter_map(|format| BitmapStrikes::with_format(font, format))
.find_map(|strikes| nearest_strike(&strikes, glyph, size))?;
let BitmapData::Png(bytes) = bitmap.data else {
return None;
};
let decoded = png::decode(bytes, &LIMITS).ok()?;
let scale = size / bitmap.ppem_y;
let width = (decoded.width() as f32 * scale).round().max(1.0) as u32;
let height = (decoded.height() as f32 * scale).round().max(1.0) as u32;
let pixels = resize(&decoded, width, height, &LIMITS).ok()?.into_pixels();
let (left, top) = match bitmap.placement_origin {
Origin::TopLeft => (
bitmap.inner_bearing_x * scale,
-bitmap.inner_bearing_y * scale,
),
Origin::BottomLeft => {
let outline = size / f32::from(units_per_em);
let bottom = bitmap.bearing_y * outline + bitmap.inner_bearing_y * scale;
(
bitmap.bearing_x * outline + bitmap.inner_bearing_x * scale,
-(bottom.round() + height as f32),
)
}
};
Some(ColorBitmap {
width,
height,
left: left.round() as i32,
top: top.round() as i32,
pixels,
})
}
fn nearest_strike<'a>(
strikes: &BitmapStrikes<'a>,
glyph: skrifa::GlyphId,
size: f32,
) -> Option<BitmapGlyph<'a>> {
let mut held: Vec<BitmapGlyph<'a>> = strikes
.iter()
.filter_map(|strike| strike.get(glyph))
.collect();
let sides: Vec<(u32, u32)> = held
.iter()
.map(|bitmap| {
let ppem = bitmap.ppem_y as u32;
(ppem, ppem)
})
.collect();
let at = nearest_size(&sides, size.ceil() as u32)?;
Some(held.swap_remove(at))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::font::fixtures;
fn strike_for(font: &crate::font::Font, c: char, size: f32) -> Option<f32> {
let font_ref = FontRef::new(font.data()).expect("reads");
let glyph = skrifa::GlyphId::new(font.glyph_id(c).0);
[BitmapFormat::Cbdt, BitmapFormat::Sbix]
.into_iter()
.filter_map(|format| BitmapStrikes::with_format(&font_ref, format))
.find_map(|strikes| nearest_strike(&strikes, glyph, size))
.map(|bitmap| bitmap.ppem_y)
}
#[test]
fn the_strike_is_the_smallest_at_least_the_size_among_those_holding_the_glyph() {
let cbdt = fixtures::cbdt();
let smile = '\u{1F600}';
for (size, want) in [
(4.0, 8.0),
(8.0, 8.0),
(8.5, 16.0),
(12.0, 16.0),
(16.0, 16.0),
(40.0, 16.0),
] {
assert_eq!(strike_for(&cbdt, smile, size), Some(want), "at {size}");
}
assert_eq!(strike_for(&cbdt, '\u{2764}', 8.0), Some(16.0));
assert_eq!(strike_for(&cbdt, 'a', 16.0), None);
let sbix = fixtures::sbix();
assert_eq!(strike_for(&sbix, smile, 6.0), Some(8.0));
assert_eq!(strike_for(&sbix, smile, 9.0), Some(16.0));
}
}