SwiftMathExample: font switcher + LaTeX playground, fix wide-formula clipping#208
Merged
Conversation
…ula clipping Add a Playground tab to the Swift example with a live LaTeX text editor and a font switcher (Latin Modern / TeX Gyre Termes / XITS), mirroring the ObjC iosMathExample. The selected font is shared state in ContentView so switching it re-renders the Examples and Gallery tabs too. Also fix a layout bug where formulas and titles were clipped on the left in the Examples tab (only with Latin Modern). MTMathUILabel refuses to be narrower than its intrinsic width, so a single formula wider than the screen (the Rogers–Ramanujan fraction in Latin Modern) stretched the whole column past the viewport, clipping every card. The representables now implement sizeThatFits to fill the offered width without demanding more, and each Examples card wraps its formula in a horizontal ScrollView so an over-wide formula scrolls instead of breaking the layout. Center/right alignment in the Gallery is preserved. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request restructures the SwiftMathExample app by adding a new "Playground" tab for live LaTeX rendering and font selection, sharing the selected font across all tabs. It also introduces horizontal scrolling for wide formulas and implements a custom width policy (sizeThatFits) for math labels on iOS and macOS. The review feedback highlights a critical performance issue in MathFont.font(size:) where instantiating MTFontManager() directly bypasses the internal font cache, causing disk reads on every render; using the shared singleton MTFontManager.fontManager() is recommended to prevent frame drops.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Brings the Swift example app closer to the ObjC
iosMathExampleand fixes a layout bug surfaced while doing so.New: Playground tab
ContentView, so switching it in the Playground also re-renders the Examples and Gallery tabs (matching the global font switch in the ObjC example).Fix: left-clipping of formulas and titles in the Examples tab
Previously, formulas and their plain-text titles were clipped on the left — but only with Latin Modern.
Root cause:
MTMathUILabelwon't size narrower than its intrinsic (natural formula) width. The Rogers–Ramanujan continued fraction renders ~405pt wide in Latin Modern — wider than the screen — so that single card stretched the wholeVStackcolumn past the viewport, and the verticalScrollViewcentered the over-wide column, pushing every card's left edge (titles included) off-screen. In TeX Gyre the same formula is narrower than the screen, so nothing overflowed.Fix:
sizeThatFitsto fill the offered width without demanding more (so a label never forces its container wider than the viewport), falling back to the natural width only when offered unbounded space. This also fixes the same latent overflow in the Gallery tab.ScrollView, so a genuinely over-wide formula scrolls instead of breaking the layout..center/.rightalignment in the Gallery is preserved (verified on-device).Testing
SwiftMathExample(iOS) andSwiftMathExampleMac(macOS) schemes both build cleanly.🤖 Generated with Claude Code