font-range 1.0.2 → 1.0.3
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 +5 -5
- package/build/main.js +74 -22
- package/package.json +16 -16
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ Google Font provides a subset of [Korean](https://design.google/news/google-font
|
|
|
21
21
|
It has dependencies on the following packages:
|
|
22
22
|
|
|
23
23
|
```sh
|
|
24
|
-
|
|
24
|
+
pip3 install fonttools[ufo,woff,unicode] zopfli brotli
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
### Basics
|
|
@@ -117,9 +117,9 @@ interface FontRangeOptionI extends FontDefaultOptionI {
|
|
|
117
117
|
```
|
|
118
118
|
- `default`: The `index` of the file name increases in the generated order
|
|
119
119
|
- `srcIndex`: The `index` of the file name uses [`src`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/src) in CSS [#9](https://github.com/black7375/font-range/issues/9)
|
|
120
|
-
-
|
|
120
|
+
- Chooses the first `url()` that matches the requested output format; otherwise use the first `url()`.
|
|
121
121
|
- `srcName`: The file name uses [`src`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/src) in CSS
|
|
122
|
-
-
|
|
122
|
+
- Uses the first `url()` that matches the requested output format; otherwise use the first `url()`.
|
|
123
123
|
|
|
124
124
|
### Font Subset
|
|
125
125
|
|
|
@@ -140,7 +140,7 @@ interface FontSubsetOptionI extends FontDefaultOptionI {
|
|
|
140
140
|
|
|
141
141
|
It offers a simple API when doing a large amount of subsets.
|
|
142
142
|
|
|
143
|
-
Performance is optimized using worker
|
|
143
|
+
Performance is optimized using worker pool and provides sharding for distributed environments.
|
|
144
144
|
|
|
145
145
|
#### Option
|
|
146
146
|
|
|
@@ -164,7 +164,7 @@ fontPipe([{ font_path1 }, { font_path2 }], "<index>/<total>");
|
|
|
164
164
|
|
|
165
165
|
- Environment Variable: Make it easy to use in CI, npm scripts, ..etc.
|
|
166
166
|
```sh
|
|
167
|
-
SHARD
|
|
167
|
+
SHARD="<index>/<total>"
|
|
168
168
|
node subset.js
|
|
169
169
|
```
|
|
170
170
|
|
package/build/main.js
CHANGED
|
@@ -104,12 +104,29 @@ const parseOptions = {
|
|
|
104
104
|
parseRulePrelude: false,
|
|
105
105
|
parseValue: false
|
|
106
106
|
};
|
|
107
|
+
function existsDir(dirPath) {
|
|
108
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
try {
|
|
110
|
+
yield (0, promises_1.stat)(dirPath);
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
if (err.code === "ENOENT") {
|
|
114
|
+
try {
|
|
115
|
+
yield (0, promises_1.mkdir)(dirPath);
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
if (err.code !== "EEXIST") {
|
|
119
|
+
throw err;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
107
126
|
function loadAST(dirPath, url = exports.targets.korean, parseOption = parseOptions) {
|
|
108
127
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
109
128
|
const cssPath = getCSSPath(dirPath, url);
|
|
110
|
-
|
|
111
|
-
yield (0, promises_1.mkdir)(dirPath);
|
|
112
|
-
}
|
|
129
|
+
yield existsDir(dirPath);
|
|
113
130
|
if (!(0, fs_1.existsSync)(cssPath)) {
|
|
114
131
|
yield saveCSS(cssPath, url);
|
|
115
132
|
}
|
|
@@ -236,27 +253,60 @@ function getOptionInfos(fontPath = "", fontOption, indexIndicate = "") {
|
|
|
236
253
|
logMsg,
|
|
237
254
|
baseOption,
|
|
238
255
|
worker,
|
|
256
|
+
format,
|
|
239
257
|
fromCSS: options.fromCSS
|
|
240
258
|
};
|
|
241
259
|
}
|
|
242
|
-
function
|
|
243
|
-
const
|
|
244
|
-
|
|
260
|
+
function getURLCandidates(src) {
|
|
261
|
+
const reStr = "url\\(";
|
|
262
|
+
const reMdl = "(.+?)";
|
|
263
|
+
const reEnd = "\\)";
|
|
264
|
+
const quote = "(\\\\?['\"])?";
|
|
265
|
+
const regex = new RegExp(reStr + quote + reMdl + quote + reEnd, "g");
|
|
266
|
+
const candidates = [];
|
|
267
|
+
let matched = regex.exec(src);
|
|
268
|
+
while (matched !== null) {
|
|
269
|
+
const candidate = matched[2].trim();
|
|
270
|
+
if (candidate !== "") {
|
|
271
|
+
candidates.push(candidate);
|
|
272
|
+
}
|
|
273
|
+
matched = regex.exec(src);
|
|
274
|
+
}
|
|
275
|
+
return candidates;
|
|
276
|
+
}
|
|
277
|
+
function getURLCandidateByFormat(candidates, format) {
|
|
278
|
+
const normalizedFormat = getFormat(format).toLowerCase();
|
|
279
|
+
const target = candidates.find((candidate) => {
|
|
280
|
+
const parsedPath = (() => {
|
|
281
|
+
try {
|
|
282
|
+
return new URL(candidate).pathname;
|
|
283
|
+
}
|
|
284
|
+
catch (_a) {
|
|
285
|
+
return candidate;
|
|
286
|
+
}
|
|
287
|
+
})();
|
|
288
|
+
const ext = (0, path_1.parse)(parsedPath.split(/[?#]/)[0])
|
|
289
|
+
.ext.toLowerCase()
|
|
290
|
+
.replace(".", "");
|
|
291
|
+
return ext === normalizedFormat;
|
|
245
292
|
});
|
|
246
|
-
|
|
293
|
+
return (typeof target === "undefined") ? candidates[0] : target;
|
|
294
|
+
}
|
|
295
|
+
function getSrcInfo(src, format) {
|
|
296
|
+
const candidates = getURLCandidates(src);
|
|
297
|
+
if (candidates.length === 0)
|
|
247
298
|
return {
|
|
248
299
|
base: "",
|
|
300
|
+
name: "",
|
|
249
301
|
index: 0
|
|
250
302
|
};
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const
|
|
255
|
-
const regex = new RegExp(reStr + quote + reMdl + quote + reEnd);
|
|
256
|
-
const urlContent = first.match(regex)[2];
|
|
257
|
-
const parsedURL = (0, path_1.parse)(urlContent);
|
|
303
|
+
const srcPath = (typeof format === "undefined")
|
|
304
|
+
? candidates[0]
|
|
305
|
+
: getURLCandidateByFormat(candidates, format);
|
|
306
|
+
const parsedURL = (0, path_1.parse)(srcPath.split(/[?#]/)[0]);
|
|
258
307
|
return {
|
|
259
308
|
base: parsedURL.base,
|
|
309
|
+
name: parsedURL.name,
|
|
260
310
|
index: parseInt(parsedURL.name.split(".").pop())
|
|
261
311
|
};
|
|
262
312
|
}
|
|
@@ -278,15 +328,18 @@ function getSubsetOption(fontSubsetOption) {
|
|
|
278
328
|
}
|
|
279
329
|
function fontRange(fontPath = "", url = exports.targets.korean, fontRangeOption) {
|
|
280
330
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
281
|
-
const { dirPath, initName, logMsg, baseOption, worker, fromCSS } = getOptionInfos(fontPath, fontRangeOption, "n");
|
|
331
|
+
const { dirPath, initName, logMsg, baseOption, worker, format, fromCSS } = getOptionInfos(fontPath, fontRangeOption, "n");
|
|
282
332
|
const ranges = yield parseCSS(dirPath, url);
|
|
283
333
|
const result = ranges.map(({ src, unicodes }, i) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
284
|
-
const srcInfo = getSrcInfo(src);
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
: initName, (fromCSS === "srcIndex" && srcInfo.base !== "")
|
|
334
|
+
const srcInfo = getSrcInfo(src, format);
|
|
335
|
+
const srcIndex = ((fromCSS === "srcIndex" && srcInfo.base !== "") &&
|
|
336
|
+
!Number.isNaN(srcInfo.index))
|
|
288
337
|
? srcInfo.index
|
|
289
|
-
: i
|
|
338
|
+
: i;
|
|
339
|
+
const srcName = (fromCSS === "srcName" && srcInfo.base !== "")
|
|
340
|
+
? srcInfo.name + "." + getFormat(format)
|
|
341
|
+
: initName;
|
|
342
|
+
const saveOption = getSaveOption(dirPath, srcName, srcIndex);
|
|
290
343
|
const unicodeRanges = unicodes.split(", ").join(",");
|
|
291
344
|
const unicodeOption = "--unicodes=" + unicodeRanges;
|
|
292
345
|
const options = [fontPath, saveOption, unicodeOption, ...baseOption];
|
|
@@ -301,8 +354,7 @@ exports.fontRange = fontRange;
|
|
|
301
354
|
function fontSubset(fontPath = "", fontSubsetOption) {
|
|
302
355
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
303
356
|
const { dirPath, initName, logMsg, baseOption, worker } = getOptionInfos(fontPath, fontSubsetOption);
|
|
304
|
-
|
|
305
|
-
yield (0, promises_1.mkdir)(dirPath);
|
|
357
|
+
yield existsDir(dirPath);
|
|
306
358
|
const subsetOption = getSubsetOption(fontSubsetOption);
|
|
307
359
|
const saveOption = getSaveOption(dirPath, initName);
|
|
308
360
|
const options = [fontPath, saveOption, subsetOption, ...baseOption];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "font-range",
|
|
3
3
|
"description": " Font subset with google font's ML result.",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.3",
|
|
5
5
|
"author": "black7375 <alstjr7375@daum.net>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"files": [
|
|
@@ -31,18 +31,18 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/command-exists": "^1.2.0",
|
|
34
|
-
"@types/jest": "^29.
|
|
35
|
-
"@types/node": "^
|
|
36
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
37
|
-
"@typescript-eslint/parser": "^
|
|
34
|
+
"@types/jest": "^29.5.14",
|
|
35
|
+
"@types/node": "^20.4.10",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^6.3.0",
|
|
37
|
+
"@typescript-eslint/parser": "^6.3.0",
|
|
38
38
|
"command-exists": "^1.2.9",
|
|
39
|
-
"eslint": "^8.
|
|
40
|
-
"eslint-config-prettier": "^
|
|
41
|
-
"eslint-plugin-jest": "^27.2.
|
|
42
|
-
"jest": "^29.
|
|
43
|
-
"prettier": "^
|
|
44
|
-
"rimraf": "^
|
|
45
|
-
"ts-jest": "^29.
|
|
39
|
+
"eslint": "^8.47.0",
|
|
40
|
+
"eslint-config-prettier": "^9.0.0",
|
|
41
|
+
"eslint-plugin-jest": "^27.2.3",
|
|
42
|
+
"jest": "^29.7.0",
|
|
43
|
+
"prettier": "^3.0.1",
|
|
44
|
+
"rimraf": "^5.0.1",
|
|
45
|
+
"ts-jest": "^29.4.6",
|
|
46
46
|
"tsutils": "^3.21.0",
|
|
47
47
|
"typescript": "^4.9.4"
|
|
48
48
|
},
|
|
@@ -53,16 +53,16 @@
|
|
|
53
53
|
"build": "tsc -p tsconfig.release.json",
|
|
54
54
|
"build:watch": "tsc -w -p tsconfig.release.json",
|
|
55
55
|
"lint": "eslint . --ext .ts,.tsx",
|
|
56
|
-
"test": "jest --coverage --detectOpenHandles",
|
|
56
|
+
"test": "jest --coverage --detectOpenHandles --forceExit --testTimeout=120000",
|
|
57
57
|
"test:watch": "jest --watch"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@esm2cjs/execa": "^6.1.1-cjs.1",
|
|
61
|
-
"@esm2cjs/node-fetch": "^3.
|
|
62
|
-
"@types/css-tree": "^2.
|
|
61
|
+
"@esm2cjs/node-fetch": "^3.3.1",
|
|
62
|
+
"@types/css-tree": "^2.3.1",
|
|
63
63
|
"css-tree": "^2.3.1",
|
|
64
64
|
"piscina": "^3.2.0",
|
|
65
|
-
"tslib": "^2.
|
|
65
|
+
"tslib": "^2.6.1"
|
|
66
66
|
},
|
|
67
67
|
"volta": {
|
|
68
68
|
"node": "16.16.0",
|