How to download multiple formats of a web font from the (official) Google Web Fonts repo?

前端 未结 7 1233
别那么骄傲
别那么骄傲 2020-12-13 02:40

I\'ve learnt that Google automatically serves TTF, EOT, WOFF, or SVG font files depending on the

7条回答
  •  盖世英雄少女心
    2020-12-13 03:08

    I wrote a PowerShell script to automatically download the fonts served to several different User Agents. For the basic font, it gets all four formats (woff, ttf, svg, eot). Google doesn't seem to serve SVG and EOT files for the bold and italic weights.

    
    $agents = "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0",`
        "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1",`
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)",`
        "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7",`
        "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)"
    
    foreach($arg in $args) {
        $arg;
        foreach($agent in $agents) {
            $agent;
            $webclient = New-Object System.Net.WebClient
            [void]$webclient.Headers.Add("user-agent", $agent)
            $url = "http://fonts.googleapis.com/css?family=$arg"
    
            $css = $webclient.DownloadString($url)
            $css
            $fonts = $css |
                Select-String -AllMatches "http://[A-Za-z0-9/._?&=%-]+" |
                Select-Object -ExpandProperty Matches |
                Select-Object -ExpandProperty Value
    
            foreach($font in $fonts) {
                $font
                $fontfile = [System.Io.Path]::GetFileName((new-object System.Uri $font).LocalPath)
                [void]$webclient.DownloadFile($font, "$pwd\$fontfile")
            }
        }
    }
    

    Once it's in a .ps1 file, it can be called with the fonts to download as arguments:

    PS> .\DownloadFonts.ps1 "Open+Sans:400,700,400italic,700italic"
    

    The script will output the CSS pulled from Google's servers to help you figure out which file is which (e.g. in my case the SVG font was pulled as a file called "font").

    This is based on bash scripts posted by RichardN and ldeck on the blog post Locally Caching Google Web Fonts.

    For reference, here is ldeck's bash script:

    
    #!/bin/sh
    
    for family in $*; do
     for url in $( {
     for agent in \
     'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0' \
     'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1' \
     'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)' ;
     do
     curl -A "$agent" -s "http://fonts.googleapis.com/css?family=$family" | \
     grep -oE 'http://[A-Za-z0-9/._-]+'; \
     done } | sort -u ) ;
     do
     extn=${url##*.} ;
     file=$(echo "$family"| tr +[:upper:] _[:lower:]);
     echo $url $file.$extn;
     curl -s "$url" -o "$file.$extn";
     done
    done
    

    Further reference: Using HTML5 AppCache.

提交回复
热议问题