fodec 1.0.1 → 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/bin/fodec.js +1 -11
- package/lib/Fodec.d.ts +2 -3
- package/lib/Fodec.js +18 -53
- package/lib/cli/App.d.ts +1 -1
- package/lib/cli/App.js +106 -72
- package/lib/utils/encodeFont.d.ts +1 -0
- package/lib/utils/encodeFont.js +49 -0
- package/package.json +7 -9
package/bin/fodec.js
CHANGED
|
@@ -2,14 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
const {app} = require("../lib/cli/index.js");
|
|
4
4
|
|
|
5
|
-
app.run(
|
|
6
|
-
if(!res) {
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
process.stdout.write(res);
|
|
11
|
-
process.stdout.write("\n");
|
|
12
|
-
}).catch((err) => {
|
|
13
|
-
process.stderr.write(err.message);
|
|
14
|
-
process.stdout.write("\n");
|
|
15
|
-
});
|
|
5
|
+
app.run();
|
package/lib/Fodec.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
export declare class Fodec {
|
|
2
|
-
protected key: string;
|
|
2
|
+
protected readonly key: string;
|
|
3
3
|
constructor(key: string);
|
|
4
4
|
transformText(str: string): string;
|
|
5
5
|
transformJson(data: any): Promise<any>;
|
|
6
|
-
transformFont(fontPath: string): Promise<
|
|
7
|
-
transformFontV1(fontPath: string): Promise<string | ArrayBuffer | undefined>;
|
|
6
|
+
transformFont(fontPath: string): Promise<import("fonteditor-core").FontEditor.FontOutput | null>;
|
|
8
7
|
}
|
package/lib/Fodec.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
@@ -6,7 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
15
|
exports.Fodec = void 0;
|
|
7
16
|
const path_1 = __importDefault(require("path"));
|
|
8
17
|
const fs_1 = require("fs");
|
|
9
|
-
const
|
|
18
|
+
const encodeFont_1 = require("./utils/encodeFont");
|
|
10
19
|
class Fodec {
|
|
11
20
|
constructor(key) {
|
|
12
21
|
this.key = key;
|
|
@@ -17,59 +26,15 @@ class Fodec {
|
|
|
17
26
|
return index !== -1 ? this.key.charAt(index + 1 + (this.key.length - 1) / 2) : char;
|
|
18
27
|
}).join("");
|
|
19
28
|
}
|
|
20
|
-
|
|
21
|
-
return
|
|
22
|
-
|
|
23
|
-
async transformFont(fontPath) {
|
|
24
|
-
return this.transformFontV1(fontPath);
|
|
25
|
-
}
|
|
26
|
-
async transformFontV1(fontPath) {
|
|
27
|
-
const ext = path_1.default.extname(fontPath).slice(1);
|
|
28
|
-
if (ext !== "ttf") {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
const font = fonteditor_core_1.Font.create(await fs_1.promises.readFile(fontPath), {
|
|
32
|
-
type: ext
|
|
29
|
+
transformJson(data) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
return data;
|
|
33
32
|
});
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
? String.fromCharCode(glyph.unicode[0])
|
|
40
|
-
: null;
|
|
41
|
-
if (!char) {
|
|
42
|
-
glyphs.push(glyph);
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
if (this.key.indexOf(char) === -1) {
|
|
46
|
-
glyphs.push(glyph);
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
const index = this.key.indexOf(char);
|
|
50
|
-
const transformUnicode = this.key.charCodeAt((this.key.length - 1) / 2 + 1 + index);
|
|
51
|
-
const rand = list.find((item) => {
|
|
52
|
-
if (!item.unicode) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
return item.unicode[0] === transformUnicode;
|
|
56
|
-
});
|
|
57
|
-
if (!rand) {
|
|
58
|
-
glyphs.push(glyph);
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
glyphs.push({
|
|
62
|
-
...glyph,
|
|
63
|
-
name: rand.name,
|
|
64
|
-
unicode: rand.unicode
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
font.set({
|
|
68
|
-
...font.get(),
|
|
69
|
-
glyf: glyphs
|
|
70
|
-
});
|
|
71
|
-
return font.write({
|
|
72
|
-
type: ext
|
|
33
|
+
}
|
|
34
|
+
transformFont(fontPath) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const file = yield fs_1.promises.readFile(fontPath), ext = path_1.default.extname(fontPath).slice(1);
|
|
37
|
+
return (0, encodeFont_1.encodeFont)(this.key, file, ext);
|
|
73
38
|
});
|
|
74
39
|
}
|
|
75
40
|
}
|
package/lib/cli/App.d.ts
CHANGED
package/lib/cli/App.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
@@ -14,6 +23,9 @@ class App {
|
|
|
14
23
|
constructor() {
|
|
15
24
|
this.cli = new cli_1.Cli();
|
|
16
25
|
this.cli.command("completion")
|
|
26
|
+
.help({
|
|
27
|
+
disabled: true
|
|
28
|
+
})
|
|
17
29
|
.action(() => {
|
|
18
30
|
return this.cli.completionScript();
|
|
19
31
|
});
|
|
@@ -27,90 +39,112 @@ class App {
|
|
|
27
39
|
this.cli.command("build")
|
|
28
40
|
.action(() => this.build());
|
|
29
41
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const res = await fs_1.promises.readFile("fodec.key");
|
|
40
|
-
return res.toString();
|
|
42
|
+
getConfig() {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
const configPath = "fodec.json";
|
|
45
|
+
if (!(0, fs_1.existsSync)(configPath)) {
|
|
46
|
+
throw new Error("Config not found");
|
|
47
|
+
}
|
|
48
|
+
const res = yield fs_1.promises.readFile(configPath);
|
|
49
|
+
return JSON.parse(res.toString());
|
|
50
|
+
});
|
|
41
51
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
let config = {};
|
|
48
|
-
config.basePath = await (0, utils_1.promptText)({
|
|
49
|
-
required: true,
|
|
50
|
-
type: "string",
|
|
51
|
-
message: "Base Path:",
|
|
52
|
-
default: "src"
|
|
52
|
+
getKey() {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
const res = yield fs_1.promises.readFile("fodec.key");
|
|
55
|
+
return res.toString();
|
|
53
56
|
});
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
}
|
|
58
|
+
init() {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const configPath = "./fodec-2.json";
|
|
61
|
+
if ((0, fs_1.existsSync)(configPath)) {
|
|
62
|
+
throw new Error("Config already exists");
|
|
63
|
+
}
|
|
64
|
+
let config = {};
|
|
65
|
+
config.basePath = yield (0, utils_1.promptText)({
|
|
56
66
|
required: true,
|
|
57
67
|
type: "string",
|
|
58
|
-
message: "
|
|
59
|
-
default: "
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
message: "Base Path:",
|
|
69
|
+
default: "src"
|
|
70
|
+
});
|
|
71
|
+
config.output = {
|
|
72
|
+
path: yield (0, utils_1.promptText)({
|
|
73
|
+
required: true,
|
|
74
|
+
type: "string",
|
|
75
|
+
message: "Output path:",
|
|
76
|
+
default: "build"
|
|
77
|
+
})
|
|
78
|
+
};
|
|
79
|
+
config.alphabets = (yield (0, utils_1.promptSelect)({
|
|
80
|
+
multiple: true,
|
|
81
|
+
options: [
|
|
82
|
+
"cyrillic",
|
|
83
|
+
"latin"
|
|
84
|
+
]
|
|
85
|
+
}));
|
|
86
|
+
config.resources = [];
|
|
87
|
+
yield fs_1.promises.writeFile(configPath, JSON.stringify(config, null, 4));
|
|
68
88
|
});
|
|
69
|
-
config.resources = [];
|
|
70
|
-
await fs_1.promises.writeFile(configPath, JSON.stringify(config, null, 4));
|
|
71
89
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
90
|
+
generate() {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
const { alphabets } = yield this.getConfig();
|
|
93
|
+
const key = (0, generateKey_1.generateKey)({
|
|
94
|
+
alphabets
|
|
95
|
+
});
|
|
96
|
+
console.log(key);
|
|
97
|
+
yield fs_1.promises.writeFile("fodec.key", key);
|
|
76
98
|
});
|
|
77
|
-
console.log(key);
|
|
78
|
-
await fs_1.promises.writeFile("fodec.key", key);
|
|
79
99
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
100
|
+
build() {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
const key = yield this.getKey();
|
|
103
|
+
const { basePath = "./", output: { path: outPath = "./build" } = {}, fonts, resources } = yield this.getConfig();
|
|
104
|
+
const fodec = new Fodec_1.Fodec(key);
|
|
105
|
+
for (const resource of resources) {
|
|
106
|
+
let res;
|
|
107
|
+
if (resource.type === "font") {
|
|
108
|
+
const transformedFont = yield fodec.transformFont(path_1.default.join(basePath, resource.path));
|
|
109
|
+
if (!transformedFont || transformedFont instanceof ArrayBuffer) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
res = transformedFont;
|
|
113
|
+
}
|
|
114
|
+
else if (resource.type === "json") {
|
|
115
|
+
const json = yield fs_1.promises.readFile(path_1.default.join(basePath, resource.path));
|
|
116
|
+
const data = JSON.parse(json.toString());
|
|
117
|
+
res = JSON.stringify(data, (key, value) => {
|
|
118
|
+
if (typeof value === "string") {
|
|
119
|
+
return fodec.transformText(value);
|
|
120
|
+
}
|
|
121
|
+
return value;
|
|
122
|
+
}, 4);
|
|
123
|
+
}
|
|
124
|
+
if (res) {
|
|
125
|
+
const outFullPath = path_1.default.join(outPath, path_1.default.dirname(resource.output || resource.path));
|
|
126
|
+
(0, fs_1.mkdirSync)(outFullPath, {
|
|
127
|
+
recursive: true
|
|
128
|
+
});
|
|
129
|
+
yield fs_1.promises.writeFile(path_1.default.join(outPath, resource.output || resource.path), res);
|
|
90
130
|
}
|
|
91
|
-
res = transformedFont;
|
|
92
131
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
run() {
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
try {
|
|
137
|
+
const res = yield this.cli.run(process.argv);
|
|
138
|
+
if (res) {
|
|
139
|
+
process.stdout.write(res);
|
|
140
|
+
process.stdout.write("\n");
|
|
141
|
+
}
|
|
102
142
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
(
|
|
106
|
-
recursive: true
|
|
107
|
-
});
|
|
108
|
-
await fs_1.promises.writeFile(path_1.default.join(outPath, resource.output || resource.path), res);
|
|
143
|
+
catch (err) {
|
|
144
|
+
process.stderr.write(err.message);
|
|
145
|
+
process.stdout.write("\n");
|
|
109
146
|
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
async run() {
|
|
113
|
-
return this.cli.run(process.argv);
|
|
147
|
+
});
|
|
114
148
|
}
|
|
115
149
|
}
|
|
116
150
|
exports.App = App;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const encodeFont: (key: string, file: string | Buffer, ext: string) => import("fonteditor-core").FontEditor.FontOutput | null;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeFont = void 0;
|
|
4
|
+
const fonteditor_core_1 = require("fonteditor-core");
|
|
5
|
+
const encodeFont = (key, file, ext) => {
|
|
6
|
+
if (ext !== "ttf") {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
if (typeof file === "string") {
|
|
10
|
+
file = Buffer.from(file);
|
|
11
|
+
}
|
|
12
|
+
const font = fonteditor_core_1.Font.create(file, {
|
|
13
|
+
type: ext
|
|
14
|
+
});
|
|
15
|
+
const { glyf: list } = font.get();
|
|
16
|
+
const glyphs = [];
|
|
17
|
+
for (let i = 0; i < list.length; i++) {
|
|
18
|
+
const glyph = list[i];
|
|
19
|
+
const char = glyph.unicode
|
|
20
|
+
? String.fromCharCode(glyph.unicode[0])
|
|
21
|
+
: null;
|
|
22
|
+
if (!char) {
|
|
23
|
+
glyphs.push(glyph);
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (key.indexOf(char) === -1) {
|
|
27
|
+
glyphs.push(glyph);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const index = key.indexOf(char);
|
|
31
|
+
const transformUnicode = key.charCodeAt((key.length - 1) / 2 + 1 + index);
|
|
32
|
+
const rand = list.find((item) => {
|
|
33
|
+
if (!item.unicode) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
return item.unicode[0] === transformUnicode;
|
|
37
|
+
});
|
|
38
|
+
if (!rand) {
|
|
39
|
+
glyphs.push(glyph);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
glyphs.push(Object.assign(Object.assign({}, glyph), { name: rand.name, unicode: rand.unicode }));
|
|
43
|
+
}
|
|
44
|
+
font.set(Object.assign(Object.assign({}, font.get()), { glyf: glyphs }));
|
|
45
|
+
return font.write({
|
|
46
|
+
type: ext
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
exports.encodeFont = encodeFont;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fodec",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"author": "Kris Papercut <krispcut@gmail.com>",
|
|
5
5
|
"description": "Font encoder",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
"fodec": "./bin/fodec.js"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"
|
|
17
|
-
"build": "tsc --build",
|
|
18
|
-
"watch": "tsc --
|
|
16
|
+
"prepublishOnly": "npm run build",
|
|
17
|
+
"build": "tsc --project tsconfig.build.json",
|
|
18
|
+
"watch": "tsc -w --project tsconfig.build.json",
|
|
19
19
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
20
20
|
},
|
|
21
21
|
"homepage": "https://kearisp.github.io/fodec",
|
|
@@ -27,14 +27,12 @@
|
|
|
27
27
|
"url": "https://github.com/kearisp/fodec/issues"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@kearisp/cli": "^2.0.
|
|
30
|
+
"@kearisp/cli": "^2.0.4",
|
|
31
31
|
"@wocker/utils": "^1.0.2",
|
|
32
|
-
"fonteditor-core": "^2.3.2"
|
|
33
|
-
"fs": "^0.0.1-security",
|
|
34
|
-
"path": "^0.12.7"
|
|
32
|
+
"fonteditor-core": "^2.3.2"
|
|
35
33
|
},
|
|
36
34
|
"devDependencies": {
|
|
37
|
-
"@types/node": "^
|
|
35
|
+
"@types/node": "^22.10.2",
|
|
38
36
|
"typescript": "^5.3.3"
|
|
39
37
|
}
|
|
40
38
|
}
|