use crate::renderer::TextStyle;
pub(crate) fn needs_font_metrics(node: &crate::renderer::render_tree::RenderNode) -> bool {
use crate::renderer::render_tree::RenderNodeType;
matches!(&node.node_type, RenderNodeType::TextRun(run)
if run.style.shadow_type == 2 && !run.is_vertical)
|| node.children.iter().any(needs_font_metrics)
}
#[derive(Clone, Debug, PartialEq)]
pub struct ContinuousTextShadow {
delta: [i32; 2],
device_pixel: f64,
pub source_hash: [u8; 32],
pub face_index: u32,
}
crate::storage::clone_fields!(ContinuousTextShadow, {delta, device_pixel, source_hash, face_index});
impl ContinuousTextShadow {
pub fn offsets(&self) -> impl Iterator<Item = [f64; 2]> + '_ {
let count = i64::from(self.delta[0])
.abs()
.max(i64::from(self.delta[1]).abs())
+ 1;
(1..=count).map(move |step| {
self.delta.map(|delta| {
(step * i64::from(delta) / count) as f64 * self.device_pixel
})
})
}
pub fn extent(&self) -> [f64; 2] {
self.delta.map(|value| f64::from(value) * self.device_pixel)
}
}
pub(crate) fn offsets(style: &TextStyle) -> impl Iterator<Item = [f64; 2]> + '_ {
offsets_for(
style.shadow_type,
[style.shadow_offset_x, style.shadow_offset_y],
style.continuous_shadow.as_ref(),
)
}
pub(crate) fn offsets_for(
kind: u8,
nominal: [f64; 2],
plan: Option<&ContinuousTextShadow>,
) -> impl Iterator<Item = [f64; 2]> + '_ {
let continuous = plan.filter(|_| kind == 2).map(|plan| plan.offsets());
let endpoint = (kind > 0 && continuous.is_none()).then_some(nominal);
continuous.into_iter().flatten().chain(endpoint)
}
pub(super) fn write_offsets_json(buf: &mut String, kind: u8, plan: Option<&ContinuousTextShadow>) {
use std::fmt::Write;
let Some(plan) = plan.filter(|_| kind == 2) else {
return;
};
buf.push_str(",\"shadowOffsets\":[");
for (index, [x, y]) in plan.offsets().enumerate() {
if index != 0 {
buf.push(',');
}
let _ = write!(buf, "[{x},{y}]");
}
buf.push(']');
}
pub(super) fn flow_bounds(
node: &super::LayerNode,
) -> Option<crate::renderer::render_tree::BoundingBox> {
use super::{GroupKind, LayerNodeKind, PaintOp};
use crate::renderer::render_tree::BoundingBox;
let union = |a: BoundingBox, b: BoundingBox| {
let x = a.x.min(b.x);
let y = a.y.min(b.y);
BoundingBox::new(
x,
y,
(a.x + a.width).max(b.x + b.width) - x,
(a.y + a.height).max(b.y + b.height) - y,
)
};
match &node.kind {
LayerNodeKind::Leaf { ops } => ops
.iter()
.filter_map(|op| {
let PaintOp::TextRun {
bbox,
run,
paint_transform: None,
..
} = op
else {
return None;
};
if run.style.shadow_type != 2
|| run.rotation != 0.0
|| run.is_vertical
|| crate::renderer::text_glyph_visibility::is_nonprinting_cluster(
run.display_or_text(),
)
{
return None;
}
let [dx, dy] = run.style.continuous_shadow.as_ref()?.extent();
Some(union(
*bbox,
BoundingBox::new(bbox.x + dx, bbox.y + dy, bbox.width, bbox.height),
))
})
.reduce(union),
LayerNodeKind::Group {
group_kind: GroupKind::TextBox | GroupKind::Group(_),
..
}
| LayerNodeKind::ClipRect { .. }
| LayerNodeKind::ClipPath { .. } => None,
LayerNodeKind::Group { children, .. } => {
children.iter().filter_map(flow_bounds).reduce(union)
}
}
}
fn u16_at(bytes: &[u8], offset: usize) -> Option<u16> {
Some(u16::from_be_bytes(
bytes.get(offset..offset.checked_add(2)?)?.try_into().ok()?,
))
}
fn signed_at(bytes: &[u8], offset: usize) -> Option<i16> {
u16_at(bytes, offset).map(|value| value as i16)
}
fn vdmx_height(bytes: &[u8], ppem: u16) -> Option<i32> {
if u16_at(bytes, 0)? > 1 {
return None;
}
let ratios = usize::from(u16_at(bytes, 4)?);
let offsets = 6usize.checked_add(ratios.checked_mul(4)?)?;
bytes.get(..offsets.checked_add(ratios.checked_mul(2)?)?)?;
for index in 0..ratios {
let ratio = 6 + index * 4;
let x = bytes[ratio + 1];
let low = bytes[ratio + 2];
let high = bytes[ratio + 3];
if !((x == 0 && low == 0 && high == 0) || (low <= x && x <= high)) {
continue;
}
let group = usize::from(u16_at(bytes, offsets + index * 2)?);
let count = usize::from(u16_at(bytes, group)?);
for entry in 0..count {
let at = group.checked_add(4)?.checked_add(entry.checked_mul(6)?)?;
let size = u16_at(bytes, at)?;
if size == ppem {
let height =
i32::from(signed_at(bytes, at + 2)?) - i32::from(signed_at(bytes, at + 4)?);
return (height > 0).then_some(height);
}
if size > ppem {
return None;
}
}
return None;
}
None
}
fn font_cell(face: &ttf_parser::Face<'_>, ppem: u16) -> Option<i32> {
face.raw_face()
.table(ttf_parser::Tag::from_bytes(b"glyf"))?;
let height = face
.raw_face()
.table(ttf_parser::Tag::from_bytes(b"VDMX"))
.and_then(|bytes| vdmx_height(bytes, ppem))
.or_else(|| {
let os2 = face
.raw_face()
.table(ttf_parser::Tag::from_bytes(b"OS/2"))?;
let ascent = u16_at(os2, 74)?;
let descent = u16_at(os2, 76)?;
let units = f64::from(face.units_per_em());
(ascent != 0 || descent != 0).then(|| {
(f64::from(ascent) * f64::from(ppem) / units).round() as i32
+ (f64::from(descent) * f64::from(ppem) / units).round() as i32
})
})?;
(height > 0).then_some(height + (height - i32::from(ppem)))
}
pub(super) fn prepare(
fonts: &super::metafile_fonts::MetafileFonts,
text: &str,
style: &TextStyle,
layout_dpi: f64,
) -> Option<ContinuousTextShadow> {
if style.shadow_type != 2
|| crate::renderer::text_glyph_visibility::is_nonprinting_cluster(text)
{
return None;
}
let percent = style.shadow_offset_percent?;
let (size, _) = style.script_draw_metrics(style.font_size, 0.0);
let em = (size * super::metafile::HANCOM_2024_PRINT_DEVICE_DPI / layout_dpi).round();
if !em.is_finite() || !(1.0..=f64::from(u16::MAX)).contains(&em) {
return None;
}
let database = fonts.database();
for id in fonts.supplied_faces_for_style(style) {
let plan = database
.with_face_data(id, |bytes, face_index| {
let face = ttf_parser::Face::parse(bytes, face_index).ok()?;
if !text
.chars()
.filter(|ch| {
!crate::renderer::text_glyph_visibility::is_nonprinting_character(*ch)
})
.all(|ch| face.glyph_index(ch).is_some_and(|glyph| glyph.0 != 0))
{
return None;
}
let cell = font_cell(&face, em as u16)?;
let basis_hu = (f64::from(cell) * 7200.0
/ super::metafile::HANCOM_2024_PRINT_DEVICE_DPI)
.round();
let delta = percent.map(|gap| {
let logical = (basis_hu * f64::from(gap) / 100.0).trunc();
(logical * super::metafile::HANCOM_2024_PRINT_DEVICE_DPI / 7200.0).round()
as i32
});
Some(ContinuousTextShadow {
delta,
device_pixel: layout_dpi / super::metafile::HANCOM_2024_PRINT_DEVICE_DPI,
source_hash: *blake3::hash(bytes).as_bytes(),
face_index,
})
})
.flatten();
if plan.is_some() {
return plan;
}
}
None
}