use super::layout::{LayoutBox, LayoutKind};
#[derive(Clone, Debug, serde::Serialize)]
pub struct NegationGeometry {
pub start: (f64, f64),
pub end: (f64, f64),
pub thickness: f64,
}
pub(super) fn math_origin_shift(fs: f64, dpi: f64) -> f64 {
(fs * 600.0 / dpi * 0.05).round() * dpi / 600.0
}
fn ordinary_glyph(body: &LayoutBox) -> bool {
match &body.kind {
LayoutKind::Text(text) | LayoutKind::Number(text) => {
text.chars().count() == 1 && !super::text_has_cjk(text)
}
LayoutKind::FontGlyph { text, .. } => {
matches!(text.as_str(), "{" | "}")
|| (text.chars().count() == 1
&& (text.chars().all(|ch| ch.is_ascii_alphabetic())
|| super::layout::is_styled_variable(text)))
}
LayoutKind::Color { body, .. } | LayoutKind::FontStyle { body, .. } => ordinary_glyph(body),
LayoutKind::Row(children) if children.len() == 1 => ordinary_glyph(&children[0]),
_ => false,
}
}
pub(super) fn source_geometry(body: &LayoutBox, dpi: f64) -> NegationGeometry {
let ordinary = ordinary_glyph(body);
let equality = matches!(&body.kind,
LayoutKind::Symbol(text) | LayoutKind::FontGlyph { text, .. } if text == "=");
if !ordinary && !equality {
return geometry(body);
}
let fs = body.font_size;
let natural_advance = (fs * 600.0 / dpi * 0.5).round();
if natural_advance <= 0.0 {
return geometry(body);
}
let width_scale = if ordinary { 1.2 } else { 1.0 };
let horizontal_scale = (body.width * 600.0 / dpi * width_scale).round() / natural_advance;
let dx = 396.0_f64;
let dy = 1029.0_f64;
let length = dx.hypot(dy);
let half_stroke = 22.0;
let start = (
57.0 + half_stroke * dy / length,
206.0 - half_stroke * dx / length,
);
let end = (
453.0 - half_stroke * dy / length,
-823.0 + half_stroke * dx / length,
);
let base_dx = end.0 - start.0;
let base_dy = end.1 - start.1;
let origin_y = body.y + body.baseline - math_origin_shift(fs, dpi);
NegationGeometry {
start: (
body.x + fs * horizontal_scale * start.0 / 1024.0,
origin_y + fs * start.1 / 1024.0,
),
end: (
body.x + fs * horizontal_scale * end.0 / 1024.0,
origin_y + fs * end.1 / 1024.0,
),
thickness: fs * 44.0 / 1024.0 * horizontal_scale * base_dx.hypot(base_dy)
/ (base_dx * horizontal_scale).hypot(base_dy),
}
}
pub(crate) fn geometry(body: &LayoutBox) -> NegationGeometry {
let horizontal_scale: f64 = if angular_relation(body) { 2.0 } else { 1.5 };
let fs = body.font_size;
let base_length = 0.36_f64.hypot(0.97);
let stretched_length = (0.36 * horizontal_scale).hypot(0.97);
NegationGeometry {
start: (
body.x + fs * horizontal_scale * 0.07,
body.y + body.baseline + fs * 0.18,
),
end: (
body.x + fs * horizontal_scale * 0.43,
body.y + body.baseline - fs * 0.79,
),
thickness: fs * 0.044 * horizontal_scale * base_length / stretched_length,
}
}
fn angular_relation(body: &LayoutBox) -> bool {
match &body.kind {
LayoutKind::Symbol(text) | LayoutKind::MathSymbol(text) => {
matches!(text.as_str(), "<" | ">" | "≤" | "≥")
}
LayoutKind::Color { body, .. } | LayoutKind::FontStyle { body, .. } => {
angular_relation(body)
}
LayoutKind::Row(children) if children.len() == 1 => angular_relation(&children[0]),
_ => false,
}
}