Skip to content
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

Create CustomFont Interface #140

Merged
merged 2 commits into from
Nov 11, 2024

Conversation

froggomad
Copy link
Contributor

PR Description: Add CustomFont Struct for Custom Font Creation and PageElement Extensions For Custom Font Management

Summary

This PR introduces a new struct, CustomFont, and extends PageElement to support custom font management, allowing developers to define, add, and use custom fonts easily.

Key Changes

  1. CustomFont Struct:

    • Provides a way to define a custom font using a font family name and a URL string (which can be local or remote).
    • Adds optional fallback font families that are joined in CSS syntax, allowing for a smooth transition if the primary font fails to load.
    • Includes a computed property, fontURLString, which returns a valid URL string or an "Invalid URL" message if the URL is malformed.
  2. PageElement Extensions:

    • addCustomFontFace(_:): Adds a CSS @font-face rule using the CustomFont instance to make the font available across the application.
    • useCustomFont(_:): Applies the custom font to elements, leveraging the fallback font stack if specified.

Example Usage

The PR provides an example for usage with an enum, BrandFont, that returns predefined CustomFont instances. The example demonstrates adding the custom font faces to the HTML and using a specific font as a default on the Body element.

Additional Notes

  • This setup supports local and remote font resources by specifying a valid URL.
  • The structured approach ensures that custom fonts are consistently referenced, providing flexibility with fallback options and easy integration into different parts of the project.

Benefits

  • Enhances the styling capabilities by making font integration more straightforward and scalable.
  • Improves code readability and reuse by centralizing font management.

Testing

  • Ensure custom fonts load correctly in various browsers and environments (local/remote fonts).
  • Verify fallback behavior by disabling network access to test fallback font rendering.

@twostraws
Copy link
Owner

Hello! Thank you for this contribution – I think it's an important step forward. I'm going to merge it in now, but I want to find some time to noodle around with the API a little more to see if we can make it a little more transparent in use.

@twostraws twostraws merged commit 0049ddd into twostraws:main Nov 11, 2024
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.

2 participants