use swash::scale::{Render, ScaleContext, Source, StrikeWith};
use swash::zeno::{Format, Transform, Vector};
use swash::{CacheKey, FontRef};
use crate::font::synthesis::{self, FaceSynthesis};
const EMBOLDEN_TO_SWASH: f32 = 0.5;
pub const SUBPIXEL_BINS: u8 = 4;
pub fn split_subpixel_x(device_x: f32) -> (f32, u8) {
let base = device_x.floor();
let bins = f32::from(SUBPIXEL_BINS);
let bin = ((device_x - base) * bins).round();
if bin >= bins {
(base + 1.0, 0)
} else {
(base, bin as u8)
}
}
pub fn subpixel_offset(bin: u8) -> f32 {
f32::from(bin) / f32::from(SUBPIXEL_BINS)
}
#[non_exhaustive]
pub struct GlyphImage {
pub width: u32,
pub height: u32,
pub placement_left: i32,
pub placement_top: i32,
pub data: Vec<u8>,
pub is_color: bool,
}
#[allow(clippy::too_many_arguments)]
pub fn rasterize_glyph(
scale_context: &mut ScaleContext,
font_data: &[u8],
face_index: u32,
cache_key: CacheKey,
glyph_id: u16,
size_px: f32,
font_weight: u32,
hinted: bool,
) -> Option<GlyphImage> {
rasterize_glyph_styled(
scale_context,
font_data,
face_index,
cache_key,
glyph_id,
size_px,
font_weight,
hinted,
false,
)
}
#[allow(clippy::too_many_arguments)]
pub fn rasterize_glyph_styled(
scale_context: &mut ScaleContext,
font_data: &[u8],
face_index: u32,
cache_key: CacheKey,
glyph_id: u16,
size_px: f32,
font_weight: u32,
hinted: bool,
italic: bool,
) -> Option<GlyphImage> {
rasterize_glyph_positioned(
scale_context,
font_data,
face_index,
cache_key,
glyph_id,
size_px,
font_weight,
hinted,
italic,
0.0,
)
}
#[allow(clippy::too_many_arguments)]
pub fn rasterize_glyph_positioned(
scale_context: &mut ScaleContext,
font_data: &[u8],
face_index: u32,
cache_key: CacheKey,
glyph_id: u16,
size_px: f32,
font_weight: u32,
hinted: bool,
italic: bool,
x_offset: f32,
) -> Option<GlyphImage> {
if let Some(image) = crate::atlas::colr::paint_colr_v1(font_data, face_index, glyph_id, size_px)
{
return Some(image);
}
let base = FontRef::from_index(font_data, face_index as usize)?;
let font_ref = FontRef {
data: base.data,
offset: base.offset,
key: cache_key,
};
let mut scaler = scale_context
.builder(font_ref)
.size(size_px)
.hint(hinted)
.variations(&[("wght", font_weight as f32)])
.build();
let synthesis = FaceSynthesis::read(font_data, face_index);
let embolden = if synthesis.synthesizes_bold(font_weight.min(1000) as u16) {
synthesis::embolden_strength(size_px) * EMBOLDEN_TO_SWASH
} else {
0.0
};
let slant = synthesis
.synthesizes_oblique(italic)
.then(|| Transform::new(1.0, 0.0, synthesis::OBLIQUE_SHEAR, 1.0, 0.0, 0.0));
let mut image = Render::new(&[
Source::ColorOutline(0),
Source::ColorBitmap(StrikeWith::BestFit),
Source::Outline,
])
.format(Format::Alpha)
.offset(Vector::new(x_offset, 0.0))
.embolden(embolden)
.transform(slant)
.render(&mut scaler, glyph_id);
let drew_nothing = image
.as_ref()
.is_none_or(|img| img.placement.width == 0 || img.placement.height == 0);
if drew_nothing && crate::atlas::colr::has_colr_v1(font_data, face_index, glyph_id) {
image = Render::new(&[Source::Outline])
.format(Format::Alpha)
.offset(Vector::new(x_offset, 0.0))
.render(&mut scaler, 0);
}
let image = image?;
let is_color = matches!(image.content, swash::scale::image::Content::Color);
let bytes_per_pixel = if is_color { 4 } else { 1 };
let expected_len = (image.placement.width * image.placement.height) as usize * bytes_per_pixel;
if image.data.len() != expected_len {
debug_assert!(
false,
"swash image data length {} disagrees with Content {:?} for {}x{} glyph \
(expected {} bytes); blit_{} would corrupt the atlas — returning None",
image.data.len(),
image.content,
image.placement.width,
image.placement.height,
expected_len,
if is_color { "rgba" } else { "mask" },
);
return None;
}
Some(GlyphImage {
width: image.placement.width,
height: image.placement.height,
placement_left: image.placement.left,
placement_top: image.placement.top,
data: image.data,
is_color,
})
}