Google Web Fonts is a very large collection of freely-distributable fonts (some great, some not so great
). They provide a variety of methods for using these in your web pages, which are all fine if you need just a couple of them. Unfortunately a recent project requires several hundred of them. Loading them over the web at runtime is a non-starter — it’s just too slow (I need to load every font so I can display the full collection as type samples). You could download the fonts one at a time, but I’m way too lazy for that. Ruby to the rescue!
Update: I see that WordPress (or at least this theme) eats indentation even if you wrap the code in <code></code> tags. Fail. Oh, well. If you have a use for this, I’d bet money that your editor will handle reindenting the code for you. Heh.
# Font downloader for Google Web Fonts.
# Puts the fonts in a file called "Fontname.ttf" (spaces in the font name are removed)
# and outputs the CSS code to load the fonts.
# Run with something like ruby snagfonts.rb > fontloader.css to get the CSS font directives in a nice file.
# Copyright 2012 by Contraterrene eLearning Group, LLC
# MIT License or Ruby License, your choice.
require 'rubygems'
require 'open-uri'
fonts = [
"Aclonica",
"Annie+Use+Your+Telescope",
"Anonymous+Pro",
"Allerta+Stencil",
"Allerta",
"Amaranth"
# Add all you want
Just be sure to replace any spaces in the font name with +
]
baseURL = 'http://fonts.googleapis.com/css?family='
fontcss = "";
fonts.each do |font|
fontURL = baseURL + font
open(fontURL) {|f|
# This file contains the Google-based CSS rules for the font.
fontCSS = f.read()
# Extract the URL for the actual font data.
urlstr = fontCSS.match(/url\(\'([^\']+)/)
url = urlstr[1]
# Read the TrueType font data
gttf = open(url)
ttffontCSS = gttf.read()
# I'm putting my fonts in a fonts/ subdirectory, which needs to exist ahead of time.
fname = "fonts/" + font + ".ttf"
# Get rid of any + characters in the font name - some OSes don't like that.
fname = fname.gsub(/\+/,'');
fontFile = File.new(fname, "w")
fontFile.syswrite(ttffontCSS)
fontFile.close
gttf.close
# Tweak the CSS font directive so it uses our local copy rather than Google's copy
fontCSS = fontCSS.sub(url,fname)
fontcss = fontcss + fontCSS;
}
end
puts fontcss
This little script will download all the fonts you’ve listed in the array, and output the correct CSS to load them. As written, the fonts will be saved in the fonts/ directory (which you must create ahead of time), while the CSS goes to standard output (which you can redirect to a file). It should be simple enough to mod if you want something else.
Sample CSS output:
@font-face {
font-family: 'Aclonica';
font-style: normal;
font-weight: normal;
src: local('Aclonica'), local('Aclonica-Regular'), url('fonts/Aclonica.ttf') format('truetype');
}
@font-face {
font-family: 'Annie Use Your Telescope';
font-style: normal;
font-weight: normal;
src: local('Annie Use Your Telescope'), local('AnnieUseYourTelescope'), url('fonts/AnnieUseYourTelescope.ttf') format('truetype');
}
@font-face {
font-family: 'Anonymous Pro';
font-style: normal;
font-weight: normal;
src: local('Anonymous Pro'), local('AnonymousPro'), url('fonts/AnonymousPro.ttf') format('truetype');
}
@font-face {
font-family: 'Allerta Stencil';
font-style: normal;
font-weight: normal;
src: local('Allerta Stencil Regular'), local('AllertaStencil-Regular'), url('fonts/AllertaStencil.ttf') format('truetype');
}
@font-face {
font-family: 'Allerta';
font-style: normal;
font-weight: normal;
src: local('Allerta Regular'), local('Allerta-Regular'), url('fonts/Allerta.ttf') format('truetype');
}
@font-face {
font-family: 'Amaranth';
font-style: normal;
font-weight: normal;
src: local('Amaranth'), url('fonts/Amaranth.ttf') format('truetype');
}



















