@docx-editor.dev/fonts-cjk 2.32.0 → 2.33.0

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/README.md CHANGED
@@ -40,6 +40,12 @@ For deployment examples, see [Integrate PDF conversion](../docx-to-pdf/docs/inte
40
40
 
41
41
  ## Use the font in the editor
42
42
 
43
+ For a browser editor, install this package alongside your existing editor dependencies:
44
+
45
+ ```bash
46
+ npm install @docx-editor.dev/fonts-cjk
47
+ ```
48
+
43
49
  The `@docx-editor.dev/fonts-cjk/browser` entry exports `cjkFonts()`, a font resolver with the same shape as `packagedFonts()` and `googleFonts()` from `@docx-editor.dev/fonts`. Put it after the origins whose fonts you prefer:
44
50
 
45
51
  ```ts
@@ -50,9 +56,13 @@ import { cjkFonts } from '@docx-editor.dev/fonts-cjk/browser';
50
56
  const fonts = [packagedFonts(), googleFonts(), cjkFonts()];
51
57
  ```
52
58
 
59
+ The first paint does not wait for this download. The editor paints the document with its other fonts, then lays it out again when this font arrives. To install it with the other fonts instead, pass `cjkFonts({ loading: 'wait' })`. A document export that receives this resolver always waits for the font.
60
+
61
+ If you can load fonts from a third party, `googleFonts({ cjk: true })` from `@docx-editor.dev/fonts/google` is an alternative. It loads a smaller regional face for each language a document uses. Do not combine `cjk: true` with `cjkFonts()`: both fonts would download, and the origin listed first would paint.
62
+
53
63
  The resolver downloads the font only when a document names one of the `CJK_SUBSTITUTED_FAMILIES`, or one of their localized names in `LOCALIZED_CJK_FAMILIES`. Your bundler emits the font file as an asset, and the resolver fetches it from there. No request goes to a third party. The browser entry is ECMAScript modules only, because it finds the file through `import.meta.url`.
54
64
 
55
- A failed download calls the `onFailure` option, or writes a console warning. The document then opens without the font.
65
+ A failed download calls the `onFailure` option, or writes a console warning. The document then keeps its other fonts.
56
66
 
57
67
  ## Use the font directly
58
68
 
@@ -69,10 +79,7 @@ Use the exported URL to read the font without constructing a package path:
69
79
 
70
80
  ```ts
71
81
  import { readFile } from 'node:fs/promises';
72
- import {
73
- NOTO_SANS_CJK_JP_FAMILY,
74
- NOTO_SANS_CJK_JP_URL,
75
- } from '@docx-editor.dev/fonts-cjk';
82
+ import { NOTO_SANS_CJK_JP_FAMILY, NOTO_SANS_CJK_JP_URL } from '@docx-editor.dev/fonts-cjk';
76
83
 
77
84
  const bytes = await readFile(NOTO_SANS_CJK_JP_URL);
78
85
  console.log(NOTO_SANS_CJK_JP_FAMILY, bytes.byteLength);
package/dist/browser.cjs CHANGED
@@ -175,6 +175,13 @@ function cjkFonts(options = {}) {
175
175
  if (painted.has(NOTO_SANS_CJK_JP_FAMILY.toLowerCase())) {
176
176
  return { sources: [], substitutions: substitutionsFor(planned) };
177
177
  }
178
+ if ((options.loading ?? "background") === "background") {
179
+ const deferred = downloadFace(planned, request).catch(() => EMPTY);
180
+ return { sources: [], substitutions: [], deferred };
181
+ }
182
+ return downloadFace(planned, request);
183
+ }
184
+ async function downloadFace(planned, request) {
178
185
  const fetcher = options.fetcher ?? defaultFetch;
179
186
  let url = "";
180
187
  let bytes;
@@ -212,6 +219,7 @@ function cjkFonts(options = {}) {
212
219
  Object.defineProperty(resolveCjkFonts, FONT_RESOLVER_MARK_KEY, descriptor);
213
220
  return resolveCjkFonts;
214
221
  }
222
+ var EMPTY = Object.freeze({ sources: [], substitutions: [] });
215
223
  function substitutionsFor(families) {
216
224
  return families.flatMap(
217
225
  (family) => FACES.map(([weight, style]) => ({ from: { family, weight, style }, to: PACKAGED_FACE }))
@@ -104,7 +104,27 @@ interface CjkFontsFragment {
104
104
  readonly from: CjkFontFaceRequest;
105
105
  readonly to: CjkFontFaceRequest;
106
106
  }[];
107
+ /**
108
+ * With `loading: 'background'`, the face and its substitutions, settled after the
109
+ * download. The editor paints the document first and lays it out again on arrival.
110
+ */
111
+ readonly deferred?: Promise<CjkFontsFragment>;
107
112
  }
113
+ /**
114
+ * Whether a large CJK font download may delay the first paint.
115
+ *
116
+ * - `'background'`: the face installs in a separate step, after the document's other fonts.
117
+ * The first paint never waits for it, and text in it can move once when it arrives.
118
+ * - `'wait'`: the face installs in the same step as the document's other fonts. A large
119
+ * document (over about 512 KB of uncompressed content) waits up to 2 seconds for them
120
+ * before its first paint. A small document paints at once and lays out again when all
121
+ * of its fonts arrive.
122
+ *
123
+ * Either way, a document export that receives this resolver waits for the face.
124
+ *
125
+ * @public
126
+ */
127
+ type CjkFontLoading = 'background' | 'wait';
108
128
  /**
109
129
  * A fetch of the packaged face that failed for a reason other than cancellation.
110
130
  *
@@ -126,8 +146,13 @@ interface CjkFontLoadFailure {
126
146
  interface CjkFontsOptions {
127
147
  /** Injectable for tests; defaults to the global `fetch`. */
128
148
  readonly fetcher?: typeof fetch;
129
- /** Fetch failures. Defaults to a console warning. The document then opens without the face. */
149
+ /** Fetch failures. Defaults to a console warning. The document keeps its other fonts. */
130
150
  readonly onFailure?: (failure: CjkFontLoadFailure) => void;
151
+ /**
152
+ * Whether the download may delay the document. Defaults to `'background'`, because the
153
+ * face is about 16 MB. See {@link CjkFontLoading}.
154
+ */
155
+ readonly loading?: CjkFontLoading;
131
156
  }
132
157
  /**
133
158
  * The marker the editor reads to call a function per document.
@@ -151,16 +176,21 @@ type CjkFontsResolver = ((request: CjkFontsRequest) => Promise<CjkFontsFragment>
151
176
  * A document without one never downloads the 16 MB face.
152
177
  *
153
178
  * The face has one weight and one style, so every face of a named family points at it. Put
154
- * it after the origins whose faces you prefer: a family an earlier origin already paints is
155
- * left to that origin.
179
+ * it after the origins whose faces you prefer. A family an earlier origin paints by the time
180
+ * this resolver runs is left to that origin. A face an earlier origin loads in the background
181
+ * is not painted yet, so this face still downloads.
182
+ *
183
+ * The download does not delay the first paint by default: the document paints with its other
184
+ * fonts and lays out again when this face arrives. See {@link CjkFontLoading} for
185
+ * `loading: 'wait'`. A document export that receives this resolver waits for the face.
156
186
  *
157
187
  * The face is the file inside this package, fetched from where your bundler emits it. Nothing
158
- * comes from a third party. A failed fetch reports through `onFailure` and the document opens
159
- * without the face; cancellation rejects with `signal.reason`.
188
+ * comes from a third party. A failed fetch reports through `onFailure` and the document keeps
189
+ * its other fonts; cancellation rejects with `signal.reason`.
160
190
  *
161
191
  * @public
162
192
  */
163
193
  declare function cjkFonts(options?: CjkFontsOptions): CjkFontsResolver;
164
194
 
165
195
  export { cjkFonts };
166
- export type { CjkFontFaceRequest, CjkFontLoadFailure, CjkFontResolverMark, CjkFontSource, CjkFontsFragment, CjkFontsOptions, CjkFontsRequest, CjkFontsResolver, CjkResolvedFontFace };
196
+ export type { CjkFontFaceRequest, CjkFontLoadFailure, CjkFontLoading, CjkFontResolverMark, CjkFontSource, CjkFontsFragment, CjkFontsOptions, CjkFontsRequest, CjkFontsResolver, CjkResolvedFontFace };
package/dist/browser.d.ts CHANGED
@@ -104,7 +104,27 @@ interface CjkFontsFragment {
104
104
  readonly from: CjkFontFaceRequest;
105
105
  readonly to: CjkFontFaceRequest;
106
106
  }[];
107
+ /**
108
+ * With `loading: 'background'`, the face and its substitutions, settled after the
109
+ * download. The editor paints the document first and lays it out again on arrival.
110
+ */
111
+ readonly deferred?: Promise<CjkFontsFragment>;
107
112
  }
113
+ /**
114
+ * Whether a large CJK font download may delay the first paint.
115
+ *
116
+ * - `'background'`: the face installs in a separate step, after the document's other fonts.
117
+ * The first paint never waits for it, and text in it can move once when it arrives.
118
+ * - `'wait'`: the face installs in the same step as the document's other fonts. A large
119
+ * document (over about 512 KB of uncompressed content) waits up to 2 seconds for them
120
+ * before its first paint. A small document paints at once and lays out again when all
121
+ * of its fonts arrive.
122
+ *
123
+ * Either way, a document export that receives this resolver waits for the face.
124
+ *
125
+ * @public
126
+ */
127
+ type CjkFontLoading = 'background' | 'wait';
108
128
  /**
109
129
  * A fetch of the packaged face that failed for a reason other than cancellation.
110
130
  *
@@ -126,8 +146,13 @@ interface CjkFontLoadFailure {
126
146
  interface CjkFontsOptions {
127
147
  /** Injectable for tests; defaults to the global `fetch`. */
128
148
  readonly fetcher?: typeof fetch;
129
- /** Fetch failures. Defaults to a console warning. The document then opens without the face. */
149
+ /** Fetch failures. Defaults to a console warning. The document keeps its other fonts. */
130
150
  readonly onFailure?: (failure: CjkFontLoadFailure) => void;
151
+ /**
152
+ * Whether the download may delay the document. Defaults to `'background'`, because the
153
+ * face is about 16 MB. See {@link CjkFontLoading}.
154
+ */
155
+ readonly loading?: CjkFontLoading;
131
156
  }
132
157
  /**
133
158
  * The marker the editor reads to call a function per document.
@@ -151,16 +176,21 @@ type CjkFontsResolver = ((request: CjkFontsRequest) => Promise<CjkFontsFragment>
151
176
  * A document without one never downloads the 16 MB face.
152
177
  *
153
178
  * The face has one weight and one style, so every face of a named family points at it. Put
154
- * it after the origins whose faces you prefer: a family an earlier origin already paints is
155
- * left to that origin.
179
+ * it after the origins whose faces you prefer. A family an earlier origin paints by the time
180
+ * this resolver runs is left to that origin. A face an earlier origin loads in the background
181
+ * is not painted yet, so this face still downloads.
182
+ *
183
+ * The download does not delay the first paint by default: the document paints with its other
184
+ * fonts and lays out again when this face arrives. See {@link CjkFontLoading} for
185
+ * `loading: 'wait'`. A document export that receives this resolver waits for the face.
156
186
  *
157
187
  * The face is the file inside this package, fetched from where your bundler emits it. Nothing
158
- * comes from a third party. A failed fetch reports through `onFailure` and the document opens
159
- * without the face; cancellation rejects with `signal.reason`.
188
+ * comes from a third party. A failed fetch reports through `onFailure` and the document keeps
189
+ * its other fonts; cancellation rejects with `signal.reason`.
160
190
  *
161
191
  * @public
162
192
  */
163
193
  declare function cjkFonts(options?: CjkFontsOptions): CjkFontsResolver;
164
194
 
165
195
  export { cjkFonts };
166
- export type { CjkFontFaceRequest, CjkFontLoadFailure, CjkFontResolverMark, CjkFontSource, CjkFontsFragment, CjkFontsOptions, CjkFontsRequest, CjkFontsResolver, CjkResolvedFontFace };
196
+ export type { CjkFontFaceRequest, CjkFontLoadFailure, CjkFontLoading, CjkFontResolverMark, CjkFontSource, CjkFontsFragment, CjkFontsOptions, CjkFontsRequest, CjkFontsResolver, CjkResolvedFontFace };
package/dist/browser.js CHANGED
@@ -100,6 +100,13 @@ function cjkFonts(options = {}) {
100
100
  if (painted.has(NOTO_SANS_CJK_JP_FAMILY.toLowerCase())) {
101
101
  return { sources: [], substitutions: substitutionsFor(planned) };
102
102
  }
103
+ if ((options.loading ?? "background") === "background") {
104
+ const deferred = downloadFace(planned, request).catch(() => EMPTY);
105
+ return { sources: [], substitutions: [], deferred };
106
+ }
107
+ return downloadFace(planned, request);
108
+ }
109
+ async function downloadFace(planned, request) {
103
110
  const fetcher = options.fetcher ?? defaultFetch;
104
111
  let url = "";
105
112
  let bytes;
@@ -137,6 +144,7 @@ function cjkFonts(options = {}) {
137
144
  Object.defineProperty(resolveCjkFonts, FONT_RESOLVER_MARK_KEY, descriptor);
138
145
  return resolveCjkFonts;
139
146
  }
147
+ var EMPTY = Object.freeze({ sources: [], substitutions: [] });
140
148
  function substitutionsFor(families) {
141
149
  return families.flatMap(
142
150
  (family) => FACES.map(([weight, style]) => ({ from: { family, weight, style }, to: PACKAGED_FACE }))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docx-editor.dev/fonts-cjk",
3
- "version": "2.32.0",
3
+ "version": "2.33.0",
4
4
  "description": "Noto Sans CJK JP fallback font for Chinese, Japanese, and Korean text in @docx-editor.dev/docx-to-pdf and the editor.",
5
5
  "sideEffects": false,
6
6
  "type": "module",