-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[google_fonts] Add WOFF and WOFF2 font format support for web #10703
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
base: main
Are you sure you want to change the base?
Conversation
Adds support for WOFF and WOFF2 font formats when running on web platforms, in addition to the existing TTF and OTF support. This improves web performance as WOFF2 is a more compressed format optimized for web delivery. Non-web platforms continue to support only .ttf and .otf.
There was a problem hiding this 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 support for WOFF and WOFF2 font formats on the web, which is a great performance improvement due to smaller file sizes. The changes are well-implemented, introducing a check for kIsWeb to conditionally allow the new formats. The findFamilyWithVariantAssetPath function has been thoughtfully refactored to be testable, and the new tests are comprehensive, covering web, non-web, and common scenarios. The decision to maintain the existing asset selection logic based on manifest order to ensure backward compatibility is sensible. Overall, this is a high-quality contribution.
packages/google_fonts/CHANGELOG.md
Outdated
| @@ -1,3 +1,7 @@ | |||
| ## 6.4.0 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please pull updates and correct version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 1bac768.
packages/google_fonts/CHANGELOG.md
Outdated
| @@ -1,3 +1,7 @@ | |||
| ## 6.4.0 | |||
|
|
|||
| - Adds support for WOFF2 and WOFF font formats on web platforms for improved performance and smaller bundle sizes. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please clarify that this only concerns loading fonts locally during development, or bundled with the app (or in this case hosted in the site's assets).
When relevant files aren't found in the asset manifest, the package fetches font files from the Google Fonts API, and I'm not sure what the current behavior is (does it detect web requests and return woff2?). In any case, please also create an issue to associate with this PR and any future work regarding HTTP fetching of woff2 files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please clarify that this only concerns loading fonts locally during development, or bundled with the app (or in this case hosted in the site's assets).
Done in 1bac768.
When relevant files aren't found in the asset manifest, the package fetches font files from the Google Fonts API, and I'm not sure what the current behavior is (does it detect web requests and return woff2?).
I've tested this and it downloads a ttf, not a woff2. When checking the source I learned that it's hard-coded to ttf:
packages/packages/google_fonts/lib/src/google_fonts_descriptor.dart
Lines 36 to 51 in 2e46240
| class GoogleFontsFile { | |
| /// Creates a font file descriptor with expected hash and length validation. | |
| /// | |
| /// The [expectedFileHash] is used to verify the integrity of the downloaded | |
| /// file, and [expectedLength] is checked to ensure the file size is correct. | |
| GoogleFontsFile(this.expectedFileHash, this.expectedLength); | |
| /// The expected hash of the font file for validation. | |
| final String expectedFileHash; | |
| /// The expected length in bytes of the font file. | |
| final int expectedLength; | |
| /// The URL from which the font file can be downloaded. | |
| String get url => 'https://fonts.gstatic.com/s/a/$expectedFileHash.ttf'; | |
| } |
In any case, please also create an issue to associate with this PR and any future work regarding HTTP fetching of woff2 files.
I'll do that and add a link to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do that and add a link to this PR.
Done: flutter/flutter#180998.
|
Thanks for the PR! just a couple of notes, since I updated this package recently |
Thanks for reviewing! I've made the requested changes and created the issue for HTTP WOFF2 fetching (flutter/flutter#180998). Let me know if there's anything else needed to get this merged. Also, feel free to push any changes directly to this branch if that's easier for you. |
guidezpl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks!
Description
Web applications benefit significantly from using WOFF2 font files instead of TTF/OTF. WOFF2 is a compressed format
specifically designed for web delivery, resulting in smaller file sizes and faster load times. This is especially
important for Flutter web apps where every byte counts for initial page load performance.
Size comparison (Roboto Regular):
Currently,
google_fontsonly supports TTF and OTF formats, even on web platforms where WOFF2 would be more efficient.This PR adds support for WOFF2 and WOFF formats on web, allowing developers to bundle more efficient font files for their
web applications.
What
This PR adds support for
.woff2and.wofffont file extensions when running on web platforms, while maintainingbackward compatibility with
.ttfand.otffiles.Changes:
kIsWebcheck to support web-optimized font formats (.woff2,.woff) in addition to.ttfand.otffindFamilyWithVariantAssetPathtestable by adding an optionalisWebparameter (defaults tokIsWeb)@visibleForTestingannotation to enable comprehensive testing without exposing internal APIPlatform behavior:
.woff2,.woff,.ttf,.otf.ttf,.otf(unchanged)Implementation Notes
The implementation maintains the existing behavior where the order of assets in the manifest determines which file type
is chosen when multiple formats are available for the same font. While this feels a bit odd (ideally, there would be a
preference order), this is how the function already works for TTF/OTF. In practice, this is not an issue when only one
file type per font is bundled, which is the common case.
Getting WOFF2 Files
Developers can convert TTF/OTF fonts to WOFF2 using Google's
woff2_compresstool:https://github.com/google/woff2
Example:
Testing
All existing tests pass, plus 20 new tests that verify:
Breaking Changes
None. This is a backward-compatible addition that only expands the supported file formats on web platforms.
EOF
Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3