Skip to content

Commit

Permalink
Avoid failing in the code that counts the savings when fonttools isn'…
Browse files Browse the repository at this point in the history
…t available
  • Loading branch information
papandreou committed Nov 2, 2019
1 parent 5afe8b8 commit 8bb72e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/subfont.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ module.exports = async function subfont(
let maxUsedCodePoints = 0;
let maxOriginalCodePoints = 0;
for (const fontUsage of fontUsages) {
sumSmallestSubsetSize += fontUsage.smallestSubsetSize;
sumSmallestSubsetSize += fontUsage.smallestSubsetSize || 0;
sumSmallestOriginalSize += fontUsage.smallestOriginalSize;
maxUsedCodePoints = Math.max(
fontUsage.codepoints.used.length,
Expand Down Expand Up @@ -279,21 +279,30 @@ module.exports = async function subfont(
const variantShortName = `${fontUsage.props['font-weight']}${
fontUsage.props['font-style'] === 'italic' ? 'i' : ' '
}`;
let status;
if (
fontUsage.smallestOriginalSize !== undefined &&
fontUsage.smallestSubsetSize !== undefined
) {
status = `${prettyBytes(fontUsage.smallestOriginalSize)} (${
fontUsage.smallestOriginalFormat
}) => ${prettyBytes(fontUsage.smallestSubsetSize)} (${
fontUsage.smallestSubsetFormat
}`;
totalSavings +=
fontUsage.smallestOriginalSize - fontUsage.smallestSubsetSize;
} else {
status = 'no subset font created';
}
console.log(
` ${variantShortName}: ${String(
fontUsage.codepoints.used.length
).padStart(String(maxUsedCodePoints).length)}/${String(
fontUsage.codepoints.original.length
).padStart(
String(maxOriginalCodePoints).length
)} codepoints used, ${prettyBytes(fontUsage.smallestOriginalSize)} (${
fontUsage.smallestOriginalFormat
}) => ${prettyBytes(fontUsage.smallestSubsetSize)} (${
fontUsage.smallestSubsetFormat
})`
)} codepoints used, ${status}`
);
totalSavings +=
fontUsage.smallestOriginalSize - fontUsage.smallestSubsetSize;
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/subfont.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const expect = require('unexpected')
.use(require('unexpected-sinon'));
const subfont = require('../lib/subfont');
const httpception = require('httpception');
const proxyquire = require('proxyquire');
const pathModule = require('path');
const openSansBold = require('fs').readFileSync(
pathModule.resolve(
Expand Down Expand Up @@ -407,4 +408,35 @@ describe('subfont', function() {
expect(mockConsole.error, 'was not called');
});
});

describe('without fonttools available', function() {
const subfontWithoutFontTools = proxyquire('../lib/subfont', {
'../lib/subsetFonts': proxyquire('../lib/subsetFonts', {
'./subsetLocalFont': null
})
});

// Regression test for pretty-bytes(NaN) error
it('should not fail', async function() {
const root = encodeURI(
`file://${pathModule.resolve(
__dirname,
'..',
'testdata',
'subsetFonts',
'local-mixed'
)}`
);

await subfontWithoutFontTools(
{
root,
inputFiles: [`${root}/index.html`],
silent: true,
dryRun: true
},
console
);
});
});
});

0 comments on commit 8bb72e1

Please sign in to comment.