#![cfg(target_os = "macos")]
use slate_text::{CoreTextBackend, Font, TEST_FONT, TextBackend};
#[test]
fn shapes_hello_world() {
let mut backend = CoreTextBackend::new().unwrap();
let font = backend
.load_font_from_bytes(TEST_FONT, 16.0, 2.0)
.expect("failed to load font");
let line = backend
.shape_line(&font, "Hello")
.expect("failed to shape text");
assert_eq!(line.glyphs.len(), 5, "expected 5 glyphs for 'Hello'");
for (i, glyph) in line.glyphs.iter().enumerate() {
assert!(glyph.glyph_id != 0, "glyph {} has zero ID", i);
}
assert!(line.width_lpx > 0.0, "width should be positive");
let mut last_pos = -f32::INFINITY;
for glyph in &line.glyphs {
assert!(
glyph.position_lpx[0] >= last_pos,
"glyphs should progress left-to-right (pos {} < last {})",
glyph.position_lpx[0],
last_pos,
);
last_pos = glyph.position_lpx[0];
}
}
#[test]
fn empty_string_returns_empty_line() {
let mut backend = CoreTextBackend::new().unwrap();
let font = backend
.load_font_from_bytes(TEST_FONT, 16.0, 1.0)
.expect("failed to load font");
let line = backend
.shape_line(&font, "")
.expect("failed to shape empty string");
assert!(line.glyphs.is_empty(), "empty string should have no glyphs");
assert_eq!(line.width_lpx, 0.0, "empty string should have zero width");
}
#[test]
fn missing_glyph_returns_notdef_or_zero() {
let mut backend = CoreTextBackend::new().unwrap();
let font = backend
.load_font_from_bytes(TEST_FONT, 16.0, 1.0)
.expect("failed to load font");
let line = backend
.shape_line(&font, "\u{E000}")
.expect("failed to shape PUA character");
assert_eq!(
line.glyphs.len(),
1,
"PUA character should produce one glyph"
);
}
#[test]
fn metrics_are_reasonable() {
let mut backend = CoreTextBackend::new().unwrap();
let font = backend
.load_font_from_bytes(TEST_FONT, 16.0, 1.0)
.expect("failed to load font");
let metrics = font.metrics();
assert!(metrics.ascent_lpx > 0.0, "ascent should be positive");
assert!(metrics.descent_lpx < 0.0, "descent should be negative");
let line_height = metrics.ascent_lpx - metrics.descent_lpx + metrics.line_gap_lpx;
assert!(
line_height > 10.0 && line_height < 100.0,
"line height should be reasonable: {}",
line_height
);
assert!(
metrics.units_per_em == 1000 || metrics.units_per_em == 2048,
"units_per_em should be 1000 or 2048, got {}",
metrics.units_per_em
);
}