13 Text

13.1 Introduction

SVG allows text to be inserted into the drawing. All of the same styling attributes available for paths and vector graphic shapes are also available for text. (See Filling, Stroking and Paint Servers.)

13.2 The <text> element

The <text> element adds text to a drawing.

In the example below, the string "Hello, out there" is drawn in blue:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG July 1999//EN" 
<svg width="4in" height="3in">
  <text x=".5in" y="2in" 
        style="fill:blue">Hello, out there</text>

Download this example

<text> elements which are not positioned along a path (see Text On A Path below) can supply optional attributes x= and y= which represents the location in user coordinates (or Transformed CSS units or a percentage of the current viewport) for the initial current text position. Default values for x= and y= are zero in the user coordinate system. For left-aligned character strings, the placement point of the first glyph will be positioned exactly at the current text position and the glyph will be rendered based on the current set of text and font properties. After the glyph is rendered, the current text position is advanced based on the metrics of the glyph that were used along with the current set of text and font properties. For Roman text, the current text position is advanced along the x-axis by the width of that glyph. The cycle repeats until all characters in the <text> element have been rendered.

Within a <text> element, text and font properties and the current text position can be modified by including a <tspan> element. The <tspan> element can contain attributes style (which allows new visual rendering attributes to be specified) and the following attributes, which perform adjustments on the current text position:

The x= and dx= values are cumulative; thus, if both are provided, the new current text position will have an X-coordinate of x+dx. Similarly, if both x= and dx= are provided, the new current text position will have a Y-coordinate of y+dy.

(Internationalization issues need to be addressed.)

A <tspan> element can also be used to specify that the character data from a different element should be used as character data for the given <tspan> element. In the example below, the first <text> element (with id="TextToUse") will not draw because it is part of a <defs> element. The second <text> element draws the string "ABC". The third text element draws the string "XYZ" because it includes a <tspan> element which is a reference to element "TextToUse", and that element's character data is "XYZ":

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG July 1999//EN" 
<svg width="4in" height="3in">
    <text id="TextToUse">XYZ</text>
    <tspan href="#TextToUse"/>

Download this example

If a <tspan> element has both an href attribute and its own character data, the character data from the href attribute draws before its own character data.

13.3 White space handling

SVG supports the standard XML attribute xml:space for determining how it should handle white space characters within a given <text> element's character data. xml:space is an inheritable attribute which can have one of two values:

13.4 Text selection

SVG user agents running on systems with have clipboards for copy/paste operations and which are equipped with input devices that allow for text selection should support the selection of text from an SVG document and the ability to copy selected text strings to the system clipboard.

Within an SVG user agent which supports text selection and pointer devices such as a mouse, the following behavior should exist. When the pointing device is clicked over an SVG character and then dragged, then whenever the mouse goes over another character defined within the same <text> elements, all characters whose position in the document is between the initial character and the current character should be highlighted, no matter where they might be located on the canvas.

When feasible, generators of SVG should attempt to order their text strings to facilitate properly ordered text selection within SVG viewing applications such as Web browsers.

13.5 Text and font properties

(Descriptions (or references to the CSS2 spec) for all of the text and font properties from CSS2 go here. The following are SVG-specific text properties which go beyond the text and font properties already defined in CSS2:)

13.6 Ligatures and alternate glyphs

There are situations such as ligatures, special-purpose fonts (e.g., a font for music symbols) or alternate glyphs for Asian text strings where a different glyph should be used to render some text than the glyph which normally corresponds to the given character data. Also, the W3C Character Model (??? add link) requires early normalization of character data to facilitate meaningful and unambiguous exchange of character data and correct comparisons between character strings. The W3C Character Model will bring about many common situations where the normalized character data will be different than the glyphs which the user want to use to render that character data.

To allow for control over the glyphs used to render particular character data, the 'altglyph' property is available.

Value:   unicode(<value>) |
glyphname(<string>) |
glyphid(<value>) |
ROS(<value>) cid(<value>) |
Initial:   none
Applies to:  <text> elements
Inherited:  yes
Percentages:  N/A
Media:  visual
where <value> indicates a string of Unicode characters that should replace the text within the <text> element
where <string> provides a string of which is the name of the glyph that should be used to replace the text within the <text> element
where <value> a string of which is numeric ID/index of the glyph that should be used to replace the text within the <text> or <t> element
ROS and cid
are required for Web fonts in OpenType/CFF format and operate similar to glyphid

13.7 Text on a path

In addition to text drawn in a straight line, SVG also includes the ability to place text along the shape of a <path> element. The basic algorithm is such that each glyph is positioned at a specific point on the path, with the glyph getting rotated such that the baseline of the glyph is either parallel or tangent to the tangent of the curve at the given point. Here are some examples of text on a path:

(??? include some drawings here )

The following is a simple example which illustrates many of the basic concepts:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG July 1999//EN" 
<svg width="4in" height="3in">
  <desc>Simple text on a path
  <path id="MyPath" style="visibility: hidden"
        d="M 100 100 C 125 125 175 125 200 100" />
    <textpath href="#MyPath">Text on path</textpath>

Download this example

The start-offset attribute defines the distance from the start of the path where the first character should be positioned.

Various CSS properties control aspects of text drawn along a path. The standard set of text and font properties from CSS2 apply, including 'text-align' and 'vertical-align'. Additional, the following additional properties control how the text is formatted and rendered:

Text on a path opens the possibility of significant implementation differences due to different methods of calculating distance along a path. In order to ensure that distance calculations are sufficiently precise, the following two attributes are available on <path> elements. (??? Obviously, this section needs to be moved to the <path> element section.)

(??? Insert drawings here)

13.7.1 Vector graphics along a path

SVG's text on a path features set has been generalized to allow for arbitrary SVG along a path, by adding the use element as a valid child of text.