diff --git a/src/navigation/tests/tabs.rs b/src/navigation/tests/tabs.rs index dec8600..a7f220e 100644 --- a/src/navigation/tests/tabs.rs +++ b/src/navigation/tests/tabs.rs @@ -191,7 +191,11 @@ struct Sheets { impl Render for Sheets { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { let view = cx.entity(); - let tail = if self.long { "W".repeat(40) } else { String::new() }; + let tail = if self.long { + "W".repeat(40) + } else { + String::new() + }; let tabs = (0..30).map(|ix| Choice::new(format!("s{ix}"), format!("Sheet {ix}{tail}"))); div() .w(px(self.width)) diff --git a/src/typography/math.rs b/src/typography/math.rs index 3de1557..55239a7 100644 --- a/src/typography/math.rs +++ b/src/typography/math.rs @@ -166,7 +166,9 @@ fn laid(formula: &Formula) -> Result<(String, f32, f32), MathError> { (list.width * font, (list.height + list.depth) * font, svg) })?; let (width, height, svg) = typeset; - Ok((svg, width as f32, height as f32)) + let (width, height) = (width as f32, height as f32); + // RaTeX sizes its svg in pt; px keeps one unit. + Ok((wrapped(&svg, [0.0, 0.0, width, height])?, width, height)) } /// Runs RaTeX's layout; a panic becomes an error. @@ -187,18 +189,22 @@ fn framed(svg: &str, width: f32, height: f32) -> Result<(String, Size), let top = ink.top().min(0.0); let right = ink.right().max(width); let bottom = ink.bottom().max(height); + let (w, h) = (right - left, bottom - top); + Ok((wrapped(svg, [left, top, w, h])?, size(px(w), px(h)))) +} + +/// `svg`'s drawing under a root viewing `[left, top, w, h]` in px. +fn wrapped(svg: &str, [left, top, w, h]: [f32; 4]) -> Result { let open = svg .find('>') .ok_or_else(|| MathError::said("RaTeX wrote no svg tag"))?; let close = svg .rfind("") .ok_or_else(|| MathError::said("RaTeX left the svg open"))?; - let (w, h) = (right - left, bottom - top); - let reframed = format!( + Ok(format!( "{}", &svg[open + 1..close] - ); - Ok((reframed, size(px(w), px(h)))) + )) } /// The asset key for `source` at `font` in `window`. diff --git a/src/typography/math/tests.rs b/src/typography/math/tests.rs index 1371f85..dcef22d 100644 --- a/src/typography/math/tests.rs +++ b/src/typography/math/tests.rs @@ -256,3 +256,18 @@ fn drawn_fallback_text_stays_inside_the_frame() { ); } } + +#[test] +fn a_picture_spans_its_formula_not_a_point_rescale() { + let formula = super::Formula { + source: "x^2+y^2=r^2".into(), + display: false, + rgba: 0xff, + font: 1600, + scale: 100, + }; + let (_, width, _) = super::laid(&formula).expect("laid"); + let (_, shape) = super::svg(&formula).expect("framed"); + let ratio = shape.width.as_f32() / width; + assert!((0.95..1.05).contains(&ratio), "{} for {width}", shape.width); +}