Divya Manian

RSS Feed Youtube Channel Github

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. IE 9 Preview does have support for 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