use super::super::Rgb;
use std::fmt::Write as _;
const FONT_FAMILY: &str = "'Noto Sans CJK JP', 'Hiragino Sans', 'Yu Gothic', Meiryo, sans-serif";
const SURFACE: &str = "#ffffff";
const NARROW_CHAR_RATIO: f32 = 0.58;
const WIDE_CHAR_RATIO: f32 = 1.0;
const ELLIPSIS: char = '…';
#[derive(Debug, Clone, Copy)]
pub(crate) struct TextStyle {
pub(super) size: f32,
pub(super) fill: &'static str,
pub(super) anchor: &'static str,
pub(super) bold: bool,
pub(super) rotate: f32,
}
pub(super) struct Svg {
out: String,
}
impl Svg {
pub(super) fn new(width: f32, height: f32) -> Self {
let (width, height) = (num(width), num(height));
let mut out = String::new();
let _ = write!(
out,
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{width}pt" height="{height}pt" viewBox="0 0 {width} {height}" font-family="{FONT_FAMILY}"><rect class="surface" x="0" y="0" width="{width}" height="{height}" fill="{SURFACE}"/>"#
);
Self { out }
}
pub(super) fn rect(&mut self, class: &str, x: f32, y: f32, width: f32, height: f32, attrs: &str) {
let _ = write!(
self.out,
r#"<rect class="{class}" x="{}" y="{}" width="{}" height="{}" {attrs}/>"#,
num(x),
num(y),
num(width.max(0.0)),
num(height.max(0.0))
);
}
pub(super) fn line(&mut self, class: &str, from: (f32, f32), to: (f32, f32), stroke: &str, width: f32) {
let _ = write!(
self.out,
r#"<line class="{class}" x1="{}" y1="{}" x2="{}" y2="{}" stroke="{stroke}" stroke-width="{}"/>"#,
num(from.0),
num(from.1),
num(to.0),
num(to.1),
num(width)
);
}
pub(super) fn path(&mut self, class: &str, data: &str, attrs: &str) {
if data.is_empty() {
return;
}
let _ = write!(self.out, r#"<path class="{class}" d="{data}" {attrs}/>"#);
}
pub(super) fn marker(&mut self, at: (f32, f32), radius: f32, color: Rgb) {
let _ = write!(
self.out,
r#"<circle class="marker" cx="{}" cy="{}" r="{}" fill="{}" stroke="{SURFACE}" stroke-width="1"/>"#,
num(at.0),
num(at.1),
num(radius),
hex(color)
);
}
pub(super) fn text(&mut self, class: &str, at: (f32, f32), style: TextStyle, content: &str) {
let (x, y) = (num(at.0), num(at.1));
let _ = write!(
self.out,
r#"<text class="{class}" x="{x}" y="{y}" font-size="{}" fill="{}" text-anchor="{}""#,
num(style.size),
style.fill,
style.anchor
);
if style.bold {
self.out.push_str(r#" font-weight="bold""#);
}
if style.rotate != 0.0 {
let _ = write!(self.out, r#" transform="rotate({} {x} {y})""#, num(style.rotate));
}
let _ = write!(self.out, ">{}</text>", escape(content));
}
pub(super) fn finish(mut self) -> String {
self.out.push_str("</svg>");
self.out
}
}
#[derive(Default)]
pub(super) struct PathData {
data: String,
broken: bool,
}
impl PathData {
pub(super) fn new() -> Self {
Self {
data: String::new(),
broken: true,
}
}
pub(super) fn point(&mut self, at: (f32, f32)) {
let command = if self.broken { "M" } else { "L" };
if !self.data.is_empty() {
self.data.push(' ');
}
let _ = write!(self.data, "{command} {} {}", num(at.0), num(at.1));
self.broken = false;
}
pub(super) fn gap(&mut self) {
self.broken = true;
}
pub(super) fn close(&mut self) {
if !self.data.is_empty() {
self.data.push_str(" Z");
}
self.broken = true;
}
pub(super) fn into_string(self) -> String {
self.data
}
}
pub(super) fn num(value: f32) -> String {
let value = if value.is_finite() { value } else { 0.0 };
let text = format!("{value:.2}");
let text = text.trim_end_matches('0').trim_end_matches('.');
match text {
"-0" | "" => "0".to_string(),
other => other.to_string(),
}
}
pub(super) fn hex(color: Rgb) -> String {
format!("#{:02x}{:02x}{:02x}", color[0], color[1], color[2])
}
pub(super) fn escape(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for ch in text.chars() {
match ch {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
'\n' | '\r' | '\t' => out.push(' '),
'\u{FFFE}' | '\u{FFFF}' => {}
ch if ch.is_control() => {}
ch => out.push(ch),
}
}
out
}
pub(super) fn text_width(text: &str, size: f32) -> f32 {
text.chars().map(|ch| char_width(ch, size)).sum()
}
pub(super) fn fit(text: &str, size: f32, max_width: f32) -> String {
if text_width(text, size) <= max_width {
return text.to_string();
}
let room = max_width - char_width(ELLIPSIS, size);
let mut width = 0.0;
let mut out = String::new();
for ch in text.chars() {
width += char_width(ch, size);
if width > room {
break;
}
out.push(ch);
}
out.push(ELLIPSIS);
out
}
fn char_width(ch: char, size: f32) -> f32 {
let wide = matches!(u32::from(ch), 0x1100..=0x115F | 0x2E80..=0xA4CF | 0xAC00..=0xD7A3 | 0xF900..=0xFAFF | 0xFE30..=0xFE4F | 0xFF00..=0xFF60 | 0xFFE0..=0xFFE6 | 0x20000..=0x3FFFD);
size * if wide { WIDE_CHAR_RATIO } else { NARROW_CHAR_RATIO }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn numbers_are_short_and_finite() {
assert_eq!(num(1.0), "1");
assert_eq!(num(1.256), "1.26");
assert_eq!(num(-0.001), "0");
assert_eq!(num(f32::NAN), "0");
assert_eq!(num(f32::INFINITY), "0");
}
#[test]
fn text_is_escaped_and_measured() {
assert_eq!(escape("a<b>&\"c\u{1}\nd"), "a<b>&"c d");
assert!(text_width("東京", 10.0) > text_width("ab", 10.0));
let cut = fit("a long label that does not fit", 10.0, 40.0);
assert!(cut.ends_with(ELLIPSIS) && text_width(&cut, 10.0) <= 40.0);
assert_eq!(fit("ok", 10.0, 40.0), "ok");
}
}