Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting and rendering web fonts to base64 - keep original look

I want to defer font loading on my site inspired by deferred font loading logic for Smashing Magazine.

Main part of this is converting fonts to base64 and preparing your CSS file. My steps so far:

  1. Pick fonts on Google Web Fonts and download them.
  2. Use Font Squirrel Webfont Generator to convert downloaded TTF files to CSS file with base64 embedded WOFF fonts (Expert options -> CSS -> Base64 Encode).
  3. Load CSS file async (not important here).

CSS snippet for Open Sans Bold:

@font-face {   font-family: 'Open Sans';   src: url(data:application/x-font-woff;charset=utf-8;base64,<base64_encoded>) format('woff');   font-weight: 700;   font-style: normal; } 

The problem is, that converted fonts look a lot different. Take a look at Open Sans Bold: GWF vs base64 rendering comparison

Especially notice accents being way off and absolutely horrible letter a. Other font families and variants look very noticeably different as well (size and shape distortions, etc.).


So the question is: How do you properly encode TTF files from Google Web Fonts (or other source) to base64 format and use it in a way that the result is identical to the original file?

like image 405
Jan Peša Avatar asked Nov 11 '14 15:11

Jan Peša


People also ask

What is Base64 encoding good for?

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with ASCII. This is to ensure that the data remain intact without modification during transport.

Can you Base64 encode anything?

base64 encoding contains 64 characters to encode any string. base64 contains: 10 numeric value i.e., 0,1,2,3,…..

Can Base64 be decrypted?

Base64 is an encoding, the strings you've posted are encoded. You can DECODE the base64 values into bytes (so just a sequence of bits).

What is Base64 format image?

Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.


1 Answers

In the Font Squirrel Expert options, make sure to set the 'TrueType Hinting' option to 'Keep Existing'. Either of the other options will cause the TrueType instructions (hints) to be modified, which will in turn affect the rendering of the font.

Alternatively, if you're happy with the rendering of the font directly from GWF, you can just take that file and do the base64 encoding yourself. In OS X or Linux, use the built-in base64 command in Terminal/shell:

$ base64 myfont.ttf > fontbase64.txt

For Windows, you'll need to download a program to encode in base64 (there are several free/Open Source tools available). Copy the contents of that file, then use in your CSS as:

@font-face {     font-family: 'myfont';     src: url(data:font/truetype;charset=utf-8;base64,<<copied base64 string>>) format('truetype');     font-weight: normal;     font-style: normal; } 

(Note that you may need to make some adjustments to the various @font-face info to match your particular font data; this is just an example template)

like image 189
djangodude Avatar answered Sep 18 '22 19:09

djangodude