use super::dc::{Font, Rgb, num};
use base64::Engine as _;
const MIN_STROKE: f32 = 1.0;
#[derive(Debug, Clone, PartialEq)]
pub struct MetaText {
pub text: String,
pub x: f32,
pub y: f32,
pub end_x: f32,
pub size: f32,
pub face: String,
pub bold: bool,
pub italic: bool,
pub color: Rgb,
}
#[derive(Debug, Default)]
pub(super) struct Svg {
elements: Vec<(String, bool)>,
texts: Vec<(String, f32, f32, f32, f32, Font, Rgb)>,
}
impl Svg {
pub(super) fn is_empty(&self) -> bool {
self.elements.is_empty()
}
pub(super) fn path(&mut self, data: &str, fill: Option<Rgb>, stroke: Option<(Rgb, f32)>, even_odd: bool) {
if data.is_empty() || fill.is_none() && stroke.is_none() {
return;
}
let fill = fill.map_or_else(|| "none".to_string(), hex);
let stroke = match stroke {
Some((color, width)) => format!(
" stroke=\"{}\" stroke-width=\"{}\" stroke-linejoin=\"round\"",
hex(color),
num(width.max(MIN_STROKE))
),
None => String::new(),
};
let rule = if even_odd { " fill-rule=\"evenodd\"" } else { "" };
self.elements.push((
format!("<path d=\"{}\" fill=\"{fill}\"{rule}{stroke}/>", data.trim_end()),
false,
));
}
pub(super) fn text(
&mut self,
text: &str,
positions: &[(f32, f32)],
size: f32,
font: &Font,
color: Rgb,
spaced: bool,
) {
let Some(&(x, y)) = positions.first() else { return };
let text: String = text.chars().map(|ch| super::wmf::symbol_char(&font.face, ch)).collect();
let (xs, ys) = if spaced {
let xs: Vec<String> = positions.iter().map(|&(x, _)| num(x)).collect();
let ys: Vec<String> = positions.iter().map(|&(_, y)| num(y)).collect();
(xs.join(" "), ys.join(" "))
} else {
(num(x), num(y))
};
let weight = if font.weight >= 600 {
" font-weight=\"bold\""
} else {
""
};
let style = if font.italic { " font-style=\"italic\"" } else { "" };
let underline = if font.underline {
" text-decoration=\"underline\""
} else {
""
};
let rotate = if font.escapement == 0.0 {
String::new()
} else {
format!(
" transform=\"rotate({} {} {})\"",
num(-font.escapement / 10.0),
num(x),
num(y)
)
};
let plain = font.escapement == 0.0 && !font.underline && !text.trim().is_empty();
if plain {
let end = advance_end(positions, size);
self.texts.push((text.clone(), x, end, y, size, font.clone(), color));
}
self.elements.push((
format!(
"<text x=\"{xs}\" y=\"{ys}\" font-family=\"{}\" font-size=\"{}\" fill=\"{}\"{weight}{style}{underline}{rotate} xml:space=\"preserve\">{}</text>",
escape(&family_list(&font.face)),
num(size),
hex(color),
escape(&text)
),
plain,
));
}
pub(super) fn image(&mut self, png: &[u8], bounds: [f32; 4]) {
let [x, y, width, height] = bounds;
let (x, width) = if width < 0.0 { (x + width, -width) } else { (x, width) };
let (y, height) = if height < 0.0 {
(y + height, -height)
} else {
(y, height)
};
let data = base64::engine::general_purpose::STANDARD.encode(png);
self.elements.push((
format!(
"<image x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\" preserveAspectRatio=\"none\" href=\"data:image/png;base64,{data}\"/>",
num(x),
num(y),
num(width),
num(height)
),
false,
));
}
pub(super) fn finish(self, view: [f32; 4], size: (f32, f32)) -> String {
self.finish_parts(view, size, true).0
}
pub(super) fn finish_parts(self, view: [f32; 4], size: (f32, f32), with_text: bool) -> (String, Vec<MetaText>) {
let [x, y, width, height] = view;
let texts = self
.texts
.into_iter()
.map(|(text, pen, end, baseline, font_size, font, color)| MetaText {
text,
x: (pen - x) / width,
y: (baseline - y) / height,
end_x: (end - x) / width,
size: font_size / height,
bold: font.weight >= 600,
italic: font.italic,
face: font.face,
color,
})
.collect();
let mut out = format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{}pt\" height=\"{}pt\" viewBox=\"{} {} {} {}\" preserveAspectRatio=\"none\">",
num(size.0),
num(size.1),
num(x),
num(y),
num(width),
num(height)
);
for (element, text) in self.elements {
if with_text || !text {
out.push_str(&element);
}
}
out.push_str("</svg>");
(out, texts)
}
}
fn advance_end(positions: &[(f32, f32)], size: f32) -> f32 {
match positions {
[] => 0.0,
[only] => only.0 + size * 0.5,
[.., before, last] => last.0 + (last.0 - before.0),
}
}
fn hex([red, green, blue]: Rgb) -> String {
format!("#{red:02x}{green:02x}{blue:02x}")
}
fn family_list(face: &str) -> String {
let lower = face.to_ascii_lowercase();
let (open, generic) = match lower.as_str() {
"times new roman" | "times" => ("Liberation Serif, Tinos", "serif"),
"arial" | "helvetica" => ("Liberation Sans, Arimo", "sans-serif"),
"courier new" | "courier" => ("Liberation Mono, Cousine", "monospace"),
"calibri" => ("Carlito", "sans-serif"),
"cambria" | "cambria math" => ("Caladea", "serif"),
"symbol" | "mt extra" => ("DejaVu Serif", "serif"),
_ => ("DejaVu Sans", "sans-serif"),
};
format!("'{}', {open}, {generic}", face.replace('\'', ""))
}
fn escape(text: &str) -> String {
text.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
}