From a9a870ec2fc7f3042668f1386c9f19743102089c Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 11 May 2022 11:36:52 -0400 Subject: [PATCH] changes in serialization of data --- .gitignore | 1 + Cargo.lock | 16 +++++++++ Cargo.toml | 10 +++++- build.rs | 59 ++++++++++++++++++++++++++++-- pack_assets.sh | 3 +- src/math_app.rs | 96 +++++-------------------------------------------- 6 files changed, 92 insertions(+), 93 deletions(-) diff --git a/.gitignore b/.gitignore index b115a87..2956a74 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /tmp /assets.tar.zst /Cargo.lock +/assets/font_data diff --git a/Cargo.lock b/Cargo.lock index 8175ada..4d0f5af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,6 +41,7 @@ checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" dependencies = [ "getrandom", "once_cell", + "serde", "version_check", ] @@ -152,6 +153,15 @@ dependencies = [ "pprof", ] +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -730,6 +740,7 @@ version = "0.18.0" source = "git+https://github.com/Titaniumtown/egui.git#e9ed0e4b3ad7ca6e887edb013437077ea398d568" dependencies = [ "bytemuck", + "serde", ] [[package]] @@ -744,6 +755,7 @@ dependencies = [ "emath", "nohash-hasher", "parking_lot 0.12.0", + "serde", ] [[package]] @@ -1981,6 +1993,9 @@ name = "serde" version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" +dependencies = [ + "serde_derive", +] [[package]] name = "serde_cbor" @@ -2798,6 +2813,7 @@ version = "0.1.0" dependencies = [ "async-lock", "benchmarks", + "bincode", "cfg-if 1.0.0", "command-run", "console_error_panic_hook", diff --git a/Cargo.toml b/Cargo.toml index 90c8adb..18bb983 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,9 @@ strip = false parsing = { path = "./parsing" } eframe = { git = "https://github.com/Titaniumtown/egui.git", default-features = false } egui = { git = "https://github.com/Titaniumtown/egui.git", default-features = false } -epaint = { git = "https://github.com/Titaniumtown/egui.git", default-features = false } +epaint = { git = "https://github.com/Titaniumtown/egui.git", default-features = false, features = [ + "serde", +] } emath = { git = "https://github.com/Titaniumtown/egui.git", default-features = false } shadow-rs = { version = "0.11", default-features = false } @@ -52,6 +54,8 @@ tracing = "0.1" itertools = "0.10" static_assertions = "1.1" uuid = { version = "1", features = ["v4", "fast-rng", "js"] } +bincode = "1.3.3" + [dev-dependencies] benchmarks = { path = "./benchmarks" } @@ -59,6 +63,10 @@ benchmarks = { path = "./benchmarks" } [build-dependencies] shadow-rs = "0.11" command-run = "1.1" +epaint = { git = "https://github.com/Titaniumtown/egui.git", default-features = false, features = [ + "serde", +] } +bincode = "1.3.3" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] instant = "0.1" diff --git a/build.rs b/build.rs index 9f5f160..2bbe113 100644 --- a/build.rs +++ b/build.rs @@ -1,11 +1,64 @@ +use std::{ + collections::BTreeMap, + fs::File, + io::{BufWriter, Write}, +}; + +use epaint::{ + text::{FontData, FontDefinitions}, + FontFamily, +}; + fn main() { // rebuild if new commit or contents of `assets` folder changed println!("cargo:rerun-if-changed=.git/logs/HEAD"); println!("cargo:rerun-if-changed=assets/*"); - let _ = command_run::Command::with_args("./pack_assets.sh", &[""]) + shadow_rs::new().expect("Could not initialize shadow_rs"); + + let font_hack = FontData::from_static(include_bytes!("assets/Hack-Regular.ttf")); + let font_ubuntu_light = FontData::from_static(include_bytes!("assets/Ubuntu-Light.ttf")); + let font_notoemoji = FontData::from_static(include_bytes!("assets/NotoEmoji-Regular.ttf")); + let font_emoji_icon = FontData::from_static(include_bytes!("assets/emoji-icon-font.ttf")); + + let fonts = FontDefinitions { + font_data: BTreeMap::from([ + ("Hack".to_owned(), font_hack), + ("Ubuntu-Light".to_owned(), font_ubuntu_light), + ("NotoEmoji-Regular".to_owned(), font_notoemoji), + ("emoji-icon-font".to_owned(), font_emoji_icon), + ]), + families: BTreeMap::from([ + ( + FontFamily::Monospace, + vec![ + "Hack".to_owned(), + "Ubuntu-Light".to_owned(), + "NotoEmoji-Regular".to_owned(), + "emoji-icon-font".to_owned(), + ], + ), + ( + FontFamily::Proportional, + vec![ + "Ubuntu-Light".to_owned(), + "NotoEmoji-Regular".to_owned(), + "emoji-icon-font".to_owned(), + ], + ), + ]), + }; + + let path = "./assets/font_data"; + + let fonts_data = bincode::serialize(&fonts).unwrap(); + // ::to_string(&fonts).expect("Failed to serialize fonts"); + + let mut file = BufWriter::new(File::create(&path).expect("Could not create file")); + + file.write_all(fonts_data.as_slice()).unwrap(); + + let _ = command_run::Command::with_args("./pack_assets.sh", &[&path]) .enable_capture() .run(); - - shadow_rs::new().expect("Could not initialize shadow_rs"); } diff --git a/pack_assets.sh b/pack_assets.sh index 1054e48..a10ad19 100755 --- a/pack_assets.sh +++ b/pack_assets.sh @@ -1,4 +1,3 @@ #!/bin/bash rm -fr assets.tar.zst | true -cd assets -tar -I 'zstd --ultra -22' -cf ../assets.tar.zst *.* +tar -I 'zstd --ultra -22' --strip-components=9999 -cf ./assets.tar.zst assets/text.json $1 diff --git a/src/math_app.rs b/src/math_app.rs index 8d37b8c..1b67ee9 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -3,16 +3,14 @@ use crate::function_entry::Riemann; use crate::function_manager::FunctionManager; use crate::misc::{dyn_mut_iter, option_vec_printer, TextData}; use eframe::App; -use egui::FontTweak; use egui::{ - plot::Plot, style::Margin, vec2, Button, CentralPanel, Color32, ComboBox, Context, FontData, - FontDefinitions, FontFamily, Frame, Key, Label, Layout, RichText, SidePanel, Slider, - TopBottomPanel, Vec2, Visuals, Window, + plot::Plot, style::Margin, vec2, Button, CentralPanel, Color32, ComboBox, Context, + FontDefinitions, Frame, Key, Label, Layout, RichText, SidePanel, Slider, TopBottomPanel, Vec2, + Visuals, Window, }; use emath::{Align, Align2}; use epaint::Rounding; use instant::Duration; -use std::collections::BTreeMap; use std::{io::Read, ops::BitXorAssign, str}; #[cfg(threading)] @@ -156,10 +154,7 @@ impl MathApp { update_loading(&loading_element, 30); // Stores fonts - let mut font_ubuntu_light: Option = None; - let mut font_notoemoji: Option = None; - let mut font_hack: Option = None; - let mut font_emoji_icon: Option = None; + let mut font_data: Option = None; // Stores text let mut text_data: Option = None; @@ -179,43 +174,9 @@ impl MathApp { let path_string = path.to_string_lossy(); // Match the file extention - if path_string.ends_with(".ttf") { - // Parse font files - let font_data = FontData::from_owned(data); - match path_string.as_ref() { - "Hack-Regular.ttf" => { - #[cfg(target_arch = "wasm32")] - update_loading(&loading_element, 10); - - font_hack = Some(font_data); - } - "NotoEmoji-Regular.ttf" => { - #[cfg(target_arch = "wasm32")] - update_loading(&loading_element, 10); - - font_notoemoji = Some(font_data); - } - "Ubuntu-Light.ttf" => { - #[cfg(target_arch = "wasm32")] - update_loading(&loading_element, 10); - - font_ubuntu_light = Some(font_data); - } - "emoji-icon-font.ttf" => { - #[cfg(target_arch = "wasm32")] - update_loading(&loading_element, 10); - - font_emoji_icon = Some(font_data.tweak(FontTweak { - scale: 0.8, // make it smaller - y_offset_factor: 0.07, // move it down slightly - y_offset: 0.0, - })) - } - _ => { - panic!("Font File {} not expected!", path_string); - } - } - } else if path_string == "text.json" { + if path_string.ends_with("font_data") { + font_data = Some(bincode::deserialize(&data).unwrap()); + } else if path_string.ends_with("text.json") { #[cfg(target_arch = "wasm32")] update_loading(&loading_element, 10); text_data = Some(TextData::from_json_str(unsafe { @@ -226,49 +187,10 @@ impl MathApp { } } - let fonts = FontDefinitions { - font_data: BTreeMap::from([ - ( - "Hack".to_owned(), - font_hack.expect("Hack-Regular.ttf not found!"), - ), - ( - "Ubuntu-Light".to_owned(), - font_ubuntu_light.expect("Ubuntu-Light.ttf not found!"), - ), - ( - "NotoEmoji-Regular".to_owned(), - font_notoemoji.expect("NotoEmoji-Regular.ttf not found!"), - ), - ( - "emoji-icon-font".to_owned(), - font_emoji_icon.expect("emoji-icon-font.ttf not found!"), - ), - ]), - families: BTreeMap::from([ - ( - FontFamily::Monospace, - vec![ - "Hack".to_owned(), - "Ubuntu-Light".to_owned(), - "NotoEmoji-Regular".to_owned(), - "emoji-icon-font".to_owned(), - ], - ), - ( - FontFamily::Proportional, - vec![ - "Ubuntu-Light".to_owned(), - "NotoEmoji-Regular".to_owned(), - "emoji-icon-font".to_owned(), - ], - ), - ]), - }; - // Initialize fonts // This used to be in the `update` method, but (after a ton of digging) this actually caused OOMs. that was a pain to debug - cc.egui_ctx.set_fonts(fonts); + cc.egui_ctx + .set_fonts(font_data.expect("Failed to load font_data")); // Set dark mode by default cc.egui_ctx.set_visuals(Visuals::dark());