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

Inject unicode-range into all the @font-face declarations for the given family when glyphs are missing #103

Merged
merged 1 commit into from
Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,17 +868,21 @@ async function subsetFonts(
}
}
if (missedAny) {
const cssFontFaceSrc =
accumulatedFontFaceDeclarations[0].relations[0];
const fontFaceDeclaration = cssFontFaceSrc.node;
if (
!fontFaceDeclaration.some((node) => node.prop === 'unicode-range')
) {
fontFaceDeclaration.append({
prop: 'unicode-range',
value: unicodeRange(fontUsage.codepoints.original),
});
cssFontFaceSrc.from.markDirty();
const fontFaces = accumulatedFontFaceDeclarations.filter((fontFace) =>
fontUsage.fontFamilies.has(fontFace['font-family'])
);
for (const fontFace of fontFaces) {
const cssFontFaceSrc = fontFace.relations[0];
const fontFaceDeclaration = cssFontFaceSrc.node;
if (
!fontFaceDeclaration.some((node) => node.prop === 'unicode-range')
) {
fontFaceDeclaration.append({
prop: 'unicode-range',
value: unicodeRange(fontUsage.codepoints.original),
});
cssFontFaceSrc.from.markDirty();
}
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions test/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3236,6 +3236,68 @@ describe('subsetFonts', function () {
});
});

describe('when one out of multiple variants of a font-family has missing glyphs', function () {
it('should add a unicode-range property to all of the @font-face declarations of the font-familys', async function () {
httpception();

const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'../testdata/subsetFonts/missing-glyphs-multiple-variants/'
),
});
assetGraph.on('warn', () => {}); // Don't fail due to the missing glyphs warning

await assetGraph.loadAssets('index.html');
await assetGraph.populate({
followRelations: {
crossorigin: false,
},
});
await subsetFonts(assetGraph, {
inlineFonts: false,
});

const [outputSansRegularRelation] = assetGraph.findRelations({
type: 'CssFontFaceSrc',
to: { fileName: 'OutputSans-Regular.woff2' },
});
expect(
outputSansRegularRelation.node.toString(),
'not to contain',
'unicode-range:'
);
const [outputSansBoldRelation] = assetGraph.findRelations({
type: 'CssFontFaceSrc',
to: { fileName: 'OutputSans-Bold.woff2' },
});
expect(
outputSansBoldRelation.node.toString(),
'not to contain',
'unicode-range:'
);

const [inputMonoRegularRelation] = assetGraph.findRelations({
type: 'CssFontFaceSrc',
to: { fileName: 'InputMono-Regular.woff2' },
});
expect(
inputMonoRegularRelation.node.toString(),
'to contain',
'unicode-range:U+'
);
const [inputMonoBoldRelation] = assetGraph.findRelations({
type: 'CssFontFaceSrc',
to: { fileName: 'InputMono-Medium.woff2' },
});
expect(
inputMonoBoldRelation.node.toString(),
'to contain',
'unicode-range:U+'
);
});
});

describe('when the original @font-face declaration already contains a unicode-range property', function () {
it('should leave the existing unicode-range alone', async function () {
httpception();
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions testdata/subsetFonts/missing-glyphs-multiple-variants/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!doctype html>
<html class="fonts-loaded">
<head>
<style>
@font-face {
font-family: Output Sans;
src: url(OutputSans-Regular.woff2) format("woff2");
}

@font-face {
font-family: Output Sans;
src: url(OutputSans-Bold.woff2) format("woff2");
font-weight: 700;
}

@font-face {
font-family: Input Mono;
src: url(InputMono-Regular.woff2) format("woff2");
}

@font-face {
font-family: Input Mono;
src: url(InputMono-Medium.woff2) format("woff2");
font-weight: 700;
}

code {
font-family: Input Mono, monospace;
}
</style>
</head>
<body>
<pre><code>load('中国').then(<strong>function</strong></code></pre>

<div style="font-family: Output Sans">Hello!</div>
</body>
</html>