@web-ppt/fonts 0.8.0 → 0.9.0-beta.1

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.
@@ -4,4 +4,4 @@ export { withFontMeasurement } from './measurement';
4
4
  export type { FontMeasurementOptions, PreparedFontMeasurement } from './measurement';
5
5
  export type { FontTextSegment } from './text-scripts';
6
6
  export type FontGlyphProvider = ReturnType<typeof import('./provider').createFontProvider>;
7
- export type { FontSource, FontRequestOptions, FontRequest, FontFaceInfo, FontEmbedding, FontEmbeddingRights, FontProblem, FontFailure, FontResult, FontProviderOptions, FontScript, FontShapeOptions, PositionedGlyph, GlyphCluster, GlyphRun, ShaperGlyph, FontShaperFace, FontShaper, FontProviderLimits } from './types';
7
+ export type { FontSource, FontRequestOptions, FontRequest, FontFaceInfo, FontEmbedding, FontEmbeddingRights, FontProblem, FontFailure, FontResult, FontProviderOptions, FontScript, FontShapeOptions, PositionedGlyph, GlyphCluster, GlyphRun, ShaperGlyph, FontShaperFace, FontShaper, FontProviderLimits, } from './types';
@@ -1,78 +1,114 @@
1
- function U(b, a, t) {
2
- return new Promise((f, u) => {
3
- const n = () => {
4
- o(), u(new DOMException("字体加载已取消", "AbortError"));
5
- }, o = () => {
6
- a.removeEventListener("abort", n), t == null || t.removeEventListener("abort", n);
7
- };
8
- if (a.addEventListener("abort", n, { once: !0 }), t == null || t.addEventListener("abort", n, { once: !0 }), a.aborted || t != null && t.aborted) {
9
- o(), n();
10
- return;
11
- }
12
- Promise.resolve().then(() => b.load()).then(() => {
13
- o(), f();
14
- }, (e) => {
15
- o(), u(e);
16
- });
17
- });
1
+ // src/glyphs/browser.ts
2
+ function e(e, t, n) {
3
+ return new Promise((r, i) => {
4
+ let a = () => {
5
+ o(), i(new DOMException("字体加载已取消", "AbortError"));
6
+ }, o = () => {
7
+ t.removeEventListener("abort", a), n?.removeEventListener("abort", a);
8
+ };
9
+ t.addEventListener("abort", a, { once: !0 }), n?.addEventListener("abort", a, { once: !0 }), t.aborted || n?.aborted ? (o(), a()) : Promise.resolve().then(() => e.load()).then(() => {
10
+ o(), r();
11
+ }, (e) => {
12
+ o(), i(e);
13
+ });
14
+ });
18
15
  }
19
- function p(b, a = document) {
20
- const t = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Set(), u = new AbortController();
21
- let n = !1;
22
- const o = (e, r) => {
23
- var s;
24
- return n || b.state().disposed ? { ok: !1, reason: "provider-disposed", faceId: e } : (s = r.signal) != null && s.aborted ? { ok: !1, reason: "aborted", faceId: e } : void 0;
25
- };
26
- return {
27
- async install(e, r) {
28
- const s = o(e, r);
29
- if (s) return s;
30
- const y = await b.embedding(e, r);
31
- if (!y.ok) return y;
32
- const g = o(e, r);
33
- if (g) return g;
34
- const { info: i, bytes: m } = y.value, l = r.family ?? i.family;
35
- if (![400, 700].includes(i.weight)) return { ok: !1, reason: "face-style-mismatch", faceId: e };
36
- if (!l.trim() || l.length > 256 || /[\u0000-\u001f\u007f]/.test(l)) return { ok: !1, reason: "invalid-request", faceId: e };
37
- const d = JSON.stringify([e, l]), k = t.get(d);
38
- if (k) return { ok: !0, value: { ...k.resource } };
39
- if (f.has(d)) return { ok: !1, reason: "duplicate-face-id", faceId: e };
40
- f.add(d);
41
- let c, v;
42
- try {
43
- const L = o(e, r);
44
- if (L) return L;
45
- c = new FontFace(l, new Uint8Array(m).buffer, { weight: String(i.weight), style: i.italic ? "italic" : "normal" }), await U(c, u.signal, r.signal);
46
- const w = o(e, r);
47
- if (w) return w;
48
- a.fonts.add(c), v = URL.createObjectURL(new Blob([new Uint8Array(m).buffer], { type: "font/ttf" }));
49
- const h = { family: l, src: v, bold: i.weight === 700, italic: i.italic };
50
- return t.set(d, { id: e, font: c, resource: h, bytes: m.length }), { ok: !0, value: { ...h } };
51
- } catch {
52
- return c && a.fonts.delete(c), v && URL.revokeObjectURL(v), o(e, r) ?? { ok: !1, reason: "font-install-failed", faceId: e };
53
- } finally {
54
- f.delete(d);
55
- }
56
- },
57
- resources() {
58
- return [...t.values()].map((e) => ({ ...e.resource }));
59
- },
60
- remove(e) {
61
- for (const [r, s] of t) s.id === e && (a.fonts.delete(s.font), URL.revokeObjectURL(s.resource.src), t.delete(r));
62
- },
63
- state() {
64
- return { disposed: n, faces: t.size, sourceBytes: [...t.values()].reduce((e, r) => e + r.bytes, 0) };
65
- },
66
- dispose() {
67
- if (!n) {
68
- n = !0, u.abort();
69
- for (const e of t.values())
70
- a.fonts.delete(e.font), URL.revokeObjectURL(e.resource.src);
71
- t.clear();
72
- }
73
- }
74
- };
16
+ function t(t, n = document) {
17
+ let r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Set(), a = new AbortController(), o = !1, s = (e, n) => o || t.state().disposed ? {
18
+ ok: !1,
19
+ reason: "provider-disposed",
20
+ faceId: e
21
+ } : n.signal?.aborted ? {
22
+ ok: !1,
23
+ reason: "aborted",
24
+ faceId: e
25
+ } : void 0;
26
+ return {
27
+ async install(o, c) {
28
+ let l = s(o, c);
29
+ if (l) return l;
30
+ let u = await t.embedding(o, c);
31
+ if (!u.ok) return u;
32
+ let d = s(o, c);
33
+ if (d) return d;
34
+ let { info: f, bytes: p } = u.value, m = c.family ?? f.family;
35
+ if (![400, 700].includes(f.weight)) return {
36
+ ok: !1,
37
+ reason: "face-style-mismatch",
38
+ faceId: o
39
+ };
40
+ if (!m.trim() || m.length > 256 || /[\u0000-\u001f\u007f]/.test(m)) return {
41
+ ok: !1,
42
+ reason: "invalid-request",
43
+ faceId: o
44
+ };
45
+ let h = JSON.stringify([o, m]), g = r.get(h);
46
+ if (g) return {
47
+ ok: !0,
48
+ value: { ...g.resource }
49
+ };
50
+ if (i.has(h)) return {
51
+ ok: !1,
52
+ reason: "duplicate-face-id",
53
+ faceId: o
54
+ };
55
+ i.add(h);
56
+ let _, v;
57
+ try {
58
+ let t = s(o, c);
59
+ if (t) return t;
60
+ _ = new FontFace(m, new Uint8Array(p).buffer, {
61
+ weight: String(f.weight),
62
+ style: f.italic ? "italic" : "normal"
63
+ }), await e(_, a.signal, c.signal);
64
+ let i = s(o, c);
65
+ if (i) return i;
66
+ n.fonts.add(_), v = URL.createObjectURL(new Blob([new Uint8Array(p).buffer], { type: "font/ttf" }));
67
+ let l = {
68
+ family: m,
69
+ src: v,
70
+ bold: f.weight === 700,
71
+ italic: f.italic
72
+ };
73
+ return r.set(h, {
74
+ id: o,
75
+ font: _,
76
+ resource: l,
77
+ bytes: p.length
78
+ }), {
79
+ ok: !0,
80
+ value: { ...l }
81
+ };
82
+ } catch {
83
+ return _ && n.fonts.delete(_), v && URL.revokeObjectURL(v), s(o, c) ?? {
84
+ ok: !1,
85
+ reason: "font-install-failed",
86
+ faceId: o
87
+ };
88
+ } finally {
89
+ i.delete(h);
90
+ }
91
+ },
92
+ resources() {
93
+ return [...r.values()].map((e) => ({ ...e.resource }));
94
+ },
95
+ remove(e) {
96
+ for (let [t, i] of r) i.id === e && (n.fonts.delete(i.font), URL.revokeObjectURL(i.resource.src), r.delete(t));
97
+ },
98
+ state() {
99
+ return {
100
+ disposed: o,
101
+ faces: r.size,
102
+ sourceBytes: [...r.values()].reduce((e, t) => e + t.bytes, 0)
103
+ };
104
+ },
105
+ dispose() {
106
+ if (!o) {
107
+ o = !0, a.abort();
108
+ for (let e of r.values()) n.fonts.delete(e.font), URL.revokeObjectURL(e.resource.src);
109
+ r.clear();
110
+ }
111
+ }
112
+ };
75
113
  }
76
- export {
77
- p as createFontFaceScope
78
- };
114
+ export { t as createFontFaceScope };
@@ -1,38 +1,37 @@
1
- import { F as s } from "./fault-HMYM9DhC.js";
2
- function y(t) {
3
- let r = !1;
4
- const a = /* @__PURE__ */ new Set();
5
- return {
6
- async open(i, g) {
7
- if (r || g.aborted) throw new s("aborted");
8
- let n = new t.Font(new t.Face(new t.Blob(i.bytes)));
9
- const c = () => {
10
- n = void 0, a.delete(c);
11
- };
12
- return a.add(c), {
13
- async shape(l, o) {
14
- var f;
15
- if (!n || r || (f = o.signal) != null && f.aborted) throw new s("aborted");
16
- const e = new t.Buffer();
17
- e.addText(l), e.guessSegmentProperties(), e.setDirection(t.Direction.LTR), e.setScript(o.script), e.setLanguage(o.language), t.shape(n, e);
18
- const d = e.getGlyphInfos(), p = e.getGlyphPositions();
19
- if (d.length !== p.length) throw new s("shaper-failed");
20
- return d.map((u, w) => ({ id: u.codepoint, cluster: u.cluster, ...p[w] }));
21
- },
22
- async outline(l, o) {
23
- var e;
24
- if (!n || r || (e = o.signal) != null && e.aborted) throw new s("aborted");
25
- return n.glyphToPath(l);
26
- },
27
- dispose: c
28
- };
29
- },
30
- dispose() {
31
- r = !0;
32
- for (const i of [...a]) i();
33
- }
34
- };
1
+ import { t as e } from "./fault-Wg7mCzGf.js";
2
+ // src/glyphs/harfbuzz.ts
3
+ function t(t) {
4
+ let n = !1, r = /* @__PURE__ */ new Set();
5
+ return {
6
+ async open(i, a) {
7
+ if (n || a.aborted) throw new e("aborted");
8
+ let o = new t.Font(new t.Face(new t.Blob(i.bytes))), s = () => {
9
+ o = void 0, r.delete(s);
10
+ };
11
+ return r.add(s), {
12
+ async shape(r, i) {
13
+ if (!o || n || i.signal?.aborted) throw new e("aborted");
14
+ let a = new t.Buffer();
15
+ a.addText(r), a.guessSegmentProperties(), a.setDirection(t.Direction.LTR), a.setScript(i.script), a.setLanguage(i.language), t.shape(o, a);
16
+ let s = a.getGlyphInfos(), c = a.getGlyphPositions();
17
+ if (s.length !== c.length) throw new e("shaper-failed");
18
+ return s.map((e, t) => ({
19
+ id: e.codepoint,
20
+ cluster: e.cluster,
21
+ ...c[t]
22
+ }));
23
+ },
24
+ async outline(t, r) {
25
+ if (!o || n || r.signal?.aborted) throw new e("aborted");
26
+ return o.glyphToPath(t);
27
+ },
28
+ dispose: s
29
+ };
30
+ },
31
+ dispose() {
32
+ n = !0;
33
+ for (let e of [...r]) e();
34
+ }
35
+ };
35
36
  }
36
- export {
37
- y as createHarfBuzzShaper
38
- };
37
+ export { t as createHarfBuzzShaper };