Skip to content

Commit 830aa88

Browse files
authored
Merge pull request #8 from Akylas/fix/font-clone-typeface
fix(android): don't copy cached typeface when cloning a Font
2 parents 7dbf4c6 + 8db5b6e commit 830aa88

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎packages/core/ui/styling/font-common.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ export abstract class FontBase implements Font {
2020
Object.assign(clone, this);
2121
} else {
2222
clone.isDirty = true;
23-
// clear android cached typeface. need to find a better way
24-
clone['_typeface'] = null;
2523
}
24+
// clear android cached typeface (Object.assign copies it to clones too)
25+
// so it is recreated from the updated font properties
26+
clone['_typeface'] = null;
2627
return clone;
2728
}
2829

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Font } from './font';
2+
3+
describe('Font', () => {
4+
describe('cloneOrDirty', () => {
5+
// simulate the android cached typeface set by Font.getAndroidTypeface()
6+
const fontWithCachedTypeface = () => {
7+
const font = new Font('serif', 12, 'normal', 'normal');
8+
font['_typeface'] = { cached: true };
9+
return font;
10+
};
11+
12+
it('clones do not carry the cached typeface', () => {
13+
const font = fontWithCachedTypeface();
14+
const clones = [font.withFontWeight('700'), font.withFontFamily('monospace'), font.withFontStyle('italic'), font.withFontSize(20), font.withFontScale(2), font.withFontVariationSettings([{ axis: 'wght', value: 700 }])];
15+
for (const clone of clones) {
16+
expect(clone).not.toBe(font);
17+
expect(clone['_typeface']).toBeNull();
18+
}
19+
// source font keeps its own cache
20+
expect(font['_typeface']).toEqual({ cached: true });
21+
});
22+
23+
it('clones keep the font descriptor and apply the new property', () => {
24+
const clone = fontWithCachedTypeface().withFontWeight('700');
25+
expect(clone.fontFamily).toBe('serif');
26+
expect(clone.fontSize).toBe(12);
27+
expect(clone.fontStyle).toBe('normal');
28+
expect(clone.fontWeight).toBe('700');
29+
});
30+
31+
it('dirtied (non cloned) font clears the cached typeface', () => {
32+
const font = fontWithCachedTypeface();
33+
const result = font.withFontWeight('700', false);
34+
expect(result).toBe(font);
35+
expect(result.isDirty).toBe(true);
36+
expect(result['_typeface']).toBeNull();
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)