Skip to content

Conversation

@jason-simmons
Copy link
Member

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

@github-actions github-actions bot added a: text input Entering text in a text field or keyboard related problems engine flutter/engine related. See also e: labels. platform-web Web applications specifically e: impeller Impeller rendering backend issues and features requests labels Sep 22, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: cupertino flutter/packages/flutter/cupertino repository labels Sep 22, 2025
jason-simmons added a commit to jason-simmons/flutter_packages that referenced this pull request Sep 23, 2025
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.
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Sep 24, 2025
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.
Copy link
Contributor

@eyebrowsoffire eyebrowsoffire left a 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!

@flutter-dashboard
Copy link

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 package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #175771 at sha d411900

@flutter-dashboard flutter-dashboard bot added the will affect goldens Changes to golden files label Oct 1, 2025
skTextStyle.fontStyle = toSkFontStyle(fontWeight, fontStyle);
}

final int weightValue = fontWeight?.value ?? ui.FontWeight.normal.value;
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

@harryterkelsen harryterkelsen Oct 7, 2025

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.

Copy link
Contributor

@harryterkelsen harryterkelsen left a 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

Copy link
Contributor

@LongCatIsLooong LongCatIsLooong left a 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
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Member Author

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))

/// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Create a [FontWeight] object, which can be added to a [TextStyle] to
/// Creates a [FontWeight] object, which can be added to a [TextStyle] to

Copy link
Member Author

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);
Copy link
Contributor

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?

Copy link
Member Author

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),
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member

@loic-sharma loic-sharma Oct 20, 2025

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 {
Copy link
Contributor

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?

Copy link
Contributor

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.

/// 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.
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jason-simmons jason-simmons added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 27, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Oct 27, 2025
Merged via the queue into flutter:master with commit e090117 Oct 27, 2025
187 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 28, 2025
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Oct 28, 2025
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
jason-simmons added a commit to jason-simmons/flutter_website that referenced this pull request Oct 30, 2025
sfshaza2 added a commit to flutter/website that referenced this pull request Oct 30, 2025
…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>
flutter-zl pushed a commit that referenced this pull request Oct 31, 2025
…ht (#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 #148026
reidbaker pushed a commit to AbdeMohlbi/flutter that referenced this pull request Dec 10, 2025
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: text input Entering text in a text field or keyboard related problems e: impeller Impeller rendering backend issues and features requests engine flutter/engine related. See also e: labels. f: cupertino flutter/packages/flutter/cupertino repository framework flutter/packages/flutter repository. See also f: labels. platform-web Web applications specifically will affect goldens Changes to golden files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants