Skip to content

[iOS] Center FontImageSource glyphs by their outlines - #40522

Merged
kubaflo merged 4 commits into
dotnet:inflight/11.0.1xx-sr3from
MykhailoDav:fix-40494-ios26-fontimage-toolbar-offset
Nov 23, 2026
Merged

kubaflo merged 4 commits into
dotnet:inflight/11.0.1xx-sr3from
MykhailoDav:fix-40494-ios26-fontimage-toolbar-offset

Conversation

@MykhailoDav

@MykhailoDav MykhailoDav commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Description of Change

On iOS 26+, #39230 centers a FontImageSource glyph by CTLine.GetBounds(CTLineBoundsOptions.UseGlyphPathBounds). Despite the name, that returns the glyph bounding box stored in the font, not the bounds of the outline. For most fonts the two are the same. Fluent System Icons stores loose boxes anchored at (0,0) for almost all of its glyphs, so centering the stored box moves the visible icon. For the issue's filter glyph (U+F408) at 30 pt, the stored box's center is 11.25 pt from the bottom and the outline's is 15.27 pt, so the icon is drawn about 4 pt above the center of the image.

UIKit isn't involved: in the toolbar, the 30×30 image view and the glass button have the same center.

The image is now centered on the union of the glyph outlines (CTFont.GetPathForGlyph for each glyph in the line). Glyphs without an outline, such as emoji, which are bitmaps, add the box the font stores for them instead. This keeps #39230's approach and only changes how the ink is measured. Glyphs whose stored box matches the outline, such as the OpenSans ‹ from #31936, don't move.

Measured on an iPhone 17 simulator (iOS 26.5), offset of the icon's ink center from the button center:

Before #39230 Current net11 With this PR
Fluent U+F408, ToolbarItem (#40494) -0.27 pt (computed) -3.83 pt (above) +0.17 pt
OpenSans U+2039, back button (#31936) +3.76 pt (below, computed) 0.00 pt 0.00 pt

The glyphs in the ios-26 snapshots that #39230 updated (FA U+F015, OpenSans U+0489, system "+") move by 0.03 pt or less, measured with CoreText on macOS, not on iOS.

Tests

FontImageSourceServiceTests.GlyphOutlineIsCenteredWhenStoredGlyphBoundsDiffer (Core device tests, iOS) renders an Ionicons and a FontAwesome glyph whose stored boxes differ from their outlines. Both fonts are already embedded in the test app. On iOS 26+ it checks that the ink is within 1 px of the image center.

ImageSource category, iOS 26.5 Without the fix With the fix
Result 75 passed, 2 failed, 2 skipped (Assert.InRange() Failure, off by 4.5 px and 3.5 px) 77 passed, 2 skipped

The existing #39230 tests (GlyphIsCenteredWithoutChangingFontMetrics, RenderingModeIsPreserved) still pass.

GlyphWithoutOutlineIsNotClipped (added after review) renders "X😀" and checks that the ink doesn't touch the image edges. The first version of this PR measured outlines only, so it centered the "X" and pushed the emoji past the right edge: the test fails on that commit (ink at 169–515 px in a 515 px wide image). With the current fix the ImageSource category has 78 passed, 2 skipped.

Before / after (sandbox app built from source, iPhone 17 simulator, iOS 26.5): a pushed page whose back button uses the #31936 glyph and whose ToolbarItem uses the Fluent filter glyph. The red line is the button center, the green line the icon center.

Before After
before after

Full clips: before.mp4 · after.mp4

Not checked: Mac Catalyst, iOS 27 (my iOS 27 runtime can't run the test app), and the Issue31936 UI test under Appium (I measured its glyph with the same method instead). An emoji is centered by its stored box, which includes the transparent margin of the bitmap.

Issues Fixed

Fixes #40494

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40522

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40522"

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Nov 23, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @MykhailoDav! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@azure-pipelines

This comment has been minimized.

@kubaflo

kubaflo commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

/review expert-review-with-gate

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Warning

Your /review gate couldn't start due to a transient error triggering the pipeline. The command was left unacknowledged so scheduled recovery can retry it automatically; you can also comment /review gate again in a minute.

@github-actions github-actions Bot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 23, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Expert Review — 1 finding

Reviewed commit 70aa784.

See inline comments for details.

}

// Bitmap glyphs, such as emoji, have no outlines.
return outlines.IsEmpty ? line.GetBounds(CTLineBoundsOptions.UseGlyphPathBounds) : outlines.PathBoundingBox;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[major] Logic and Correctness - The bitmap fallback only applies when the entire line has no outlines. On iOS 26+, a FontImageSource with Glyph = "X\U0001F600" and the default font contributes the X outline but no outline for the fallback emoji, so this returns bounds for X alone. The caller centers X in an image sized for the whole string, then draws the entire CTLine, shifting the trailing emoji beyond the renderer's right edge and clipping it. Previously the complete line bounds were used. Include non-outline glyph bounds in the union, or preserve whole-line bounds for mixed bitmap/outline strings. Add a mixed-run regression case checking complete-ink centering and no clipping; the existing and new outline-only fixtures cannot detect this regression.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, that was a regression for mixed strings. Glyphs without an outline now add the box the font stores for them (65f45cb), so "X😀" is centered on both glyphs and the emoji stays inside the image. Spaces have no outline either, but their stored box is empty, so they're skipped. For an emoji on its own, the stored box is the same rect CTLine.GetBounds returned before (checked with CoreText on macOS), so that case doesn't change.

GlyphWithoutOutlineIsNotClipped (bd0e630) renders "X😀" and checks that the ink doesn't touch the image edges. On iOS 26.5 it fails on the previous commit (The ink {{169, 43}, {346, 274}} must not touch the edges of the {515, 359} image.) and passes with the fix. ImageSource category: 78 passed, 2 skipped.

@kubaflo kubaflo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you please check the ai's suggestions?

@kubaflo

kubaflo commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

/review expert-review-with-gate

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Warning

Your /review gate couldn't start due to a transient error triggering the pipeline. The command was left unacknowledged so scheduled recovery can retry it automatically; you can also comment /review gate again in a minute.

@github-actions github-actions Bot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 23, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Expert Review — 0 findings

Reviewed commit 65f45cb.

@kubaflo
kubaflo changed the base branch from net11 to inflight/11.0.1xx-sr3 November 23, 2026 19:11
@kubaflo
kubaflo merged commit f3843f5 into dotnet:inflight/11.0.1xx-sr3 Nov 23, 2026
29 of 38 checks passed
kubaflo added a commit that referenced this pull request Nov 23, 2026
### Description of Change

On iOS 26+, #39230 centers a `FontImageSource` glyph by
`CTLine.GetBounds(CTLineBoundsOptions.UseGlyphPathBounds)`. Despite the
name, that returns the glyph bounding box stored in the font, not the
bounds of the outline. For most fonts the two are the same. Fluent
System Icons stores loose boxes anchored at (0,0) for almost all of its
glyphs, so centering the stored box moves the visible icon. For the
issue's filter glyph (U+F408) at 30 pt, the stored box's center is 11.25
pt from the bottom and the outline's is 15.27 pt, so the icon is drawn
about 4 pt above the center of the image.

UIKit isn't involved: in the toolbar, the 30×30 image view and the glass
button have the same center.

The image is now centered on the union of the glyph outlines
(`CTFont.GetPathForGlyph` for each glyph in the line). Glyphs without an
outline, such as emoji, which are bitmaps, add the box the font stores
for them instead. This keeps #39230's approach and only changes how the
ink is measured. Glyphs whose stored box matches the outline, such as
the OpenSans `‹` from #31936, don't move.

Measured on an iPhone 17 simulator (iOS 26.5), offset of the icon's ink
center from the button center:

| | Before #39230 | Current `net11` | With this PR |
|---|---|---|---|
| Fluent U+F408, ToolbarItem (#40494) | -0.27 pt (computed) | -3.83 pt
(above) | +0.17 pt |
| OpenSans U+2039, back button (#31936) | +3.76 pt (below, computed) |
0.00 pt | 0.00 pt |

The glyphs in the ios-26 snapshots that #39230 updated (FA U+F015,
OpenSans U+0489, system "+") move by 0.03 pt or less, measured with
CoreText on macOS, not on iOS.

### Tests


`FontImageSourceServiceTests.GlyphOutlineIsCenteredWhenStoredGlyphBoundsDiffer`
(Core device tests, iOS) renders an Ionicons and a FontAwesome glyph
whose stored boxes differ from their outlines. Both fonts are already
embedded in the test app. On iOS 26+ it checks that the ink is within 1
px of the image center.

| ImageSource category, iOS 26.5 | Without the fix | With the fix |
|---|---|---|
| Result | 75 passed, 2 failed, 2 skipped (`Assert.InRange() Failure`,
off by 4.5 px and 3.5 px) | 77 passed, 2 skipped |

The existing #39230 tests (`GlyphIsCenteredWithoutChangingFontMetrics`,
`RenderingModeIsPreserved`) still pass.

`GlyphWithoutOutlineIsNotClipped` (added after review) renders "X😀" and
checks that the ink doesn't touch the image edges. The first version of
this PR measured outlines only, so it centered the "X" and pushed the
emoji past the right edge: the test fails on that commit (ink at 169–515
px in a 515 px wide image). With the current fix the ImageSource
category has 78 passed, 2 skipped.

**Before / after** (sandbox app built from source, iPhone 17 simulator,
iOS 26.5): a pushed page whose back button uses the #31936 glyph and
whose ToolbarItem uses the Fluent filter glyph. The red line is the
button center, the green line the icon center.

| Before | After |
|---|---|
|
![before](https://raw.githubusercontent.com/MykhailoDav/maui/media-pr-2026-11-12/40494-before.png)
|
![after](https://raw.githubusercontent.com/MykhailoDav/maui/media-pr-2026-11-12/40494-after.png)
|

Full clips:
[before.mp4](https://raw.githubusercontent.com/MykhailoDav/maui/media-pr-2026-11-12/40494-before.mp4)
·
[after.mp4](https://raw.githubusercontent.com/MykhailoDav/maui/media-pr-2026-11-12/40494-after.mp4)

Not checked: Mac Catalyst, iOS 27 (my iOS 27 runtime can't run the test
app), and the Issue31936 UI test under Appium (I measured its glyph with
the same method instead). An emoji is centered by its stored box, which
includes the transparent margin of the bitmap.

### Issues Fixed

Fixes #40494

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-image Image loading, sources, caching community ✨ Community Contribution platform/ios platform/macos macOS / Mac Catalyst

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS 26+] FontImageSource toolbar icon is drawn 3.8 pt above center since 10.0.120 (regression from #39230)

2 participants