Skip to content
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

[Cocoa] font-optical-sizing:auto doesn't set the opsz variation axis for all fonts that need it #10354

Conversation

litherum
Copy link
Contributor

@litherum litherum commented Feb 20, 2023

b2e8181

[Cocoa] font-optical-sizing:auto doesn't set the opsz variation axis for all fonts that need it
https://bugs.webkit.org/show_bug.cgi?id=252552
rdar://105662167

Reviewed by Alan Baradlay.

This is a follow-up from 260447@main. That previous patch went almost all the way toward fixing the
problem, but couldn't actually apply optical sizing to all text that requested it. The reason for
this is that the trigger we were using to enable optical sizing, `kCTFontOpticalSizeAttribute`,
enables the `opsz` variation axis, but also enables the `trak` table. Unfortunately, we have a lot
of tests which use fonts that don't support the optical sizing variation axis, but do have a `trak`
table. So, if we just naively started enabling `kCTFontOpticalSizeAttribute` for all text that
requests it, we'll get tons of test failures. It's not really great if we make tons of text on the
web look different, without it being a deliberate decision. So, the previous patch stopped just
short of enabling `kCTFontOpticalSizeAttribute` everywhere.

However, all fonts which *do* support the `opsz` variation axis should have it set automatically,
which is what 260447@main was aiming to address. We have the ability to *just* set the variation
axis without also setting `trak`, so that's what we need to do for fonts that request optical
sizing but for which we can't use `kCTFontOpticalSizeAttribute` because of compatibility.

So, we need 3 buckets:
- Disable optical sizing. This is triggered by font-optical-sizing:none. This maps to telling Core
      Text to use `kCTFontOpticalSizeAttribute`: `none`.
- Enable full optical sizing, including `trak`. This is triggered by
      text-rendering:optimizeLegibility. This maps to telling Core Text to use
      `kCTFontOpticalSizeAttribute`: `auto`.
- Enable optical sizing, but just the `opsz` variation, and not `trak`, for compatibility. This
      is what all situations not captured by the above use. This maps to telling Core Text to use
      `kCTFontVariationAttribute`: {`opsz`: font size}.

This patch implements the third bucket, which wasn't implemented in the previous patch. The third
bucket doesn't actually work correctly on older OSes, so it's guarded behind a OS version check.

https://bugs.webkit.org/show_bug.cgi?id=252592 is about deleting the compatibility third bucket in
favor of the second bucket: actually enabling `trak` for all text that should have optical sizing
enabled.

Just like 260447@main, the tests for this adds a megabyte of fonts (because we have to keep the
whole release of an OFL font together) so I'm not including the test with this PR, and will upload
another PR later with a custom font that I create from scratch for testing this.

* Source/WTF/wtf/PlatformUse.h:
* Source/WebCore/platform/graphics/cocoa/UnrealizedCoreTextFont.cpp:
(WebCore::UnrealizedCoreTextFont::addAttributesForOpticalSizing):
(WebCore::UnrealizedCoreTextFont::applyVariations):
(WebCore::UnrealizedCoreTextFont::modifyFromContext):
(WebCore::UnrealizedCoreTextFont::realize const):
(WebCore::applyVariations): Deleted.
(WebCore::modifyFromContext): Deleted.
* Source/WebCore/platform/graphics/cocoa/UnrealizedCoreTextFont.h:

Canonical link: https://commits.webkit.org/260781@main

47febba

Misc iOS, tvOS & watchOS macOS Linux Windows
βœ… πŸ§ͺ style βœ… πŸ›  ios βœ… πŸ›  mac βœ… πŸ›  wpe βœ… πŸ›  wincairo
βœ… πŸ§ͺ bindings βœ… πŸ›  ios-sim βœ… πŸ›  mac-AS-debug βœ… πŸ›  gtk
βœ… πŸ§ͺ webkitperl βœ… πŸ§ͺ ios-wk2 βœ… πŸ§ͺ api-mac ❌ πŸ§ͺ gtk-wk2
  πŸ§ͺ api-ios βœ… πŸ§ͺ mac-wk1 βœ… πŸ§ͺ api-gtk
βœ… πŸ›  πŸ§ͺ jsc βœ… πŸ›  tv βœ… πŸ§ͺ mac-wk2 βœ… πŸ›  jsc-armv7
βœ… πŸ›  πŸ§ͺ jsc-arm64 βœ… πŸ›  tv-sim βœ… πŸ§ͺ mac-AS-debug-wk2   πŸ§ͺ jsc-armv7-tests
  πŸ›  watch βœ… πŸ§ͺ mac-wk2-stress βœ… πŸ›  jsc-mips
βœ… πŸ›  πŸ§ͺ merge βœ… πŸ›  watch-sim βœ… πŸ§ͺ jsc-mips-tests

@litherum litherum self-assigned this Feb 20, 2023
@litherum litherum added the Text For bugs in text layout and rendering, including international text support. label Feb 20, 2023
@litherum
Copy link
Contributor Author

litherum commented Feb 20, 2023

All these tests pass on a new Mac operating system, so I think we need a version check.

@litherum litherum force-pushed the eng/Cocoa-font-optical-sizingauto-doesnt-set-the-opsz-variation-axis-for-all-fonts-that-need-it branch 2 times, most recently from 91751c1 to 9619f04 Compare February 20, 2023 20:07
@litherum litherum force-pushed the eng/Cocoa-font-optical-sizingauto-doesnt-set-the-opsz-variation-axis-for-all-fonts-that-need-it branch from 9619f04 to 34cbb7d Compare February 20, 2023 22:50
@litherum litherum force-pushed the eng/Cocoa-font-optical-sizingauto-doesnt-set-the-opsz-variation-axis-for-all-fonts-that-need-it branch from 34cbb7d to 8c3872d Compare February 21, 2023 02:58
@@ -391,3 +391,10 @@
// The header says "Before macOS 13.0 and iOS 16.0 this attribute is not accurate"
#define USE_KCTFONTVARIATIONAXESATTRIBUTE 1
#endif

#if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000) \
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update copyright

@litherum
Copy link
Contributor Author

Verified tests pass on Ventura

@litherum
Copy link
Contributor Author

This patch should be disabled on Monterey.

@litherum litherum force-pushed the eng/Cocoa-font-optical-sizingauto-doesnt-set-the-opsz-variation-axis-for-all-fonts-that-need-it branch from 8c3872d to e0eb411 Compare February 23, 2023 06:14
@litherum litherum marked this pull request as ready for review February 23, 2023 06:16
@litherum litherum force-pushed the eng/Cocoa-font-optical-sizingauto-doesnt-set-the-opsz-variation-axis-for-all-fonts-that-need-it branch from e0eb411 to 47febba Compare February 23, 2023 20:52
@litherum litherum added the merge-queue Applied to send a pull request to merge-queue label Feb 24, 2023
…for all fonts that need it

https://bugs.webkit.org/show_bug.cgi?id=252552
rdar://105662167

Reviewed by Alan Baradlay.

This is a follow-up from 260447@main. That previous patch went almost all the way toward fixing the
problem, but couldn't actually apply optical sizing to all text that requested it. The reason for
this is that the trigger we were using to enable optical sizing, `kCTFontOpticalSizeAttribute`,
enables the `opsz` variation axis, but also enables the `trak` table. Unfortunately, we have a lot
of tests which use fonts that don't support the optical sizing variation axis, but do have a `trak`
table. So, if we just naively started enabling `kCTFontOpticalSizeAttribute` for all text that
requests it, we'll get tons of test failures. It's not really great if we make tons of text on the
web look different, without it being a deliberate decision. So, the previous patch stopped just
short of enabling `kCTFontOpticalSizeAttribute` everywhere.

However, all fonts which *do* support the `opsz` variation axis should have it set automatically,
which is what 260447@main was aiming to address. We have the ability to *just* set the variation
axis without also setting `trak`, so that's what we need to do for fonts that request optical
sizing but for which we can't use `kCTFontOpticalSizeAttribute` because of compatibility.

So, we need 3 buckets:
- Disable optical sizing. This is triggered by font-optical-sizing:none. This maps to telling Core
      Text to use `kCTFontOpticalSizeAttribute`: `none`.
- Enable full optical sizing, including `trak`. This is triggered by
      text-rendering:optimizeLegibility. This maps to telling Core Text to use
      `kCTFontOpticalSizeAttribute`: `auto`.
- Enable optical sizing, but just the `opsz` variation, and not `trak`, for compatibility. This
      is what all situations not captured by the above use. This maps to telling Core Text to use
      `kCTFontVariationAttribute`: {`opsz`: font size}.

This patch implements the third bucket, which wasn't implemented in the previous patch. The third
bucket doesn't actually work correctly on older OSes, so it's guarded behind a OS version check.

https://bugs.webkit.org/show_bug.cgi?id=252592 is about deleting the compatibility third bucket in
favor of the second bucket: actually enabling `trak` for all text that should have optical sizing
enabled.

Just like 260447@main, the tests for this adds a megabyte of fonts (because we have to keep the
whole release of an OFL font together) so I'm not including the test with this PR, and will upload
another PR later with a custom font that I create from scratch for testing this.

* Source/WTF/wtf/PlatformUse.h:
* Source/WebCore/platform/graphics/cocoa/UnrealizedCoreTextFont.cpp:
(WebCore::UnrealizedCoreTextFont::addAttributesForOpticalSizing):
(WebCore::UnrealizedCoreTextFont::applyVariations):
(WebCore::UnrealizedCoreTextFont::modifyFromContext):
(WebCore::UnrealizedCoreTextFont::realize const):
(WebCore::applyVariations): Deleted.
(WebCore::modifyFromContext): Deleted.
* Source/WebCore/platform/graphics/cocoa/UnrealizedCoreTextFont.h:

Canonical link: https://commits.webkit.org/260781@main
@webkit-early-warning-system webkit-early-warning-system force-pushed the eng/Cocoa-font-optical-sizingauto-doesnt-set-the-opsz-variation-axis-for-all-fonts-that-need-it branch from 47febba to b2e8181 Compare February 24, 2023 04:29
@webkit-early-warning-system webkit-early-warning-system merged commit b2e8181 into WebKit:main Feb 24, 2023
@webkit-commit-queue
Copy link
Collaborator

Committed 260781@main (b2e8181): https://commits.webkit.org/260781@main

Reviewed commits have been landed. Closing PR #10354 and removing active labels.

@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Feb 24, 2023
@litherum litherum deleted the eng/Cocoa-font-optical-sizingauto-doesnt-set-the-opsz-variation-axis-for-all-fonts-that-need-it branch February 24, 2023 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Text For bugs in text layout and rendering, including international text support.
Projects
None yet
4 participants