@flighthq/font 0.3.0-next.1315.1d4eb64 → 0.3.0-next.1392.f0bd0cc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_fontFaceLoad.d.ts +6 -0
- package/dist/_fontFaceLoad.d.ts.map +1 -0
- package/dist/_fontFaceLoad.js +28 -0
- package/dist/_fontFaceLoad.js.map +1 -0
- package/dist/fontFrom.d.ts +13 -0
- package/dist/fontFrom.d.ts.map +1 -1
- package/dist/fontFrom.js +18 -18
- package/dist/fontFrom.js.map +1 -1
- package/dist/fontResourceFrom.d.ts +16 -0
- package/dist/fontResourceFrom.d.ts.map +1 -1
- package/dist/fontResourceFrom.js +21 -23
- package/dist/fontResourceFrom.js.map +1 -1
- package/package.json +3 -3
- package/src/_fontFaceLoad.test.ts +98 -0
- package/src/fontResourceFrom.test.ts +2 -2
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { FontUrl } from '@flighthq/types/contract';
|
|
2
|
+
export declare function _loadFontFaceFromBytes(family: string, bytes: Uint8Array): Promise<FontFace>;
|
|
3
|
+
export declare function _loadFontFaceFromUrl(family: string, url: string): Promise<FontFace>;
|
|
4
|
+
export declare function _loadFontFaceFromUrls(family: string, sources: readonly FontUrl[]): Promise<FontFace>;
|
|
5
|
+
export declare function _loadFontFacesFromName(family: string): Promise<FontFace[]>;
|
|
6
|
+
//# sourceMappingURL=_fontFaceLoad.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_fontFaceLoad.d.ts","sourceRoot":"","sources":["../src/_fontFaceLoad.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AAKxD,wBAAsB,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,CAGjG;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAEnF;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,CAQpG;AAED,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CAE1E"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { inferFontFormatFromUrl } from './fontFormat';
|
|
2
|
+
import { getFontShorthand } from './fontShorthand';
|
|
3
|
+
export async function _loadFontFaceFromBytes(family, bytes) {
|
|
4
|
+
const source = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
5
|
+
return loadAndRegisterFontFace(family, source);
|
|
6
|
+
}
|
|
7
|
+
export function _loadFontFaceFromUrl(family, url) {
|
|
8
|
+
return loadAndRegisterFontFace(family, `url(${url})`);
|
|
9
|
+
}
|
|
10
|
+
export function _loadFontFaceFromUrls(family, sources) {
|
|
11
|
+
const source = sources
|
|
12
|
+
.map(({ url, format }) => {
|
|
13
|
+
const resolvedFormat = format ?? inferFontFormatFromUrl(url);
|
|
14
|
+
return resolvedFormat !== null ? `url(${url}) format('${resolvedFormat}')` : `url(${url})`;
|
|
15
|
+
})
|
|
16
|
+
.join(', ');
|
|
17
|
+
return loadAndRegisterFontFace(family, source);
|
|
18
|
+
}
|
|
19
|
+
export function _loadFontFacesFromName(family) {
|
|
20
|
+
return document.fonts.load(getFontShorthand(family));
|
|
21
|
+
}
|
|
22
|
+
async function loadAndRegisterFontFace(family, source) {
|
|
23
|
+
const face = new FontFace(family, source);
|
|
24
|
+
await face.load();
|
|
25
|
+
document.fonts.add(face);
|
|
26
|
+
return face;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=_fontFaceLoad.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_fontFaceLoad.js","sourceRoot":"","sources":["../src/_fontFaceLoad.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEnD,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,MAAc,EAAE,KAAiB;IAC5E,MAAM,MAAM,GAAI,KAAK,CAAC,MAAsB,CAAC,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;IAC1G,OAAO,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AACjD,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,MAAc,EAAE,GAAW;IAC9D,OAAO,uBAAuB,CAAC,MAAM,EAAE,OAAO,GAAG,GAAG,CAAC,CAAC;AACxD,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,MAAc,EAAE,OAA2B;IAC/E,MAAM,MAAM,GAAG,OAAO;SACnB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE;QACvB,MAAM,cAAc,GAAG,MAAM,IAAI,sBAAsB,CAAC,GAAG,CAAC,CAAC;QAC7D,OAAO,cAAc,KAAK,IAAI,CAAC,CAAC,CAAC,OAAO,GAAG,aAAa,cAAc,IAAI,CAAC,CAAC,CAAC,OAAO,GAAG,GAAG,CAAC;IAC7F,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;IACd,OAAO,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AACjD,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,MAAc;IACnD,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC;AACvD,CAAC;AAED,KAAK,UAAU,uBAAuB,CAAC,MAAc,EAAE,MAA4B;IACjF,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1C,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;IAClB,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACzB,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/fontFrom.d.ts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import type { Font, FontUrl } from '@flighthq/types/contract';
|
|
2
|
+
/**
|
|
3
|
+
* Loads a font face from bytes and returns its family handle. If loading rejects, the face is not
|
|
4
|
+
* registered and no `Font` is returned.
|
|
5
|
+
*/
|
|
2
6
|
export declare function loadFontFromBytes(bytes: Uint8Array, family: string): Promise<Font>;
|
|
7
|
+
/** Looks up a registered family and returns its handle. If the lookup rejects, no `Font` is returned. */
|
|
3
8
|
export declare function loadFontFromName(name: string): Promise<Font>;
|
|
9
|
+
/**
|
|
10
|
+
* Loads a font face from one URL and returns its family handle. If loading rejects, the face is not
|
|
11
|
+
* registered and no `Font` is returned.
|
|
12
|
+
*/
|
|
4
13
|
export declare function loadFontFromUrl(url: string, family: string): Promise<Font>;
|
|
14
|
+
/**
|
|
15
|
+
* Loads a font face from fallback URLs and returns its family handle. If loading rejects, the face is
|
|
16
|
+
* not registered and no `Font` is returned.
|
|
17
|
+
*/
|
|
5
18
|
export declare function loadFontFromUrls(sources: FontUrl[], family: string): Promise<Font>;
|
|
6
19
|
//# sourceMappingURL=fontFrom.d.ts.map
|
package/dist/fontFrom.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fontFrom.d.ts","sourceRoot":"","sources":["../src/fontFrom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"fontFrom.d.ts","sourceRoot":"","sources":["../src/fontFrom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AAU9D;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGxF;AAED,yGAAyG;AACzG,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGlE;AAED;;;GAGG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGhF;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGxF"}
|
package/dist/fontFrom.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
+
import { _loadFontFaceFromBytes, _loadFontFaceFromUrl, _loadFontFaceFromUrls, _loadFontFacesFromName, } from './_fontFaceLoad';
|
|
1
2
|
import { createFont } from './font';
|
|
2
|
-
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Loads a font face from bytes and returns its family handle. If loading rejects, the face is not
|
|
5
|
+
* registered and no `Font` is returned.
|
|
6
|
+
*/
|
|
4
7
|
export async function loadFontFromBytes(bytes, family) {
|
|
5
|
-
|
|
6
|
-
await face.load();
|
|
7
|
-
document.fonts.add(face);
|
|
8
|
+
await _loadFontFaceFromBytes(family, bytes);
|
|
8
9
|
return createFont(family);
|
|
9
10
|
}
|
|
11
|
+
/** Looks up a registered family and returns its handle. If the lookup rejects, no `Font` is returned. */
|
|
10
12
|
export async function loadFontFromName(name) {
|
|
11
|
-
await
|
|
13
|
+
await _loadFontFacesFromName(name);
|
|
12
14
|
return createFont(name);
|
|
13
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Loads a font face from one URL and returns its family handle. If loading rejects, the face is not
|
|
18
|
+
* registered and no `Font` is returned.
|
|
19
|
+
*/
|
|
14
20
|
export async function loadFontFromUrl(url, family) {
|
|
15
|
-
|
|
16
|
-
await face.load();
|
|
17
|
-
document.fonts.add(face);
|
|
21
|
+
await _loadFontFaceFromUrl(family, url);
|
|
18
22
|
return createFont(family);
|
|
19
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Loads a font face from fallback URLs and returns its family handle. If loading rejects, the face is
|
|
26
|
+
* not registered and no `Font` is returned.
|
|
27
|
+
*/
|
|
20
28
|
export async function loadFontFromUrls(sources, family) {
|
|
21
|
-
|
|
22
|
-
.map(({ url, format }) => {
|
|
23
|
-
const fmt = format ?? inferFontFormatFromUrl(url);
|
|
24
|
-
return fmt !== null ? `url(${url}) format('${fmt}')` : `url(${url})`;
|
|
25
|
-
})
|
|
26
|
-
.join(', ');
|
|
27
|
-
const face = new FontFace(family, src);
|
|
28
|
-
await face.load();
|
|
29
|
-
document.fonts.add(face);
|
|
29
|
+
await _loadFontFaceFromUrls(family, sources);
|
|
30
30
|
return createFont(family);
|
|
31
31
|
}
|
|
32
32
|
//# sourceMappingURL=fontFrom.js.map
|
package/dist/fontFrom.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fontFrom.js","sourceRoot":"","sources":["../src/fontFrom.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"fontFrom.js","sourceRoot":"","sources":["../src/fontFrom.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,KAAiB,EAAE,MAAc;IACvE,MAAM,sBAAsB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC5C,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC;AAC5B,CAAC;AAED,yGAAyG;AACzG,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,IAAY;IACjD,MAAM,sBAAsB,CAAC,IAAI,CAAC,CAAC;IACnC,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAW,EAAE,MAAc;IAC/D,MAAM,oBAAoB,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACxC,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC;AAC5B,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,OAAkB,EAAE,MAAc;IACvE,MAAM,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC7C,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC;AAC5B,CAAC"}
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import type { FontResource, FontUrl } from '@flighthq/types/contract';
|
|
2
|
+
/**
|
|
3
|
+
* Loads a face from bytes into `out`. If loading rejects, no face is registered and `out.face` retains
|
|
4
|
+
* its previous value, so a failed reload does not blank a working resource.
|
|
5
|
+
*/
|
|
2
6
|
export declare function loadFontResourceFromBytes(out: FontResource, bytes: Uint8Array): Promise<FontResource>;
|
|
7
|
+
/**
|
|
8
|
+
* Looks up an existing family and attaches its first face to `out`. If the lookup rejects or returns no
|
|
9
|
+
* faces, `out.face` retains its previous value.
|
|
10
|
+
*/
|
|
3
11
|
export declare function loadFontResourceFromName(out: FontResource): Promise<FontResource>;
|
|
12
|
+
/**
|
|
13
|
+
* Loads a face from one URL into `out`. If loading rejects, no face is registered and `out.face`
|
|
14
|
+
* retains its previous value, so a failed reload does not blank a working resource.
|
|
15
|
+
*/
|
|
4
16
|
export declare function loadFontResourceFromUrl(out: FontResource, url: string): Promise<FontResource>;
|
|
17
|
+
/**
|
|
18
|
+
* Loads a face from fallback URLs into `out`. If loading rejects, no face is registered and `out.face`
|
|
19
|
+
* retains its previous value, so a failed reload does not blank a working resource.
|
|
20
|
+
*/
|
|
5
21
|
export declare function loadFontResourceFromUrls(out: FontResource, sources: FontUrl[]): Promise<FontResource>;
|
|
6
22
|
//# sourceMappingURL=fontResourceFrom.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fontResourceFrom.d.ts","sourceRoot":"","sources":["../src/fontResourceFrom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"fontResourceFrom.d.ts","sourceRoot":"","sources":["../src/fontResourceFrom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AAStE;;;GAGG;AACH,wBAAsB,yBAAyB,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC,CAI3G;AAED;;;GAGG;AACH,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAIvF;AAED;;;GAGG;AACH,wBAAsB,uBAAuB,CAAC,GAAG,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAInG;AAED;;;GAGG;AACH,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAI3G"}
|
package/dist/fontResourceFrom.js
CHANGED
|
@@ -1,40 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// working font instead of losing one. Assigning before the await would silently turn a failed reload
|
|
7
|
-
// into a broken resource.
|
|
1
|
+
import { _loadFontFaceFromBytes, _loadFontFaceFromUrl, _loadFontFaceFromUrls, _loadFontFacesFromName, } from './_fontFaceLoad';
|
|
2
|
+
/**
|
|
3
|
+
* Loads a face from bytes into `out`. If loading rejects, no face is registered and `out.face` retains
|
|
4
|
+
* its previous value, so a failed reload does not blank a working resource.
|
|
5
|
+
*/
|
|
8
6
|
export async function loadFontResourceFromBytes(out, bytes) {
|
|
9
|
-
const face =
|
|
10
|
-
await face.load();
|
|
11
|
-
document.fonts.add(face);
|
|
7
|
+
const face = await _loadFontFaceFromBytes(out.family, bytes);
|
|
12
8
|
out.face = face;
|
|
13
9
|
return out;
|
|
14
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Looks up an existing family and attaches its first face to `out`. If the lookup rejects or returns no
|
|
13
|
+
* faces, `out.face` retains its previous value.
|
|
14
|
+
*/
|
|
15
15
|
export async function loadFontResourceFromName(out) {
|
|
16
|
-
const faces = await
|
|
16
|
+
const faces = await _loadFontFacesFromName(out.family);
|
|
17
17
|
if (faces.length > 0)
|
|
18
18
|
out.face = faces[0];
|
|
19
19
|
return out;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Loads a face from one URL into `out`. If loading rejects, no face is registered and `out.face`
|
|
23
|
+
* retains its previous value, so a failed reload does not blank a working resource.
|
|
24
|
+
*/
|
|
21
25
|
export async function loadFontResourceFromUrl(out, url) {
|
|
22
|
-
const face =
|
|
23
|
-
await face.load();
|
|
24
|
-
document.fonts.add(face);
|
|
26
|
+
const face = await _loadFontFaceFromUrl(out.family, url);
|
|
25
27
|
out.face = face;
|
|
26
28
|
return out;
|
|
27
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Loads a face from fallback URLs into `out`. If loading rejects, no face is registered and `out.face`
|
|
32
|
+
* retains its previous value, so a failed reload does not blank a working resource.
|
|
33
|
+
*/
|
|
28
34
|
export async function loadFontResourceFromUrls(out, sources) {
|
|
29
|
-
const
|
|
30
|
-
.map(({ url, format }) => {
|
|
31
|
-
const fmt = format ?? inferFontFormatFromUrl(url);
|
|
32
|
-
return fmt !== null ? `url(${url}) format('${fmt}')` : `url(${url})`;
|
|
33
|
-
})
|
|
34
|
-
.join(', ');
|
|
35
|
-
const face = new FontFace(out.family, src);
|
|
36
|
-
await face.load();
|
|
37
|
-
document.fonts.add(face);
|
|
35
|
+
const face = await _loadFontFaceFromUrls(out.family, sources);
|
|
38
36
|
out.face = face;
|
|
39
37
|
return out;
|
|
40
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fontResourceFrom.js","sourceRoot":"","sources":["../src/fontResourceFrom.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"fontResourceFrom.js","sourceRoot":"","sources":["../src/fontResourceFrom.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,iBAAiB,CAAC;AAEzB;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAAC,GAAiB,EAAE,KAAiB;IAClF,MAAM,IAAI,GAAG,MAAM,sBAAsB,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC7D,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;IAChB,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAAC,GAAiB;IAC9D,MAAM,KAAK,GAAG,MAAM,sBAAsB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACvD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;QAAE,GAAG,CAAC,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IAC1C,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAAC,GAAiB,EAAE,GAAW;IAC1E,MAAM,IAAI,GAAG,MAAM,oBAAoB,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACzD,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;IAChB,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAAC,GAAiB,EAAE,OAAkB;IAClF,MAAM,IAAI,GAAG,MAAM,qBAAqB,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9D,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;IAChB,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flighthq/font",
|
|
3
|
-
"version": "0.3.0-next.
|
|
3
|
+
"version": "0.3.0-next.1392.f0bd0cc",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/flighthq/flight.git",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"clean:dist": "tsx ../../scripts/clean-package-dist.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@flighthq/entity": "0.3.0-next.
|
|
40
|
-
"@flighthq/types": "0.3.0-next.
|
|
39
|
+
"@flighthq/entity": "0.3.0-next.1392.f0bd0cc",
|
|
40
|
+
"@flighthq/types": "0.3.0-next.1392.f0bd0cc"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"typescript": "^5.3.0"
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
_loadFontFaceFromBytes,
|
|
3
|
+
_loadFontFaceFromUrl,
|
|
4
|
+
_loadFontFaceFromUrls,
|
|
5
|
+
_loadFontFacesFromName,
|
|
6
|
+
} from './_fontFaceLoad';
|
|
7
|
+
|
|
8
|
+
interface FontFaceConstruction {
|
|
9
|
+
family: string;
|
|
10
|
+
source: string | ArrayBuffer;
|
|
11
|
+
instance: MockFontFace;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let constructions: FontFaceConstruction[];
|
|
15
|
+
let addMock: ReturnType<typeof vi.fn>;
|
|
16
|
+
let loadMock: ReturnType<typeof vi.fn>;
|
|
17
|
+
|
|
18
|
+
class MockFontFace {
|
|
19
|
+
load = vi.fn().mockResolvedValue(undefined);
|
|
20
|
+
constructor(family: string, source: string | ArrayBuffer) {
|
|
21
|
+
constructions.push({ family, source, instance: this });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let originalFonts: PropertyDescriptor | undefined;
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
constructions = [];
|
|
28
|
+
addMock = vi.fn();
|
|
29
|
+
loadMock = vi.fn().mockResolvedValue([]);
|
|
30
|
+
vi.stubGlobal('FontFace', MockFontFace);
|
|
31
|
+
originalFonts = Object.getOwnPropertyDescriptor(document, 'fonts');
|
|
32
|
+
Object.defineProperty(document, 'fonts', {
|
|
33
|
+
value: { add: addMock, load: loadMock },
|
|
34
|
+
configurable: true,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
if (originalFonts) {
|
|
40
|
+
Object.defineProperty(document, 'fonts', originalFonts);
|
|
41
|
+
} else {
|
|
42
|
+
delete (document as unknown as { fonts?: unknown }).fonts;
|
|
43
|
+
}
|
|
44
|
+
vi.unstubAllGlobals();
|
|
45
|
+
vi.restoreAllMocks();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('_loadFontFaceFromBytes', () => {
|
|
49
|
+
it('loads and registers a face using only the bytes inside the supplied view', async () => {
|
|
50
|
+
const backing = new Uint8Array([1, 2, 3, 4, 5, 6]);
|
|
51
|
+
const face = await _loadFontFaceFromBytes('Family', backing.subarray(1, 5));
|
|
52
|
+
|
|
53
|
+
expect(face).toBe(constructions[0].instance);
|
|
54
|
+
expect(new Uint8Array(constructions[0].source as ArrayBuffer)).toEqual(new Uint8Array([2, 3, 4, 5]));
|
|
55
|
+
expect(constructions[0].instance.load).toHaveBeenCalledOnce();
|
|
56
|
+
expect(addMock).toHaveBeenCalledWith(face);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('_loadFontFaceFromUrl', () => {
|
|
61
|
+
it('loads and registers the single URL source', async () => {
|
|
62
|
+
await _loadFontFaceFromUrl('Family', 'font.woff2');
|
|
63
|
+
expect(constructions[0].source).toBe('url(font.woff2)');
|
|
64
|
+
expect(addMock).toHaveBeenCalledWith(constructions[0].instance);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('_loadFontFaceFromUrls', () => {
|
|
69
|
+
it('composes explicit, inferred, and absent format hints once for both public loader families', async () => {
|
|
70
|
+
await _loadFontFaceFromUrls('Family', [{ url: 'a.woff2', format: 'woff2' }, { url: 'b.ttf' }, { url: 'c.bin' }]);
|
|
71
|
+
|
|
72
|
+
expect(constructions[0].source).toBe("url(a.woff2) format('woff2'), url(b.ttf) format('truetype'), url(c.bin)");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('does not register a face when loading rejects', async () => {
|
|
76
|
+
const failing = new MockFontFace('Family', '');
|
|
77
|
+
failing.load.mockRejectedValue(new Error('bad font'));
|
|
78
|
+
vi.stubGlobal(
|
|
79
|
+
'FontFace',
|
|
80
|
+
vi.fn(function () {
|
|
81
|
+
return failing;
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
await expect(_loadFontFaceFromUrls('Family', [{ url: 'font.woff2' }])).rejects.toThrow('bad font');
|
|
86
|
+
expect(addMock).not.toHaveBeenCalled();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('_loadFontFacesFromName', () => {
|
|
91
|
+
it('uses the escaped shorthand and returns the document faces', async () => {
|
|
92
|
+
const faces = [{ family: 'Family' } as unknown as FontFace];
|
|
93
|
+
loadMock.mockResolvedValue(faces);
|
|
94
|
+
|
|
95
|
+
await expect(_loadFontFacesFromName("Family's Name")).resolves.toBe(faces);
|
|
96
|
+
expect(loadMock).toHaveBeenCalledWith("1em 'Family\\'s Name'");
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -62,8 +62,8 @@ describe('loadFontResourceFromBytes', () => {
|
|
|
62
62
|
});
|
|
63
63
|
|
|
64
64
|
it('slices only the view bytes out of a larger backing buffer', async () => {
|
|
65
|
-
//
|
|
66
|
-
//
|
|
65
|
+
// This integration assertion pins that the resource wrapper forwards its view to the canonical
|
|
66
|
+
// byte loader rather than widening it back to the whole buffer.
|
|
67
67
|
const backing = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
68
68
|
await loadFontResourceFromBytes(createFontResource('TestFont'), backing.subarray(2, 6));
|
|
69
69
|
expect(new Uint8Array(constructions[0].source as ArrayBuffer)).toEqual(new Uint8Array([3, 4, 5, 6]));
|