Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Tests: pcraft keeps text run baseline; tighten sub/superscript size check (#5645)

vangel76489feb2333d4
diff --git a/crates/format/tests/roundtrip.rs b/crates/format/tests/roundtrip.rs
index f78e93947..5a4fe87b2 100644
--- a/crates/format/tests/roundtrip.rs
+++ b/crates/format/tests/roundtrip.rs
@@ -213,6 +213,29 @@ fn exhausted_document_ids_and_maximal_text_runs_roundtrip() {
assert_eq!(text.paragraph_runs().iter().map(|r| r.len).sum::<usize>(), text.text.len());
}
+#[test]
+fn text_run_baseline_roundtrips() {
+ use photocraft_doc::text::FontBaseline;
+ let mut doc = Document::new("scripts", photocraft_doc::Size::new(2, 1), ColorMode::Rgb, SampleType::U8);
+ doc.layers.push(Layer::new(
+ "Text",
+ LayerContent::Text(TextLayer {
+ text: "H2O2".into(),
+ runs: vec![
+ TextRun { len: 1, style: CharStyle::default() },
+ TextRun { len: 1, style: CharStyle { baseline: FontBaseline::Subscript, ..Default::default() } },
+ TextRun { len: 1, style: CharStyle::default() },
+ TextRun { len: 1, style: CharStyle { baseline: FontBaseline::Superscript, ..Default::default() } },
+ ],
+ ..Default::default()
+ }),
+ ));
+ let loaded = load_from_bytes(&save_to_bytes(&doc, &SaveOptions::default()).unwrap()).unwrap();
+ let LayerContent::Text(text) = &loaded.layers.last().unwrap().content else { panic!("text layer must roundtrip") };
+ let baselines: Vec<FontBaseline> = text.char_runs().iter().map(|r| r.style.baseline).collect();
+ assert_eq!(baselines, [FontBaseline::Normal, FontBaseline::Subscript, FontBaseline::Normal, FontBaseline::Superscript]);
+}
+
#[test]
fn zip_incremental_only_new_tiles() {
let mut doc = rich_doc(ColorMode::Rgb, SampleType::U16);
diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs
index f00a8e886..4ae059be4 100644
--- a/crates/text/src/tests.rs
+++ b/crates/text/src/tests.rs
@@ -449,9 +449,11 @@ fn superscript_and_subscript_shrink_and_shift_the_run() {
let plain = rect(&mut e, FontBaseline::Normal);
let sup = rect(&mut e, FontBaseline::Superscript);
let sub = rect(&mut e, FontBaseline::Subscript);
- let ratio = |r: &photocraft_geom::Rect| r.height() as f32 / plain.height() as f32;
+ // Scripts render at 58.3% of the size. The pixel-snapped bounds of the antialiased glyph land
+ // within 2 px of the exact scaled height (measured: +2.2 / +1.2 px at 46 px plain height).
+ let expect = plain.height() as f32 * 0.583;
for r in [&sup, &sub] {
- assert!((ratio(r) - 0.583).abs() < 0.06, "plain {plain:?}, scripted {r:?}");
+ assert!((r.height() as f32 - expect).abs() <= 2.5, "plain {plain:?}, scripted {r:?}");
}
// The cap's foot sits on the shifted baseline: 20 px (33.3% of 60) above or below.
assert!((plain.y1 - sup.y1 - 20).abs() <= 2, "plain {plain:?}, superscript {sup:?}");