Nimbupani Designs

About Fonts in SVG

One area in my webfonts investigation that I wanted to know more about, was the state of SVG fonts.

What is SVG?

SVG stands for Scalable Vector Graphics. SVG was created to fill the need for a standardized vector graphic solution for the web (read about the history of SVG). SVG uses XML to describe 2D graphics. So, a circle is defined in SVG as:

	<svg width="12cm" height="4cm" viewBox="0 0 1200 400"
	     xmlns="http://www.w3.org/2000/svg" version="1.1">
	  <desc>Example Circle</desc>
	  <circle cx="600" cy="200" r="100"
	        fill="red" stroke="none" stroke-width="0"  />
	</svg>	

See the sample SVG.

You can convert almost any vector graphic to SVG format (here is how to save your Adobe Illustrator files as SVG). Typically, since SVG is XML (and a vector graphic), it will be smaller than other image formats.

The elephant in the room

Before I proceed, I must mention that IE (even IE 8) does not support SVG. Google Chrome’s native support of SVG is not as good as Opera, Firefox, or Safari either (see this excellent chart of SVG support on various browsers). Google has released SVG Web which allows SVG to be rendered on all browsers that support either SVG or Flash.

Fonts in SVG

Like in any vector graphic, SVG can have text. If you want the text to render a font on any SVG Viewer, you have 3 options:

  • Use web-safe fonts: This is the easiest. An example:

    <text x="100" y="100" style="font-family: impact, georgia, times, serif; font-weight: normal; font-style: normal">
    	Text using web safe font
    </text>		    	
    		    

    See demo of SVG rendered with web-safe font

  • Use @font-face CSS declaration to specify fonts: Browsers will render text just as they render HTML using @font-face. For example, Firefox does not allow cross-site linking of fonts, so it will not render a font if it is in another server (you need to add a HTTP header to allow cross-site linking).

    <defs>
      <style type="text/css">
       <![CDATA[
    	@font-face {
    		font-family: Delicious;
    		src: url('http://nimbupani.com/demo/svgfonts/delicious-roman.otf');
    	}
       ]]>
     </style>
    </defs>
    <text x="100" y="100" style="font-family: 'Delicious'; font-weight:normal; font-style: normal">
     Text using CSS @font-face
    </text>	
    

    See a demo of SVG text rendered with CSS @font-face

  • Use fonts defined using SVG’s font element: SVG format provides a common font format that will be supported by all confirming SVG viewers. An SVG font will have a file extension of “.svg” and will be formatted as XML. Here are some examples of SVG fonts. There are two ways of using SVG Fonts:

    • Using external SVG Fonts: You can link to an external SVG using the font-face element of SVG (not the CSS @font-face declaration).

    • Embed font within the svg file: Most Vector Graphic editors default to this option.

    This page on SVG Fonts explains exhaustively how to use SVG fonts (and how to convert fonts from other formats to SVG). It has great examples of these two methods of using SVG fonts.

Using SVG Fonts for HTML

Using the CSS @font-face declaration, you can also specify SVG fonts instead of just TTF/EOT/OTF fonts. It looks like only Opera 10 and Safari 4 support SVG fonts in CSS (see a demo of SVG Fonts in HTML). There is a bug filed for Firefox but no fix yet.

Why use SVG fonts?

  • Universal support: Any conforming SVG viewer will render the text in the SVG font specified.
  • Store multiple fonts in a single file: You can create a single SVG file with multiple fonts and use the “id” of that specific font element to declare which font you want to use in your HTML or SVG document.
  • Open Source: The font file is completely open. If the font you are using is missing glyphs you can add the glyph you need.
  • Chrome Support: (Thanks for this suggestion @paul_irish!) If you use SVG font format in CSS @font-face declaration for HTML/SVG, it will now work on the latest releases of Chrome, Safari, and Opera (though not on Firefox!).

Disadvantages of SVG fonts

  • The main drawback to SVG fonts is there is no provision for font-hinting. The SVG standard states:

    SVG fonts contain unhinted font outlines. Because of this, on many implementations there will be limitations regarding the quality and legibility of text in small font sizes. For increased quality and legibility in small font sizes, content creators may want to use an alternate font technology, such as fonts that ship with operating systems or an alternate WebFont format.

  • SVG support across browsers is still not consistent. The support for SVG fonts in HTML is even worse.

The Future

SVG has gained a lot of traction in the recent months. Projects like SVG Web can only make the adoption easier. Different browsers support SVG text differently. But, as someone said, these are interesting times for SVG.

Comments

Comments are cached. It may take up to 5 minutes for your comment to appear.
Comments are closed for this post. Please message me on twitter, if you would like to comment.
 
Guest's picture

Can you comment on the rendering performance? I once opened an India map on wikipedia and it pegged the cpu for a while to render the whole thing. It was scalable, but took a lot of time to render.

 
divya's picture

Bala, unfortunately, for a complex vector graphic SVG performance is really slow, I wrote about it here - A bit of SVG and Canvas

 
Guest's picture

Yes, the filesize definately matters. Note however that most existing svg content (on wikimedia etc) has markup that can be either stripped completely or otherwise minimized (with or without loss of precision).

It’s not uncommon for filesizes to end up around half of the original size by using e.g Scour - and that’s before applying gzip compression.

There has been some rather healthy improvements in SVG rendering performance in the latest round of browser releases as well, make sure to test them all.

 
divya's picture

Thanks Erik! That is good to hear!

 
Guest's picture

Im using CSS3 and @font-face on my blog, its pretty easy. I downloaded a font kit that had 4 different font formats including SVG, and its working well. I cant tell yet if page load is suffering or my server is slow. Firefox wasnt supposed to support font embedding until 3.6 (using the .woff extension) and its working for me in 3.5. Thats a good sign. works in IE7 as well (using the .eot extension). another good sign. thanks for the great post. new fonts! finally.