Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add woffToOTF #691

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions bin/ot
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
/* eslint no-console: off */

import fs from 'fs';
import path from 'path';
import { load } from '../src/opentype';
const process = require('process'),
fs = require('fs'),
path = require('path'),
{ load, woffToOTF } = require('../dist/opentype.js');

// Print out information about the font on the console.
function printFontInfo(font) {
Expand All @@ -30,11 +31,13 @@ function walk(dir, fn) {

// Print out usage information.
function printUsage() {
console.log('Usage: ot command [dir|file]');
console.log('Usage: ot <command> [...args]');
console.log();
console.log('Commands:');
console.log();
console.log(' info Get information of specified font or fonts in the specified directory.');
console.log(' info [dir|file] Get information of specified font or fonts in the specified directory.');
console.log();
console.log(' woff_2_otf <woff_font_file> [otf_font_file] Uncompress woff file into otf.');
console.log();
}

Expand Down Expand Up @@ -65,7 +68,7 @@ if (process.argv.length < 3) {
} else {
var command = process.argv[2];
if (command === 'info') {
var fontpath = process.argv.length === 3 ? '.' : process.argv[3];
var fontpath = process.argv.length === 4 ? '.' : process.argv[3];
if (fs.existsSync(fontpath)) {
var ext = path.extname(fontpath).toLowerCase();
if (fs.statSync(fontpath).isDirectory()) {
Expand All @@ -78,6 +81,23 @@ if (process.argv.length < 3) {
} else {
console.log('Path not found');
}
}
else if (command === 'woff_2_otf') {
if(process.argv.length < 4 ){
printUsage();
process.exit(1);
}
const fontpath = process.argv[3];
if (!fs.existsSync(fontpath)) {
console.log('Font file at path not found.');
process.exit(1);
}
const targetPath = process.argv.length >= 5
? process.argv[4]
: `${path.join(path.dirname(fontpath), path.basename(fontpath, path.extname(fontpath)))}.otf`,
buffer = fs.readFileSync(fontpath),
result = woffToOTF(buffer);
fs.writeFileSync(targetPath, new DataView(result));
} else {
printUsage();
}
Expand Down
87 changes: 84 additions & 3 deletions src/opentype.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Font from './font.mjs';
import Glyph from './glyph.mjs';
import { CmapEncoding, GlyphNames, addGlyphNames } from './encoding.mjs';
import parse from './parse.mjs';
import { encode } from './types.mjs';
import BoundingBox from './bbox.mjs';
import Path from './path.mjs';
import cpal from './tables/cpal.mjs';
Expand Down Expand Up @@ -79,6 +80,7 @@ function parseWOFFTableEntries(data, numTables) {
const offset = parse.getULong(data, p + 4);
const compLength = parse.getULong(data, p + 8);
const origLength = parse.getULong(data, p + 12);
const checksum = parse.getULong(data, p + 16);
let compression;
if (compLength < origLength) {
compression = 'WOFF';
Expand All @@ -87,7 +89,7 @@ function parseWOFFTableEntries(data, numTables) {
}

tableEntries.push({tag: tag, offset: offset, compression: compression,
compressedLength: compLength, length: origLength});
compressedLength: compLength, length: origLength, checksum: checksum});
p += 20;
}

Expand Down Expand Up @@ -436,7 +438,7 @@ function parseBuffer(buffer, opt={}) {
font.tables.meta = meta.parse(metaTable.data, metaTable.offset);
font.metas = font.tables.meta;
}

font.palettes = new PaletteManager(font);

return font;
Expand Down Expand Up @@ -465,6 +467,84 @@ function loadSync() {
console.error('DEPRECATED! migrate to: opentype.parse(require("fs").readFileSync(url), opt)');
}

/**
* Convert/Uncompress a buffer of a woff font to otf/ttf without parsing
* table contents.
* @param {ArrayBuffer}
* @return {ArrayBuffer}
*/
function woffToOTF(buffer) {
if (buffer.constructor !== ArrayBuffer)
buffer = new Uint8Array(buffer).buffer;

const data = new DataView(buffer, 0),
out = [],
signature = parse.getTag(data, 0);

if (signature !== 'wOFF')
throw new Error(`TYPE ERROR signature must be wOFF but is: "${signature}"`);

const flavor = parse.getTag(data, 4),
numTables = parse.getUShort(data, 12),
tableEntries = parseWOFFTableEntries(data, numTables),
max = [];
for (let n = 0; n < 64; n++) {
if (Math.pow(2, n) > numTables)
break;
max.splice(0, Infinity, n, 2 ** n);
}

const searchRange = max[1] * 16,
entrySelector = max[0],
rangeShift = numTables * 16 - searchRange;

out.push(
...encode.TAG(flavor),
...encode.USHORT(numTables),
...encode.USHORT(searchRange),
...encode.USHORT(entrySelector),
...encode.USHORT(rangeShift)
);
let offset = out.length + numTables * 16;

for (let i=0; i<numTables; i++) {
const tableEntry = tableEntries[i];
out.push(
...encode.TAG(tableEntry.tag),
...encode.ULONG(tableEntry.checksum),
...encode.ULONG(offset),
...encode.ULONG(tableEntry.length)
);
tableEntry.outOffset = offset;
offset += tableEntry.length;
if ((offset % 4) !== 0)
offset += 4 - (offset % 4);
}
const initialData = new Uint8Array(out.length),
buffers = [initialData];
for (let i=0,l=out.length; i<l; i++)
initialData[i] = out[i];

for (let i=0; i<numTables; i++) {
const tableEntry = tableEntries[i],
table = uncompressTable(data, tableEntry), // => {data: view, offset: 0};
offset = tableEntry.outOffset + tableEntry.length,
padding = (offset % 4) !== 0
? 4 - (offset % 4)
: 0;
buffers.push(
new Uint8Array(table.data.buffer, table.offset, tableEntry.length),
new Uint8Array(padding)
);
}
const result = new Uint8Array(buffers.reduce((accum, buffer)=>accum+buffer.byteLength, 0));
buffers.reduce((offset, buffer)=>{
result.set(buffer, offset);
return offset + buffer.byteLength;
}, 0);
return result.buffer;
}

export {
Font,
Glyph,
Expand All @@ -473,5 +553,6 @@ export {
parse as _parse,
parseBuffer as parse,
load,
loadSync
loadSync,
woffToOTF
};
Loading