Skip to content

Commit 3745604

Browse files
committed
Register builtin fonts into fontstore
1 parent a37d927 commit 3745604

4 files changed

Lines changed: 223 additions & 26 deletions

File tree

packages/font/src/font.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FontSource {
4949
this.fontStyle = fontStyle || 'normal';
5050
this.fontWeight = fontWeight || 400;
5151

52-
this.data = null;
52+
this.data = typeof src === 'object' && src.builtin ? src.builtin : null;
5353
this.options = options;
5454
this.loadResultPromise = null;
5555
}
@@ -75,6 +75,10 @@ class FontSource {
7575
}
7676

7777
async load() {
78+
if (typeof this.src === 'object' && this.src.builtin) {
79+
return Promise.resolve();
80+
}
81+
7882
if (this.loadResultPromise === null) {
7983
this.loadResultPromise = this._load();
8084
}

packages/font/src/index.js

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,67 @@
11
import font from './font';
2-
import standard from './standard';
2+
3+
const CourierFont = font.create('Courier');
4+
CourierFont.register({
5+
src: { builtin: 'Courier' },
6+
});
7+
CourierFont.register({
8+
fontStyle: 'oblique',
9+
src: { builtin: 'Courier-Oblique' },
10+
});
11+
CourierFont.register({
12+
fontWeight: 700,
13+
src: { builtin: 'Courier-Bold' },
14+
});
15+
CourierFont.register({
16+
fontWeight: 700,
17+
fontStyle: 'oblique',
18+
src: { builtin: 'Courier-BoldOblique' },
19+
});
20+
21+
const HelveticaFont = font.create('Helvetica');
22+
HelveticaFont.register({
23+
src: { builtin: 'Helvetica' },
24+
});
25+
HelveticaFont.register({
26+
fontStyle: 'oblique',
27+
src: { builtin: 'Helvetica-Oblique' },
28+
});
29+
HelveticaFont.register({
30+
fontWeight: 700,
31+
src: { builtin: 'Helvetica-Bold' },
32+
});
33+
HelveticaFont.register({
34+
fontWeight: 700,
35+
fontStyle: 'oblique',
36+
src: { builtin: 'Helvetica-BoldOblique' },
37+
});
38+
39+
const TimesRomanFont = font.create('Times-Roman');
40+
TimesRomanFont.register({
41+
src: { builtin: 'Times-Roman' },
42+
});
43+
TimesRomanFont.register({
44+
fontStyle: 'italic',
45+
src: { builtin: 'Times-Italic' },
46+
});
47+
TimesRomanFont.register({
48+
fontWeight: 700,
49+
src: { builtin: 'Times-Bold' },
50+
});
51+
TimesRomanFont.register({
52+
fontWeight: 700,
53+
fontStyle: 'italic',
54+
src: { builtin: 'Times-BoldItalic' },
55+
});
56+
57+
export const builtinFonts = {
58+
Courier: CourierFont,
59+
Helvetica: HelveticaFont,
60+
'Times-Roman': TimesRomanFont,
61+
};
362

463
function FontStore() {
5-
let fonts = {};
64+
let fonts = { ...builtinFonts };
665

766
let emojiSource = null;
867

@@ -35,9 +94,6 @@ function FontStore() {
3594

3695
this.getFont = descriptor => {
3796
const { fontFamily } = descriptor;
38-
const isStandard = standard.includes(fontFamily);
39-
40-
if (isStandard) return null;
4197

4298
if (!fonts[fontFamily]) {
4399
throw new Error(
@@ -49,11 +105,6 @@ function FontStore() {
49105
};
50106

51107
this.load = async descriptor => {
52-
const { fontFamily } = descriptor;
53-
const isStandard = standard.includes(fontFamily);
54-
55-
if (isStandard) return;
56-
57108
const f = this.getFont(descriptor);
58109

59110
// We cache the font to avoid fetching it many times
@@ -70,7 +121,7 @@ function FontStore() {
70121
};
71122

72123
this.clear = () => {
73-
fonts = {};
124+
fonts = { ...builtinFonts };
74125
};
75126

76127
this.getRegisteredFonts = () => fonts;

packages/font/src/standard.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import FontStore from '../src';
2+
3+
describe('fontStore default fonts', () => {
4+
test('should return builtin Courier', () => {
5+
const fontStore = new FontStore();
6+
7+
expect(
8+
fontStore.getFont({
9+
fontFamily: 'Courier',
10+
fontWeight: 400,
11+
fontStyle: 'normal',
12+
}).data,
13+
).toBe('Courier');
14+
expect(
15+
fontStore.getFont({
16+
fontFamily: 'Courier',
17+
fontWeight: 400,
18+
fontStyle: 'oblique',
19+
}).data,
20+
).toBe('Courier-Oblique');
21+
expect(
22+
fontStore.getFont({
23+
fontFamily: 'Courier',
24+
fontWeight: 700,
25+
fontStyle: 'normal',
26+
}).data,
27+
).toBe('Courier-Bold');
28+
expect(
29+
fontStore.getFont({
30+
fontFamily: 'Courier',
31+
fontWeight: 700,
32+
fontStyle: 'oblique',
33+
}).data,
34+
).toBe('Courier-BoldOblique');
35+
});
36+
test('should return builtin Helvetica', () => {
37+
const fontStore = new FontStore();
38+
39+
expect(
40+
fontStore.getFont({
41+
fontFamily: 'Helvetica',
42+
fontWeight: 400,
43+
fontStyle: 'normal',
44+
}).data,
45+
).toBe('Helvetica');
46+
expect(
47+
fontStore.getFont({
48+
fontFamily: 'Helvetica',
49+
fontWeight: 400,
50+
fontStyle: 'oblique',
51+
}).data,
52+
).toBe('Helvetica-Oblique');
53+
expect(
54+
fontStore.getFont({
55+
fontFamily: 'Helvetica',
56+
fontWeight: 700,
57+
fontStyle: 'normal',
58+
}).data,
59+
).toBe('Helvetica-Bold');
60+
expect(
61+
fontStore.getFont({
62+
fontFamily: 'Helvetica',
63+
fontWeight: 700,
64+
fontStyle: 'oblique',
65+
}).data,
66+
).toBe('Helvetica-BoldOblique');
67+
});
68+
test('should return builtin Times-Roman', () => {
69+
const fontStore = new FontStore();
70+
71+
expect(
72+
fontStore.getFont({
73+
fontFamily: 'Times-Roman',
74+
fontWeight: 400,
75+
fontStyle: 'normal',
76+
}).data,
77+
).toBe('Times-Roman');
78+
expect(
79+
fontStore.getFont({
80+
fontFamily: 'Times-Roman',
81+
fontWeight: 400,
82+
fontStyle: 'italic',
83+
}).data,
84+
).toBe('Times-Italic');
85+
expect(
86+
fontStore.getFont({
87+
fontFamily: 'Times-Roman',
88+
fontWeight: 700,
89+
fontStyle: 'normal',
90+
}).data,
91+
).toBe('Times-Bold');
92+
expect(
93+
fontStore.getFont({
94+
fontFamily: 'Times-Roman',
95+
fontWeight: 700,
96+
fontStyle: 'italic',
97+
}).data,
98+
).toBe('Times-BoldItalic');
99+
});
100+
101+
test('should return builtin fonts after reset', () => {
102+
const fontStore = new FontStore();
103+
104+
fontStore.reset();
105+
106+
expect(
107+
fontStore.getFont({
108+
fontFamily: 'Courier',
109+
fontWeight: 400,
110+
fontStyle: 'normal',
111+
}).data,
112+
).toBe('Courier');
113+
expect(
114+
fontStore.getFont({
115+
fontFamily: 'Helvetica',
116+
fontWeight: 400,
117+
fontStyle: 'normal',
118+
}).data,
119+
).toBe('Helvetica');
120+
expect(
121+
fontStore.getFont({
122+
fontFamily: 'Times-Roman',
123+
fontWeight: 400,
124+
fontStyle: 'normal',
125+
}).data,
126+
).toBe('Times-Roman');
127+
});
128+
129+
test('should return builtin fonts after clear', () => {
130+
const fontStore = new FontStore();
131+
132+
fontStore.clear();
133+
134+
expect(
135+
fontStore.getFont({
136+
fontFamily: 'Courier',
137+
fontWeight: 400,
138+
fontStyle: 'normal',
139+
}).data,
140+
).toBe('Courier');
141+
expect(
142+
fontStore.getFont({
143+
fontFamily: 'Helvetica',
144+
fontWeight: 400,
145+
fontStyle: 'normal',
146+
}).data,
147+
).toBe('Helvetica');
148+
expect(
149+
fontStore.getFont({
150+
fontFamily: 'Times-Roman',
151+
fontWeight: 400,
152+
fontStyle: 'normal',
153+
}).data,
154+
).toBe('Times-Roman');
155+
});
156+
});

0 commit comments

Comments
 (0)