Pixelastic

You can cut our wings but we will always remember what it was like to fly.

Posts tagged with "font"

Using fonts hosted on a subdomain with @font-face and Firefox

As a security reason, Firefox do not allow an @font-face rule to load fonts hosted on a different domain (even a subdomain).

I don't exactly understand why, I guess it has something to do with preventing crosslinking and copyright violation. I think we should keep the website author handle all this stuff and not required the browser to make assumptions like that.

Anyway, I recently tried to move my CSS file to a subdomain, to reduce pages loading times. Doing so I saw that my fonts did not correctly load on Firefox.

After some digging, I found that I had to manually allow them to be linked from an other domain, server-side. Here is the little snippet I added to my .htaccess

<FilesMatch "\.(ttf|otf|woff)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>

 

Gzipping your font files

When using @font-face to display fonts, you have to create a whole bunch of font files on your server to accomodate the quirks of the various browsers.

If you do things right (or follow the automatic kit build of FontSquirell), you should get a .eot file for IE, a .ttf/.otf file for current browsers, a .svg file for Chrome and the iPhone/iPad and a .woff file for the next browser generation.

Unfortunatly, you'll have to cope with that because there isn't much we can do about it at the moment.

But you can compress those files to make the font rendering faster. Some browsers even download all the fonts even if they will only use one, so compress them !

The easiest way is to configure your server to automatically gzip them. You should already have done that for your css and js file so it is just a matter of adding new types.

As far as I know .otf and .ttf files don't have registered mimetype, so I had to create a dummy one for them in my .htaccess :

AddType x-font/otf    .otf
AddType x-font/ttf    .ttf
AddType x-font/eot    .eot

I also added the .eot because even if an application/vnd.ms-fontobject mimetype is registered for this obscure microsoft format, when I tried to add a deflate rule on it, my Apache crashed so I took the safest way of defining a custom mimetype.

I prefixed them with an x- to make sure that it won't mess with existing mimetypes.

The second part was to add gziping to those

AddOutputFilterByType DEFLATE x-font/otf x-font/ttf x-font/eot

SVG files are in fact xml files, and you should already have them gzipped, so no need to add them here.

I haven't included .woff files because .woff files are already compressed files, so you don't need to gzip them.