Repository navigation
feat(ui-egui): font boxes filter, recents, script groups, add font, sample fonts #396
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
Open
mameen
wants to merge
16
commits into
AlienTechSoftware:main
Choose a base branch
from
mameen:feat/wordcraft-font-picker/agent-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,253
−94
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
92a6036
feat(ui-egui): font family box filters as you type
alientechsw d4eaa11
test(ui-egui): the font name box takes typed text in the filter test
alientechsw c8cc4a5
feat(ui-egui): font boxes list recently used fonts first
alientechsw f2016b5
feat(ui-egui): font box warns when the font is not installed
alientechsw c215950
feat(ui-egui): font list draws each family in its own font
alientechsw 100211b
feat(ui-egui): font list groups families by script
alientechsw a0c05bc
test(ui-egui): compare the family name without a redundant borrow
alientechsw 1dcfaa2
feat(ui-egui): add font files, searchable script headings, usage events
alientechsw f1d1264
feat(ui-egui): font names indent under their script heading
alientechsw 9420ff7
fix(ui-egui): font catalogue and file dialog only on desktop
alientechsw a09fccd
fix(ui-egui): address review of the font picker
alientechsw 577790a
test(ui-egui): wait for background font lookup; note Add Font limits
alientechsw 77dd914
test(ui-egui): let frames settle after a font size click
alientechsw a973a49
fix(ui-egui): drop an import the Android build does not use
alientechsw 364b9c2
Merge branch 'main' into feat/wordcraft-font-picker/agent-1
alientechsw 4d62a7d
Merge branch 'main' into feat/wordcraft-font-picker/agent-1
alientechsw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| //! Adds font files to the user's own fonts folder, so the family list can show them. Only files | ||
| //! with a font extension are copied; anything else is reported and left alone. No egui in here. | ||
|
|
||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| /// The extensions a font file may have. | ||
| const FONT_EXTENSIONS: [&str; 4] = ["ttf", "otf", "ttc", "otc"]; | ||
|
|
||
| /// What one install did: the file names copied, and each file skipped with the reason. | ||
| #[derive(Debug, Default, PartialEq, Eq)] | ||
| pub struct Report { | ||
| pub copied: Vec<String>, | ||
| pub skipped: Vec<(String, String)>, | ||
| } | ||
|
|
||
| /// The fonts folder of the current user, where a font can be added without administrator rights. | ||
| pub fn user_fonts_dir() -> Option<PathBuf> { | ||
| #[cfg(target_os = "windows")] | ||
| { | ||
| std::env::var_os("LOCALAPPDATA").map(|local| PathBuf::from(local).join("Microsoft").join("Windows").join("Fonts")) | ||
|
mameen marked this conversation as resolved.
Resolved
|
||
| } | ||
| #[cfg(target_os = "macos")] | ||
| { | ||
| std::env::var_os("HOME").map(|home| PathBuf::from(home).join("Library").join("Fonts")) | ||
| } | ||
| #[cfg(target_os = "linux")] | ||
| { | ||
| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".local").join("share").join("fonts")) | ||
| } | ||
| #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] | ||
| { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// True when the file name ends with a font extension (case does not matter). | ||
| pub fn is_font_file(path: &Path) -> bool { | ||
| path.extension().and_then(|ext| ext.to_str()).is_some_and(|ext| FONT_EXTENSIONS.iter().any(|known| known.eq_ignore_ascii_case(ext))) | ||
| } | ||
|
|
||
| /// True when both paths name the same file on disk. A target that does not exist yet is never the | ||
| /// same file. | ||
| fn same_file(source: &Path, target: &Path) -> bool { | ||
| match (std::fs::canonicalize(source), std::fs::canonicalize(target)) { | ||
| (Ok(a), Ok(b)) => a == b, | ||
| _ => false, | ||
| } | ||
| } | ||
|
|
||
| /// Copies each font file into `dir`, creating it when needed. A file that is not a font, or that | ||
| /// cannot be copied, is skipped and reported with its reason. | ||
| pub fn install(files: &[PathBuf], dir: &Path) -> Report { | ||
| let mut report = Report::default(); | ||
| let name_of = |path: &Path| path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); | ||
| if let Err(error) = std::fs::create_dir_all(dir) { | ||
| for file in files { | ||
| report.skipped.push((name_of(file), format!("the fonts folder cannot be created: {error}"))); | ||
| } | ||
| return report; | ||
| } | ||
| for file in files { | ||
| let name = name_of(file); | ||
| if !is_font_file(file) { | ||
| report.skipped.push((name, "not a font file".to_owned())); | ||
| continue; | ||
| } | ||
| let target = dir.join(&name); | ||
| // The file is already in the fonts folder: copying it onto itself would empty it. | ||
| if same_file(file, &target) { | ||
| report.skipped.push((name, "already in the fonts folder".to_owned())); | ||
| continue; | ||
| } | ||
| // Another font with this name is installed: refuse rather than overwrite it. | ||
| if target.exists() { | ||
| report.skipped.push((name, "a different font with this name is already installed".to_owned())); | ||
| continue; | ||
| } | ||
| match std::fs::copy(file, &target) { | ||
| Ok(_) => report.copied.push(name), | ||
| Err(error) => report.skipped.push((name, error.to_string())), | ||
| } | ||
| } | ||
| report | ||
| } | ||
|
|
||
| #[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))] | ||
| /// Asks for font files (several may be picked), installs them in the user's fonts folder, and | ||
| /// reports what happened. `None` when the dialog was cancelled. Events and log lines go out here, | ||
| /// so every caller records the same things. | ||
| pub fn pick_and_install() -> Option<Report> { | ||
| let picked = rfd::FileDialog::new() | ||
| .set_title(crate::tl_id!("ui.add_font_dialog_title")) | ||
| .add_filter(crate::tl_id!("ui.font_files_filter").to_string(), &FONT_EXTENSIONS) | ||
| .pick_files(); | ||
| let Some(files) = picked else { | ||
| crate::usage::record(crate::usage::Level::Usage, "font_add_cancelled", &[]); | ||
| return None; | ||
| }; | ||
| let Some(dir) = user_fonts_dir() else { | ||
| crate::usage::record(crate::usage::Level::Error, "font_add_no_folder", &[]); | ||
| log::error!("Add font: this platform has no user fonts folder"); | ||
| return Some(Report { copied: Vec::new(), skipped: files.iter().map(|f| (f.display().to_string(), "no fonts folder".to_owned())).collect() }); | ||
| }; | ||
| let report = install(&files, &dir); | ||
| crate::usage::record( | ||
| crate::usage::Level::Usage, | ||
| "font_added", | ||
| &[("copied", &report.copied.len().to_string()), ("skipped", &report.skipped.len().to_string())], | ||
| ); | ||
| for (name, reason) in &report.skipped { | ||
| crate::usage::record(crate::usage::Level::Warning, "font_add_skipped", &[("reason", reason)]); | ||
| log::warn!("Add font: {name} was not added: {reason}"); | ||
| } | ||
| log::info!("Add font: {} added to {}", report.copied.len(), dir.display()); | ||
| Some(report) | ||
| } | ||
|
|
||
| #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))] | ||
| /// Adding fonts needs the file dialog, which only the desktop builds have. | ||
| pub fn pick_and_install() -> Option<Report> { | ||
| None | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn scratch(name: &str) -> PathBuf { | ||
| let dir = std::env::temp_dir().join(format!("notecraft-font-install-{name}-{}", std::process::id())); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| dir | ||
| } | ||
|
|
||
| #[test] | ||
| fn font_extensions_are_recognised_whatever_their_case() { | ||
| assert!(is_font_file(Path::new("A.TTF"))); | ||
| assert!(is_font_file(Path::new("b.otf"))); | ||
| assert!(is_font_file(Path::new("c.ttc"))); | ||
| assert!(!is_font_file(Path::new("notes.md"))); | ||
| assert!(!is_font_file(Path::new("noextension"))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn font_files_are_copied_and_other_files_are_reported() { | ||
| let source = scratch("source"); | ||
| let target = scratch("target").join("fonts"); | ||
| let font = source.join("Demo.ttf"); | ||
| let note = source.join("readme.txt"); | ||
| std::fs::write(&font, b"font bytes").expect("font"); | ||
| std::fs::write(¬e, b"not a font").expect("note"); | ||
|
|
||
| let report = install(&[font, note], &target); | ||
|
|
||
| assert_eq!(report.copied, vec!["Demo.ttf".to_owned()]); | ||
| assert_eq!(report.skipped, vec![("readme.txt".to_owned(), "not a font file".to_owned())]); | ||
| assert_eq!(std::fs::read(target.join("Demo.ttf")).expect("copied"), b"font bytes"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_font_already_in_the_folder_is_not_emptied() { | ||
| let folder = scratch("in-place"); | ||
| let font = folder.join("Kept.otf"); | ||
| std::fs::write(&font, b"keep these bytes").expect("font"); | ||
|
|
||
| let report = install(std::slice::from_ref(&font), &folder); | ||
|
|
||
| assert!(report.copied.is_empty()); | ||
| assert_eq!(report.skipped[0].1, "already in the fonts folder"); | ||
| assert_eq!(std::fs::read(&font).expect("still there"), b"keep these bytes"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_different_font_with_the_same_name_is_not_overwritten() { | ||
| let source = scratch("clash-source"); | ||
| let folder = scratch("clash-target"); | ||
| std::fs::write(folder.join("Same.ttf"), b"installed").expect("installed"); | ||
| let incoming = source.join("Same.ttf"); | ||
| std::fs::write(&incoming, b"incoming").expect("incoming"); | ||
|
|
||
| let report = install(&[incoming], &folder); | ||
|
|
||
| assert!(report.copied.is_empty()); | ||
| assert_eq!(std::fs::read(folder.join("Same.ttf")).expect("kept"), b"installed"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_missing_file_is_reported_with_its_reason() { | ||
| let target = scratch("missing"); | ||
| let report = install(&[PathBuf::from("does-not-exist.otf")], &target); | ||
| assert!(report.copied.is_empty()); | ||
| assert_eq!(report.skipped.len(), 1); | ||
| assert_eq!(report.skipped[0].0, "does-not-exist.otf"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.