All Projects → zachleat → Fontfaceonload

zachleat / Fontfaceonload

Licence: mit
A simple utility to execute a callback when a webfont loads.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Fontfaceonload

Glyphhanger
Your web font utility belt. It can subset web fonts. It can find unicode-ranges for you automatically. It makes julienne fries.
Stars: ✭ 1,099 (+199.46%)
Mutual labels:  font, web-fonts
Webfont
Awesome generator of webfont
Stars: ✭ 170 (-53.68%)
Mutual labels:  font, web-fonts
Faux Pas
A script to highlight elements that are mismatched incorrectly to @​font-face blocks, which may result in shoddy faux bold or faux italic rendering.
Stars: ✭ 93 (-74.66%)
Mutual labels:  font, web-fonts
Pokemon Font
GAME BOY font from Pokémon R/G/B/Y/G/S/C, Unicode extended.
Stars: ✭ 437 (+19.07%)
Mutual labels:  font, web-fonts
Brick
Open-source webfont service
Stars: ✭ 2,886 (+686.38%)
Mutual labels:  font, web-fonts
Webfont Test
Test and analyze fonts for the web: Google fonts, system fonts and custom fonts.
Stars: ✭ 36 (-90.19%)
Mutual labels:  font, web-fonts
Karmilla
An expanded version of the amazing Karla webfont, adding support for various languages (French, German, Norse, Hungarian, Latvian, Icelandic...)
Stars: ✭ 130 (-64.58%)
Mutual labels:  font, web-fonts
Web Font Loading Recipes
A bunch of demos for different web font loading strategies. Companion to https://www.zachleat.com/web/comprehensive-webfonts/ Read more: https://www.zachleat.com/web/recipes/
Stars: ✭ 964 (+162.67%)
Mutual labels:  font, web-fonts
gatsby-omni-font-loader
Font loader optimized for maximum performance. Removes render-blocking font resources and loads them asynchronusly. Handle FOUT & FOUC with font loading status watcher. Supports both local-hosted fonts and web fonts.
Stars: ✭ 98 (-73.3%)
Mutual labels:  font, web-fonts
Hack
A typeface designed for source code
Stars: ✭ 14,543 (+3862.67%)
Mutual labels:  font, web-fonts
Woff2 Feature Test
A simple feature test for the WOFF2 font format
Stars: ✭ 106 (-71.12%)
Mutual labels:  font, web-fonts
webfont-kit-generator
Create @ font-face kits easily
Stars: ✭ 52 (-85.83%)
Mutual labels:  font, web-fonts
Powerline Web Fonts
Powerline Web Fonts for Chromebook
Stars: ✭ 178 (-51.5%)
Mutual labels:  font, web-fonts
glyphhanger
Your web font utility belt. It can subset web fonts. It can find unicode-ranges for you automatically. It makes julienne fries.
Stars: ✭ 422 (+14.99%)
Mutual labels:  font, web-fonts
fonts
Web fonts that you probably won't find in a CDN
Stars: ✭ 26 (-92.92%)
Mutual labels:  font, web-fonts
Keyrune
Magic: the Gathering set symbol pictographic font
Stars: ✭ 272 (-25.89%)
Mutual labels:  font
Ios Uifont Names
A handy cheatsheet for calling UIFont(name: size:)
Stars: ✭ 305 (-16.89%)
Mutual labels:  font
Decovar
A multistyle decorative variable font by David Berlow
Stars: ✭ 269 (-26.7%)
Mutual labels:  font
Polyicon
Flutter icon set generator
Stars: ✭ 268 (-26.98%)
Mutual labels:  font
Devicon
Set of icons representing programming languages, designing & development tools
Stars: ✭ 4,536 (+1135.97%)
Mutual labels:  font

fontfaceonload

Build Status Dependency Status devDependency Status

Demo

Usage

Use with any existing @font-face declaration.

@font-face {
    font-family: My Custom Font Family;
    /* src and other properties as normal */
}

Include the library. Call the JavaScript.

FontFaceOnload("My Custom Font Family", {
    success: function() {},
    error: function() {},
    timeout: 5000 // in ms. Optional, default is 10 seconds
});

Use with Content Fonts

To allow the fallback font to be visible while the @font-face is loading, simply use FontFaceOnload like so:

FontFaceOnload("My Custom Font Family", {
    success: function() {
        document.documentElement.className += " my-custom-font-family";
    }
});

and then use the class to scope your usage of the custom font:

body {
    font-family: sans-serif;
}
.my-custom-font-family body {
    font-family: My Custom Font Family, sans-serif;
}

An alternative approach that will also eliminate the FOIT as well as not require you to change your CSS is to use the loadCSS library to load the @font-face with a Data URI source block dynamically. loadCSS is smaller than fontfaceonload. The limitations to this approach include requiring you to manage which format to load (WOFF, WOFF2, TTF) and it will not work as well with icon fonts (since you need to a CSS class to style other sibling elements, like the descriptive text).

Use with Icon Fonts

To hide the fallback font so that weird fallback characters are not visible while the icon font is loading, use FontFaceOnload with the glyphs option like so:

FontFaceOnload("My Custom Font Icon", {
    success: function() {
        document.documentElement.className += " my-custom-font-icon";
    },
    glyphs: "\uE600\uE601\uE602\uE605" // Optional, default is "". Useful for icon fonts: a few code points from your custom font icon.
});

and then use the class to scope your usage of the custom font:

.icon {
    display: none;
}
.my-custom-font-family .icon {
    display: inline-block;
    font-family: My Custom Font Icon, sans-serif;
}

How it Works

This uses the CSS Font Loading Module when available (currently in Chrome 35+ and Opera 22+). When that isn’t available, it uses a very similar approach to the one used in the TypeKit Web Font Loader (which is currently 7.1KB GZIP).

Basically, it creates an element with a font stack including the web font and a default serif/sans-serif typeface. It then uses a test string and measures the dimensions of the element at a certain interval. When the dimensions are different than the default fallback fonts, the font is considered to have loaded successfully.

If you’d like a full polyfill for the CSS Font Loading Module, follow along with Bram Stein’s Font Loader. I believe the specification has changed since he launched this polyfill, but he’s working on an updated version.

Building the project

Run these commands:

  • npm install
  • bower install
  • grunt init
  • grunt as normal.

Configuring Grunt

Rather than one giant Gruntfile.js, this project is using a modular Grunt setup. Each individual grunt configuration option key has its own file located in grunt/config-lib/ (readonly upstream configs, do not modify these directly) or grunt/config/ (project specific configs). You may use the same key in both directories, the objects are smartly combined using Lo-Dash merge.

For concatenation in the previous Gruntfile setup, you’d add another key to the giant object passed into grunt.initConfig like this: grunt.initConfig({ concat: { /* YOUR CONFIG */ } });. In the new configuration, you’ll create a grunt/config/concat.js with module.exports = { /* YOUR CONFIG */ };.

License

MIT License

Alternatives

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].