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

Only download truetype files from Google Web Fonts and convert them locally #115

Merged
merged 4 commits into from
Jul 26, 2020

Conversation

papandreou
Copy link
Collaborator

@papandreou papandreou commented Jul 16, 2020

Fixes #109, local font conversion tools lifted from #56

Avoids the problem where we end up with multiple @font-face declarations when downloading the woff2 version given enough characters in the subset: #109 (comment)

It causes a small (2%) increase in the size of the woff2 file in one of the test cases: https://github.com/Munter/subfont/pull/115/files#diff-fab359c44278133fd8ef28aafc5b4a2cR479

Not sure if it's significant enough that we should try to track it down.

@papandreou papandreou self-assigned this Jul 16, 2020
@papandreou papandreou requested a review from Munter July 16, 2020 04:22
@coveralls
Copy link

coveralls commented Jul 16, 2020

Pull Request Test Coverage Report for Build 474

  • 39 of 41 (95.12%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.09%) to 89.687%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/convertFontBuffer.js 20 22 90.91%
Totals Coverage Status
Change from base Build 466: 0.09%
Covered Lines: 1052
Relevant Lines: 1125

💛 - Coveralls

@papandreou
Copy link
Collaborator Author

papandreou commented Jul 16, 2020

Not sure if it's significant enough that we should try to track it down.

I couldn't resist:

$ curl -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.835.2 Safari/537.36' 'https://fonts.googleapis.com/css?family=Open+Sans:400&text=Helo&format=woff2'
@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://fonts.gstatic.com/l/font?kit=mem8YaGs126MiZpBA-U1V48aHa0AbQ&skey=62c1cbfccc78b4b2&v=v17) format('woff2');
}
$ curl 'https://fonts.googleapis.com/css?family=Open+Sans:400&text=Helo'
@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://fonts.gstatic.com/l/font?kit=mem8YaGs126MiZpBA-U1Uo8aHa0AbQ&skey=62c1cbfccc78b4b2&v=v17) format('truetype');
}
$ curl -s0 -o opensans_helo_google.woff2 'https://fonts.gstatic.com/l/font?kit=mem8YaGs126MiZpBA-U1V48aHa0AbQ&skey=62c1cbfccc78b4b2&v=v17'
$ curl -s0 -o opensans_helo_google.ttf 'https://fonts.gstatic.com/l/font?kit=mem8YaGs126MiZpBA-U1Uo8aHa0AbQ&skey=62c1cbfccc78b4b2&v=v17'
$ node -e "fs=require('fs').promises;(async()=>{await fs.writeFile('opensans_helo_local.woff2', await require('wawoff2').compress(await fs.readFile('opensans_helo_google.ttf')))})()"
$ ls -l opensans_helo_* | cut -d" " -f 5,9
4044 opensans_helo_google.ttf
2500 opensans_helo_google.woff2
2416 opensans_helo_local.woff2

... It seems like wawoff2 actually produces a slightly smaller woff2 file than the one we get from Google. The apparent increase in size must be because this upgrades the mock to a newer version of the Open Sans font, then.

Also, according to diff <(ttx -o - opensans_helo_google.woff2 2>/dev/null) <(ttx -o - opensans_helo_local.woff2 2>/dev/null) there's no difference in the OpenType data inside the woff2 containers, so we're actually really doing better. Which is interesting, given that wawoff2 is a wasm port of Google's own woff2 library 🤔

@Yankovsky
Copy link

As I understand we need woff/woff2 full versions for fallback if it is enabled.

@Yankovsky
Copy link

TIL about ttx tool from fonttools library.

@Yankovsky
Copy link

Also, according to diff <(ttx -o - opensans_helo_google.woff2 2>/dev/null) <(ttx -o - opensans_helo_local.woff2 2>/dev/null) there's no difference in the OpenType data inside the woff2 containers, so we're actually really doing better.

So what is the difference then? Some meta data?

@papandreou
Copy link
Collaborator Author

So what is the difference then? Some meta data?

It must be more efficient brotli compression settings in wawoff2. If I use woff2_decompress to convert the woff2 that Google Web Fonts provided, then recompress it with wawoff2, I also end up with a file that's 2416 bytes long.

@papandreou
Copy link
Collaborator Author

As I understand we need woff/woff2 full versions for fallback if it is enabled.

Yeah, at present the fallback in the Google Web Fonts case consists of us async-loading the original CSS from their servers after the document has loaded:

subfont/lib/subsetFonts.js

Lines 220 to 245 in 8770e73

const asyncCssLoadingRelation = htmlAsset.addRelation(
{
type: 'HtmlScript',
hrefType: 'inline',
to: {
type: 'JavaScript',
text: `
(function () {
var el = document.createElement('link');
el.href = '${htmlAsset.assetGraph.buildRootRelativeUrl(
originalRelation.to.url,
htmlAsset.url
)}'.toString('url');
el.rel = 'stylesheet';
${
originalRelation.media
? `el.media = '${originalRelation.media}';`
: ''
}
document.body.appendChild(el);
}())
`,
},
},
'lastInBody'
);

There are some good things about this, but we've been meaning to also fully self-host the fallback for a long time: #31

This PR is a step towards that -- I can try looking into it next.

@papandreou papandreou merged commit 237bdfb into master Jul 26, 2020
@papandreou papandreou deleted the fix/issue-109 branch July 26, 2020 00:12
papandreou added a commit that referenced this pull request Jul 26, 2020
papandreou added a commit that referenced this pull request Jul 26, 2020
* master: (53 commits)
  Use the conversion tools introduced in #115 when creating a subset font with harfbuzz
  Update font-snapper to ^1.2.0
  Limit the concurrency of wasm operations and lower the heap size
  Don't break when a @font-face declaration is missing font-family or src
  Wow, Travis is slow, lower to 2 iterations of the generated HTML test
  Generated HTML test: Don't generate the same stylesheet each time
  Improve the property-based tests and run 5 iterations instead of 1
  Update font-snapper to ^1.1.0
  Include unicode-range in the fallback @font-face declarations
  Simplify HB_TAG
  Revert "Revert "Get hb-subset.wasm from harfbuzzjs instead of vendoring it in""
  Allocate more memory for the wasm heap so we can subset more fonts concurrently
  Undo change to testdata/referenceImages/withoutLigatures/index.html
  Test: Add missing check for truetype format
  Simplify HB_TAG
  Remove the ability to shell out to hb-subset again
  Configure hb-subset.wasm to preserve the GSUB, GPOS, and GDEF tables
  Update the vendored in vendor/hb-subset.wasm
  Default to using the wasm build
  Pass --drop-tables-=GSUB,GPOS,GDEF to the hb-subset binary
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing glyph fallback detected
4 participants