Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/core/ui/styling/font-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export abstract class FontBase implements Font {
Object.assign(clone, this);
} else {
clone.isDirty = true;
// clear android cached typeface. need to find a better way
clone['_typeface'] = null;
}
// clear android cached typeface (Object.assign copies it to clones too)
// so it is recreated from the updated font properties
clone['_typeface'] = null;
return clone;
}

Expand Down
39 changes: 39 additions & 0 deletions packages/core/ui/styling/font.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Font } from './font';

describe('Font', () => {
describe('cloneOrDirty', () => {
// simulate the android cached typeface set by Font.getAndroidTypeface()
const fontWithCachedTypeface = () => {
const font = new Font('serif', 12, 'normal', 'normal');
font['_typeface'] = { cached: true };
return font;
};

it('clones do not carry the cached typeface', () => {
const font = fontWithCachedTypeface();
const clones = [font.withFontWeight('700'), font.withFontFamily('monospace'), font.withFontStyle('italic'), font.withFontSize(20), font.withFontScale(2), font.withFontVariationSettings([{ axis: 'wght', value: 700 }])];
for (const clone of clones) {
expect(clone).not.toBe(font);
expect(clone['_typeface']).toBeNull();
}
// source font keeps its own cache
expect(font['_typeface']).toEqual({ cached: true });
});

it('clones keep the font descriptor and apply the new property', () => {
const clone = fontWithCachedTypeface().withFontWeight('700');
expect(clone.fontFamily).toBe('serif');
expect(clone.fontSize).toBe(12);
expect(clone.fontStyle).toBe('normal');
expect(clone.fontWeight).toBe('700');
});

it('dirtied (non cloned) font clears the cached typeface', () => {
const font = fontWithCachedTypeface();
const result = font.withFontWeight('700', false);
expect(result).toBe(font);
expect(result.isDirty).toBe(true);
expect(result['_typeface']).toBeNull();
});
});
});
Loading