use core::ops::Range;
use glam::Vec2;
#[cfg(feature = "parsed-font")]
use std::sync::{Arc, Mutex, PoisonError};
use super::{Font, GlyphId, cluster, fallback};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ShapedGlyph {
pub face: u8,
pub glyph: GlyphId,
pub cluster: usize,
pub advance: f32,
pub offset: Vec2,
pub unsafe_to_break: bool,
}
pub fn shape(
font: &Font,
size: f32,
text: &str,
range: Range<usize>,
rtl: bool,
out: &mut Vec<ShapedGlyph>,
) {
let mut run: Option<(u8, Range<usize>)> = None;
for cluster in cluster::clusters(&text[range.clone()]) {
let bytes = range.start + cluster.start..range.start + cluster.end;
let face = fallback::cluster_face(font, &text[bytes.clone()]);
match &mut run {
Some((current, run)) if *current == face => run.end = bytes.end,
_ => {
if let Some((face, run)) = run.take() {
shape_run(font, face, size, text, run, rtl, out);
}
run = Some((face, bytes));
}
}
}
if let Some((face, run)) = run {
shape_run(font, face, size, text, run, rtl, out);
}
}
#[cfg(not(feature = "parsed-font"))]
fn shape_run(
font: &Font,
face: u8,
_size: f32,
_text: &str,
_range: Range<usize>,
_rtl: bool,
_out: &mut Vec<ShapedGlyph>,
) {
match font.face(face).source.never {}
}
#[cfg(feature = "parsed-font")]
fn shape_run(
font: &Font,
face: u8,
size: f32,
text: &str,
range: Range<usize>,
rtl: bool,
out: &mut Vec<ShapedGlyph>,
) {
let drawn = font.face(face);
let metrics = drawn.metrics();
let scale = metrics.scale(size);
let bold = if drawn.synthesis().bold {
f32::from(metrics.units_per_em) * super::face::SYNTHETIC_BOLD_EM
} else {
0.0
};
let Some(shaper) = &drawn.parsed().shaper else {
nominal(drawn, face, scale, text, range, out);
return;
};
#[cfg(test)]
shaper
.runs
.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
let buffer = run_shaper(shaper, text, range, rtl);
let first = out.len();
for (info, position) in buffer.glyph_infos().iter().zip(buffer.glyph_positions()) {
let mut advance = position.x_advance as f32;
if position.x_advance != 0 {
advance += bold;
}
out.push(ShapedGlyph {
face,
glyph: GlyphId(info.glyph_id),
cluster: info.cluster as usize,
advance: advance * scale,
offset: Vec2::new(
position.x_offset as f32 * scale,
-(position.y_offset as f32) * scale,
),
unsafe_to_break: info.unsafe_to_break(),
});
}
if rtl {
out[first..].reverse();
}
if let Some(glyph) = out.get_mut(first) {
glyph.unsafe_to_break = false;
}
}
#[cfg(feature = "parsed-font")]
fn run_shaper(shaper: &Shaper, text: &str, range: Range<usize>, rtl: bool) -> harfrust::Buffer {
let mut buffer = harfrust::Buffer::new();
for (at, c) in text[range.clone()].char_indices() {
let cluster = u32::try_from(range.start + at)
.expect("a text no longer than `u32::MAX` bytes, as `shape` documents");
buffer.push(u32::from(c), cluster);
}
buffer.set_pre_context(&text[..range.start]);
buffer.set_post_context(&text[range.end..]);
buffer.set_direction(if rtl {
harfrust::Direction::RightToLeft
} else {
harfrust::Direction::LeftToRight
});
buffer.guess_segment_properties();
buffer.set_flags(harfrust::BufferFlags::REMOVE_DEFAULT_IGNORABLES);
let plan = shaper.plan(&buffer);
harfrust::shape(
&harfrust::ShaperFont::new(&shaper.font),
&mut buffer,
harfrust::ShapeOptions::new().plan(Some(&plan)),
)
.expect("a buffer with its direction set, and a plan made for its direction and script");
buffer
}
#[cfg(feature = "parsed-font")]
const PLANS_KEPT: usize = 16;
#[cfg(feature = "parsed-font")]
pub(super) struct Shaper {
font: harfrust::Font,
plans: Mutex<Vec<Arc<harfrust::ShapePlan>>>,
#[cfg(test)]
compiled: core::sync::atomic::AtomicUsize,
#[cfg(test)]
runs: core::sync::atomic::AtomicUsize,
}
#[cfg(feature = "parsed-font")]
impl Shaper {
pub(super) fn new(font: harfrust::Font) -> Self {
Self {
font,
plans: Mutex::new(Vec::new()),
#[cfg(test)]
compiled: core::sync::atomic::AtomicUsize::new(0),
#[cfg(test)]
runs: core::sync::atomic::AtomicUsize::new(0),
}
}
fn plan(&self, buffer: &harfrust::Buffer) -> Arc<harfrust::ShapePlan> {
let (direction, script, language) =
(buffer.direction(), buffer.script(), buffer.language());
let key = harfrust::ShapePlanKey::new(&self.font, script, direction).language(language);
let mut plans = self.plans.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(at) = plans.iter().position(|plan| key.matches(plan)) {
plans[..=at].rotate_right(1);
return Arc::clone(&plans[0]);
}
let plan = Arc::new(harfrust::ShapePlan::new(
&self.font,
direction,
script,
language,
&[],
));
#[cfg(test)]
self.compiled
.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
plans.truncate(PLANS_KEPT - 1);
plans.insert(0, Arc::clone(&plan));
plan
}
}
#[cfg(feature = "parsed-font")]
pub(super) fn shapes_to_colour(face: &Font, cluster: &str) -> bool {
match &face.parsed().shaper {
Some(shaper) => run_shaper(shaper, cluster, 0..cluster.len(), false)
.glyph_infos()
.iter()
.any(|info| face.has_color_glyph(GlyphId(info.glyph_id))),
None => cluster
.chars()
.any(|c| face.has_color_glyph(face.glyph_id(c))),
}
}
#[cfg(not(feature = "parsed-font"))]
pub(super) fn shapes_to_colour(face: &Font, _cluster: &str) -> bool {
match face.source.never {}
}
#[cfg(feature = "parsed-font")]
fn nominal(
face: &Font,
index: u8,
scale: f32,
text: &str,
range: Range<usize>,
out: &mut Vec<ShapedGlyph>,
) {
for (at, c) in text[range.clone()].char_indices() {
if fallback::is_invisible(c) {
continue;
}
let glyph = face.glyph_id(c);
out.push(ShapedGlyph {
face: index,
glyph,
cluster: range.start + at,
advance: face.advance(glyph) * scale,
offset: Vec2::ZERO,
unsafe_to_break: false,
});
}
}
#[cfg(all(test, feature = "parsed-font"))]
pub(crate) fn shaped_runs(font: &Font) -> usize {
font.parsed()
.shaper
.as_ref()
.expect("a parsed face")
.runs
.load(core::sync::atomic::Ordering::Relaxed)
}
#[cfg(all(test, feature = "parsed-font"))]
mod tests {
use super::*;
use crate::font::fixtures;
const SIZE: f32 = 16.0;
fn shaped(font: &Font, text: &str) -> Vec<ShapedGlyph> {
let mut out = Vec::new();
shape(font, SIZE, text, 0..text.len(), false, &mut out);
out
}
fn reference(text: &str) -> Vec<(u32, u32, i32, i32, i32)> {
let font = harfrust::Font::new(crate::font::SANS_TTF, 0).expect("reads");
let mut buffer = harfrust::Buffer::new();
buffer.push_str(text);
buffer.guess_segment_properties();
harfrust::shape(
&harfrust::ShaperFont::new(&font),
&mut buffer,
harfrust::ShapeOptions::new(),
)
.expect("shapes");
buffer
.glyph_infos()
.iter()
.zip(buffer.glyph_positions())
.map(|(info, at)| {
(
info.glyph_id,
info.cluster,
at.x_advance,
at.x_offset,
at.y_offset,
)
})
.collect()
}
#[test]
fn advances_and_offsets_are_harfrusts_scaled_to_the_size() {
let font = Font::sans();
let scale = font.metrics().scale(SIZE);
for text in ["AVATAR", "h\u{301}x", "The quick"] {
let got = shaped(font, text);
let want = reference(text);
assert_eq!(got.len(), want.len(), "{text:?}");
for (got, (glyph, cluster, advance, x, y)) in got.iter().zip(want) {
assert_eq!(got.face, 0);
assert_eq!(got.glyph, GlyphId(glyph), "{text:?}");
assert_eq!(got.cluster, cluster as usize, "{text:?}");
assert_eq!(got.advance, advance as f32 * scale, "{text:?}");
assert_eq!(got.offset, Vec2::new(x as f32, -y as f32) * scale);
}
}
let pair = shaped(font, "AV");
let a = font.glyph_id('A');
assert!(
pair[0].advance < font.advance(a) * scale,
"AV is not kerned"
);
let mark = shaped(font, "h\u{301}");
assert_eq!(mark.len(), 2);
assert_eq!(mark[1].cluster, 0, "the mark is in its base's cluster");
assert_eq!(mark[1].advance, 0.0);
assert_ne!(mark[1].offset, Vec2::ZERO, "the mark is not positioned");
}
#[test]
fn a_synthesised_bold_widens_what_advances() {
let font = Font::sans();
let bold = font.synthesized(crate::font::face::Synthesis {
bold: true,
italic: false,
});
let extra = SIZE * crate::font::face::SYNTHETIC_BOLD_EM;
let (plain, wide) = (shaped(font, "xh\u{301}"), shaped(&bold, "xh\u{301}"));
for (plain, wide) in plain.iter().zip(&wide).take(2) {
assert!((wide.advance - plain.advance - extra).abs() < 1e-4);
}
assert_eq!(wide[2].advance, 0.0, "the mark was widened");
}
#[test]
fn the_committed_fonts_gsub_applies() {
let font = Font::sans();
let glyphs = shaped(font, "i\u{307}");
assert_eq!(glyphs[0].glyph, font.glyph_id('\u{131}'));
assert_ne!(glyphs[0].glyph, font.glyph_id('i'));
}
#[test]
fn sequences_and_flags_ligate_into_colour_glyphs() {
let face = fixtures::shaped();
let font = Font::sans().with_fallback(&[fixtures::cbdt(), face.clone()]);
let text = "a\u{2764}\u{200D}\u{1F525}\u{1F1FA}\u{1F1F8}b";
let glyphs = shaped(&font, text);
let faces: Vec<u8> = glyphs.iter().map(|glyph| glyph.face).collect();
assert_eq!(faces, [0, 2, 2, 0]);
let clusters: Vec<usize> = glyphs.iter().map(|glyph| glyph.cluster).collect();
assert_eq!(clusters, [0, 1, 11, 19]);
for ligature in &glyphs[1..3] {
assert!(face.has_color_glyph(ligature.glyph));
assert_eq!(ligature.advance, SIZE, "an em wide, as every glyph is");
}
let hearts = shaped(&face, "\u{2764}\u{FE0F}\u{2764}");
assert_eq!(hearts.len(), 2, "the selector left a glyph");
for component in ['\u{2764}', '\u{1F525}', '\u{1F1FA}', '\u{1F1F8}'] {
assert!(!face.has_color_glyph(face.glyph_id(component)));
}
}
#[test]
fn a_right_to_left_run_is_harfrusts_reversed() {
let font = Font::sans();
let scale = font.metrics().scale(SIZE);
let text = "AVAT";
let mut got = Vec::new();
shape(font, SIZE, text, 0..text.len(), true, &mut got);
let shaper = harfrust::Font::new(crate::font::SANS_TTF, 0).expect("reads");
let mut buffer = harfrust::Buffer::new();
buffer.push_str(text);
buffer.set_direction(harfrust::Direction::RightToLeft);
buffer.guess_segment_properties();
harfrust::shape(
&harfrust::ShaperFont::new(&shaper),
&mut buffer,
harfrust::ShapeOptions::new(),
)
.expect("shapes");
let want: Vec<(GlyphId, usize, f32)> = buffer
.glyph_infos()
.iter()
.zip(buffer.glyph_positions())
.rev()
.map(|(info, at)| {
(
GlyphId(info.glyph_id),
info.cluster as usize,
at.x_advance as f32 * scale,
)
})
.collect();
let got: Vec<(GlyphId, usize, f32)> = got
.iter()
.map(|glyph| (glyph.glyph, glyph.cluster, glyph.advance))
.collect();
assert_eq!(got, want);
let clusters: Vec<usize> = got.iter().map(|glyph| glyph.1).collect();
assert_eq!(clusters, [0, 1, 2, 3], "not in logical order");
assert_ne!(
got,
{
let mut ltr = Vec::new();
shape(font, SIZE, text, 0..text.len(), false, &mut ltr);
ltr.iter()
.map(|glyph| (glyph.glyph, glyph.cluster, glyph.advance))
.collect::<Vec<_>>()
},
"the direction changed nothing, so the test cannot tell it was set"
);
}
fn compiled(font: &Font) -> usize {
font.parsed()
.shaper
.as_ref()
.expect("a parsed face")
.compiled
.load(core::sync::atomic::Ordering::Relaxed)
}
#[test]
fn a_face_compiles_one_plan_per_direction_and_script() {
let font = Font::parse(crate::font::SANS_TTF).expect("parses");
let first = shaped(&font, "Iron Sword");
assert_eq!(compiled(&font), 1);
assert_eq!(
shaped(&font, "Iron Sword"),
first,
"a kept plan shaped it differently"
);
shaped(&font, "Health Potion");
assert_eq!(compiled(&font), 1, "a second Latin label compiled a plan");
let mut rtl = Vec::new();
shape(&font, SIZE, "AVAT", 0..4, true, &mut rtl);
assert_eq!(
compiled(&font),
2,
"right to left shared left to right's plan"
);
shaped(&font, "\u{3b1}\u{3b2}");
assert_eq!(compiled(&font), 3, "Greek shared Latin's plan");
shaped(&font, "Iron Sword");
shape(&font, SIZE, "VA", 0..2, true, &mut rtl);
assert_eq!(compiled(&font), 3, "a kept plan was compiled again");
}
#[test]
fn the_least_recently_used_plan_goes_past_the_bound() {
let font = Font::parse(crate::font::SANS_TTF).expect("parses");
let scripts = [
"a", "\u{3b1}", "\u{431}", "\u{561}", "\u{5d0}", "\u{628}", "\u{915}", "\u{995}",
"\u{a15}", "\u{a95}", "\u{b15}", "\u{b95}", "\u{c15}", "\u{c95}", "\u{d15}", "\u{e01}",
"\u{10d0}",
];
assert_eq!(scripts.len(), PLANS_KEPT + 1);
let (last, kept) = scripts.split_last().expect("not empty");
for letter in kept {
shaped(&font, letter);
}
assert_eq!(compiled(&font), PLANS_KEPT, "two scripts shared a plan");
shaped(&font, scripts[0]);
shaped(&font, last);
assert_eq!(compiled(&font), PLANS_KEPT + 1);
shaped(&font, scripts[0]);
assert_eq!(
compiled(&font),
PLANS_KEPT + 1,
"the recently used plan was dropped"
);
shaped(&font, scripts[1]);
assert_eq!(
compiled(&font),
PLANS_KEPT + 2,
"the least recently used plan was kept"
);
}
#[test]
fn only_the_range_is_shaped_with_clusters_in_the_whole_text() {
let font = Font::sans();
let mut out = Vec::new();
shape(font, SIZE, "ab cd", 3..5, false, &mut out);
let clusters: Vec<usize> = out.iter().map(|glyph| glyph.cluster).collect();
assert_eq!(clusters, [3, 4]);
assert_eq!(out[0].glyph, font.glyph_id('c'));
}
}