How to host google web fonts on my own server?

前端 未结 19 1911
闹比i
闹比i 2020-11-27 09:10

I need to use some google fonts on an intranet application. The clients may or may not have internet connection. Reading the license terms, it appears that its legally allo

相关标签:
19条回答
  • 2020-11-27 09:22

    I wrote a bash script that fetches the CSS file on Google's servers with different user agents, downloads the different font formats to a local directory and writes a CSS file including them. Note that the script needs Bash version 4.x.

    See https://neverpanic.de/blog/2014/03/19/downloading-google-web-fonts-for-local-hosting/ for the script (I'm not reproducing it here so I only have to update it in one place when I need to).

    Edit: Moved to https://github.com/neverpanic/google-font-download

    0 讨论(0)
  • 2020-11-27 09:22

    The contents of the CSS file (from the include URL) depends on what browser I view it from. For example, when browsing to http://fonts.googleapis.com/css?family=Open+Sans using Chrome, the file only contained WOFF links. Using Internet Explorer (below), it included both EOT and WOFF. I pasted all the links into my browser to download them.

    @font-face {
      font-family: 'Open Sans';
      font-style: normal;
      font-weight: 400;
      src: url(http://themes.googleusercontent.com/static/fonts/opensans/v6/cJZKeOuBrn4kERxqtaUH3fY6323mHUZFJMgTvxaG2iE.eot);
      src: local('Open Sans'), local('OpenSans'), url(http://themes.googleusercontent.com/static/fonts/opensans/v6/cJZKeOuBrn4kERxqtaUH3fY6323mHUZFJMgTvxaG2iE.eot) format('embedded-opentype'), url(http://themes.googleusercontent.com/static/fonts/opensans/v6/cJZKeOuBrn4kERxqtaUH3T8E0i7KZn-EPnyo3HZu7kw.woff) format('woff');
    }
    

    When you host your own web fonts, you need to correctly link to each font type, deal with legacy browser bugs, etc. When you use Google Web Fonts (hosted by Google), Google automatically links to the correct font types for that browser.

    0 讨论(0)
  • 2020-11-27 09:22

    I used grunt-local-googlefont in a grunt task.

    module.exports = function(grunt) {
    
        grunt.initConfig({
           pkg: grunt.file.readJSON('package.json'),
    
            "local-googlefont" : {
                "opensans" : {
                    "options" : {
                        "family" : "Open Sans",
                        "sizes" : [
                            300,
                            400,
                            600
                        ],
                        "userAgents" : [
                            "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)",  //download eot
                            "Mozilla/5.0 (Linux; U; Android 4.1.2; nl-nl; GT-I9300 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", //download ttf
                            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1944.0 Safari/537.36" //download woff and woff2
                        ],
                        "cssDestination" : "build/fonts/css",
                        "fontDestination" : "build/fonts",
                        "styleSheetExtension" : "css",
                        "fontDestinationCssPrefix" : "fonts"
    
                    }
                }
            }
        });
    
        grunt.loadNpmTasks('grunt-local-googlefont');
     };
    

    Then, to retrieve them:

    grunt local-googlefont:opensans
    

    Note, I'm using a fork from the original, which works better when retrieving fonts with whitespaces in their names.

    0 讨论(0)
  • 2020-11-27 09:22

    If you are using Nuxt, you can use their dedicated module for this purpose: https://github.com/nuxt-community/google-fonts-module For me it works much better than the webfonts helper, which often had problems downloading the fonts during build and generated CSS files without Unicode ranges.

    0 讨论(0)
  • 2020-11-27 09:23

    Easiest Approach - Using google-webfonts-helper

    Lets say we want to host the font Red Rose:

    • Open google-webfonts-helper and filter for the required font on top left (type Red Rose and enter..)
    • Choose from the charsets (default is latin; select others like latin-ext if you want extended support)
    • Select the styles (deafult is regular)
    • From the Copy CSS tab
      • Select Modern Browser if you wish to support only modern browsers (woff2, woff)
      • Select Best Support if you wish to support all browsers (I chose this - woff2, woff,ttf,svg,eot)
    • In case your font files do not reside in ../fonts/ path, you can edit it to represent your actual path (for me it was ../assets/fonts/)
    • Copy the CSS and keep for future use
    • Download the zip file named red-rose-v1-latin-ext_latin; unzip it and place all fonts directly into your assets/fonts directory (based on what you gave earlier)
    • In your stylesheet where you wish to embed, paste the copied CSS. It will look like the below if you chose these options
    /* red-rose-regular - latin-ext_latin */
    @font-face {
      font-family: 'Red Rose';
      font-style: normal;
      font-weight: 400;
      src: url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.eot'); /* IE9 Compat Modes */
      src: local('Red Rose Regular'), local('RedRose-Regular'),
           url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
           url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.woff2') format('woff2'), /* Super Modern Browsers */
           url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.woff') format('woff'), /* Modern Browsers */
           url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.ttf') format('truetype'), /* Safari, Android, iOS */
           url('../assets/fonts/red-rose-v1-latin-ext_latin-regular.svg#RedRose') format('svg'); /* Legacy iOS */
    }
    
    /* Red Rose will now be available for use in your stylesheet, provide this font */
    
    :root {
      font-family: 'Red Rose', cursive, sans-serif;
    }
    
    • Thats ALL!
    0 讨论(0)
  • 2020-11-27 09:25

    As you want to host all fonts (or some of them) at your own server, you a download fonts from this repo and use it the way you want: https://github.com/praisedpk/Local-Google-Fonts

    If you just want to do this to fix the leverage browser caching issue that comes with Google Fonts, you can use alternative fonts CDN, and include fonts as:

    <link href="https://pagecdn.io/lib/easyfonts/fonts.css" rel="stylesheet" />
    

    Or a specific font, as:

    <link href="https://pagecdn.io/lib/easyfonts/lato.css" rel="stylesheet" />
    
    0 讨论(0)
提交回复
热议问题