use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use font_model::{Font, GlyphId};
use crate::error::ShapeError;
use crate::glyph::DeviceMetrics;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LayoutDirection {
#[default]
LeftToRight,
RightToLeft,
Auto,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BidiClass {
L,
R,
Al,
En,
An,
Nsm,
Et,
Other,
}
impl BidiClass {
#[must_use]
pub const fn is_strong(self) -> bool {
matches!(self, BidiClass::L | BidiClass::R | BidiClass::Al)
}
#[must_use]
pub const fn is_rtl(self) -> bool {
matches!(self, BidiClass::R | BidiClass::Al)
}
}
#[must_use]
pub fn bidi_class(ch: char) -> BidiClass {
let c = ch as u32;
match c {
0x0300..=0x036F
| 0x0483..=0x0489
| 0x0591..=0x05C7
| 0x0610..=0x061A
| 0x064B..=0x065F
| 0x0670
| 0x06D6..=0x06DC => BidiClass::Nsm,
0x0590..=0x05FF => BidiClass::R, 0x0600..=0x07BF => {
match c {
0x0660..=0x0669 | 0x06F0..=0x06F9 | 0x066B | 0x066C | 0x06DD => BidiClass::An,
_ => BidiClass::Al,
}
}
0xFB1D..=0xFB4F => BidiClass::R, 0xFB50..=0xFDFF => BidiClass::Al, 0xFE70..=0xFEFF => BidiClass::Al, 0x200B..=0x200D => BidiClass::Other, 0x2010..=0x2027 => BidiClass::Other, 0x2000..=0x200A | 0x2028 | 0x2029 | 0x205F | 0x3000 => BidiClass::Other,
0x30FB => BidiClass::Other, 0x20A0..=0x20CF => BidiClass::Et, 0x00A2..=0x00A5 | 0x00B0 | 0x00B1 => BidiClass::Et, 0x0030..=0x0039 => BidiClass::En,
0x00B2 | 0x00B3 | 0x00B9 => BidiClass::En, 0x2070 | 0x2074..=0x2079 => BidiClass::En,
0xFF10..=0xFF19 => BidiClass::En, 0x0000..=0x002F | 0x003A..=0x0040 | 0x005B..=0x0060 | 0x007B..=0x007E => BidiClass::Other,
0x00A0..=0x00BF => match c {
0x00A2..=0x00A5 | 0x00B0 | 0x00B1 => BidiClass::Et,
_ => BidiClass::Other,
},
0x2030..=0x205E => BidiClass::Other, 0x2100..=0x214F => BidiClass::Other, 0x2190..=0x2BFF => BidiClass::Other, 0xFB00..=0xFB17 => BidiClass::Other, _ => BidiClass::L,
}
}
#[must_use]
pub const fn is_rtl_level(level: u8) -> bool {
level % 2 == 1
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BidiLevels {
pub levels: Vec<u8>,
pub paragraph_level: u8,
}
impl BidiLevels {
#[must_use]
pub fn level(&self, index: usize) -> u8 {
self.levels
.get(index)
.copied()
.unwrap_or(self.paragraph_level)
}
#[must_use]
pub fn visual_order(&self) -> Vec<usize> {
reorder_indices(&self.levels)
}
}
#[must_use]
pub fn reorder_indices(levels: &[u8]) -> Vec<usize> {
let n = levels.len();
let mut order: Vec<usize> = (0..n).collect();
if n == 0 {
return order;
}
let mut lowest_odd = u8::MAX;
for &l in levels {
if l % 2 == 1 && l < lowest_odd {
lowest_odd = l;
}
}
if lowest_odd == u8::MAX {
return order;
}
let highest = levels.iter().copied().fold(0u8, |a, b| a.max(b));
let level_of = |slot: usize, order: &[usize]| -> Option<u8> {
order.get(slot).and_then(|&j| levels.get(j)).copied()
};
let mut level = highest;
while level >= lowest_odd {
let at_level = |slot: usize, order: &[usize]| -> bool {
level_of(slot, order).is_some_and(|l| l >= level)
};
let mut i = 0usize;
while i < n {
if at_level(i, &order) {
let start = i;
while i < n && at_level(i, &order) {
i += 1;
}
if let Some(run) = order.get_mut(start..i) {
run.reverse();
}
} else {
i += 1;
}
}
if level == 0 {
break;
}
level -= 1;
}
order
}
#[must_use]
pub fn resolve_bidi(text: &str) -> BidiLevels {
let chars: Vec<char> = text.chars().collect();
resolve_bidi_chars(&chars)
}
#[must_use]
pub fn resolve_bidi_chars(chars: &[char]) -> BidiLevels {
let paragraph_level = paragraph_level_for(chars);
let mut classes: Vec<BidiClass> = chars.iter().copied().map(bidi_class).collect();
apply_w_rules(chars, &mut classes);
let base_rtl = is_rtl_level(paragraph_level);
let resolved: Vec<BidiClass> = classes
.iter()
.enumerate()
.map(|(i, &cls)| {
if matches!(
cls,
BidiClass::L | BidiClass::R | BidiClass::En | BidiClass::An
) {
cls
} else {
neutral_direction(
cls,
i.checked_sub(1).and_then(|k| classes.get(k).copied()),
classes.get(i + 1).copied(),
base_rtl,
)
}
})
.collect();
let mut levels: Vec<u8> = resolved
.iter()
.map(|&cls| implicit_level(cls, base_rtl, paragraph_level))
.collect();
for (level, ch) in levels.iter_mut().rev().zip(chars.iter().rev()) {
if is_separator(*ch) || ch.is_whitespace() {
*level = paragraph_level;
} else {
break;
}
}
BidiLevels {
levels,
paragraph_level,
}
}
fn is_separator(ch: char) -> bool {
matches!(ch, '\u{2029}' | '\u{2028}' | '\r' | '\n')
}
fn paragraph_level_for(chars: &[char]) -> u8 {
for &ch in chars {
match bidi_class(ch) {
BidiClass::L => return 0,
BidiClass::R | BidiClass::Al => return 1,
_ => {}
}
}
0
}
fn apply_w_rules(chars: &[char], classes: &mut [BidiClass]) {
let mut prev = BidiClass::Other;
for cls in classes.iter_mut() {
if *cls == BidiClass::Nsm {
*cls = prev;
}
prev = *cls;
}
let mut last_strong: Option<BidiClass> = None;
for cls in classes.iter_mut() {
if cls.is_strong() {
last_strong = Some(*cls);
}
if *cls == BidiClass::En && last_strong == Some(BidiClass::Al) {
*cls = BidiClass::An;
}
}
for cls in classes.iter_mut() {
if *cls == BidiClass::Al {
*cls = BidiClass::R;
}
}
let snapshot: Vec<BidiClass> = classes.to_vec();
let is_number = |c: BidiClass| matches!(c, BidiClass::En | BidiClass::An);
for i in 1..snapshot.len().saturating_sub(1) {
let (Some(&before), Some(&after), Some(&ch)) =
(snapshot.get(i - 1), snapshot.get(i + 1), chars.get(i))
else {
continue;
};
if !is_common_separator(ch) || !is_number(before) || before != after {
continue;
}
if let Some(slot) = classes.get_mut(i) {
*slot = before;
}
}
let snapshot: Vec<BidiClass> = classes.to_vec();
let is_et = |i: usize| snapshot.get(i) == Some(&BidiClass::Et);
let mut i = 0usize;
while i < snapshot.len() {
if !is_et(i) {
i += 1;
continue;
}
let start = i;
while i < snapshot.len() && is_et(i) {
i += 1;
}
let before_en = start > 0 && snapshot.get(start - 1) == Some(&BidiClass::En);
let after_en = snapshot.get(i) == Some(&BidiClass::En);
if before_en || after_en {
for slot in classes.iter_mut().take(i).skip(start) {
if *slot == BidiClass::Et {
*slot = BidiClass::En;
}
}
}
}
for cls in classes.iter_mut() {
if *cls == BidiClass::Et {
*cls = BidiClass::Other;
}
}
let mut last_strong = None;
for cls in classes.iter_mut() {
if cls.is_strong() {
last_strong = Some(*cls);
}
if *cls == BidiClass::En && last_strong == Some(BidiClass::L) {
*cls = BidiClass::L;
}
}
}
fn is_common_separator(ch: char) -> bool {
matches!(
ch,
',' | '.'
| '/'
| ':'
| '\u{060C}'
| '\u{202F}'
| '\u{2044}'
| '\u{FF0C}'
| '\u{FF0E}'
| '\u{FF1A}'
)
}
fn neutral_direction(
_cls: BidiClass,
prev: Option<BidiClass>,
next: Option<BidiClass>,
base_rtl: bool,
) -> BidiClass {
let side = |c: Option<BidiClass>| -> Option<BidiClass> {
match c {
Some(BidiClass::En) | Some(BidiClass::An) => {
Some(if base_rtl { BidiClass::R } else { BidiClass::L })
}
Some(BidiClass::L) | Some(BidiClass::R) => c,
_ => None,
}
};
let base = if base_rtl { BidiClass::R } else { BidiClass::L };
match (side(prev), side(next)) {
(Some(b), Some(a)) if b == a => b,
_ => base,
}
}
fn implicit_level(cls: BidiClass, base_rtl: bool, paragraph_level: u8) -> u8 {
let number = matches!(cls, BidiClass::En | BidiClass::An);
if base_rtl {
if cls.is_rtl() && !number {
paragraph_level
} else {
paragraph_level + 1
}
} else if cls.is_rtl() {
paragraph_level + 1
} else if number {
paragraph_level + 2
} else {
paragraph_level
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct GlyphPlacement {
pub glyph: GlyphId,
pub cluster: u32,
pub level: u8,
pub x: f32,
pub y: f32,
pub x_offset: f32,
pub advance_px: f32,
}
impl GlyphPlacement {
#[must_use]
pub fn origin_px(&self) -> (f32, f32) {
(self.x + self.x_offset, self.y)
}
#[must_use]
pub const fn is_rtl(&self) -> bool {
is_rtl_level(self.level)
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ShapedLine {
placements: Vec<GlyphPlacement>,
advance_px: f32,
ascent_px: f32,
descent_px: f32,
line_height_px: f32,
ink_left_px: Option<f32>,
ink_right_px: Option<f32>,
paragraph_level: u8,
}
impl ShapedLine {
#[must_use]
pub const fn empty() -> Self {
ShapedLine {
placements: Vec::new(),
advance_px: 0.0,
ascent_px: 0.0,
descent_px: 0.0,
line_height_px: 0.0,
ink_left_px: None,
ink_right_px: None,
paragraph_level: 0,
}
}
#[must_use]
pub fn placements(&self) -> &[GlyphPlacement] {
&self.placements
}
#[must_use]
pub fn into_placements(self) -> Vec<GlyphPlacement> {
self.placements
}
#[must_use]
pub fn len(&self) -> usize {
self.placements.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.placements.is_empty()
}
#[must_use]
pub fn get(&self, index: usize) -> Option<&GlyphPlacement> {
self.placements.get(index)
}
#[must_use]
pub const fn advance_px(&self) -> f32 {
self.advance_px
}
#[must_use]
pub const fn ascent_px(&self) -> f32 {
self.ascent_px
}
#[must_use]
pub const fn descent_px(&self) -> f32 {
self.descent_px
}
#[must_use]
pub const fn line_height_px(&self) -> f32 {
self.line_height_px
}
#[must_use]
pub const fn ink_extent_px(&self) -> Option<(f32, f32)> {
match (self.ink_left_px, self.ink_right_px) {
(Some(l), Some(r)) => Some((l, r)),
_ => None,
}
}
#[must_use]
pub const fn paragraph_level(&self) -> u8 {
self.paragraph_level
}
#[must_use]
pub const fn metrics(&self) -> DeviceMetrics {
DeviceMetrics {
ascent: self.ascent_px,
descent: self.descent_px,
line_gap: self.line_height_px - self.ascent_px - self.descent_px,
line_height: self.line_height_px,
}
}
#[must_use]
pub fn by_cluster(&self) -> Vec<(u32, Vec<&GlyphPlacement>)> {
let mut clusters: Vec<(u32, Vec<&GlyphPlacement>)> = Vec::new();
for p in &self.placements {
match clusters.last_mut() {
Some((c, list)) if *c == p.cluster => list.push(p),
_ => clusters.push((p.cluster, vec![p])),
}
}
clusters.sort_by_key(|(cluster, _)| *cluster);
clusters
}
}
pub fn set_explicit_levels(levels: &mut [u8], explicit: &[u8]) {
for (slot, &v) in levels.iter_mut().zip(explicit.iter()) {
*slot = if v > 8 { 8 } else { v };
}
}
#[allow(clippy::result_large_err)]
pub fn measure_glyph(font: &Font, glyph: GlyphId, size_px: f32) -> Result<f32, ShapeError> {
if !size_px.is_finite() || size_px <= 0.0 {
return Err(ShapeError::InvalidScale { scale: size_px });
}
let g = font.glyph(glyph).ok_or(ShapeError::UnknownGlyph {
glyph: glyph.to_u32(),
glyph_count: font.glyph_count(),
})?;
let scale = size_px / f32::from(font.units_per_em().max(1));
Ok(f32::from(g.advance_width()) * scale)
}
#[allow(clippy::result_large_err)]
pub fn measure_run(font: &Font, text: &str, size_px: f32) -> Result<f32, ShapeError> {
if !size_px.is_finite() || size_px <= 0.0 {
return Err(ShapeError::InvalidScale { scale: size_px });
}
let scale = size_px / f32::from(font.units_per_em().max(1));
let mut pen = 0.0f32;
let mut prev: Option<GlyphId> = None;
for ch in text.chars() {
let glyph = glyph_for_char(font, ch);
pen += f32::from(advance_of(font, glyph)) * scale;
if let Some(p) = prev {
pen += f32::from(font.kern(p, glyph)) * scale;
}
prev = Some(glyph);
}
Ok(pen)
}
#[allow(clippy::result_large_err)]
pub fn shape_line(font: &Font, text: &str, size_px: f32) -> Result<ShapedLine, ShapeError> {
shape_line_with(font, text, size_px, LayoutDirection::LeftToRight)
}
#[allow(clippy::result_large_err)]
pub fn shape_line_with(
font: &Font,
text: &str,
size_px: f32,
direction: LayoutDirection,
) -> Result<ShapedLine, ShapeError> {
if !size_px.is_finite() || size_px <= 0.0 {
return Err(ShapeError::InvalidScale { scale: size_px });
}
let metrics = crate::glyph::scaled_metrics(font, size_px)?;
let scale = size_px / f32::from(font.units_per_em().max(1));
let chars: Vec<char> = text.chars().collect();
let mut bidi = resolve_bidi_chars(&chars);
if direction == LayoutDirection::RightToLeft {
bidi.paragraph_level = 1;
for level in bidi.levels.iter_mut() {
*level = (*level & 0xFE) | 1;
}
}
let mut logical: Vec<GlyphPlacement> = Vec::with_capacity(chars.len());
let mut pen = 0.0f32;
let mut prev: Option<GlyphId> = None;
for (i, &ch) in chars.iter().enumerate() {
let glyph = glyph_for_char(font, ch);
let advance = f32::from(advance_of(font, glyph)) * scale;
let kern = match prev {
Some(p) => f32::from(font.kern(p, glyph)) * scale,
None => 0.0,
};
logical.push(GlyphPlacement {
glyph,
cluster: i as u32,
level: bidi.level(i),
x: pen + kern,
y: 0.0,
x_offset: 0.0,
advance_px: advance,
});
pen += advance + kern;
prev = Some(glyph);
}
let order = bidi.visual_order();
let placements: Vec<GlyphPlacement> = order
.iter()
.filter_map(|i| logical.get(*i).cloned())
.collect();
Ok(ShapedLine {
placements,
advance_px: pen,
ascent_px: metrics.ascent,
descent_px: metrics.descent,
line_height_px: metrics.line_height,
ink_left_px: None,
ink_right_px: None,
paragraph_level: bidi.paragraph_level,
})
}
pub fn ascii_cmap() -> font_model::Cmap {
let mut cmap = font_model::Cmap::format4();
for (cp, gid) in [
(b'A' as u32, GlyphId::new(1)),
(b'B' as u32, GlyphId::new(2)),
] {
let updated = cmap
.subtables()
.first()
.map_or_else(font_model::CmapSubtable::empty_format4, |sub| {
font_model::insert_entry(sub, cp, gid)
});
if let Some(slot) = cmap.subtables_mut().first_mut() {
*slot = updated;
}
}
cmap
}
fn glyph_for_char(font: &Font, ch: char) -> GlyphId {
font.glyph_for(ch as u32)
.or_else(|| font.notdef().map(|g| g.id()))
.unwrap_or(GlyphId::NOTDEF)
}
fn advance_of(font: &Font, glyph: GlyphId) -> u16 {
font.glyph(glyph)
.map_or(0, font_model::Glyph::advance_width)
}
#[must_use]
pub fn describe_run(font: &Font, text: &str, size_px: f32) -> String {
let mut s = String::new();
for ch in text.chars() {
s.push(ch);
}
let advance = measure_run(font, text, size_px).unwrap_or(0.0);
alloc::format!("{s:?} at {size_px}px = {advance:.2}px")
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing,
clippy::float_cmp
)]
use super::{
bidi_class, measure_glyph, measure_run, reorder_indices, resolve_bidi, resolve_bidi_chars,
set_explicit_levels, shape_line, shape_line_with, BidiClass, LayoutDirection, ShapedLine,
};
use crate::error::ShapeError;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use font_model::{Font, Glyph, GlyphId};
fn close(a: f32, b: f32) -> bool {
(a - b).abs() < 1e-3
}
fn blank_font(widths: &[(u32, u16)]) -> Font {
let mut font = Font::builder().units_per_em(1000);
font = font.glyph(Glyph::empty(GlyphId::NOTDEF, 500));
let mut cmap = font_model::Cmap::format4();
for (cp, w) in widths {
let index = widths.iter().position(|(c, _)| c == cp).unwrap_or(0);
let gid = GlyphId::new(index as u16 + 1);
let g = Glyph::empty(gid, *w);
font = font.glyph(g);
cmap.subtables_mut()[0] = font_model::insert_entry(&cmap.subtables()[0], *cp, gid);
}
font.cmap(cmap).build().expect("font")
}
#[test]
fn bidi_class_covers_the_script_families() {
assert_eq!(bidi_class('a'), BidiClass::L);
assert_eq!(bidi_class('Z'), BidiClass::L);
assert_eq!(bidi_class('\u{4E00}'), BidiClass::L, "CJK is L");
assert_eq!(bidi_class('\u{05D0}'), BidiClass::R, "Hebrew");
assert_eq!(bidi_class('\u{0627}'), BidiClass::Al, "Arabic");
assert_eq!(bidi_class('\u{0660}'), BidiClass::An, "Arabic-Indic digit");
assert_eq!(
bidi_class('\u{06F0}'),
BidiClass::An,
"extended Arabic digit"
);
assert_eq!(bidi_class('7'), BidiClass::En);
assert_eq!(bidi_class('\u{FF10}'), BidiClass::En, "fullwidth digit");
assert_eq!(bidi_class('\u{0301}'), BidiClass::Nsm);
assert_eq!(bidi_class(' '), BidiClass::Other);
assert_eq!(
bidi_class('.'),
BidiClass::Other,
"ASCII period is a neutral"
);
assert_eq!(bidi_class('-'), BidiClass::Other, "and so is a hyphen");
assert_eq!(bidi_class('\u{20AC}'), BidiClass::Et, "euro sign");
assert!(BidiClass::L.is_strong() && BidiClass::R.is_strong());
assert!(BidiClass::Al.is_strong());
assert!(!BidiClass::En.is_strong() && !BidiClass::Nsm.is_strong());
assert!(!BidiClass::L.is_rtl() && BidiClass::R.is_rtl() && BidiClass::Al.is_rtl());
assert!(!BidiClass::En.is_rtl());
}
#[test]
fn rtl_level_test() {
assert!(super::is_rtl_level(1));
assert!(!super::is_rtl_level(0));
assert!(super::is_rtl_level(3) && !super::is_rtl_level(2));
}
#[test]
fn pure_ltr_resolves_to_level_zero() {
let b = resolve_bidi("hello world");
assert_eq!(b.paragraph_level, 0);
assert!(b.levels.iter().all(|&l| l == 0), "{b:?}");
assert_eq!(b.visual_order(), (0..11).collect::<Vec<_>>());
assert_eq!(b.level(0), 0);
assert_eq!(b.level(999), 0);
}
#[test]
fn leading_rtl_sets_the_paragraph_to_rtl() {
let b = resolve_bidi("\u{05D0}\u{05D1}abc");
assert_eq!(b.paragraph_level, 1);
assert_eq!(b.levels, vec![1, 1, 2, 2, 2], "{b:?}");
assert_eq!(b.visual_order(), vec![2, 3, 4, 1, 0]);
let visual: String = b
.visual_order()
.into_iter()
.filter_map(|i| "\u{05D0}\u{05D1}abc".chars().nth(i))
.collect();
assert_eq!(visual, "abc\u{05D1}\u{05D0}");
}
#[test]
fn embedded_rtl_in_ltr_reverses_only_its_run() {
let b = resolve_bidi("a\u{05D0}\u{05D1}b");
assert_eq!(b.paragraph_level, 0);
assert_eq!(b.levels, vec![0, 1, 1, 0], "{b:?}");
assert_eq!(b.visual_order(), vec![0, 2, 1, 3]);
let visual: String = b
.visual_order()
.into_iter()
.filter_map(|i| "a\u{05D0}\u{05D1}b".chars().nth(i))
.collect();
assert_eq!(visual, "a\u{05D1}\u{05D0}b");
}
#[test]
fn numbers_inside_rtl_take_an_even_level() {
let b = resolve_bidi("\u{05D0}5\u{05D1}");
assert_eq!(b.levels, vec![1, 2, 1], "{b:?}");
assert_eq!(b.visual_order(), vec![2, 1, 0]);
let visual: String = b
.visual_order()
.into_iter()
.filter_map(|i| "\u{05D0}5\u{05D1}".chars().nth(i))
.collect();
assert_eq!(visual, "\u{05D1}5\u{05D0}");
}
#[test]
fn numbers_inside_ltr_stay_ltr() {
let b = resolve_bidi("a5b");
assert_eq!(b.levels, vec![0, 0, 0], "{b:?}");
assert_eq!(b.visual_order(), vec![0, 1, 2]);
}
#[test]
fn a_neutral_between_mixed_strong_runs_resolves_to_the_base() {
let b = resolve_bidi("a \u{05D0}");
assert_eq!(b.paragraph_level, 0);
assert_eq!(b.levels, vec![0, 0, 1], "{b:?}");
}
#[test]
fn a_neutral_between_two_ltr_stays_ltr() {
let b = resolve_bidi("a b");
assert_eq!(b.levels, vec![0, 0, 0], "{b:?}");
}
#[test]
fn combining_marks_take_their_neighbour_direction() {
let b = resolve_bidi("\u{05D0}\u{05B0}");
assert_eq!(b.levels, vec![1, 1], "mark after Hebrew is RTL: {b:?}");
let b = resolve_bidi("a\u{0301}");
assert_eq!(b.levels, vec![0, 0], "mark after Latin is LTR: {b:?}");
}
#[test]
fn reorder_indices_is_the_identity_for_uniform_levels() {
assert_eq!(reorder_indices(&[]), Vec::<usize>::new());
assert_eq!(reorder_indices(&[0]), vec![0]);
assert_eq!(reorder_indices(&[0, 0, 0, 0]), vec![0, 1, 2, 3]);
assert_eq!(reorder_indices(&[1, 1, 1, 1]), vec![3, 2, 1, 0]);
assert_eq!(reorder_indices(&[2, 2, 2]), vec![0, 1, 2]);
}
#[test]
fn reorder_handles_nested_levels() {
assert_eq!(reorder_indices(&[0, 1, 2, 2, 1]), vec![0, 4, 2, 3, 1]);
assert_eq!(reorder_indices(&[0, 1, 1, 0]), vec![0, 2, 1, 3]);
}
#[test]
fn set_explicit_levels_clamps_and_applies() {
let mut b = resolve_bidi("AB");
set_explicit_levels(&mut b.levels, &[1, 1]);
assert_eq!(b.levels, vec![1, 1]);
assert_eq!(b.visual_order(), vec![1, 0]);
set_explicit_levels(&mut b.levels, &[99, 4]);
assert_eq!(b.levels, vec![8, 4]);
set_explicit_levels(&mut b.levels, &[0]);
assert_eq!(b.levels, vec![0, 4]);
}
#[test]
fn measure_glyph_scales_the_advance() {
let font = blank_font(&[(b'A' as u32, 600)]);
assert!(close(
measure_glyph(&font, GlyphId::new(1), 50.0).expect("ok"),
30.0
));
assert!(close(
measure_glyph(&font, GlyphId::new(1), 10.0).expect("ok"),
6.0
));
assert!(matches!(
measure_glyph(&font, GlyphId::new(1), 0.0),
Err(ShapeError::InvalidScale { .. })
));
match measure_glyph(&font, GlyphId::new(9), 12.0).unwrap_err() {
ShapeError::UnknownGlyph { glyph, glyph_count } => {
assert_eq!(glyph, 9);
assert_eq!(glyph_count, 2);
}
other => panic!("expected UnknownGlyph, got {other:?}"),
}
}
#[test]
fn numbers_keep_their_order_inside_an_rtl_paragraph() {
let b = resolve_bidi("\u{05D0}1.5\u{05D1}");
assert_eq!(b.levels, vec![1, 2, 2, 2, 1], "{b:?}");
let visual: String = b
.visual_order()
.into_iter()
.filter_map(|i| "\u{05D0}1.5\u{05D1}".chars().nth(i))
.collect();
assert_eq!(visual, "\u{05D1}1.5\u{05D0}");
}
#[test]
fn arabic_digits_inside_arabic_text_stay_in_place() {
let text = "\u{0627}\u{0661}\u{0628}\u{0662}\u{0627}";
let b = resolve_bidi(text);
assert_eq!(b.levels, vec![1, 2, 1, 2, 1], "{b:?}");
let visual: String = b
.visual_order()
.into_iter()
.filter_map(|i| text.chars().nth(i))
.collect();
assert_eq!(visual, "\u{0627}\u{0662}\u{0628}\u{0661}\u{0627}");
}
#[test]
fn measure_run_sums_advances_and_kerning() {
let mut font = blank_font(&[(b'A' as u32, 500), (b'B' as u32, 700)]);
font.kerning_mut()
.insert(GlyphId::new(1), GlyphId::new(2), -100);
assert!(close(measure_run(&font, "AB", 100.0).expect("ok"), 110.0));
assert!(close(measure_run(&font, "BA", 100.0).expect("ok"), 120.0));
assert!(close(
measure_run(&font, "AAB", 100.0).expect("ok"),
50.0 + 50.0 + 70.0 - 10.0
));
assert!(close(
measure_run(&font, "AAAB", 100.0).expect("ok"),
50.0 * 3.0 + 70.0 - 10.0
));
assert!(close(
measure_run(&font, "AAAA", 200.0).expect("ok"),
4.0 * 500.0 * 0.2
));
}
#[test]
fn measure_run_edge_cases() {
let font = blank_font(&[(b'A' as u32, 500)]);
assert_eq!(measure_run(&font, "", 12.0).expect("ok"), 0.0);
assert!(close(measure_run(&font, "z", 100.0).expect("ok"), 50.0));
for bad in [0.0, -1.0, f32::NAN, f32::INFINITY] {
assert!(
matches!(
measure_run(&font, "A", bad),
Err(ShapeError::InvalidScale { .. })
),
"{bad}"
);
}
}
#[test]
fn shape_line_places_glyphs_left_to_right() {
let font = blank_font(&[(b'A' as u32, 500), (b'B' as u32, 500)]);
let line = shape_line(&font, "AB", 100.0).expect("ok");
assert_eq!(line.len(), 2);
assert!(!line.is_empty());
assert!(close(line.advance_px(), 100.0));
assert!(close(line.get(0).unwrap().x, 0.0));
assert!(close(line.get(1).unwrap().x, 50.0));
assert!(close(line.get(1).unwrap().advance_px, 50.0));
assert!(close(line.get(1).unwrap().origin_px().0, 50.0));
assert_eq!(line.get(1).unwrap().origin_px().1, 0.0);
assert_eq!(line.get(0).unwrap().cluster, 0);
assert_eq!(line.get(1).unwrap().cluster, 1);
assert!(!line.get(0).unwrap().is_rtl());
}
#[test]
fn shape_line_carries_the_font_metrics() {
let mut font = blank_font(&[(b'A' as u32, 500)]);
*font.metrics_mut() = font_model::FontMetrics {
ascender: 800,
descender: -200,
..font_model::FontMetrics::default()
};
let line = shape_line(&font, "A", 100.0).expect("ok");
assert!(close(line.ascent_px(), 80.0), "{}", line.ascent_px());
assert!(close(line.descent_px(), 20.0));
assert!(close(line.line_height_px(), 100.0));
let m = line.metrics();
assert!(close(m.ascent, 80.0) && close(m.line_height, 100.0));
let empty = shape_line(&font, "", 100.0).expect("ok");
assert!(empty.is_empty());
assert_eq!(empty.advance_px(), 0.0);
assert_eq!(empty.ink_extent_px(), None);
}
#[test]
fn shape_line_right_to_left_reverses_visual_order() {
let font = blank_font(&[(b'A' as u32, 500), (b'B' as u32, 500)]);
let line = shape_line_with(&font, "AB", 100.0, LayoutDirection::RightToLeft).expect("ok");
assert_eq!(line.paragraph_level(), 1);
assert_eq!(line.get(0).unwrap().cluster, 1, "B first");
assert_eq!(line.get(1).unwrap().cluster, 0, "then A");
let a = line
.placements()
.iter()
.find(|p| p.cluster == 0)
.expect("A");
assert!(close(a.x, 0.0));
assert!(a.is_rtl());
}
#[test]
fn shape_line_auto_takes_direction_from_the_text() {
let font = blank_font(&[(b'A' as u32, 500)]);
let auto = shape_line_with(&font, "A", 100.0, LayoutDirection::Auto).expect("ok");
assert_eq!(auto.paragraph_level(), 0);
assert_eq!(LayoutDirection::default(), LayoutDirection::LeftToRight);
}
#[test]
fn shape_line_honours_the_bidi_resolution() {
let mut font = Font::builder()
.units_per_em(1000)
.glyph(Glyph::empty(GlyphId::NOTDEF, 500))
.glyph(Glyph::empty(GlyphId::new(1), 500))
.glyph(Glyph::empty(GlyphId::new(2), 500))
.build()
.expect("font");
let mut cmap = font_model::Cmap::format4();
cmap.subtables_mut()[0] =
font_model::insert_entry(&cmap.subtables()[0], b'A' as u32, GlyphId::new(1));
cmap.subtables_mut()[0] =
font_model::insert_entry(&cmap.subtables()[0], '\u{05D0}' as u32, GlyphId::new(2));
*font.cmap_mut() = cmap;
let line = shape_line(&font, "A\u{05D0}", 100.0).expect("ok");
assert_eq!(line.paragraph_level(), 0);
let hebrew = line
.placements()
.iter()
.find(|p| p.glyph == GlyphId::new(2))
.expect("he");
assert_eq!(hebrew.level, 1);
assert!(hebrew.is_rtl());
}
#[test]
fn shape_line_kerns_in_logical_order() {
let mut font = blank_font(&[(b'A' as u32, 500), (b'B' as u32, 500)]);
font.kerning_mut()
.insert(GlyphId::new(1), GlyphId::new(2), -50);
let ltr = shape_line(&font, "AB", 100.0).expect("ok");
assert!(
close(ltr.get(1).unwrap().x, 45.0),
"{}",
ltr.get(1).unwrap().x
);
let rtl = shape_line_with(&font, "AB", 100.0, LayoutDirection::RightToLeft).expect("ok");
let a = rtl.placements().iter().find(|p| p.cluster == 0).expect("A");
assert!(close(a.x, 0.0), "A keeps its logical position");
}
#[test]
fn by_cluster_regroups_visual_placements() {
let font = blank_font(&[(b'A' as u32, 500), (b'B' as u32, 500)]);
let line = shape_line_with(&font, "AB", 100.0, LayoutDirection::RightToLeft).expect("ok");
assert_eq!(line.get(0).unwrap().cluster, 1);
assert_eq!(line.get(1).unwrap().cluster, 0);
let clusters = line.by_cluster();
assert_eq!(clusters.len(), 2);
assert_eq!(clusters[0].0, 0);
assert_eq!(clusters[1].0, 1);
assert_eq!(clusters[0].1[0].glyph, GlyphId::new(1));
}
#[test]
fn get_out_of_range_is_none() {
let font = blank_font(&[(b'A' as u32, 500)]);
let line = shape_line(&font, "A", 100.0).expect("ok");
assert!(line.get(0).is_some());
assert!(line.get(1).is_none());
assert!(line.get(9999).is_none());
assert_eq!(line.ink_extent_px(), None);
}
#[test]
fn empty_line_and_empty_text() {
let line = ShapedLine::empty();
assert!(line.is_empty());
assert_eq!(line.len(), 0);
assert_eq!(line.advance_px(), 0.0);
assert!(line.by_cluster().is_empty());
assert!(line.into_placements().is_empty());
}
#[test]
fn shape_line_with_bad_size_is_a_typed_error() {
let font = blank_font(&[(b'A' as u32, 500)]);
for bad in [0.0, -1.0, f32::NAN] {
assert!(
matches!(
shape_line_with(&font, "A", bad, LayoutDirection::Auto),
Err(ShapeError::InvalidScale { .. })
),
"{bad}"
);
}
}
#[test]
fn resolve_over_char_slice_matches_resolve_bidi() {
let text = "a\u{05D0}b5";
let chars: Vec<char> = text.chars().collect();
assert_eq!(
resolve_bidi(text),
resolve_bidi_chars(&chars),
"the slice form must agree with the string form"
);
let empty = resolve_bidi_chars(&[]);
assert!(empty.levels.is_empty());
assert_eq!(empty.paragraph_level, 0);
assert!(empty.visual_order().is_empty());
}
#[test]
fn describe_run_is_a_readable_summary() {
let font = blank_font(&[(b'A' as u32, 500)]);
let s = super::describe_run(&font, "AA", 100.0);
assert!(s.contains("100"), "{s}");
assert!(s.contains("AA"), "{s}");
let s = super::describe_run(&font, "A", 0.0);
assert!(!s.is_empty());
let _: String = s;
}
#[test]
fn placement_origin_and_is_rtl() {
let p = super::GlyphPlacement {
glyph: GlyphId::new(1),
cluster: 0,
level: 1,
x: 10.0,
y: 0.0,
x_offset: -2.0,
advance_px: 5.0,
};
assert!(close(p.origin_px().0, 8.0));
assert!(p.is_rtl());
}
}