@docx-editor.dev/fonts-cjk 2.32.0 → 2.34.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 +12 -5
- package/dist/browser.cjs +30 -8
- package/dist/browser.d.cts +36 -6
- package/dist/browser.d.ts +36 -6
- package/dist/browser.js +30 -8
- package/package.json +1 -1
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
|
|
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
|
@@ -161,20 +161,42 @@ function loadFace(fetcher, url, signal) {
|
|
|
161
161
|
}
|
|
162
162
|
function cjkFonts(options = {}) {
|
|
163
163
|
async function resolveCjkFonts(request) {
|
|
164
|
-
const
|
|
165
|
-
(request.resolvedFaces ?? []).
|
|
164
|
+
const faces = new Set(
|
|
165
|
+
(request.resolvedFaces ?? []).map(
|
|
166
|
+
(face) => `${face.family.trim().toLowerCase()}/${face.weight}/${face.style}`
|
|
167
|
+
)
|
|
166
168
|
);
|
|
169
|
+
const isPainted = (family, weight = 400, style = "normal") => faces.has(`${family.trim().toLowerCase()}/${weight}/${style}`);
|
|
170
|
+
const aliases = [];
|
|
167
171
|
const planned = request.families.filter((family) => {
|
|
168
172
|
const key = family.trim().toLowerCase();
|
|
169
|
-
|
|
173
|
+
if (!SUBSTITUTED.has(key) || isPainted(family)) return false;
|
|
174
|
+
const english = LOCALIZED_CJK_FAMILIES.get(family.trim());
|
|
175
|
+
if (english === void 0 || !isPainted(english)) return true;
|
|
176
|
+
for (const [weight, style] of FACES)
|
|
177
|
+
aliases.push({
|
|
178
|
+
from: { family, weight, style },
|
|
179
|
+
to: isPainted(english, weight, style) ? { family: english, weight, style } : { family: english, weight: 400, style: "normal" }
|
|
180
|
+
});
|
|
181
|
+
return false;
|
|
170
182
|
});
|
|
171
183
|
const named = request.families.some(
|
|
172
184
|
(family) => family.trim().toLowerCase() === NOTO_SANS_CJK_JP_FAMILY.toLowerCase()
|
|
173
185
|
);
|
|
174
|
-
if (planned.length === 0 && !named) return { sources: [], substitutions:
|
|
175
|
-
if (
|
|
176
|
-
return { sources: [], substitutions: substitutionsFor(planned) };
|
|
186
|
+
if (planned.length === 0 && !named) return { sources: [], substitutions: aliases };
|
|
187
|
+
if (isPainted(NOTO_SANS_CJK_JP_FAMILY)) {
|
|
188
|
+
return { sources: [], substitutions: [...aliases, ...substitutionsFor(planned)] };
|
|
177
189
|
}
|
|
190
|
+
if ((options.loading ?? "background") === "background") {
|
|
191
|
+
const deferred = downloadFace(planned, request, aliases).catch(() => ({
|
|
192
|
+
sources: [],
|
|
193
|
+
substitutions: aliases
|
|
194
|
+
}));
|
|
195
|
+
return { sources: [], substitutions: aliases, deferred };
|
|
196
|
+
}
|
|
197
|
+
return downloadFace(planned, request, aliases);
|
|
198
|
+
}
|
|
199
|
+
async function downloadFace(planned, request, aliases) {
|
|
178
200
|
const fetcher = options.fetcher ?? defaultFetch;
|
|
179
201
|
let url = "";
|
|
180
202
|
let bytes;
|
|
@@ -191,7 +213,7 @@ function cjkFonts(options = {}) {
|
|
|
191
213
|
};
|
|
192
214
|
if (options.onFailure) options.onFailure(failure);
|
|
193
215
|
else console.warn(`[fonts-cjk] ${failure.family} (${failure.url}): ${failure.diagnostic}`);
|
|
194
|
-
return { sources: [], substitutions:
|
|
216
|
+
return { sources: [], substitutions: aliases };
|
|
195
217
|
}
|
|
196
218
|
if (request.signal?.aborted) throw request.signal.reason;
|
|
197
219
|
return {
|
|
@@ -204,7 +226,7 @@ function cjkFonts(options = {}) {
|
|
|
204
226
|
faceIndex: 0
|
|
205
227
|
}
|
|
206
228
|
],
|
|
207
|
-
substitutions: substitutionsFor(planned)
|
|
229
|
+
substitutions: [...aliases, ...substitutionsFor(planned)]
|
|
208
230
|
};
|
|
209
231
|
}
|
|
210
232
|
const descriptor = { value: true, enumerable: false, configurable: true };
|
package/dist/browser.d.cts
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
|
|
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
|
|
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
|
|
159
|
-
*
|
|
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
|
|
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
|
|
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
|
|
159
|
-
*
|
|
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
|
@@ -86,20 +86,42 @@ function loadFace(fetcher, url, signal) {
|
|
|
86
86
|
}
|
|
87
87
|
function cjkFonts(options = {}) {
|
|
88
88
|
async function resolveCjkFonts(request) {
|
|
89
|
-
const
|
|
90
|
-
(request.resolvedFaces ?? []).
|
|
89
|
+
const faces = new Set(
|
|
90
|
+
(request.resolvedFaces ?? []).map(
|
|
91
|
+
(face) => `${face.family.trim().toLowerCase()}/${face.weight}/${face.style}`
|
|
92
|
+
)
|
|
91
93
|
);
|
|
94
|
+
const isPainted = (family, weight = 400, style = "normal") => faces.has(`${family.trim().toLowerCase()}/${weight}/${style}`);
|
|
95
|
+
const aliases = [];
|
|
92
96
|
const planned = request.families.filter((family) => {
|
|
93
97
|
const key = family.trim().toLowerCase();
|
|
94
|
-
|
|
98
|
+
if (!SUBSTITUTED.has(key) || isPainted(family)) return false;
|
|
99
|
+
const english = LOCALIZED_CJK_FAMILIES.get(family.trim());
|
|
100
|
+
if (english === void 0 || !isPainted(english)) return true;
|
|
101
|
+
for (const [weight, style] of FACES)
|
|
102
|
+
aliases.push({
|
|
103
|
+
from: { family, weight, style },
|
|
104
|
+
to: isPainted(english, weight, style) ? { family: english, weight, style } : { family: english, weight: 400, style: "normal" }
|
|
105
|
+
});
|
|
106
|
+
return false;
|
|
95
107
|
});
|
|
96
108
|
const named = request.families.some(
|
|
97
109
|
(family) => family.trim().toLowerCase() === NOTO_SANS_CJK_JP_FAMILY.toLowerCase()
|
|
98
110
|
);
|
|
99
|
-
if (planned.length === 0 && !named) return { sources: [], substitutions:
|
|
100
|
-
if (
|
|
101
|
-
return { sources: [], substitutions: substitutionsFor(planned) };
|
|
111
|
+
if (planned.length === 0 && !named) return { sources: [], substitutions: aliases };
|
|
112
|
+
if (isPainted(NOTO_SANS_CJK_JP_FAMILY)) {
|
|
113
|
+
return { sources: [], substitutions: [...aliases, ...substitutionsFor(planned)] };
|
|
102
114
|
}
|
|
115
|
+
if ((options.loading ?? "background") === "background") {
|
|
116
|
+
const deferred = downloadFace(planned, request, aliases).catch(() => ({
|
|
117
|
+
sources: [],
|
|
118
|
+
substitutions: aliases
|
|
119
|
+
}));
|
|
120
|
+
return { sources: [], substitutions: aliases, deferred };
|
|
121
|
+
}
|
|
122
|
+
return downloadFace(planned, request, aliases);
|
|
123
|
+
}
|
|
124
|
+
async function downloadFace(planned, request, aliases) {
|
|
103
125
|
const fetcher = options.fetcher ?? defaultFetch;
|
|
104
126
|
let url = "";
|
|
105
127
|
let bytes;
|
|
@@ -116,7 +138,7 @@ function cjkFonts(options = {}) {
|
|
|
116
138
|
};
|
|
117
139
|
if (options.onFailure) options.onFailure(failure);
|
|
118
140
|
else console.warn(`[fonts-cjk] ${failure.family} (${failure.url}): ${failure.diagnostic}`);
|
|
119
|
-
return { sources: [], substitutions:
|
|
141
|
+
return { sources: [], substitutions: aliases };
|
|
120
142
|
}
|
|
121
143
|
if (request.signal?.aborted) throw request.signal.reason;
|
|
122
144
|
return {
|
|
@@ -129,7 +151,7 @@ function cjkFonts(options = {}) {
|
|
|
129
151
|
faceIndex: 0
|
|
130
152
|
}
|
|
131
153
|
],
|
|
132
|
-
substitutions: substitutionsFor(planned)
|
|
154
|
+
substitutions: [...aliases, ...substitutionsFor(planned)]
|
|
133
155
|
};
|
|
134
156
|
}
|
|
135
157
|
const descriptor = { value: true, enumerable: false, configurable: true };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docx-editor.dev/fonts-cjk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.34.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",
|