Skip to content

[Breaking change] Load platform fonts fallback to avoid squares instead of glyphs - #44

Closed
MaximeRougieux wants to merge 7 commits into
mainfrom
feat/platform-fallback-fonts
Closed

[Breaking change] Load platform fonts fallback to avoid squares instead of glyphs#44
MaximeRougieux wants to merge 7 commits into
mainfrom
feat/platform-fallback-fonts

Conversation

@MaximeRougieux

Copy link
Copy Markdown
Contributor

This PR adds the following changes :

  • The fonts loaded by the package now also includes fallback system fonts that are not declared in the manifest, but that are sometimes used in Flutter widgets like Material and Cupertino widgets.
  • The package now warns when a test creates a golden of a widget that uses an unknown font

Both behavior are optional and can be opted-out (though they are on by default)

MaximeRougieux and others added 7 commits July 31, 2026 16:27
A font bundled by a dependency is exposed by the font manifest as
`packages/my_theme/Roboto`, and `loadFonts()` only registered it under that
name. A widget styled with `TextStyle(fontFamily: 'Roboto', package:
'my_theme')` — or a theme defaulting to that family — asks for the bare
`Roboto` instead, so no font was found and the text rendered as placeholder
blocks in the goldens.

Register both names. `loadFonts()` now returns the families it registered,
which is also what lets the tests assert on it without shared state.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The families a widget gets when it specifies no font — `Roboto` on Android,
`CupertinoSystemText` on iOS, `Segoe UI` on Windows — are declared in no
`pubspec.yaml`: they either ship with the Flutter SDK or belong to the host OS.
`loadFonts()` could not find them in the font manifest, so a `DatePickerDialog`
or a `CupertinoButton` rendered as placeholder blocks in the goldens.

The list is gathered from `Typography` and `CupertinoTextThemeData` instead of
being hardcoded, so it follows the SDK. The families the SDK ships are loaded
from its font cache, found through `FLUTTER_ROOT` or by walking up from the
test executable. The ones only the OS can provide are aliased to the typeface
the SDK ships: the glyphs then differ from a real device, but the goldens stay
readable. Opt out with `setLoadPlatformFallbackFonts(false)`.

`FontLoadingPolicy` holds what is registered on top of the manifest, and with
it the arbitration introduced here: a dependency font does not claim the bare
name of a platform default the SDK ships a font for. It usually bundles a
single weight of such a family while the SDK ships all of them, and on a device
it would not shadow the platform font either.

The SDK font files are sorted before being registered: a directory listing
comes back in file system order, which differs between APFS, ext4 and NTFS,
and goldens must not depend on the machine that generated them.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
A missing font is invisible until someone looks at the golden and notices the
blocks, which is exactly what a reviewer skims past. Before taking the
snapshot, `expectGolden` now collects the families the tree asks for, compares
them against the ones `loadFonts()` registered, and reports the difference.

A family is only reported when none of the fallbacks of the style is covered
either, since the engine would have a real font to use in that case.

Warnings are on by default and emitted once per family. Turn them into
failures — recommended on CI once the suite is clean — or silence them with
`setMissingFontsBehavior`.

`findUnregisteredFontFamilies` and `reportUnregisteredFontFamilies` take what
they need and return what they found, so the state that has to survive between
`flutter_test_config.dart` and a test file lives in the configuration the
package already exposes rather than in globals of their own.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The matrix only covered the three example apps, so nothing under `test/` ever
ran on CI. Format and analyze are left out of this job for now: the package
sources still carry pre-existing analyzer infos.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Goldens that contained placeholder blocks change with this version, hence the
minor bump rather than a patch.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@MaximeRougieux

Copy link
Copy Markdown
Contributor Author

Closed in favor of several PRs

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.

Bug: Fallback system fonts render as square if the fallback fonts are not included in the pubspec

1 participant