Skip to content

SwiftMathExample: font switcher + LaTeX playground, fix wide-formula clipping#208

Merged
kostub merged 2 commits into
masterfrom
worktree-swift-ex
May 30, 2026
Merged

SwiftMathExample: font switcher + LaTeX playground, fix wide-formula clipping#208
kostub merged 2 commits into
masterfrom
worktree-swift-ex

Conversation

@kostub
Copy link
Copy Markdown
Owner

@kostub kostub commented May 29, 2026

Summary

Brings the Swift example app closer to the ObjC iosMathExample and fixes a layout bug surfaced while doing so.

New: Playground tab

  • A live LaTeX text editor — type any expression and see it render immediately.
  • A font switcher (Latin Modern / TeX Gyre Termes / XITS) via a segmented picker.
  • The selected font is shared state in 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: MTMathUILabel won'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 whole VStack column past the viewport, and the vertical ScrollView centered 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:

  • Both platform representables now implement sizeThatFits to 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.
  • Each Examples card wraps its formula in a horizontal ScrollView, so a genuinely over-wide formula scrolls instead of breaking the layout.
  • .center/.right alignment in the Gallery is preserved (verified on-device).

Testing

  • Verified on the iOS Simulator (iPhone 16): Examples and Gallery tabs render with no clipping; titles fully visible; wide formulas contained/scrollable; Gallery center/right alignment intact.
  • SwiftMathExample (iOS) and SwiftMathExampleMac (macOS) schemes both build cleanly.

🤖 Generated with Claude Code

…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>
Copy link
Copy Markdown

@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 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.

Comment thread SwiftMathExample/MathLabel.swift Outdated
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@kostub kostub merged commit 76a1f9a into master May 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant