Typography: a formula picture is as wide as its formula (px, not pt)
This commit is contained in:
3 files changed
+31
-6
No files matched your search
@@ -191,7 +191,11 @@ struct Sheets {
|
||||
impl Render for Sheets {
|
||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> 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))
|
||||
|
||||
+11
-5
@@ -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<Pixels>),
|
||||
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<String, MathError> {
|
||||
let open = svg
|
||||
.find('>')
|
||||
.ok_or_else(|| MathError::said("RaTeX wrote no svg tag"))?;
|
||||
let close = svg
|
||||
.rfind("</svg>")
|
||||
.ok_or_else(|| MathError::said("RaTeX left the svg open"))?;
|
||||
let (w, h) = (right - left, bottom - top);
|
||||
let reframed = format!(
|
||||
Ok(format!(
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"{left} {top} {w} {h}\" width=\"{w}\" height=\"{h}\">{}</svg>",
|
||||
&svg[open + 1..close]
|
||||
);
|
||||
Ok((reframed, size(px(w), px(h))))
|
||||
))
|
||||
}
|
||||
|
||||
/// The asset key for `source` at `font` in `window`.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user