use core::ops::Range;
use glam::Vec2;
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 fn shape(font: &Font, size: f32, text: &str, range: Range<usize>, 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, out);
}
run = Some((face, bytes));
}
}
}
if let Some((face, run)) = run {
shape_run(font, face, size, text, run, out);
}
}
#[cfg(not(feature = "parsed-font"))]
fn shape_run(
font: &Font,
face: u8,
_size: f32,
_text: &str,
_range: Range<usize>,
_out: &mut Vec<ShapedGlyph>,
) {
match font.face(face).parsed.never {}
}
#[cfg(feature = "parsed-font")]
fn shape_run(
font: &Font,
face: u8,
size: f32,
text: &str,
range: Range<usize>,
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;
};
let buffer = run_shaper(shaper, text, range);
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,
),
});
}
}
#[cfg(feature = "parsed-font")]
fn run_shaper(shaper: &harfrust::Font, text: &str, range: Range<usize>) -> 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(harfrust::Direction::LeftToRight);
buffer.guess_segment_properties();
buffer.set_flags(harfrust::BufferFlags::REMOVE_DEFAULT_IGNORABLES);
harfrust::shape(
&harfrust::ShaperFont::new(shaper),
&mut buffer,
harfrust::ShapeOptions::new(),
)
.expect("a fresh buffer with its direction set is everything shaping asks for");
buffer
}
#[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())
.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.parsed.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,
});
}
}
#[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(), &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 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, &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'));
}
}