Skip to content

Commit eab791a

Browse files
committed
[bin/ot] add woff_2_otf as subcommand
1 parent c8961ee commit eab791a

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

bin/ot

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env node
22
/* eslint no-console: off */
33

4-
import fs from 'fs';
5-
import path from 'path';
6-
import { load } from '../src/opentype';
4+
const process = require('process')
5+
, fs = require('fs')
6+
, path = require('path')
7+
, { load, woff_to_otf } = require('../dist/opentype.js')
8+
;
79

810
// Print out information about the font on the console.
911
function printFontInfo(font) {
@@ -30,11 +32,13 @@ function walk(dir, fn) {
3032

3133
// Print out usage information.
3234
function printUsage() {
33-
console.log('Usage: ot command [dir|file]');
35+
console.log('Usage: ot <command> [...args]');
3436
console.log();
3537
console.log('Commands:');
3638
console.log();
37-
console.log(' info Get information of specified font or fonts in the specified directory.');
39+
console.log(' info [dir|file] Get information of specified font or fonts in the specified directory.');
40+
console.log();
41+
console.log(' woff_2_otf <woff_font_file> [otf_font_file] Uncompress woff file into otf.');
3842
console.log();
3943
}
4044

@@ -65,7 +69,7 @@ if (process.argv.length < 3) {
6569
} else {
6670
var command = process.argv[2];
6771
if (command === 'info') {
68-
var fontpath = process.argv.length === 3 ? '.' : process.argv[3];
72+
var fontpath = process.argv.length === 4 ? '.' : process.argv[3];
6973
if (fs.existsSync(fontpath)) {
7074
var ext = path.extname(fontpath).toLowerCase();
7175
if (fs.statSync(fontpath).isDirectory()) {
@@ -78,6 +82,24 @@ if (process.argv.length < 3) {
7882
} else {
7983
console.log('Path not found');
8084
}
85+
}
86+
else if (command === 'woff_2_otf') {
87+
if(process.argv.length < 4 ){
88+
printUsage();
89+
process.exit(1);
90+
}
91+
const fontpath = process.argv[3];
92+
if (!fs.existsSync(fontpath)) {
93+
console.log(`Font file at path not found.`);
94+
process.exit(1);
95+
}
96+
const targetPath = process.argv.length >= 5
97+
? process.argv[4]
98+
: `${path.join(path.dirname(fontpath), path.basename(fontpath, path.extname(fontpath)))}.otf`
99+
, buffer = fs.readFileSync(fontpath)
100+
, result = woff_to_otf(buffer)
101+
;
102+
fs.writeFileSync(targetPath, new DataView(result));
81103
} else {
82104
printUsage();
83105
}

0 commit comments

Comments
 (0)