• Stars
    star
    332
  • Rank 126,957 (Top 3 %)
  • Language
    JavaScript
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Webfont Downloader

webfont-dl https://github.com/mmastrac/webfont-dl/actions https://www.npmjs.com/package/webfont-dl

Web font downloader/inliner.

Downloads a set of web fonts specified by @font-face rules in a CSS file. By default, woff2 equivalents are inlined as the modern browsers all support it.

By inlining woff files, this reduces the number of server roundtrips by two in the best case (the external CSS and woff files), one in the worst (just the external CSS). By reducing roundtrips we can reduce the amount of time we risk showing a flash of unstyled or hidden text content.

This tool is currently designed to work against Google's font server but should work against any hosted CSS font that uses @font-face.

But what about CDNs?

Content Distribution Networks work well in the case where you know with a high degree of certainty that a user has visited a site linking to the same piece of content. If you're using fonts that big names like Google are using (ie: Roboto) you might have a great cache hit rate. For fonts that are more "long tail" or customized via font subsetting this cache hit rate will drop off dramatically.

Google's font CDN caches the fonts themselves using a strong cache identifier: the fonts are hashed and that hash is used as a strong, long-lived cache key. The CSS that references the font, however, is only cached for a short period of time. This means that your visitors will have to frequently download the indirect CSS file, adding an extra request and latency to the page load.

By inlining the fonts in your stylesheets, you avoid the one or two extra requests that happen on cache misses. In addition, the fonts will be loaded as part of your site's CSS and you can avoid the flash of unstyled or invisible text associated with long-running font loads.

Examples

Want to see it in action? Check out the test page here.

Install it globally:

npm install -g webfont-dl

Download "Crimson Text" in 400/normal and 400/italic and "Raleway" in 500/normal from Google's font API. Inlines woff (version 1) format files, puts the CSS and fonts into css/:

webfont-dl "http://fonts.googleapis.com/css?family=Crimson+Text:400,400italic|Raleway:500" \
  -o css/font.css

Download "Crimson Text" in 400/normal and 400/italic and "Raleway" in 500/normal from Google's font API. Doesn't inline any files, puts CSS into css/, and fonts in font/:

webfont-dl "http://fonts.googleapis.com/css?family=Crimson+Text:400,400italic|Raleway:500" \
  -o css/font.css --font-out=font --css-rel=../font --woff1=link

Download "Crimson Text" in 400/normal in both woff (version 1) and woff2 formats:

webfont-dl "http://fonts.googleapis.com/css?family=Crimson+Text:400 --woff1=link --woff2=link

Download the OpenSans collection from github:

webfont-dl -d "https://github.com/steakejjs/OpenSans-CSS/raw/master/OpenSans.css" \
  -o css/font.css --font-out=font --css-rel=../font

Output

The CSS output from the tool contains a number of features:

  • The fonts are strongly named using a sha-1 hash
  • local() names are preserved from the input CSS
  • IE6-8 compatibility is enabled by hoisting a copy of eot files into a separate src: line
  • Selected font formats are inlined

Example output:

@font-face {
  font-family: 'Crimson Text';
  font-style: normal;
  font-weight: 400;
  /* IE6-8 compat */
  src: url("/font/crimsontext-5a145c6f1863c2b986e33b7172a7059e4c6557dd.eot");
  src: local("Crimson Text"), 
    local("CrimsonText-Roman"), 
    url("data:application/font-woff;base64,...") format("woff"), 
    url("/font/crimsontext-5a145c6f1863c2b986e33b7172a7059e4c6557dd.eot") format("embedded-opentype"), 
    url("/font/crimsontext-528b74b55df7980a8f694e9a56d2feeaa1ae351e.svg#CrimsonText") format("svg"), 
    url("/font/crimsontext-381297740916b3fe12a631447ee8a802af3bea16.ttf") format("truetype");
}

Usage

Web font downloader. 

Given a font definition file in webfontloader style, outputs a single CSS file 
and downloaded fonts in a given output directory.

usage: webfont-dl <css-url-or-file> --out=FILE [options]

options:
    --help,-h           Prints help
    --out=FILE -o FILE  Output file for CSS
    --font-out=DIR      Font output directory [default: --out]
    --css-rel=PATH      CSS-relative path for fonts [default: ./]
    --woff2=<mode>      Processing mode for woff v2 fonts: data, link or omit [default: data]
    --woff1=<mode>      Processing mode for woff v1 fonts: data, link or omit [default: link]
    --svg=<mode>        Processing mode for svg fonts: data, link or omit [default: omit]
    --ttf=<mode>        Processing mode for ttf fonts: data, link or omit [default: link]
    --eot=<mode>        Processing mode for eot fonts: data, link or omit [default: link]
    -d                  Debug info

More Repositories

1

rust-ctor

Module initialization/global constructor functions for Rust
Rust
714
star
2

stylus

Lightweight status page for home infrastructure
Rust
122
star
3

nanojson

A tiny, compliant JSON parser and writer for Java
Java
101
star
4

longshot

API and CLI for ECAM-based Delonghi machines
Rust
90
star
5

automedia

A set of tools for automatically managing bitrot and format in large quantities of media
Python
90
star
6

rust-libc-print

#[no_std] print equivalent for Rust
Rust
57
star
7

bootstrap

Bootstrapping VM for bare metal to trusted C compilation
Assembly
45
star
8

jquery-noclickdelay

Removes the 300ms click delay from iOS webviews
JavaScript
32
star
9

keepcalm

Simple shared types for multi-threaded Rust programs
Rust
23
star
10

progscrape

progscrape.com source
Rust
21
star
11

brew-a-coffee-demo

Demo repo for a Github Action that brews coffee (simulator only)
20
star
12

dress-simulator

Opinionated Dress Color Simulator
HTML
18
star
13

oblivious-cpu

A re-implementation of ShapeCPU
Java
18
star
14

adventure

Port of the 550 point adventure game to the web
Java
16
star
15

kalos

Kalos embeddable scripting language
C
12
star
16

progscrape-android

Android app for progscrape.com
Java
10
star
17

Charbase

The source for charbase.com
Python
6
star
18

Stencil

Rich templating system for Java webapps
Java
6
star
19

gibbet-hill

Gibbet Hill - Bram Stoker
6
star
20

javausb

Java USB interface, build on JNA/libusb
Java
5
star
21

reedsolomon-ecc

Reed-solomon ECC generator in Java
Java
5
star
22

unifi-rpi-docker

UniFi Docker Raspberry Pi
Dockerfile
4
star
23

snippets

Random snippets
Rascal
3
star
24

ffmpeg-omx-rpi-docker

FFMPEG configured for OMX on Raspberry Pi
Dockerfile
2
star
25

gst-omx-rpi-docker

Dockerfile for working GStreamer w/OMX
Dockerfile
2
star
26

replies-and-more

Replies and More for Google+
JavaScript
2
star
27

video4all

Automatically exported from code.google.com/p/video4all
JavaScript
2
star
28

android-docker-slim

Slimmed down version of https://hub.docker.com/r/bitriseio/android-ndk/
1
star
29

unsync_channel

Rust
1
star
30

deno-op2-presentation

Jupyter Notebook
1
star
31

gwt-rpc-plus

Automatically exported from code.google.com/p/gwt-rpc-plus
Java
1
star
32

requirejs-ember-handlebars

Require.js plugin to pre-compile Ember.js handlebar templates
JavaScript
1
star
33

Stencil-Web

Website for Stencil
1
star
34

no-mans-sky-recipes

Recipes for No Man's Sky
JavaScript
1
star
35

gpsclock

GPS Clock
KiCad Layout
1
star