55 lines
1,014 B
Rust
55 lines
1,014 B
Rust
|
use std::{fs::File, io::{BufWriter, Write}};
|
||
|
|
||
|
use crate::font::{GlyphHeader, GlyphPoint};
|
||
|
|
||
|
use super::Visitor;
|
||
|
|
||
|
pub struct SvgWriter {
|
||
|
first_point: bool,
|
||
|
writer: BufWriter<File>
|
||
|
}
|
||
|
|
||
|
impl SvgWriter {
|
||
|
pub fn new(file: File) -> Self {
|
||
|
SvgWriter {
|
||
|
first_point: true,
|
||
|
writer: BufWriter::new(file)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Visitor for SvgWriter {
|
||
|
fn write_prefix(&mut self, header: &GlyphHeader) {
|
||
|
write!(
|
||
|
self.writer,
|
||
|
"<svg width=\"1000\" height=\"1000\" viewBox=\"{} {} {} {}\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"",
|
||
|
header.xmin, header.ymin,
|
||
|
header.xmax - header.xmin,
|
||
|
header.ymax - header.ymin
|
||
|
);
|
||
|
}
|
||
|
|
||
|
fn write_point(&mut self, point: &GlyphPoint) {
|
||
|
if !point.on_curve {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
let prefix = if self.first_point { "M" } else { "L" };
|
||
|
|
||
|
write!(
|
||
|
self.writer,
|
||
|
"{prefix}{} {} ",
|
||
|
point.x, point.y
|
||
|
);
|
||
|
|
||
|
self.first_point = false;
|
||
|
}
|
||
|
|
||
|
fn write_suffix(&mut self) {
|
||
|
write!(
|
||
|
self.writer,
|
||
|
"\" style=\"fill:none; stroke:black; stroke-width:3;\" /></svg>"
|
||
|
);
|
||
|
}
|
||
|
}
|