Skip to content

Use system fonts as catalog on Desktop App#4035

Draft
Macman1234 wants to merge 1 commit intoGraphiteEditor:masterfrom
Macman1234:desktop-system-fonts
Draft

Use system fonts as catalog on Desktop App#4035
Macman1234 wants to merge 1 commit intoGraphiteEditor:masterfrom
Macman1234:desktop-system-fonts

Conversation

@Macman1234
Copy link
Copy Markdown

WIP. Current code is definitely placed in the wrong part of the codebase, may not work on platforms other than Linux, needs an option to switch between system and remote font catalogs, and does not populate the default font. Also I need to write a better description here of what the code actually does.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds system font loading for desktop environments using the parley crate. The implementation eagerly loads all system font data and sends it through the message system, which presents significant performance and memory efficiency concerns. Feedback suggests transitioning to lazy loading, avoiding expensive collection clones, and replacing unwrap() calls with safer error handling to prevent application panics. Additionally, the logging level for font discovery should be downgraded from error to debug.

Comment on lines +465 to +471
let mut font_data_vec = Vec::new();
font_data_vec.extend(font.load(None).unwrap().data());
responses.add(PortfolioMessage::FontLoaded {
font_family: name.to_owned(),
font_style: style.to_named_style(),
data: font_data_vec,
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Loading the full data for every system font and sending it through the message system is extremely inefficient. This will cause significant performance degradation and high memory consumption on startup, especially for users with many installed fonts. Font data should be loaded lazily only when a specific font is required for rendering, rather than populating the entire cache upfront.

// simultaneously?? call font loaded a bunch of times to shove data into font data cache
let mut system_font_family_names = Vec::new();
system_font_family_names.extend(system_font_collection.family_names());
log::error!("fonts are: {:?}", system_font_family_names);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using log::error for informational logging of font names is inappropriate. This should be changed to log::debug or log::trace to avoid cluttering the error logs.

Suggested change
log::error!("fonts are: {:?}", system_font_family_names);
log::debug!("fonts are: {:?}", system_font_family_names);

log::error!("fonts are: {:?}", system_font_family_names);
let mut families_for_catalog = Vec::new();
for name in system_font_family_names {
let family = system_font_collection_again.family_by_name(name).unwrap();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Avoid using unwrap() on family_by_name. If a font family name returned by the collection is unexpectedly missing, this will cause the application to panic. It is safer to handle this case gracefully.

Suggested change
let family = system_font_collection_again.family_by_name(name).unwrap();
let Some(family) = system_font_collection_again.family_by_name(name) else { continue };

url: "".to_owned(),
};
let mut font_data_vec = Vec::new();
font_data_vec.extend(font.load(None).unwrap().data());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

font.load(None) can fail if a font file is corrupt or inaccessible. Using unwrap() here makes the application fragile. Use a guard or if let to skip fonts that cannot be loaded.

								let Some(font_data) = font.load(None) else { continue };
								font_data_vec.extend(font_data.data());

if Editor::environment().is_desktop() {
let system_font_context = FontContext::new(); // create system font context
let mut system_font_collection = system_font_context.collection;
let mut system_font_collection_again = system_font_collection.clone(); // because parley was not made correctly
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Cloning the entire font collection is an expensive operation. If parley requires ownership or a mutable reference that prevents using the original system_font_collection, consider if there is a way to restructure the access to avoid this clone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant