Skip to content

fix(android): don't copy cached typeface when cloning a Font - #8

Merged
farfromrefug merged 1 commit into
mainfrom
fix/font-clone-typeface
Oct 8, 2026
Merged

farfromrefug merged 1 commit into
mainfrom
fix/font-clone-typeface

Conversation

@farfromrefug

Copy link
Copy Markdown

Problem

FontBase.cloneOrDirty() clones with Object.assign(clone, this), which also copies the Android cached _typeface set by Font.getAndroidTypeface(). Only the non-clone (dirty) branch cleared it.

So withFontWeight / withFontFamily / withFontStyle / withFontSize / withFontScale / withFontVariationSettings returned a clone with the new property but the old cached typeface, and getAndroidTypeface() kept returning it (it only recreates when !this._typeface).

Seen in oss-weather: @nativescript-community/ui-canvas Paint.setFontWeight() calls font.withFontWeight(weight). Once the paint had drawn, weight changes were ignored (bold text rendered regular).

Fix

Reset _typeface on the returned font in both branches, so clones rebuild their typeface from their own properties.

iOS needs no change: Font there has no per-instance cache; getUIFont() goes through the global uiFontCache keyed by the font descriptor.

Tests

Added packages/core/ui/styling/font.spec.ts:

  • every with* clone has a null _typeface; source font keeps its own
  • clone keeps descriptor fields and applies the new property
  • non-clone path (forceClone = false) still marks dirty and clears _typeface

The first test fails without the fix (expected { cached: true } to be null).

Note: run locally with vitest and a temporary config (same test settings and ~ alias) because @nx/vite isn't installed in that checkout; the standard nx test config was not run.

Follow-up

Once released, oss-weather can drop app/helpers/fontClone.ts and the fixFontCloneTypeface() call in app/main.ts.

🤖 Generated with Claude Code

FontBase.cloneOrDirty() used Object.assign to copy the source font into
the clone, which also copied the Android cached `_typeface`. Clones
returned by withFontWeight/withFontFamily/withFontStyle/withFontSize/...
then kept returning the stale typeface from getAndroidTypeface(), so the
new property was ignored once the source font had been used.

Reset `_typeface` for clones too, not only for dirtied fonts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@farfromrefug
farfromrefug merged commit 830aa88 into main Oct 8, 2026
1 of 3 checks 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