fixed font rendering in svg

This commit is contained in:
elvis
2025-11-03 16:44:41 +01:00
parent 62545cb800
commit 866e4606da
3 changed files with 23 additions and 4 deletions

View File

@ -15,6 +15,7 @@ colored = "*"
lalrpop-util = "*"
petgraph = ">=0.8"
resvg = "*"
fontdb = "*"
dyn-clone = "*"
petgraph-graphml = "*"
egui_node_graph2 = { path = "../egui_node_graph2" }

View File

@ -1981,7 +1981,7 @@ impl eframe::App for AppHandle {
);
if let Err(e) = err {
let text = get_layout(Err(e), &self.translator.lock().unwrap(), ctx);
self.cached_last_value = Some(content.clone());
self.cached_last_value = Some(text);
} else if let Some(l_b_v) = self.cache.get_last_state() {
if let BasicValue::SaveBytes { path, value } = &l_b_v {
use std::io::Write;
@ -1992,7 +1992,7 @@ impl eframe::App for AppHandle {
return;
}
};
if let Err(e) = write!(f, "{}", value) {
if let Err(e) = f.write_all(value) {
println!("Error writing to file {path}: {e}");
return;
}

View File

@ -21,6 +21,9 @@ pub(crate) struct Svg {
impl Svg {
pub(crate) fn parse_dot_string(dot_str: &str) -> Result<Svg, String> {
let mut fontdb = fontdb::Database::new();
fontdb.load_system_fonts();
let mut parser = gv::DotParser::new(dot_str);
let g = match parser.process() {
Ok(g) => g,
@ -41,7 +44,15 @@ impl Svg {
);
let content = svg.finalize();
let svg_tree = match resvg::usvg::Tree::from_str(&content, &resvg::usvg::Options::default()) {
let svg_tree = match resvg::usvg::Tree::from_str(
&content,
&resvg::usvg::Options {
dpi: 92.,
font_family: "Andale Mono".into(),
fontdb: Arc::new(fontdb),
..Default::default()
}
) {
Ok(svg) => svg,
Err(err) => return Err(format!("{}", err)),
};
@ -77,7 +88,14 @@ impl Svg {
}
pub(crate) fn rasterize(&self) -> Result<Vec<u8>, String> {
let svg_tree = match resvg::usvg::Tree::from_str(&self.original, &resvg::usvg::Options::default()) {
let svg_tree = match resvg::usvg::Tree::from_str(
&self.original,
&resvg::usvg::Options {
dpi: 92.,
font_family: "Andale Mono".into(),
..Default::default()
}
) {
Ok(svg) => svg,
Err(err) => return Err(format!("{}", err)),
};