[google_fonts] Add WOFF and WOFF2 font format support for web (#10703) # 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):** - TTF: 146,004 bytes - WOFF2: 63,424 bytes (~56% smaller) Currently, `google_fonts` only 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 `.woff2` and `.woff` font file extensions when running on web platforms, while maintaining backward compatibility with `.ttf` and `.otf` files. **Changes:** - Add `kIsWeb` check to support web-optimized font formats (`.woff2`, `.woff`) in addition to `.ttf` and `.otf` - Make `findFamilyWithVariantAssetPath` testable by adding an optional `isWeb` parameter (defaults to `kIsWeb`) - Add `@visibleForTesting` annotation to enable comprehensive testing without exposing internal API - Add 20 tests covering web-specific, non-web-specific, and common behavior **Platform behavior:** - **Web:** Supports `.woff2`, `.woff`, `.ttf`, `.otf` - **Non-web:** Supports `.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_compress` tool: https://github.com/google/woff2 Example: ```bash woff2_compress Roboto-Regular.ttf ``` ## Testing All existing tests pass, plus 20 new tests that verify: - ✅ WOFF2 and WOFF formats work on web - ✅ WOFF2 and WOFF formats are ignored on non-web platforms - ✅ TTF and OTF continue to work on all platforms - ✅ Common behavior (null handling, family/variant matching) works consistently ## Breaking Changes None. This is a backward-compatible addition that only expands the supported file formats on web platforms. EOF ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot 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. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
This repo is a companion repo to the main flutter repo. It contains the source code for Flutter's first-party packages (i.e., packages developed by the core Flutter team). Check the packages directory to see all packages.
These packages are also available on pub.
Please file any issues, bugs, or feature requests in the main flutter repo. Issues pertaining to this repository are labeled “package”.
If you wish to contribute a new package to the Flutter ecosystem, please see the documentation for developing packages. You can store your package source code in any GitHub repository (the present repo is only intended for packages developed by the core Flutter team). Once your package is ready you can publish to the pub repository.
If you wish to contribute a change to any of the existing packages in this repo, please review our contribution guide, and send a pull request.
These are the packages hosted in this repository: