Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Font API uses browser detection. How to get all font variations for font-face

I just discovered that Google Font API uses browser detection

This means that when you open http://fonts.googleapis.com/css?family=Racing+Sans+One it will show the content needed only for your current browser

So I can't just grab that and use in my own font-face, if I want it to work in all browsers. Many people do this, not knowing it then won't work everywhere

What would be the method to get all variations of the font? woff2/woff/ttf/etc.? (to use as fallbacks in font-face)

I just tried opening that api page in Chrome, changing browser useragent spoofing in Developer, and go through the variations. I see at least 4

Is woff2/woff/ttf enough, or how many versions do I need to be safe? Do I need EOT/SVG version too?

like image 508
mowgli Avatar asked Jul 29 '14 08:07

mowgli


People also ask

How to use Google’s font API?

You don’t need to be a hardcore web developer to use Google Font API. To use Google Font API, all you need to do is add one stylesheet link element in your web pages and then you can start using that font in your CSS. Here’s the generalized process of using Google’s Font API: Search Google’s font collection to see what available fonts you can use.

What is Google Fonts?

Google Fonts is a library of free licensed font families and APIs for conveniently using the fonts via CSS and Android. We also provide delightful, beautifully crafted icons for common actions and...

How can I reduce the size of my Google Fonts API request?

This allows Google to return a font file that's optimized for your request. In some cases, this can reduce the size of the font file by up to 90%. To use this feature, simply add text= to your Google Fonts API requests.

How can I use Google Fonts to create beautiful display text?

To simplify your work, Google has provided a collection of font effects that you can use with minimal effort to produce beautiful display text. For example: This is a font effect! To use this beta feature, simply add effect= to your Google Fonts API request and add the corresponding class name to the HTML element (s) that you want to affect.


3 Answers

The right answer is, use this website by MajoDev:

https://google-webfonts-helper.herokuapp.com/

It generates the CSS for all browsers and lets you download the files for self-hosting Google font files. Works great!

like image 71
guidod Avatar answered Oct 27 '22 19:10

guidod


Try downloading atleast these variations of the webfonts : eot , woff , ttf , svg and then you can use them like this for maximum cross browser benefits :

@font-face {
  font-family: 'MyWebFont';
  src: url('webfont.eot'); /* IE9 Compat Modes */
  src: url('webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
       url('webfont.woff') format('woff'), /* Modern Browsers */
       url('webfont.ttf')  format('truetype'), /* Safari, Android, iOS */
       url('webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
}

See this answer for more information: Link

like image 29
Abhinav Gauniyal Avatar answered Oct 27 '22 19:10

Abhinav Gauniyal


Using Open Sans as an example.

http://fonts.googleapis.com/css?family=Open+Sans

Make requests with different user agents to see the different fonts: .eot, .woff, woff2, .ttf, .svg

For .eot use these user agents:

IE8
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)

IE7
Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: url(http://fonts.gstatic.com/s/opensans/v10/cJZKeOuBrn4kERxqtaUH3fY6323mHUZFJMgTvxaG2iE.eot);
}

For .woff2 use these user agents:

Firefox 36.0
Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans'), local('OpenSans'), url(http://fonts.gstatic.com/s/opensans/v10/cJZKeOuBrn4kERxqtaUH3VtXRa8TVwTICgirnJhmVJw.woff2) format('woff2'), url(http://fonts.gstatic.com/s/opensans/v10/cJZKeOuBrn4kERxqtaUH3T8E0i7KZn-EPnyo3HZu7kw.woff) format('woff');
}

For .woff use these user agents:

Safari 6.0
Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25

Internet Explorer 11.0
Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko

Internet Explorer 10.0
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)

Internet Explorer 9.0
Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US))

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans'), local('OpenSans'), url(http://fonts.gstatic.com/s/opensans/v10/cJZKeOuBrn4kERxqtaUH3bO3LdcAZYWl9Si6vvxL-qU.woff) format('woff');
}

For .ttf use these user agents:

Safari 5.0.5
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

Safari 3.1
Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_2; en-gb) AppleWebKit/526+ (KHTML, like Gecko) Version/3.1 iPhone

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans'), local('OpenSans'), url(http://fonts.gstatic.com/s/opensans/v10/cJZKeOuBrn4kERxqtaUH3SZ2oysoEQEeKwjgmXLRnTc.ttf) format('truetype');
}

I found another .ttf when trying other user agents. Not sure what the difference is.

Safari 4.1
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-en) AppleWebKit/533.16 (KHTML, like Gecko) Version/4.1 Safari/533.16

Firefox 5.0
Mozilla/5.0 (X11; Linux) Gecko Firefox/5.0

Firefox 3.5.9
Mozilla/5.0 (Windows; U; Windows NT 6.1; et; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9

Safari 3.1.1
Mozilla/5.0 (Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_0_1 like Mac OS X; fr-fr) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans'), local('OpenSans'), url(http://fonts.gstatic.com/s/opensans/v10/cJZKeOuBrn4kERxqtaUH3aCWcynf_cDxXwCLxiixG1c.ttf) format('truetype');
}

For .svg use these user agents:

Safari 4.0.4
Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10gin_lib.cc

Safari 3.0
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3

Safari 3.0
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans'), local('OpenSans'), url(http://fonts.gstatic.com/l/font?kit=cJZKeOuBrn4kERxqtaUH3Zbd9NUM7myrQQz30yPaGQ4#OpenSans) format('svg');
}

Which font formats are supported in which browsers?

  1. http://caniuse.com/#search=eot
  2. http://caniuse.com/#search=ttf
  3. http://caniuse.com/#search=svg
  4. http://caniuse.com/#search=woff
  5. http://caniuse.com/#search=woff2
like image 37
Mike Causer Avatar answered Oct 27 '22 19:10

Mike Causer