-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Application size? #31
Comments
It'll include all of them. This is not ideal and I'm working on solutions. The easiest one would be to simply use a font subsetter, but there doesn't seem to be a good Rust option for that and I don't want to include python/node tooling in a rust crate. I'm experimenting with both manually subsetting the fonts and also generating functions that could paint the icons with an egui painter at a given rect. |
Interesting. I see that they offer a full zip download that includes the icons as SVGs and fonts. I guess you are using Python to download the zip and extract the TTF files. Curious... can this Python code not be easily re-written in Rust? I see others using Python for helpers, but not sure why. Just thinking how I would tackle the problem. I would definitely be Rust-only (my strong bias, but makes things easier in one dimension at least if there is only one language). I think I would write some Rust to scan the Rust source and determine which icons are included... then create a new TTF file with just those required icons. Ok, it sounds easy :-) I see that the entire TTF file is about 500 kb so I guess that's not so bad, still it seems silly to include what isn't being used. |
It can. I don't really mind which language is used for generating code if the code ends up committed to the repo and will never have to be run by a user of this crate, which is currently the case. Source scanning is unreliable when macros are involved. I want to end up with a build.rs macro where you can specify which icons to include. If a user of the crate wants to then scan their own code to generate the line of code that calls that, they may. Generating TTFs / subsetting is the part I have not found a good solution for. There are loads of parsing crates, but not many that allow you to manipulate the fonts. There's at least one which is a work in progress, but last time I checked it did not support subsetting yet. |
This is what ChatGPT suggests LOL. Converting an SVG image to a TTF font in Rust involves several steps, as Rust does not have a direct library to handle the entire process. You can break the task into manageable parts using libraries for each step:
Here's an outline of the steps and code to achieve this: 1. Parsing the SVG FileYou can use the Add DependenciesAdd the following to your [dependencies]
usvg = "0.31"
ttfgen = "0.3" 2. Extract Paths and Create a FontHere’s an example workflow for converting an SVG into a TTF font: Code Exampleuse usvg::{NodeExt, Options, Tree};
use ttfgen::{Font, Glyph};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load and parse the SVG file
let svg_data = std::fs::read_to_string("example.svg")?;
let options = Options::default();
let rtree = Tree::from_str(&svg_data, &options.to_ref())?;
// Create a TTF font
let mut font = Font::new("CustomFont", "Custom");
// Iterate through SVG paths and convert them to glyphs
for (index, node) in rtree.root().descendants().enumerate() {
if let usvg::NodeKind::Path(ref path) = *node.borrow() {
let mut glyph = Glyph::new_empty(index as u32);
// Convert SVG path commands to font glyph points
for segment in &path.data.segments {
match segment {
usvg::PathSegment::MoveTo { x, y } => {
glyph.move_to(*x as f32, *y as f32);
}
usvg::PathSegment::LineTo { x, y } => {
glyph.line_to(*x as f32, *y as f32);
}
usvg::PathSegment::CurveTo { x1, y1, x2, y2, x, y } => {
glyph.curve_to(*x1 as f32, *y1 as f32, *x2 as f32, *y2 as f32, *x as f32, *y as f32);
}
usvg::PathSegment::ClosePath => {
glyph.close_path();
}
}
}
// Add the glyph to the font
font.add_glyph(glyph);
}
}
// Save the font to a TTF file
let font_data = font.to_bytes()?;
std::fs::write("output.ttf", font_data)?;
println!("Font created successfully: output.ttf");
Ok(())
} Explanation
Limitations
Advanced OptionsIf you need a complete solution with hinting and advanced font features, you might need to use external tools like Let me know if you'd like further assistance! |
What is ttfgen?
Key Features
Why Use
|
Not sure if any of that is new or helpful. If not, delete :-) |
How does application size change with usage of this crate?
For example, if I add a single icon, are ALL icons included in the binary, or does the compiler (or crate code) exclude all others?
Perhaps you could add the answer into the Description.
The text was updated successfully, but these errors were encountered: