ttf2eot
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -0,1 +1,9 @@ | ||
3.0.0 / 2021-07-29 | ||
------------------ | ||
- Use node.js `Buffer` instead of `microbuffer` module internally. | ||
- `require('ttf2eot')(buf)` now accepts `Uint8Array` and returns `Uint8Array` instead | ||
of `microbuffer` (you now should use `result` directly instead of `result.buffer`). | ||
2.0.0 / 2016-03-06 | ||
@@ -2,0 +10,0 @@ ------------------ |
121
index.js
@@ -9,3 +9,2 @@ /* | ||
var ByteBuffer = require('microbuffer'); | ||
@@ -91,37 +90,37 @@ /** | ||
function strbuf(str) { | ||
var b = new ByteBuffer(str.length + 4); | ||
var arr = Buffer.alloc(str.length + 4); | ||
b.setUint16 (0, str.length, true); | ||
arr.writeUint16LE(str.length, 0); | ||
for (var i = 0; i < str.length; i += 2) { | ||
b.setUint16 (i + 2, str.getUint16 (i), true); | ||
arr.writeUint16LE(str.readUint16BE(i), i + 2); | ||
} | ||
b.setUint16 (b.length - 2, 0, true); | ||
arr.writeUint16LE(0, arr.length - 2); | ||
return b; | ||
return arr; | ||
} | ||
// Takes TTF font on input and returns ByteBuffer with EOT font | ||
// Takes TTF font on input and returns Uint8Array with EOT font | ||
// | ||
// Params: | ||
// | ||
// - arr(Array|Uint8Array) | ||
// - arr(Uint8Array) | ||
// | ||
function ttf2eot(arr) { | ||
var buf = new ByteBuffer(arr); | ||
var out = new ByteBuffer(SIZEOF.EOT_PREFIX), | ||
i, j; | ||
arr = Buffer.from(arr.buffer, arr.byteOffset, arr.length); | ||
out.fill(0); | ||
out.setUint32(EOT_OFFSET.FONT_LENGTH, buf.length, true); | ||
out.setUint32(EOT_OFFSET.VERSION, MAGIC.EOT_VERSION, true); | ||
out.setUint8(EOT_OFFSET.CHARSET, MAGIC.EOT_CHARSET); | ||
out.setUint16(EOT_OFFSET.MAGIC, MAGIC.EOT_MAGIC, true); | ||
var out = Buffer.alloc(SIZEOF.EOT_PREFIX, 0); | ||
var i, j; | ||
var familyName = [], | ||
subfamilyName = [], | ||
fullName = [], | ||
versionString = []; | ||
out.writeUint32LE (arr.length, EOT_OFFSET.FONT_LENGTH); | ||
out.writeUint32LE (MAGIC.EOT_VERSION, EOT_OFFSET.VERSION); | ||
out.writeUint8 (MAGIC.EOT_CHARSET, EOT_OFFSET.CHARSET); | ||
out.writeUint16LE (MAGIC.EOT_MAGIC, EOT_OFFSET.MAGIC); | ||
var familyName = Buffer.of(0), | ||
subfamilyName = Buffer.of(0), | ||
fullName = Buffer.of(0), | ||
versionString = Buffer.of(0); | ||
var haveOS2 = false, | ||
@@ -131,13 +130,13 @@ haveName = false, | ||
var numTables = buf.getUint16 (SFNT_OFFSET.NUMTABLES); | ||
var numTables = arr.readUint16BE (SFNT_OFFSET.NUMTABLES); | ||
for (i = 0; i < numTables; ++i) { | ||
var data = new ByteBuffer(buf, SIZEOF.SFNT_HEADER + i * SIZEOF.SFNT_TABLE_ENTRY); | ||
var data = arr.subarray(SIZEOF.SFNT_HEADER + i * SIZEOF.SFNT_TABLE_ENTRY); | ||
var tableEntry = { | ||
tag: data.toString (SFNT_OFFSET.TABLE_TAG, 4), | ||
offset: data.getUint32 (SFNT_OFFSET.TABLE_OFFSET), | ||
length: data.getUint32 (SFNT_OFFSET.TABLE_LENGTH) | ||
tag: String.fromCharCode.apply(null, data.subarray(SFNT_OFFSET.TABLE_TAG, SFNT_OFFSET.TABLE_TAG + 4)), | ||
offset: data.readUint32BE (SFNT_OFFSET.TABLE_OFFSET), | ||
length: data.readUint32BE (SFNT_OFFSET.TABLE_LENGTH) | ||
}; | ||
var table = new ByteBuffer(buf, tableEntry.offset, tableEntry.length); | ||
var table = arr.subarray(tableEntry.offset, tableEntry.offset + tableEntry.length); | ||
@@ -148,15 +147,17 @@ if (tableEntry.tag === 'OS/2') { | ||
for (j = 0; j < 10; ++j) { | ||
out.setUint8 (EOT_OFFSET.FONT_PANOSE + j, table.getUint8 (SFNT_OFFSET.OS2_FONT_PANOSE + j)); | ||
out.writeUint8 (table.readUint8 (SFNT_OFFSET.OS2_FONT_PANOSE + j), EOT_OFFSET.FONT_PANOSE + j); | ||
} | ||
/*jshint bitwise:false */ | ||
out.setUint8 (EOT_OFFSET.ITALIC, table.getUint16 (SFNT_OFFSET.OS2_FS_SELECTION) & 0x01); | ||
out.setUint32 (EOT_OFFSET.WEIGHT, table.getUint16 (SFNT_OFFSET.OS2_WEIGHT), true); | ||
out.writeUint8 (table.readUint16BE (SFNT_OFFSET.OS2_FS_SELECTION) & 0x01, EOT_OFFSET.ITALIC); | ||
out.writeUint32LE (table.readUint16BE (SFNT_OFFSET.OS2_WEIGHT), EOT_OFFSET.WEIGHT); | ||
for (j = 0; j < 4; ++j) { | ||
out.setUint32 (EOT_OFFSET.UNICODE_RANGE + j * 4, table.getUint32 (SFNT_OFFSET.OS2_UNICODE_RANGE + j * 4), true); | ||
out.writeUint32LE (table.readUint32BE (SFNT_OFFSET.OS2_UNICODE_RANGE + j * 4), | ||
EOT_OFFSET.UNICODE_RANGE + j * 4); | ||
} | ||
for (j = 0; j < 2; ++j) { | ||
out.setUint32 (EOT_OFFSET.CODEPAGE_RANGE + j * 4, table.getUint32 (SFNT_OFFSET.OS2_CODEPAGE_RANGE + j * 4), true); | ||
out.writeUint32LE (table.readUint32BE (SFNT_OFFSET.OS2_CODEPAGE_RANGE + j * 4), | ||
EOT_OFFSET.CODEPAGE_RANGE + j * 4); | ||
} | ||
@@ -167,3 +168,3 @@ | ||
haveHead = true; | ||
out.setUint32 (EOT_OFFSET.CHECKSUM_ADJUSTMENT, table.getUint32 (SFNT_OFFSET.HEAD_CHECKSUM_ADJUSTMENT), true); | ||
out.writeUint32LE (table.readUint32BE (SFNT_OFFSET.HEAD_CHECKSUM_ADJUSTMENT), EOT_OFFSET.CHECKSUM_ADJUSTMENT); | ||
@@ -175,20 +176,22 @@ } else if (tableEntry.tag === 'name') { | ||
var nameTable = { | ||
format: table.getUint16 (SFNT_OFFSET.NAMETABLE_FORMAT), | ||
count: table.getUint16 (SFNT_OFFSET.NAMETABLE_COUNT), | ||
stringOffset: table.getUint16 (SFNT_OFFSET.NAMETABLE_STRING_OFFSET) | ||
format: table.readUint16BE (SFNT_OFFSET.NAMETABLE_FORMAT), | ||
count: table.readUint16BE (SFNT_OFFSET.NAMETABLE_COUNT), | ||
stringOffset: table.readUint16BE (SFNT_OFFSET.NAMETABLE_STRING_OFFSET) | ||
}; | ||
for (j = 0; j < nameTable.count; ++j) { | ||
var nameRecord = new ByteBuffer(table, SIZEOF.SFNT_NAMETABLE + j * SIZEOF.SFNT_NAMETABLE_ENTRY); | ||
var nameRecord = table.subarray(SIZEOF.SFNT_NAMETABLE + j * SIZEOF.SFNT_NAMETABLE_ENTRY); | ||
var name = { | ||
platformID: nameRecord.getUint16 (SFNT_OFFSET.NAME_PLATFORM_ID), | ||
encodingID: nameRecord.getUint16 (SFNT_OFFSET.NAME_ENCODING_ID), | ||
languageID: nameRecord.getUint16 (SFNT_OFFSET.NAME_LANGUAGE_ID), | ||
nameID: nameRecord.getUint16 (SFNT_OFFSET.NAME_NAME_ID), | ||
length: nameRecord.getUint16 (SFNT_OFFSET.NAME_LENGTH), | ||
offset: nameRecord.getUint16 (SFNT_OFFSET.NAME_OFFSET) | ||
platformID: nameRecord.readUint16BE (SFNT_OFFSET.NAME_PLATFORM_ID), | ||
encodingID: nameRecord.readUint16BE (SFNT_OFFSET.NAME_ENCODING_ID), | ||
languageID: nameRecord.readUint16BE (SFNT_OFFSET.NAME_LANGUAGE_ID), | ||
nameID: nameRecord.readUint16BE (SFNT_OFFSET.NAME_NAME_ID), | ||
length: nameRecord.readUint16BE (SFNT_OFFSET.NAME_LENGTH), | ||
offset: nameRecord.readUint16BE (SFNT_OFFSET.NAME_OFFSET) | ||
}; | ||
if (name.platformID === 3 && name.encodingID === 1 && name.languageID === MAGIC.LANGUAGE_ENGLISH) { | ||
var s = strbuf (new ByteBuffer(table, nameTable.stringOffset + name.offset, name.length)); | ||
var s = strbuf(table.subarray( | ||
nameTable.stringOffset + name.offset, | ||
nameTable.stringOffset + name.offset + name.length)); | ||
@@ -219,28 +222,18 @@ switch (name.nameID) { | ||
// Calculate final length | ||
var len = | ||
out.length + | ||
familyName.length + | ||
subfamilyName.length + | ||
versionString.length + | ||
fullName.length + | ||
2 + | ||
buf.length; | ||
// Create final buffer with the the same array type as input one. | ||
var eot = new ByteBuffer(len); | ||
var eot = Buffer.concat([ | ||
out, | ||
familyName, | ||
subfamilyName, | ||
versionString, | ||
fullName, | ||
Buffer.from([ 0, 0 ]), | ||
arr | ||
]); | ||
eot.writeBytes(out.buffer); | ||
eot.writeBytes(familyName.buffer); | ||
eot.writeBytes(subfamilyName.buffer); | ||
eot.writeBytes(versionString.buffer); | ||
eot.writeBytes(fullName.buffer); | ||
eot.writeBytes([ 0, 0 ]); | ||
eot.writeBytes(buf.buffer); | ||
eot.writeUint32LE (eot.length, EOT_OFFSET.LENGTH); // Calculate overall length | ||
eot.setUint32(EOT_OFFSET.LENGTH, len, true); // Calculate overall length | ||
return eot; | ||
return new Uint8Array(eot.buffer, eot.byteOffset, eot.length); | ||
} | ||
module.exports = ttf2eot; |
{ | ||
"name": "ttf2eot", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Convert TTF font to EOT", | ||
@@ -26,4 +26,3 @@ "keywords": [ | ||
"dependencies": { | ||
"argparse": "^1.0.6", | ||
"microbuffer": "^1.0.0" | ||
"argparse": "^2.0.1" | ||
}, | ||
@@ -30,0 +29,0 @@ "devDependencies": { |
@@ -17,9 +17,8 @@ #!/usr/bin/env node | ||
var parser = new ArgumentParser ({ | ||
version: require('./package.json').version, | ||
addHelp: true, | ||
add_help: true, | ||
description: 'TTF to EOT font converter' | ||
}); | ||
parser.addArgument ( | ||
[ 'infile' ], | ||
parser.add_argument ( | ||
'infile', | ||
{ | ||
@@ -31,4 +30,4 @@ nargs: '?', | ||
parser.addArgument ( | ||
[ 'outfile' ], | ||
parser.add_argument ( | ||
'outfile', | ||
{ | ||
@@ -40,7 +39,16 @@ nargs: '?', | ||
parser.add_argument ( | ||
'-v', '--version', | ||
{ | ||
action: 'version', | ||
version: require('./package.json').version, | ||
help: "show program's version number and exit" | ||
} | ||
); | ||
/* eslint-disable no-console */ | ||
var args = parser.parseArgs(); | ||
var args = parser.parse_args(); | ||
var input, size; | ||
var input; | ||
@@ -51,5 +59,3 @@ try { | ||
} else { | ||
size = fs.fstatSync(process.stdin.fd).size; | ||
input = new Buffer(size); | ||
fs.readSync(process.stdin.fd, input, 0, size, 0); | ||
input = fs.readFileSync(process.stdin.fd); | ||
} | ||
@@ -61,4 +67,3 @@ } catch (e) { | ||
var ttf = new Uint8Array(input); | ||
var eot = new Buffer(ttf2eot(ttf).buffer); | ||
var eot = ttf2eot(input); | ||
@@ -65,0 +70,0 @@ if (args.outfile) { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10888
1
0
243
+ Addedargparse@2.0.1(transitive)
- Removedmicrobuffer@^1.0.0
- Removedargparse@1.0.10(transitive)
- Removedmicrobuffer@1.0.0(transitive)
- Removedsprintf-js@1.0.3(transitive)
Updatedargparse@^2.0.1