Skip to content

Instantly share code, notes, and snippets.

@xnuk
Created July 4, 2021 16:51
Show Gist options
  • Save xnuk/f5d366a008e653aca29d946246138d47 to your computer and use it in GitHub Desktop.
Save xnuk/f5d366a008e653aca29d946246138d47 to your computer and use it in GitHub Desktop.
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15af2628f6890fe2609a3b91bef4c83450512802e59489f9c1cb1fa5df064a61"
[[package]]
name = "ttf-parser"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ae2f58a822f08abdaf668897e96a5656fe72f5a9ce66422423e8849384872e6"
[[package]]
name = "ttfp"
version = "0.1.0"
dependencies = [
"anyhow",
"ttf-parser",
]
[package]
name = "ttfp"
version = "0.1.0"
edition = "2018"
[dependencies]
ttf-parser = "^0.12.3"
anyhow = "^1.0.41"
use anyhow::Error;
use std::env::args_os;
use std::fs::read;
use ttf_parser::{Face, GlyphId, OutlineBuilder};
#[derive(Debug)]
struct Path(Vec<String>);
impl Path {
fn new() -> Self {
Path(vec![])
}
fn to_string(&self) -> String {
self.0.join(" ")
}
}
impl OutlineBuilder for Path {
fn move_to(&mut self, x: f32, y: f32) {
self.0.push(format!("M {} {}", x, y));
}
fn line_to(&mut self, x: f32, y: f32) {
self.0.push(format!("L {} {}", x, y));
}
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
self.0.push(format!("Q {} {} {} {}", x1, y1, x, y));
}
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
self.0
.push(format!("C {} {} {} {} {} {}", x1, y1, x2, y2, x, y));
}
fn close(&mut self) {
let str = self.0.join(" ");
self.0.clear();
self.0.push(str);
}
}
fn main() -> anyhow::Result<()> {
let path = args_os()
.skip(1)
.last()
.ok_or(Error::msg("Usage: sex [FILE]"))?;
let file = read(path)?;
let face = Face::from_slice(file.as_slice(), 0)?;
for index in 0..face.number_of_glyphs() {
let id = GlyphId(index);
let mut path = Path::new();
if let Some(rect) = face.outline_glyph(id, &mut path) {
let fallback = format!("@{}", index);
let name = face.glyph_name(id).unwrap_or(&fallback);
let viewbox = format!(
"{} {} {} {}",
rect.x_min,
rect.y_min,
rect.width(),
rect.height()
);
println!(
"<svg id={:?} viewBox={:?}><path d={:?} /></svg>",
name,
viewbox,
path.to_string()
);
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment