Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save muinjs/4c18e9db3437012d1895dee3b5e16847 to your computer and use it in GitHub Desktop.

Select an option

Save muinjs/4c18e9db3437012d1895dee3b5e16847 to your computer and use it in GitHub Desktop.
Downloads the .woff2 formats of the specified Google Font for the specified language types. This also creates the appropriate CSS for the downloaded font files
<?php
/**
* Downloads the .woff2 formats of the specified Google Font for the specified
* language types. This also creates the appropriate CSS for the downloaded
* font files
*
* Usage:
* php google-font-download.php 'Open Sans' ['latin-ext,latin' [/path/to/write/to]]
*/
if (empty($argv[1])) {
echo "You must specify a font to download.\n";
exit(1);
}
$fontToDownload = $argv[1];
$langsToDownload = ['latin-ext', 'latin'];
$savePath = getcwd();
$fontDirname = strtolower(preg_replace('/[^A-Za-z0-9]/', '-', $fontToDownload));
$cssFilename = $fontDirname . '.css';
if (!empty($argv[2])) {
$langsToDownload = explode(',', $argv[2]);
}
if (!empty($argv[3])) {
$savePath = realpath($argv[3]);
if (!$savePath) {
echo "The path {$savePath} does not exist.\n";
exit(1);
}
}
if (!is_writable($savePath)) {
echo "Cannot write to {$savePath}.\n";
exit(1);
}
if (!chdir($savePath)) {
echo "Unable to change to {$savePath}.\n";
exit(1);
}
$fontPath = $savePath . DIRECTORY_SEPARATOR . $fontDirname;
if (!file_exists($fontPath) && !mkdir($fontPath)) {
echo "Unable to create {$fontDirname} in {$savePath}.\n";
exit(1);
}
// Change to the location where we want to download these files
if (!chdir($fontPath)) {
echo "Unable to change to {$fontPath}.\n";
exit(1);
}
$qs = [
'family' => $fontToDownload . ':100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i',
];
$userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36';
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => "User-Agent: {$userAgent}\r\n",
],
]);
$googleFontUrl = 'https://fonts.googleapis.com/css?' . http_build_query($qs);
$googleFontCss = @file_get_contents($googleFontUrl, false, $context);
if (!$googleFontCss) {
echo "Unable to download CSS from Google.\n";
exit(1);
}
$currentWeight = null;
$currentLang = null;
$lines = explode("\n", $googleFontCss);
$linesToInclude = [];
foreach ($lines as $line) {
$matches = [];
// What language is this font?
if (preg_match('/^\/\* (.*) \*\/$/', $line, $matches) === 1) {
$currentLang = $matches[1];
}
if (!in_array($currentLang, $langsToDownload)) {
continue;
}
// What font-weight are we using?
if (preg_match('/^ font-weight: (\d{3}).*$/', $line, $matches) === 1) {
$currentWeight = $matches[1];
}
// What is the URL and file name for this font?
if (preg_match("/^ src: local\(.*\), local\('(.*)'\), url\((.*)\) .*$/", $line, $matches) === 1) {
$filename = strtolower($matches[1] . '_' . $currentLang . '_' . $currentWeight) . '.woff2';
$fontDownloadUrl = $matches[2];
$line = str_replace($fontDownloadUrl, $filename, $line);
// Download the file to the current directory
echo "Downloading font file for {$matches[1]}, {$currentWeight}, {$currentLang}.\n";
$fontContents = @file_get_contents($fontDownloadUrl, false, $context);
if (!$fontContents) {
echo "Skipping {$filename}. Unable to download font from {$fontDownloadUrl}.\n";
continue;
}
file_put_contents($filename, $fontContents);
}
$linesToInclude[] = $line;
}
file_put_contents($cssFilename, implode("\n", $linesToInclude));
echo "Font files downloaded to {$fontPath} and CSS file created.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment