mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-02 22:19:00 +00:00
Tests: text and doc tests check what the code returns, not only that it is there
Tests in vectorcraft-text and vectorcraft-doc that could pass on an empty result, or that only checked a result exists, now assert the values: - Blend: gradient steps assert their stop count before checking each stop's swatch, tint and plates; the odd dash blended to solid asserts the whole pattern; a copied step asserts its members. - Symbols: limiting depth keeps every symbol; a stroke override on type keeps the run it styles. - File fonts: an extreme descent clamps to the lowest table value and the Windows descent still fits. - Invert wrap on a tangled shape asserts the line still meets it. - Bidi, ligature carets, Thai and whitespace fallback: the glyph and line counts are asserted, Hebrew points stay in their letters' clusters, and every character of an isolate test has a glyph. - Glyph categories, trace views, compound shape modes and digit systems check written-out ids instead of a round trip over every value. Removed joins_on_a_circle_are_measured, which printed measurements and asserted nothing, and chord_placement_leaves_the_join_gap_above_the_baseline, which checked arithmetic done in the test itself; joined_glyphs_meet_on_a_curved_path covers joined type on a curve.
This commit is contained in:
+17
-3
@@ -2784,10 +2784,13 @@ mod tests {
|
||||
Paint::Gradient(Box::new(GradientPaint::new(Gradient { stops, ..Gradient::default() })))
|
||||
};
|
||||
let Paint::Gradient(m) = lerp_paint(&spot_gradient(0.2), &spot_gradient(1.0), 0.5) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
assert!(m.gradient.stops.iter().all(|s| s.swatch.as_deref() == Some("Spot") && (s.tint - 0.6).abs() < 1e-6), "{:?}", m.gradient.stops);
|
||||
let Paint::Gradient(m) = lerp_paint(&tinted(0.2), &spot_gradient(1.0), 0.5) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
assert!(m.gradient.stops.iter().all(|s| s.swatch.as_deref() == Some("Spot") && (s.tint - 0.6).abs() < 1e-6), "{:?}", m.gradient.stops);
|
||||
let Paint::Gradient(m) = lerp_paint(&Paint::solid(Color::BLACK), &spot_gradient(1.0), 0.5) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
assert!(m.gradient.stops.iter().all(|s| s.swatch.is_none()), "{:?}", m.gradient.stops);
|
||||
}
|
||||
|
||||
@@ -2840,6 +2843,7 @@ mod tests {
|
||||
let a = gradient_paint(vec![spot_stop(0.0, 0.2, Some(p)), spot_stop(1.0, 0.2, Some(p))]);
|
||||
let b = gradient_paint(vec![spot_stop(0.0, 1.0, Some(q)), spot_stop(1.0, 1.0, Some(q))]);
|
||||
let Paint::Gradient(m) = lerp_paint(&a, &b, 0.25) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
for s in &m.gradient.stops {
|
||||
assert_eq!(s.swatch.as_deref(), Some("Spot"));
|
||||
assert!((s.tint - 0.4).abs() < 1e-6, "{}", s.tint);
|
||||
@@ -2848,6 +2852,7 @@ mod tests {
|
||||
// Without plates on either side the steps print the spot ink alone, as before.
|
||||
let bare = |tint| gradient_paint(vec![spot_stop(0.0, tint, None), spot_stop(1.0, tint, None)]);
|
||||
let Paint::Gradient(m) = lerp_paint(&bare(0.2), &bare(1.0), 0.5) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
assert!(m.gradient.stops.iter().all(|s| s.plates.is_none()), "{:?}", m.gradient.stops);
|
||||
}
|
||||
|
||||
@@ -2882,10 +2887,12 @@ mod tests {
|
||||
let with = gradient_paint(vec![spot_stop(0.0, 1.0, Some(p)), spot_stop(1.0, 1.0, Some(p))]);
|
||||
let without = gradient_paint(vec![spot_stop(0.0, 1.0, None), spot_stop(1.0, 1.0, None)]);
|
||||
let Paint::Gradient(m) = lerp_paint(&without, &with, 0.25) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
assert!(m.gradient.stops.iter().all(|s| s.swatch.as_deref() == Some("Spot")), "{:?}", m.gradient.stops);
|
||||
m.gradient.stops.iter().for_each(|s| assert_plates(s.plates, [0.1, 0.2, 0.0, 0.05]));
|
||||
// The ends keep their own plates.
|
||||
let Paint::Gradient(m) = lerp_paint(&without, &with, 0.0) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
assert!(m.gradient.stops.iter().all(|s| s.plates.is_none()), "{:?}", m.gradient.stops);
|
||||
|
||||
// Another swatch, or an unlinked stop: process steps with no plates, as before.
|
||||
@@ -2897,6 +2904,7 @@ mod tests {
|
||||
let plain = gradient_paint(vec![plain.clone(), GradientStop { offset: 1.0, ..plain }]);
|
||||
for b in [&other, &plain] {
|
||||
let Paint::Gradient(m) = lerp_paint(&with, b, 0.5) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
assert!(m.gradient.stops.iter().all(|s| s.swatch.is_none() && s.plates.is_none()), "{:?}", m.gradient.stops);
|
||||
}
|
||||
}
|
||||
@@ -2906,11 +2914,14 @@ mod tests {
|
||||
let p = [0.4, 0.8, 0.0, 0.2];
|
||||
let gradient = gradient_paint(vec![spot_stop(0.0, 1.0, Some(p)), spot_stop(1.0, 1.0, Some(p))]);
|
||||
let Paint::Gradient(g) = &gradient else { panic!() };
|
||||
assert!(solid_gradient(Color::BLACK, Some("Spot"), 0.2, &g.gradient).stops.iter().all(|s| s.plates.is_none()));
|
||||
let linked = solid_gradient(Color::BLACK, Some("Spot"), 0.2, &g.gradient);
|
||||
assert_eq!(linked.stops.len(), 2);
|
||||
assert!(linked.stops.iter().all(|s| s.plates.is_none() && s.swatch.as_deref() == Some("Spot") && s.tint == 0.2), "{:?}", linked.stops);
|
||||
let solid = Paint::Solid { color: Color::cmyk(1.0, 0.7, 0.0, 0.4).tinted(0.2), swatch: Some("Spot".into()), tint: 0.2 };
|
||||
// The solid's end has no plates: they grow from none toward the gradient's, both ways round.
|
||||
for (a, b, t) in [(&solid, &gradient, 0.25), (&gradient, &solid, 0.75)] {
|
||||
let Paint::Gradient(m) = lerp_paint(a, b, t) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
for s in &m.gradient.stops {
|
||||
assert_eq!(s.swatch.as_deref(), Some("Spot"));
|
||||
assert!((s.tint - 0.4).abs() < 1e-6, "{}", s.tint);
|
||||
@@ -2919,6 +2930,7 @@ mod tests {
|
||||
}
|
||||
// A process solid unlinks the steps, and they take no plates.
|
||||
let Paint::Gradient(m) = lerp_paint(&Paint::solid(Color::BLACK), &gradient, 0.5) else { panic!() };
|
||||
assert_eq!(m.gradient.stops.len(), 2);
|
||||
assert!(m.gradient.stops.iter().all(|s| s.swatch.is_none() && s.plates.is_none()), "{:?}", m.gradient.stops);
|
||||
}
|
||||
|
||||
@@ -3401,7 +3413,8 @@ mod tests {
|
||||
#[test]
|
||||
fn odd_dash_blends_to_solid_with_closed_gaps() {
|
||||
let d = lerp_dash(Some(&Dash { pattern: vec![2.0, 3.0, 4.0], ..Default::default() }), None, 1.0).unwrap();
|
||||
assert!(d.pattern.iter().skip(1).step_by(2).all(|g| *g == 0.0), "{:?}", d.pattern);
|
||||
// Doubled to dash, gap, dash, gap, dash, gap; each dash takes the gap after it.
|
||||
assert_eq!(d.pattern, [5.0, 0.0, 6.0, 0.0, 7.0, 0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -3412,7 +3425,8 @@ mod tests {
|
||||
let mid = lerp_node(&a, &b, 0.4);
|
||||
let mut ids = vec![];
|
||||
mid.walk(&mut |n| ids.push(n.id));
|
||||
assert!(ids.iter().all(|i| *i == NodeId(0)), "{ids:?}");
|
||||
// The step and its two members, all of them new.
|
||||
assert_eq!(ids, [NodeId(0); 3]);
|
||||
}
|
||||
|
||||
/// A blend of `a` and `b` spaced by `spacing`.
|
||||
|
||||
@@ -64,11 +64,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn modes_read_back_and_unknown_ones_are_add() {
|
||||
for m in ShapeMode::ALL {
|
||||
let v = serde_json::to_value(m).unwrap();
|
||||
assert_eq!(v, serde_json::json!(m.name()));
|
||||
assert_eq!(serde_json::from_value::<ShapeMode>(v).unwrap(), m);
|
||||
}
|
||||
assert_eq!(serde_json::to_value(ShapeMode::Subtract).unwrap(), serde_json::json!("subtract"));
|
||||
assert_eq!(serde_json::from_value::<ShapeMode>(serde_json::json!("exclude")).unwrap(), ShapeMode::Exclude);
|
||||
assert_eq!(ShapeMode::parse("intersect"), Some(ShapeMode::Intersect));
|
||||
assert_eq!(ShapeMode::parse("Intersect"), None);
|
||||
for v in [serde_json::json!("divide"), serde_json::json!(3), serde_json::json!(null), serde_json::json!({"a": [1]})] {
|
||||
assert_eq!(serde_json::from_value::<ShapeMode>(v).unwrap(), ShapeMode::Add);
|
||||
}
|
||||
|
||||
@@ -613,6 +613,7 @@ mod tests {
|
||||
d.limit_depth_noting(&mut warnings);
|
||||
assert_eq!(warnings, [symbols_note()]);
|
||||
assert!(!d.too_deep(None), "{count} {last}");
|
||||
assert_eq!(d.symbols.len(), count, "every symbol is kept");
|
||||
assert!(d.symbols.iter().all(|s| s.art.children().is_some()), "the art is still a group");
|
||||
}
|
||||
// An edit that makes one hold itself is seen, whatever else stayed as it was.
|
||||
|
||||
@@ -253,6 +253,7 @@ mod tests {
|
||||
let mut a = Node::group(NodeId(1), vec![Arc::new(text)]);
|
||||
apply(&mut a, &[SymbolOverride { path: vec![0], stroke: Some(red.clone()), ..Default::default() }]);
|
||||
let Some(NodeKind::Text(t)) = part(&a, &[0]).map(|n| &n.kind) else { panic!("type") };
|
||||
assert_eq!(t.runs.len(), 1);
|
||||
assert!(t.runs.iter().all(|r| r.style.stroke == red && r.style.stroke_width == 1.0), "the stroke draws at 1 pt");
|
||||
|
||||
// A path whose stroke has neither paint nor weight gets 1 pt too.
|
||||
|
||||
@@ -217,11 +217,10 @@ fn indic_digits_map_to_their_scripts_block() {
|
||||
assert_eq!(map(d, "a-Z ٪"), "a-Z ٪");
|
||||
}
|
||||
assert_eq!(map(Digits::Devanagari, "page 12"), "page १२");
|
||||
for d in Digits::ALL {
|
||||
assert_eq!(serde_json::to_value(d).unwrap(), d.id());
|
||||
assert_eq!(serde_json::from_value::<Digits>(d.id().into()).unwrap(), d);
|
||||
assert_eq!(Digits::from_id(d.id()), Some(d));
|
||||
}
|
||||
assert_eq!(serde_json::to_value(Digits::Devanagari).unwrap(), "devanagari");
|
||||
assert_eq!(serde_json::from_value::<Digits>("farsi".into()).unwrap(), Digits::Farsi);
|
||||
assert_eq!(Digits::Malayalam.id(), "malayalam");
|
||||
assert_eq!(Digits::from_id("default"), Some(Digits::Default));
|
||||
assert_eq!(Digits::from_id("roman"), None);
|
||||
assert!(serde_json::from_value::<Digits>("roman".into()).is_err());
|
||||
}
|
||||
|
||||
@@ -98,11 +98,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn ids_round_trip_and_unknown_views_show_the_result() {
|
||||
for v in TraceView::ALL {
|
||||
assert_eq!(TraceView::from_id(v.id()), Some(v));
|
||||
assert_eq!(TraceView::from_id(&v.id().to_ascii_uppercase()), Some(v));
|
||||
assert!(v.shows_result() || v.shows_outlines() || v.shows_source(), "{v:?} shows something");
|
||||
}
|
||||
assert_eq!(TraceView::Source.id(), "sourceImage");
|
||||
assert_eq!(TraceView::from_id("outlinesWithSourceImage"), Some(TraceView::OutlinesWithSource));
|
||||
assert_eq!(TraceView::from_id("TRACINGRESULT"), Some(TraceView::Result), "case does not matter");
|
||||
assert_eq!(TraceView::from_id("tracing"), None);
|
||||
let mut g = Node::group(crate::NodeId(1), vec![]);
|
||||
assert_eq!(g.trace_view(), TraceView::Result);
|
||||
g.extras_mut().trace = Some(Box::new(serde_json::json!({"view": "outlines"})));
|
||||
|
||||
@@ -456,7 +456,14 @@ mod tests {
|
||||
fn extreme_negative_descent_does_not_overflow() {
|
||||
let mut i = info();
|
||||
i.descent = -100_000.0;
|
||||
let bytes = build_file_font(&i, &glyphs("a"));
|
||||
assert!(bytes.is_some());
|
||||
let bytes = build_file_font(&i, &glyphs("a")).unwrap();
|
||||
// The descent stops at the lowest value the tables hold, and the Windows descent, its
|
||||
// magnitude, still fits.
|
||||
let font = skrifa::FontRef::new(&bytes).unwrap();
|
||||
let metrics = font.metrics(skrifa::instance::Size::unscaled(), skrifa::instance::LocationRef::default());
|
||||
assert_eq!(metrics.descent, f32::from(i16::MIN));
|
||||
assert_eq!(metrics.ascent, 1760.0, "the ascent is the default 0.88 em");
|
||||
use skrifa::raw::TableProvider;
|
||||
assert_eq!(font.os2().unwrap().us_win_descent(), 32_768);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -687,9 +687,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn kinds_round_trip_through_their_ids() {
|
||||
for kind in GlyphCategoryKind::all() {
|
||||
assert_eq!(GlyphCategoryKind::from_id(&kind.id()), Some(kind));
|
||||
}
|
||||
assert_eq!(GlyphCategoryKind::StylisticSet(3).id(), "ss03");
|
||||
assert_eq!(GlyphCategoryKind::from_id("ss03"), Some(GlyphCategoryKind::StylisticSet(3)));
|
||||
assert_eq!(GlyphCategoryKind::from_id("ss3"), None);
|
||||
assert_eq!(GlyphCategoryKind::from_id("stylisticAlternates"), Some(GlyphCategoryKind::StylisticAlternates));
|
||||
assert_eq!(GlyphCategoryKind::from_id("figures"), Some(GlyphCategoryKind::Figures));
|
||||
assert_eq!(GlyphCategoryKind::StylisticSet(7).features(), [*b"ss07"]);
|
||||
assert_eq!(GlyphCategoryKind::StylisticSet(20).features(), [*b"ss20"]);
|
||||
assert_eq!(GlyphCategoryKind::from_id("ss21"), None);
|
||||
|
||||
@@ -1696,6 +1696,7 @@ fn invert_wrap_with_an_offset_bounds_its_work_on_a_tangled_shape() {
|
||||
assert!(GROWN_BAND_WORK.get() <= 2 * MAX_INVERT_WRAP_WORK, "{} steps", GROWN_BAND_WORK.get());
|
||||
let contour: Vec<(f64, f64)> = star.iter().map(|p| (p.x, p.y)).collect();
|
||||
let spans = invert_wrap_spans(&contour, 1.0, 10.0, 90.0);
|
||||
assert!(!spans.is_empty(), "the line still meets the shape");
|
||||
assert!(spans.iter().all(|&(a, b)| a >= 4.0 && b <= 96.0), "{spans:?}");
|
||||
let square = [(40.0, 40.0), (60.0, 40.0), (60.0, 60.0), (40.0, 60.0)];
|
||||
assert_spans(&invert_wrap_spans(&square, f64::NAN, 45.0, 55.0), &invert_wrap_spans(&square, 0.0, 45.0, 55.0));
|
||||
|
||||
@@ -1028,6 +1028,7 @@ fn whitespace_the_primary_font_lacks_takes_the_fallback_face() {
|
||||
assert!((width - 60.0).abs() < 0.01, "あ あ sets at 3 em: {:?}", l.glyphs.iter().map(|g| g.advance).collect::<Vec<_>>());
|
||||
assert_ne!(l.glyphs[1].font_id, primary.id());
|
||||
let l = layout(db(), &point("a\tb", style(20.0)));
|
||||
assert_eq!(l.glyphs.len(), 3, "{:?}", l.glyphs);
|
||||
assert!(l.glyphs.iter().all(|g| g.font_id == primary.id()));
|
||||
}
|
||||
|
||||
@@ -2649,6 +2650,8 @@ fn thai_loanwords_stay_whole() {
|
||||
let lines: Vec<&str> = l.lines.iter().map(|ln| &text[ln.start..ln.end]).collect();
|
||||
// No line ends inside the word, whatever the Thai font's widths put beside it.
|
||||
let word = text.find("โปรแกรม").unwrap();
|
||||
assert!(lines.len() > 1, "70 pt wraps the sentence: {lines:?}");
|
||||
assert!(lines.iter().any(|ln| ln.contains("โปรแกรม")), "one line holds the word: {lines:?}");
|
||||
assert!(l.lines.iter().all(|ln| ln.end <= word || ln.end >= word + "โปรแกรม".len()), "{lines:?}");
|
||||
}
|
||||
|
||||
|
||||
@@ -254,6 +254,9 @@ fn paragraph_direction_is_independent_and_neutrals_do_not_force_ltr() {
|
||||
fn hebrew_marks_stay_with_base_clusters() {
|
||||
let s = "שָׁלוֹם";
|
||||
let l = text_layout(s);
|
||||
// Each glyph's source starts at one of the four letters, never at a point after one.
|
||||
let starts: std::collections::BTreeSet<usize> = l.glyphs.iter().map(|g| g.byte).collect();
|
||||
assert_eq!(starts, [0, 6, 8, 12].into(), "{:?}", l.glyphs.iter().map(|g| (g.byte, g.len)).collect::<Vec<_>>());
|
||||
for g in &l.glyphs {
|
||||
assert!(s.get(g.byte..g.byte + g.len).is_some());
|
||||
}
|
||||
@@ -413,6 +416,10 @@ fn bidi_isolates_numbers_and_brackets_keep_valid_clusters() {
|
||||
for g in &l.glyphs {
|
||||
assert!(s.get(g.byte..g.byte + g.len).is_some());
|
||||
}
|
||||
// Every character but the isolate marks is drawn by a glyph whose source holds it.
|
||||
for (i, c) in s.char_indices().filter(|&(_, c)| !matches!(c, '\u{2067}' | '\u{2069}')) {
|
||||
assert!(l.glyphs.iter().any(|g| (g.byte..g.byte + g.len).contains(&i)), "{s}: {c:?} at {i} has no glyph");
|
||||
}
|
||||
assert!(!selection_quads(&l, 0, s.len()).is_empty());
|
||||
}
|
||||
}
|
||||
@@ -432,6 +439,7 @@ fn right_aligned_wrapped_rtl_keeps_trailing_spaces_outside_content() {
|
||||
#[test]
|
||||
fn visual_arrows_cross_rtl_paragraph_boundaries() {
|
||||
let l = text_layout("שלום\nעולם");
|
||||
assert_eq!(l.lines.len(), 2);
|
||||
assert!(l.lines.iter().all(|l| l.rtl));
|
||||
assert_eq!(caret_horizontal(&l, 8, false), 9);
|
||||
assert_eq!(caret_horizontal(&l, 9, true), 8);
|
||||
@@ -492,6 +500,7 @@ fn paragraph_direction_overrides_the_first_strong_character() {
|
||||
#[test]
|
||||
fn left_to_right_text_is_untouched() {
|
||||
let l = text_layout("Plain text, 123 (and more).");
|
||||
assert_eq!((l.glyphs.len(), l.lines.len()), ("Plain text, 123 (and more).".len(), 1));
|
||||
assert!(l.glyphs.iter().all(|g| !g.rtl) && l.lines.iter().all(|l| !l.rtl));
|
||||
let bytes: Vec<_> = l.glyphs.iter().map(|g| g.byte).collect();
|
||||
assert!(bytes.windows(2).all(|w| w[0] < w[1]));
|
||||
|
||||
@@ -529,7 +529,8 @@ fn manual_kerning_moves_a_right_to_left_ligature_stop_with_the_glyph() {
|
||||
fn a_letter_and_its_mark_has_no_inner_stop() {
|
||||
let db = FontDb::with_font_dirs(vec![]);
|
||||
let l = layout(&db, &point("e\u{0301}", "Source Sans 3"));
|
||||
assert!(l.glyphs.iter().all(|g| g.carets.is_empty()), "{:?}", l.glyphs);
|
||||
assert!(!l.glyphs.is_empty());
|
||||
assert!(l.glyphs.iter().all(|g| (g.byte, g.len) == (0, 3) && g.carets.is_empty()), "{:?}", l.glyphs);
|
||||
}
|
||||
|
||||
/// On a path, a click on either half of the ligature goes to its nearer stop, and the selection of
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//! stroke, is the measure. Fonts are installed ones (none is kept in the repository): the tests
|
||||
//! that need an Arabic font say so and stop without one.
|
||||
use crate::*;
|
||||
use kurbo::{Affine, BezPath, Point, Shape};
|
||||
use kurbo::{BezPath, Point, Shape};
|
||||
use std::sync::Arc;
|
||||
use vectorcraft_doc::{CharStyle, TextKind};
|
||||
|
||||
@@ -20,90 +20,6 @@ fn circle(r: f64) -> vectorcraft_geom::PathData {
|
||||
vectorcraft_geom::PathData::from_bezpath(&kurbo::Circle::new((0.0, 0.0), r).to_path(1e-3))
|
||||
}
|
||||
|
||||
/// Type `s` in `style` on a circle of radius `r`, the whole loop for it.
|
||||
fn on_circle(s: &str, style: CharStyle, r: f64) -> TextLayout {
|
||||
let mut t = TextObject::point(Point::ZERO, s, style);
|
||||
t.kind = TextKind::OnPath { path: circle(r), start: 0.25, end: None };
|
||||
layout(FontDb::global(), &t)
|
||||
}
|
||||
|
||||
/// For each pair of neighbouring glyphs on the line (in visual order, inside a word), how far
|
||||
/// apart the right edge of the left one and the left edge of the right one are, at the baseline
|
||||
/// and `h` above it.
|
||||
fn gaps(l: &TextLayout, text: &str, h: f64) -> Vec<(f64, f64)> {
|
||||
// Visual order: the glyphs as placed, sorted along the line by where their left edge is on it.
|
||||
let edges: Vec<(Point, Point, Point, Point, usize)> = l
|
||||
.glyphs
|
||||
.iter()
|
||||
.filter(|g| text.get(g.byte..g.byte + g.len).is_some_and(|c| !c.trim().is_empty()))
|
||||
.map(|g| {
|
||||
let [a, b, ..] = g.xf.as_coeffs();
|
||||
let sx = a.hypot(b);
|
||||
let w = g.advance / sx;
|
||||
let up = h / sx;
|
||||
(g.xf * Point::new(0.0, 0.0), g.xf * Point::new(w, 0.0), g.xf * Point::new(0.0, -up), g.xf * Point::new(w, -up), g.byte)
|
||||
})
|
||||
.collect();
|
||||
// Which word a byte is in: the spaces before it.
|
||||
let word = |b: usize| text.get(..b).map_or(0, |t| t.chars().filter(|c| c.is_whitespace()).count());
|
||||
edges.windows(2).filter(|w| word(w[0].4) == word(w[1].4)).map(|w| ((w[0].1 - w[1].0).hypot(), (w[0].3 - w[1].2).hypot())).collect()
|
||||
}
|
||||
|
||||
/// The largest gaps (baseline, joining height) of type `s` on a circle of radius `r` at `size`.
|
||||
fn worst(face: &FontFace, s: &str, size: f64, r: f64) -> (f64, f64) {
|
||||
let style = CharStyle { font_family: face.family.clone(), font_style: face.style.clone(), size, ..CharStyle::default() };
|
||||
let l = on_circle(s, style, r);
|
||||
gaps(&l, s, size * 0.25).into_iter().fold((0.0, 0.0), |(a, b), (x, y)| (f64::max(a, x), f64::max(b, y)))
|
||||
}
|
||||
|
||||
/// The measure, printed: joined Arabic and separate Latin on circles at three sizes.
|
||||
#[test]
|
||||
fn joins_on_a_circle_are_measured() {
|
||||
let Some(face) = arabic_face() else {
|
||||
eprintln!("SKIPPED: joins_on_a_circle_are_measured; no installed font covers Arabic");
|
||||
return;
|
||||
};
|
||||
eprintln!("ASSERTIONS RUN: joins_on_a_circle_are_measured; Arabic font {} {}", face.family, face.style);
|
||||
for size in [12.0, 29.0, 72.0] {
|
||||
for r in [60.0, 110.0, 300.0] {
|
||||
let (b, j) = worst(&face, "سلسلة متصلة", size, r);
|
||||
let (lb, lj) = worst(&face, "minimum", size, r);
|
||||
eprintln!("size {size:>4} r {r:>5}: arabic baseline {b:.4} join {j:.4} | latin baseline {lb:.4} at {lj:.4}");
|
||||
}
|
||||
}
|
||||
let _ = BezPath::new();
|
||||
let _ = Affine::IDENTITY;
|
||||
}
|
||||
|
||||
/// Chord placement, worked out here only (not in the layout): each glyph's baseline ends on the
|
||||
/// circle, the glyph turned to the chord between them. Neighbours then meet at the baseline, and `h`
|
||||
/// above it they stand apart by `h` times the angle between their chords, (a_i + a_{i+1}) / 2R.
|
||||
#[test]
|
||||
fn chord_placement_leaves_the_join_gap_above_the_baseline() {
|
||||
let Some(face) = arabic_face() else {
|
||||
eprintln!("SKIPPED: chord_placement_leaves_the_join_gap_above_the_baseline; no installed font covers Arabic");
|
||||
return;
|
||||
};
|
||||
let (size, r) = (29.0, 110.0);
|
||||
let style = CharStyle { font_family: face.family.clone(), font_style: face.style.clone(), size, ..CharStyle::default() };
|
||||
let s = "سلسلة";
|
||||
let l = on_circle(s, style, r);
|
||||
let h = size * 0.25;
|
||||
let advances: Vec<f64> = l.glyphs.iter().map(|g| g.advance).filter(|a| *a > 1e-6).collect();
|
||||
let mut worst: f64 = 0.0;
|
||||
for w in advances.windows(2) {
|
||||
let (a, b) = (w[0], w[1]);
|
||||
// The chords' directions differ by the angle between their middles on the circle.
|
||||
let turn = (a + b) / (2.0 * r);
|
||||
let gap = 2.0 * h * (turn * 0.5).sin();
|
||||
let predicted = h * (a + b) / (2.0 * r);
|
||||
assert!((gap - predicted).abs() < 0.01, "{gap} vs {predicted}");
|
||||
worst = worst.max(gap);
|
||||
}
|
||||
eprintln!("chord placement at {size} pt on radius {r}: baseline gap 0, join-height gap {worst:.3}");
|
||||
assert!(worst > 0.5, "chord placement alone leaves a visible gap: {worst}");
|
||||
}
|
||||
|
||||
/// A circle of radius `r` run the other way (clockwise): type on it stands on the inside, its
|
||||
/// tops towards the centre, as Flip puts it.
|
||||
fn circle_reversed(r: f64) -> vectorcraft_geom::PathData {
|
||||
|
||||
@@ -1151,6 +1151,7 @@ fn thai_falls_back_to_a_font_made_for_thai() {
|
||||
let l = layout(db, &TextObject::point(kurbo::Point::ZERO, "Thai ภาษาไทย", st));
|
||||
let family = |g: &PositionedGlyph| db.face_by_id(g.font_id).map(|f| f.family.clone()).unwrap_or_default();
|
||||
let (latin, thai_glyphs) = l.glyphs.split_at(5);
|
||||
assert_eq!(thai_glyphs.len(), "ภาษาไทย".chars().count(), "a glyph for each Thai letter");
|
||||
assert!(latin.iter().all(|g| family(g) == FALLBACK_FAMILY), "{:?}", latin.iter().map(family).collect::<Vec<_>>());
|
||||
let first = crate::fontdb::THAI_FAMILIES.iter().find(|f| thai_glyphs.iter().all(|g| family(g).eq_ignore_ascii_case(f)));
|
||||
assert!(first.is_some(), "Thai drawn in {:?}; {thai} is installed", thai_glyphs.iter().map(family).collect::<Vec<_>>());
|
||||
|
||||
Reference in New Issue
Block a user