-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Set the font weight variation axis based on the text style's FontWeight #175771
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
Conversation
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 refactors FontWeight to be based on an integer value rather than a discrete index, enabling support for variable font weights. The changes are consistently applied across the Dart and C++ layers, including the CanvasKit and Skwasm backends, by setting the wght font variation axis from TextStyle.fontWeight. New tests are also included to validate this functionality. My review includes suggestions to enhance API robustness by adding assertions to the FontWeight constructor and to refactor a section of the font variation logic for improved performance and readability.
engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart
Outdated
Show resolved
Hide resolved
A proposed engine PR will allow construction of arbitrary FontWeight instances and thus needs to overload the FontWeight equality operator (see flutter/flutter#175771) With that change it will no longer be possible to use FontWeight as the key in a const map.
A proposed engine PR will allow construction of arbitrary FontWeight instances and thus needs to overload the FontWeight equality operator (see flutter/flutter#175771) With that change it will no longer be possible to use FontWeight as the key in a const map.
c76d2ce to
72b7627
Compare
eyebrowsoffire
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.
Skwasm changes look good!
|
Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change). If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review. For more guidance, visit Writing a golden file test for Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
| skTextStyle.fontStyle = toSkFontStyle(fontWeight, fontStyle); | ||
| } | ||
|
|
||
| final int weightValue = fontWeight?.value ?? ui.FontWeight.normal.value; |
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.
We want to avoid adding fontVariations to the text style when we can because it requires us to malloc an array on the WASM side.
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.
Unfortunately I don't know of a way to work around this.
Some variable fonts may not have the default value of the wght attribute configured to match the value of FontWeight.normal. If the engine does not explicitly set the wght value in fontVariations, then these fonts will not render as intended at the normal weight.
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.
That makes sense. I guess we can live with it, given that it seems that browsers apparently always set axes according to CSS, it makes sense for us to set them according to Flutter text style properties (source: https://groups.google.com/g/skia-discuss/c/eFFDObvJyQ8). But let's keep an eye on text benchmarks after this lands.
harryterkelsen
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.
LGTM for web_ui parts
LongCatIsLooong
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.
LGTM with nits.
| /// | ||
| /// Varies the stroke thickness of the font, similar to [FontWeight] but on a | ||
| /// continuous axis. | ||
| /// Applications should avoid using this and should instead declare font |
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.
Should this be marked as deprecated then?
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.
Or do we want to keep this in case people want to use weight for selecting a specific type face they have in mind and wght for the actual weight used for rendering?
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.
FontVariation.weight is being kept for backward compatibility and for scenarios like the one you described where the app wants direct control over wght (see #148026 (comment))
engine/src/flutter/lib/ui/text.dart
Outdated
| /// constructed using values other than the predefined values. | ||
| class FontWeight { | ||
| const FontWeight._(this.index, this.value); | ||
| /// Create a [FontWeight] object, which can be added to a [TextStyle] to |
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.
| /// Create a [FontWeight] object, which can be added to a [TextStyle] to | |
| /// Creates a [FontWeight] object, which can be added to a [TextStyle] to |
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
|
|
||
| /// The encoded integer value of this font weight. | ||
| final int index; | ||
| int get index => (value ~/ 100 - 1).clamp(0, 8); |
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.
Do we still need this? I think this should be deprecated / discouraged in the docs?
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.
index is a public API that is being retained for backward compatibility. Marked it as deprecated.
| } | ||
| return values[_lerpInt((a ?? normal).index, (b ?? normal).index, t).round().clamp(0, 8)]; | ||
| return FontWeight( | ||
| _lerpInt((a ?? normal).value, (b ?? normal).value, t).round().clamp(100, 900), |
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.
Is there a reason for still clamping the weight value to the [100, 900] range?
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.
This is also for backward compatibility. The docs for FontWeight.lerp say that "The result is clamped to the range w100–w900" (https://api.flutter.dev/flutter/dart-ui/FontWeight/lerp.html)
The wght attribute can potentially range from 1 to 1000. But 100-900 covers most typical use cases.
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.
Consider adding a quick comment as it's not immediately obvious why the clamp is necessary.
Edit: On second thought, that's not necessary as this is commented on the API already.
| /// See [FontVariation.weight] for details. | ||
| /// When using these fonts, applications can specify [FontWeight] instances | ||
| /// constructed using values other than the predefined values. | ||
| class FontWeight { |
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 wonder if this can be an extension type now that it is just an int under the hood?
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.
Ah there are FontWeight subclasses in the wild but since we were using index to communicate the weight I don't think subclassing does anything.
engine/src/flutter/lib/ui/text.dart
Outdated
| /// Some modern fonts allow the weight to be adjusted in arbitrary increments. | ||
| /// See [FontVariation.weight] for details. | ||
| /// When using these fonts, applications can specify [FontWeight] instances | ||
| /// constructed using values other than the predefined values. |
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.
nit: maybe cross ref FontVariation.weight and how it interacts with FontWeight?
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
358c13b to
832d515
Compare
This makes it possible for applications to set a FontWeight and get the expected result for both variable fonts and fonts that provide separate assets for each weight. See flutter#148026
f470ac9 to
a49fef4
Compare
flutter/flutter@4c91098...7cf0dc1 2025-10-28 [email protected] Roll Skia from 602bbd4af8f9 to e4d3d8f31aef (4 revisions) (flutter/flutter#177647) 2025-10-28 [email protected] Fix AppBar Semantics namesRoute for mismatched platforms (flutter/flutter#176694) 2025-10-28 [email protected] Fix Popup menu Semantics label for mismatched platforms (flutter/flutter#177049) 2025-10-28 [email protected] Roll Skia from 8b2d056701df to 602bbd4af8f9 (1 revision) (flutter/flutter#177628) 2025-10-28 [email protected] Roll Skia from 5723f87f8530 to 8b2d056701df (3 revisions) (flutter/flutter#177626) 2025-10-27 [email protected] Roll Skia from 170c11f1ddc5 to 5723f87f8530 (6 revisions) (flutter/flutter#177618) 2025-10-27 [email protected] Enhance DropdownMenuEntry's labelWidget docs (flutter/flutter#177160) 2025-10-27 [email protected] Regenerated lockfiles for New Template Values (flutter/flutter#177617) 2025-10-27 [email protected] Correct editable text and placeholder position in baseline aligned stack (flutter/flutter#177342) 2025-10-27 49699333+dependabot[bot]@users.noreply.github.com Bump actions/upload-artifact from 4 to 5 in the all-github-actions group (flutter/flutter#177620) 2025-10-27 [email protected] add gn flag to optimize builds for size (flutter/flutter#176835) 2025-10-27 [email protected] disable metal for crosscompile from mac to linux (flutter/flutter#176639) 2025-10-27 [email protected] [DDM] enable host builds in the merge queue (flutter/flutter#177446) 2025-10-27 [email protected] Disable vulkan X11 support when building for minimal linux (flutter/flutter#176697) 2025-10-27 [email protected] Roll Skia from 77348c40d101 to 170c11f1ddc5 (6 revisions) (flutter/flutter#177602) 2025-10-27 [email protected] Set the font weight variation axis based on the text style's FontWeight (flutter/flutter#175771) 2025-10-27 [email protected] Fixed `RuntimeEffect` with `ImageFilter.compose` (flutter/flutter#177510) 2025-10-27 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Clean before building when framework headers change (#177512)" (flutter/flutter#177610) 2025-10-27 [email protected] [skia] Disable legacy png encoding/decoding in skp (flutter/flutter#177462) 2025-10-27 [email protected] Clean before building when framework headers change (flutter/flutter#177512) 2025-10-27 [email protected] Roll dartdoc to 9.0.0 (flutter/flutter#177590) 2025-10-27 [email protected] Fix typo in comment about `manifestFile` in `DeepLinkJsonFromManifestTaskHelper.kt` (flutter/flutter#177538) 2025-10-27 [email protected] Fix RoundedSuperellipse crashes for tiny corners (flutter/flutter#177070) 2025-10-27 [email protected] Roll Packages from 53d6138 to bbf96a0 (7 revisions) (flutter/flutter#177588) 2025-10-27 [email protected] Fix missing list indicators in CHANGELOG.md (flutter/flutter#177484) 2025-10-27 [email protected] [ Tool ] Add `Stream.transformWithCallSite` to provide more useful stack traces (flutter/flutter#177470) 2025-10-27 [email protected] Roll Skia from 06243224ecf0 to 77348c40d101 (1 revision) (flutter/flutter#177585) 2025-10-27 [email protected] Add guided error for precompiled cache error (flutter/flutter#177327) 2025-10-27 [email protected] Roll Fuchsia Linux SDK from tKrvmvTOQITL81oOC... to ir6J2isKAYa1jNLyJ... (flutter/flutter#177578) 2025-10-27 [email protected] Roll Skia from 784ed1787bd6 to 06243224ecf0 (1 revision) (flutter/flutter#177575) 2025-10-27 [email protected] Roll Skia from de52b3a7585a to 784ed1787bd6 (5 revisions) (flutter/flutter#177571) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
This adds documentation of the change made by flutter/flutter#175771
…12615) This adds documentation of the change made by flutter/flutter#175771 --------- Co-authored-by: Shams Zakhour <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…ht (flutter#175771) This makes it possible for applications to set a FontWeight and get the expected result for both variable fonts and fonts that provide separate assets for each weight. See flutter#148026
This makes it possible for applications to set a FontWeight and get the expected result for both variable fonts and fonts that provide separate assets for each weight.
See #148026