-
Notifications
You must be signed in to change notification settings - Fork 14
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
Expose System Fonts #837
Comments
Ideas from ChatGPT Yes, it's possible to use system fonts in JavaScript and CSS. System fonts are the fonts that are installed on the user's device and can vary depending on the operating system. Here's how you can list and use them: 1. Using System Fonts in CSSYou can specify system fonts in CSS by using a generic font-family stack that includes common system fonts. Here's an example: body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
} This stack includes:
2. Listing System Fonts in JavaScriptJavaScript cannot directly list all system fonts, but you can detect which fonts are available on the user's system by trying to render text in different fonts and comparing their rendered width or height. Here's a simple example: function isFontAvailable(font) {
const testString = "abcdefghijklmnopqrstuvwxyz0123456789";
const testSize = "72px";
// Create a span element with the test string
const span = document.createElement("span");
span.style.fontSize = testSize;
span.style.visibility = "hidden";
span.style.position = "absolute";
span.innerHTML = testString;
document.body.appendChild(span);
// Default font to compare against
const defaultFont = "monospace";
span.style.fontFamily = defaultFont;
const defaultWidth = span.offsetWidth;
// Test with the desired font
span.style.fontFamily = `${font}, ${defaultFont}`;
const newWidth = span.offsetWidth;
// Remove the span from the DOM
document.body.removeChild(span);
// If the width changed, the font is available
return newWidth !== defaultWidth;
}
const fontsToTest = ["Arial", "Courier", "Georgia", "Tahoma", "Verdana"];
fontsToTest.forEach((font) => {
if (isFontAvailable(font)) {
console.log(`${font} is available`);
} else {
console.log(`${font} is not available`);
}
}); This code checks if a given font is available on the user's system by comparing the width of a string rendered in that font against a default font. If the width changes, the font is considered available. 3. Using System Fonts Dynamically in JavaScriptIf you want to dynamically apply a system font based on availability, you could extend the above JavaScript approach: const preferredFonts = ["Segoe UI", "Roboto", "Arial", "Verdana", "sans-serif"];
let chosenFont = preferredFonts.find(isFontAvailable);
if (chosenFont) {
document.body.style.fontFamily = chosenFont;
} This script will apply the first available font from your preferred list to the 4. Using CSS
|
Yes, there are some NPM libraries that can help you work with system fonts, though the range of functionality may vary depending on what exactly you're looking to achieve. Here are a few libraries that can assist with system fonts: 1.
|
The text was updated successfully, but these errors were encountered: