@nanawan/webfont 0.0.11 → 0.0.13
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/index.cjs +200 -0
- package/dist/index.js +163 -0
- package/package.json +3 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
webfont: () => webfont
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/glyphsData.ts
|
|
38
|
+
var import_fs_extra = __toESM(require("fs-extra"), 1);
|
|
39
|
+
var import_metadata = __toESM(require("svgicons2svgfont/src/metadata.js"), 1);
|
|
40
|
+
var import_xml2js = __toESM(require("xml2js"), 1);
|
|
41
|
+
var getGlyphsData = (files, options) => {
|
|
42
|
+
const metadataProvider = options.metadataProvider || (0, import_metadata.default)({
|
|
43
|
+
prependUnicode: options.prependUnicode,
|
|
44
|
+
startUnicode: options.startUnicode
|
|
45
|
+
});
|
|
46
|
+
const xmlParser = new import_xml2js.default.Parser();
|
|
47
|
+
return Promise.all(
|
|
48
|
+
files.map(
|
|
49
|
+
(srcPath) => new Promise((resolve, reject) => {
|
|
50
|
+
const glyphContents = import_fs_extra.default.readFileSync(srcPath, "utf-8");
|
|
51
|
+
if (glyphContents.length === 0) {
|
|
52
|
+
reject(new Error(`Empty file ${srcPath}`));
|
|
53
|
+
}
|
|
54
|
+
xmlParser.parseString(glyphContents, (error) => {
|
|
55
|
+
if (error) {
|
|
56
|
+
reject(error);
|
|
57
|
+
}
|
|
58
|
+
const glyphData = {
|
|
59
|
+
contents: glyphContents,
|
|
60
|
+
srcPath
|
|
61
|
+
};
|
|
62
|
+
resolve(glyphData);
|
|
63
|
+
});
|
|
64
|
+
})
|
|
65
|
+
)
|
|
66
|
+
).then((glyphsData) => {
|
|
67
|
+
const { ligatures } = options;
|
|
68
|
+
return Promise.all(
|
|
69
|
+
glyphsData.map(
|
|
70
|
+
(glyphData) => new Promise((resolve, reject) => {
|
|
71
|
+
metadataProvider(glyphData.srcPath, (error, metadata) => {
|
|
72
|
+
if (error) {
|
|
73
|
+
return reject(error);
|
|
74
|
+
}
|
|
75
|
+
if (ligatures) {
|
|
76
|
+
metadata.unicode.push(metadata.name.replace(/-/gu, "_"));
|
|
77
|
+
}
|
|
78
|
+
glyphData.metadata = metadata;
|
|
79
|
+
return resolve(glyphData);
|
|
80
|
+
});
|
|
81
|
+
})
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// src/options.ts
|
|
88
|
+
var getOptions = (initialOptions) => {
|
|
89
|
+
if (!initialOptions || !initialOptions.files) {
|
|
90
|
+
throw new Error("You must pass webfont a `files` glob");
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
centerHorizontally: false,
|
|
94
|
+
descent: 0,
|
|
95
|
+
fixedWidth: false,
|
|
96
|
+
fontHeight: null,
|
|
97
|
+
fontId: null,
|
|
98
|
+
fontName: "webfont",
|
|
99
|
+
fontStyle: "",
|
|
100
|
+
fontWeight: "",
|
|
101
|
+
formats: ["svg", "ttf"],
|
|
102
|
+
formatsOptions: {
|
|
103
|
+
ttf: {
|
|
104
|
+
copyright: null,
|
|
105
|
+
ts: null,
|
|
106
|
+
version: null
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
glyphTransformFn: void 0,
|
|
110
|
+
ligatures: true,
|
|
111
|
+
maxConcurrency: 100,
|
|
112
|
+
metadata: null,
|
|
113
|
+
metadataProvider: null,
|
|
114
|
+
normalize: false,
|
|
115
|
+
prependUnicode: false,
|
|
116
|
+
round: 1e13,
|
|
117
|
+
startUnicode: 59905,
|
|
118
|
+
...initialOptions
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// src/index.ts
|
|
123
|
+
var import_stream = require("stream");
|
|
124
|
+
var import_shared = require("@nanawan/shared");
|
|
125
|
+
var import_svgicons2svgfont = __toESM(require("svgicons2svgfont"), 1);
|
|
126
|
+
var import_globby = __toESM(require("globby"), 1);
|
|
127
|
+
var import_svg2ttf = __toESM(require("svg2ttf"), 1);
|
|
128
|
+
var toSvg = (glyphsData, options) => {
|
|
129
|
+
let result = "";
|
|
130
|
+
return new Promise((resolve, reject) => {
|
|
131
|
+
const fontStream = new import_svgicons2svgfont.default({
|
|
132
|
+
ascent: options.ascent,
|
|
133
|
+
centerHorizontally: options.centerHorizontally,
|
|
134
|
+
descent: options.descent,
|
|
135
|
+
fixedWidth: options.fixedWidth,
|
|
136
|
+
fontHeight: options.fontHeight,
|
|
137
|
+
fontId: options.fontId,
|
|
138
|
+
fontName: options.fontName,
|
|
139
|
+
fontStyle: options.fontStyle,
|
|
140
|
+
fontWeight: options.fontWeight,
|
|
141
|
+
log: () => {
|
|
142
|
+
},
|
|
143
|
+
metadata: options.metadata,
|
|
144
|
+
normalize: options.normalize,
|
|
145
|
+
round: options.round
|
|
146
|
+
}).on("finish", () => resolve(result)).on("data", (data) => {
|
|
147
|
+
result += data;
|
|
148
|
+
}).on("error", (error) => reject(error));
|
|
149
|
+
glyphsData.forEach((glyphData) => {
|
|
150
|
+
const glyphStream = new import_stream.Readable();
|
|
151
|
+
glyphStream.push(glyphData.contents);
|
|
152
|
+
glyphStream.push(null);
|
|
153
|
+
glyphStream.metadata = glyphData.metadata;
|
|
154
|
+
fontStream.write(glyphStream);
|
|
155
|
+
});
|
|
156
|
+
fontStream.end();
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
var toTtf = (buffer, options) => Buffer.from((0, import_svg2ttf.default)(buffer, options).buffer);
|
|
160
|
+
var webfont = async (initialOptions) => {
|
|
161
|
+
const options = getOptions(initialOptions);
|
|
162
|
+
const foundFiles = await (0, import_globby.default)((0, import_shared.normalizeToArray)(options.files));
|
|
163
|
+
if (foundFiles.length === 0) {
|
|
164
|
+
throw new Error("Files glob patterns specified did not match any files");
|
|
165
|
+
}
|
|
166
|
+
let glyphsData = await getGlyphsData(foundFiles, options);
|
|
167
|
+
if ((0, import_shared.isFunction)(options.glyphTransformFn)) {
|
|
168
|
+
const transformedGlyphs = glyphsData.map(async (glyphData) => {
|
|
169
|
+
const metadata = await options.glyphTransformFn(glyphData.metadata);
|
|
170
|
+
return {
|
|
171
|
+
...glyphData,
|
|
172
|
+
metadata
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
glyphsData = await Promise.all(transformedGlyphs);
|
|
176
|
+
}
|
|
177
|
+
let ttfOptions = {};
|
|
178
|
+
if (options.formatsOptions && options.formatsOptions.ttf) {
|
|
179
|
+
ttfOptions = options.formatsOptions.ttf;
|
|
180
|
+
}
|
|
181
|
+
const svg = await toSvg(glyphsData, options);
|
|
182
|
+
const ttf = toTtf(svg, ttfOptions);
|
|
183
|
+
const result = {
|
|
184
|
+
glyphsData,
|
|
185
|
+
svg,
|
|
186
|
+
ttf
|
|
187
|
+
};
|
|
188
|
+
if (!options.formats.includes("svg")) {
|
|
189
|
+
delete result.svg;
|
|
190
|
+
}
|
|
191
|
+
if (!options.formats.includes("ttf")) {
|
|
192
|
+
delete result.ttf;
|
|
193
|
+
}
|
|
194
|
+
result.config = options;
|
|
195
|
+
return result;
|
|
196
|
+
};
|
|
197
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
198
|
+
0 && (module.exports = {
|
|
199
|
+
webfont
|
|
200
|
+
});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// src/glyphsData.ts
|
|
2
|
+
import fse from "fs-extra";
|
|
3
|
+
import getMetadataService from "svgicons2svgfont/src/metadata.js";
|
|
4
|
+
import xml2js from "xml2js";
|
|
5
|
+
var getGlyphsData = (files, options) => {
|
|
6
|
+
const metadataProvider = options.metadataProvider || getMetadataService({
|
|
7
|
+
prependUnicode: options.prependUnicode,
|
|
8
|
+
startUnicode: options.startUnicode
|
|
9
|
+
});
|
|
10
|
+
const xmlParser = new xml2js.Parser();
|
|
11
|
+
return Promise.all(
|
|
12
|
+
files.map(
|
|
13
|
+
(srcPath) => new Promise((resolve, reject) => {
|
|
14
|
+
const glyphContents = fse.readFileSync(srcPath, "utf-8");
|
|
15
|
+
if (glyphContents.length === 0) {
|
|
16
|
+
reject(new Error(`Empty file ${srcPath}`));
|
|
17
|
+
}
|
|
18
|
+
xmlParser.parseString(glyphContents, (error) => {
|
|
19
|
+
if (error) {
|
|
20
|
+
reject(error);
|
|
21
|
+
}
|
|
22
|
+
const glyphData = {
|
|
23
|
+
contents: glyphContents,
|
|
24
|
+
srcPath
|
|
25
|
+
};
|
|
26
|
+
resolve(glyphData);
|
|
27
|
+
});
|
|
28
|
+
})
|
|
29
|
+
)
|
|
30
|
+
).then((glyphsData) => {
|
|
31
|
+
const { ligatures } = options;
|
|
32
|
+
return Promise.all(
|
|
33
|
+
glyphsData.map(
|
|
34
|
+
(glyphData) => new Promise((resolve, reject) => {
|
|
35
|
+
metadataProvider(glyphData.srcPath, (error, metadata) => {
|
|
36
|
+
if (error) {
|
|
37
|
+
return reject(error);
|
|
38
|
+
}
|
|
39
|
+
if (ligatures) {
|
|
40
|
+
metadata.unicode.push(metadata.name.replace(/-/gu, "_"));
|
|
41
|
+
}
|
|
42
|
+
glyphData.metadata = metadata;
|
|
43
|
+
return resolve(glyphData);
|
|
44
|
+
});
|
|
45
|
+
})
|
|
46
|
+
)
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/options.ts
|
|
52
|
+
var getOptions = (initialOptions) => {
|
|
53
|
+
if (!initialOptions || !initialOptions.files) {
|
|
54
|
+
throw new Error("You must pass webfont a `files` glob");
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
centerHorizontally: false,
|
|
58
|
+
descent: 0,
|
|
59
|
+
fixedWidth: false,
|
|
60
|
+
fontHeight: null,
|
|
61
|
+
fontId: null,
|
|
62
|
+
fontName: "webfont",
|
|
63
|
+
fontStyle: "",
|
|
64
|
+
fontWeight: "",
|
|
65
|
+
formats: ["svg", "ttf"],
|
|
66
|
+
formatsOptions: {
|
|
67
|
+
ttf: {
|
|
68
|
+
copyright: null,
|
|
69
|
+
ts: null,
|
|
70
|
+
version: null
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
glyphTransformFn: void 0,
|
|
74
|
+
ligatures: true,
|
|
75
|
+
maxConcurrency: 100,
|
|
76
|
+
metadata: null,
|
|
77
|
+
metadataProvider: null,
|
|
78
|
+
normalize: false,
|
|
79
|
+
prependUnicode: false,
|
|
80
|
+
round: 1e13,
|
|
81
|
+
startUnicode: 59905,
|
|
82
|
+
...initialOptions
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/index.ts
|
|
87
|
+
import { Readable } from "stream";
|
|
88
|
+
import { isFunction, normalizeToArray } from "@nanawan/shared";
|
|
89
|
+
import SVGIcons2SVGFontStream from "svgicons2svgfont";
|
|
90
|
+
import globby from "globby";
|
|
91
|
+
import svg2ttf from "svg2ttf";
|
|
92
|
+
var toSvg = (glyphsData, options) => {
|
|
93
|
+
let result = "";
|
|
94
|
+
return new Promise((resolve, reject) => {
|
|
95
|
+
const fontStream = new SVGIcons2SVGFontStream({
|
|
96
|
+
ascent: options.ascent,
|
|
97
|
+
centerHorizontally: options.centerHorizontally,
|
|
98
|
+
descent: options.descent,
|
|
99
|
+
fixedWidth: options.fixedWidth,
|
|
100
|
+
fontHeight: options.fontHeight,
|
|
101
|
+
fontId: options.fontId,
|
|
102
|
+
fontName: options.fontName,
|
|
103
|
+
fontStyle: options.fontStyle,
|
|
104
|
+
fontWeight: options.fontWeight,
|
|
105
|
+
log: () => {
|
|
106
|
+
},
|
|
107
|
+
metadata: options.metadata,
|
|
108
|
+
normalize: options.normalize,
|
|
109
|
+
round: options.round
|
|
110
|
+
}).on("finish", () => resolve(result)).on("data", (data) => {
|
|
111
|
+
result += data;
|
|
112
|
+
}).on("error", (error) => reject(error));
|
|
113
|
+
glyphsData.forEach((glyphData) => {
|
|
114
|
+
const glyphStream = new Readable();
|
|
115
|
+
glyphStream.push(glyphData.contents);
|
|
116
|
+
glyphStream.push(null);
|
|
117
|
+
glyphStream.metadata = glyphData.metadata;
|
|
118
|
+
fontStream.write(glyphStream);
|
|
119
|
+
});
|
|
120
|
+
fontStream.end();
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
var toTtf = (buffer, options) => Buffer.from(svg2ttf(buffer, options).buffer);
|
|
124
|
+
var webfont = async (initialOptions) => {
|
|
125
|
+
const options = getOptions(initialOptions);
|
|
126
|
+
const foundFiles = await globby(normalizeToArray(options.files));
|
|
127
|
+
if (foundFiles.length === 0) {
|
|
128
|
+
throw new Error("Files glob patterns specified did not match any files");
|
|
129
|
+
}
|
|
130
|
+
let glyphsData = await getGlyphsData(foundFiles, options);
|
|
131
|
+
if (isFunction(options.glyphTransformFn)) {
|
|
132
|
+
const transformedGlyphs = glyphsData.map(async (glyphData) => {
|
|
133
|
+
const metadata = await options.glyphTransformFn(glyphData.metadata);
|
|
134
|
+
return {
|
|
135
|
+
...glyphData,
|
|
136
|
+
metadata
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
glyphsData = await Promise.all(transformedGlyphs);
|
|
140
|
+
}
|
|
141
|
+
let ttfOptions = {};
|
|
142
|
+
if (options.formatsOptions && options.formatsOptions.ttf) {
|
|
143
|
+
ttfOptions = options.formatsOptions.ttf;
|
|
144
|
+
}
|
|
145
|
+
const svg = await toSvg(glyphsData, options);
|
|
146
|
+
const ttf = toTtf(svg, ttfOptions);
|
|
147
|
+
const result = {
|
|
148
|
+
glyphsData,
|
|
149
|
+
svg,
|
|
150
|
+
ttf
|
|
151
|
+
};
|
|
152
|
+
if (!options.formats.includes("svg")) {
|
|
153
|
+
delete result.svg;
|
|
154
|
+
}
|
|
155
|
+
if (!options.formats.includes("ttf")) {
|
|
156
|
+
delete result.ttf;
|
|
157
|
+
}
|
|
158
|
+
result.config = options;
|
|
159
|
+
return result;
|
|
160
|
+
};
|
|
161
|
+
export {
|
|
162
|
+
webfont
|
|
163
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nanawan/webfont",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "Generator of fonts from svg icons, svg icons to svg font, svg font to ttf, ttf to eot, ttf to woff, ttf to woff2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@nanawan/shared": "^3.18.
|
|
26
|
+
"@nanawan/shared": "^3.18.6",
|
|
27
27
|
"fs-extra": "^11.2.0",
|
|
28
28
|
"globby": "^11.0.0",
|
|
29
29
|
"p-limit": "^3.1.0",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/fs-extra": "11.0.4",
|
|
36
36
|
"@types/node": "15.14.9",
|
|
37
|
-
"@nanawan/release": "^2.2.
|
|
37
|
+
"@nanawan/release": "^2.2.8",
|
|
38
38
|
"rimraf": "5.0.7",
|
|
39
39
|
"tsup": "^8.0.1",
|
|
40
40
|
"typescript": "^5.2.2"
|