#[cfg(feature = "parsed-font")]
mod bitmap;
mod cache;
#[cfg(feature = "parsed-font")]
pub mod colr;
pub use cache::{ColorGlyph, ColorGlyphCache, ColorGlyphKey, ColorGlyphStats};
#[cfg(feature = "parsed-font")]
use skrifa::bitmap::{BitmapFormat, BitmapStrikes};
#[cfg(feature = "parsed-font")]
use skrifa::{FontRef, MetadataProvider};
use super::{Font, GlyphId};
pub const MAX_COLOR_GLYPH_SIDE: u32 = crate::image::PAGE_SIZE / 2;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ColorBitmap {
pub width: u32,
pub height: u32,
pub left: i32,
pub top: i32,
pub pixels: Vec<u8>,
}
#[cfg(feature = "parsed-font")]
pub(crate) fn has_color_glyph(font: &FontRef<'_>, glyph: skrifa::GlyphId) -> bool {
font.color_glyphs().get(glyph).is_some()
|| [BitmapFormat::Cbdt, BitmapFormat::Sbix]
.into_iter()
.filter_map(|format| BitmapStrikes::with_format(font, format))
.any(|strikes| strikes.iter().any(|strike| strike.get(glyph).is_some()))
}
#[cfg(not(feature = "parsed-font"))]
pub(crate) fn rasterize(font: &Font, _glyph: GlyphId, _size: f32) -> Option<ColorBitmap> {
match font.parsed.never {}
}
#[cfg(feature = "parsed-font")]
pub(crate) fn rasterize(font: &Font, glyph: GlyphId, size: f32) -> Option<ColorBitmap> {
if !(size.is_finite() && size > 0.0) {
return None;
}
let font_ref = FontRef::new(font.data()).ok()?;
let glyph = skrifa::GlyphId::new(glyph.0);
let units_per_em = font.metrics().units_per_em;
colr::rasterize(
&font_ref,
glyph,
size,
units_per_em,
colr::PaintLimits::DEFAULT,
)
.or_else(|| bitmap::rasterize(&font_ref, glyph, size, units_per_em))
}
#[cfg(all(test, feature = "parsed-font"))]
mod tests {
use crcbl_image::resize::resize;
use crcbl_image::{Limits, Rgba8Image, png};
use super::*;
use crate::font::fixtures;
fn drawn(font: &Font, c: char, size: f32) -> Option<ColorBitmap> {
rasterize(font, font.glyph_id(c), size)
}
fn strike_png(font: &Font, c: char, ppem: f32) -> Rgba8Image {
let font_ref = FontRef::new(font.data()).expect("reads");
let strikes = BitmapStrikes::with_format(&font_ref, BitmapFormat::Cbdt).expect("CBDT");
let strike = strikes
.iter()
.find(|strike| strike.ppem() == ppem)
.expect("the strike");
let bitmap = strike
.get(skrifa::GlyphId::new(font.glyph_id(c).0))
.expect("in the strike");
let skrifa::bitmap::BitmapData::Png(bytes) = bitmap.data else {
panic!("a PNG");
};
png::decode(bytes, &Limits::DEFAULT).expect("decodes")
}
fn pixel(bitmap: &ColorBitmap, x: u32, y: u32) -> [u8; 4] {
let at = ((y * bitmap.width + x) * 4) as usize;
bitmap.pixels[at..at + 4].try_into().expect("four bytes")
}
#[test]
fn a_cbdt_glyph_at_a_strikes_size_is_its_png_placed_by_its_bearing() {
let cbdt = fixtures::cbdt();
let at16 = drawn(&cbdt, '\u{1F600}', 16.0).expect("in colour");
assert_eq!(
(at16.width, at16.height, at16.left, at16.top),
(16, 16, 0, -12)
);
for y in 0..16 {
for x in 0..16 {
let want = if x < 8 {
[255, 0, 0, 255]
} else {
[0, 0, 255, 255]
};
assert_eq!(pixel(&at16, x, y), want, "({x}, {y})");
}
}
let at8 = drawn(&cbdt, '\u{1F600}', 8.0).expect("in colour");
assert_eq!((at8.width, at8.height, at8.left, at8.top), (8, 8, 0, -6));
assert!(at8.pixels.chunks(4).all(|p| p == [0, 255, 0, 255]));
}
#[test]
fn a_cbdt_glyph_between_strikes_is_the_nearest_resized() {
let cbdt = fixtures::cbdt();
let at32 = drawn(&cbdt, '\u{1F600}', 32.0).expect("in colour");
let want = resize(
&strike_png(&cbdt, '\u{1F600}', 16.0),
32,
32,
&Limits::DEFAULT,
)
.expect("resizes");
assert_eq!(
(at32.width, at32.height, at32.left, at32.top),
(32, 32, 0, -24)
);
assert_eq!(at32.pixels, want.pixels());
let at12 = drawn(&cbdt, '\u{1F600}', 12.0).expect("in colour");
let want = resize(
&strike_png(&cbdt, '\u{1F600}', 16.0),
12,
12,
&Limits::DEFAULT,
)
.expect("resizes");
assert_eq!((at12.width, at12.top), (12, -9));
assert_eq!(at12.pixels, want.pixels());
}
#[test]
fn an_sbix_glyph_sits_on_its_origin_offset() {
let sbix = fixtures::sbix();
let at16 = drawn(&sbix, '\u{1F600}', 16.0).expect("in colour");
assert_eq!(
(at16.width, at16.height, at16.left, at16.top),
(16, 16, 0, -12)
);
assert!(at16.pixels.chunks(4).all(|p| p == [255, 255, 0, 255]));
let at8 = drawn(&sbix, '\u{1F600}', 8.0).expect("in colour");
assert_eq!((at8.width, at8.top), (8, -6));
assert!(at8.pixels.chunks(4).all(|p| p == [0, 255, 255, 255]));
}
#[test]
fn a_corrupt_png_or_a_size_of_nothing_draws_no_colour_glyph() {
let cbdt = fixtures::cbdt();
let broken = cbdt.glyph_id('\u{1F4A5}');
assert!(cbdt.has_color_glyph(broken));
assert_eq!(rasterize(&cbdt, broken, 16.0), None);
for size in [0.0, -16.0, f32::NAN, f32::INFINITY] {
assert_eq!(drawn(&cbdt, '\u{1F600}', size), None, "{size}");
}
assert_eq!(drawn(&fixtures::text(), '\u{2764}', 16.0), None);
}
}