Home > Others > SVG: How to Work With Text and Font

SVG: How to Work With Text and Font

January 3rd, 2016 Leave a comment Go to comments

The SVG format is not only useful for the display of vector based shapes. You can also place and edit font in it. In contrast to semantic font labelling in HTML, there are some particular things you need to keep in mind. You should also know some differences to the markup via CSS.

Marking Fonts Via SVG and Editing With CSS

While HTML knows several semantic elements that allow you to mark text as a headline, paragraph, list or grid, the SVG format only knows the „“ element. You can place and edit font within it.

<text>Lorem ipsum.</text>

The design of the text is done just like it is in HTML using CSS. You can integrate web fonts and define the font type, font size, and orientation. While the font and its size are specified with the known properties „font-family“ and „font-size“, there is the property „text-anchor“ for the orientation. It knows the values „start“ for left-aligned, „end“ for right-aligned and „middle“ for centered text.

text {
  font-family: "Source Sans";
  font-size: 30px;
  text-anchor: middle;
}

As SVG doesn’t know the classic multiline text, setting line spacing is unnecessary. Like every other SVG element, the „“ element is placed using „x“ and „y“ coordinates. The „y“ value only defines a text’s body line. When the value 0 is assigned to the „y“ trait, the text disappears from the upper image border.

SVG: Creating Multiline Text

The SVG syntax doesn’t know an element like „
“, which would allow it to add a word wrap within a text. However, there is the „“ element, which you can use to divide a text and arrange it individually.

<text>
  <tspan>Lorem ipsum.</tspan>
  <tspan>Lorem ipsum.</tspan>
</text>

By default, the „“ elements are placed next to each other, just like the „“ element in HTML. The placement of SVG elements is done with the attributes „x“ and „y“. However, these attributes cause an absolute placement of elements. That means, the „“ elements are neither placed in relation to the superior „“ element nor in relation to other „“ elements.

text-schrift-svg_tspan1
Standard Arrangement of „“ Elements

However, for „“, the individual attributes „dx“ and „dy“ are available. These are responsible for a relative placement to the previously marked „“ element.

text-schrift-svg_tspan2
„“ elements have been placed via „dx“ and „dy“

Using „dy“ allows you to define the line spacing between single „“ elements. As „dx“ is relative to the previously defined „“ element as well, you need to use „x“ for the vertical placement.

<text>
  <tspan x="0" dy="0">Lorem ipsum.</tspan>
  <tspan x="0" dy="36px">Lorem ipsum.</tspan>
  <tspan x="0" dy="36px">Lorem ipsum.</tspan>
</text>

This example causes all text lines to start on the x coordinate 0 and makes them move down by 36 pixels per line on the y-axis.

text-schrift-svg_tspan3
„“ elements, that are placed using „x“ and „dy“

SVG: Text Following to the path

One advantage of the SVG format over HTML is that SVG allows you to align a text on a path. Thus, you are able to place a text on a circular path, for example. To do so, you need to use the „“ element.

First, you need to define a path to orientate the text on. It is necessary to create this path via „“ element. Even when you want the text to be aligned in a circle shape, you can not mark this circle using „“. „“, as the name already tells, only allows you to mark path elements.

<defs>
  <path id="textpath" cd="M200,125c0,41.4-33.6,75-75,75s-75-33.6-75-75s33.6-75…" />
</defs>

In the example, we’re drawing a circle using „“. As the circle itself shouldn’t be visible, you can mark it within the „“ area. You also need to assign an ID to the path.

text-schrift-svg_textpath
Text aligned on a circle path

Afterwards, the „“ element is placed between the „“ and the „“ element. The path is assigned according to the ID.

<text>
  <textPath xlink:href="#textpfad" startOffset="50%">
    <tspan >Lorem ipsum.</tspan>
  </textPath>
</text>

You decide at which position on the path the text should start using the „startOffset“ attribute. Both absolute indication and indication in percent is possible.

Embedding fonts in SVG

Besides the option to integrate web fonts via CSS, you can also embed fonts in SVG. To do that, there are the elements „“ and „“. „“ is used to define a font. Every symbol of the font is marked with „“. The font’s contour is deposited as a path.

When you create SVG files in Illustrator, you can also embed fonts this way. This comes in handy when you need an SVG file that works without reference to external data. As embedded fonts cause much larger files than web fonts, you should preferably embed them via Stylesheets.

In general, when dealing with SVG, you should keep the files as small as possible, especially with the mobile web in mind.

(dpe)

Categories: Others Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.