Text

Text that is to be rendered as part of an SVG document fragment is specified using the text element. The characters to be drawn are expressed as XML character data inside the text element.

class svgwrite.text.Text(text, insert=None, x=None, y=None, dx=None, dy=None, rotate=None, **extra)

The Text element defines a graphics element consisting of text. The characters to be drawn are expressed as XML character data inside the Text element.

Refer to TSpan SVG Attributes

TSpan

class svgwrite.text.TSpan(text, insert=None, x=None, y=None, dx=None, dy=None, rotate=None, **extra)

Within a Text element, text and font properties and the current text position can be adjusted with absolute or relative coordinate values by using the TSpan element. The characters to be drawn are expressed as XML character data inside the TSpan element.

TSpan.__init__(text, insert=None, x=None, y=None, dx=None, dy=None, rotate=None, **extra)
Parameters:
  • text (string) – tspan content
  • insert (2-tuple) – The insert parameter is the absolute insert point of the text, don’t use this parameter in combination with the x or the y parameter.
  • x (list) – list of absolute x-axis values for characters
  • y (list) – list of absolute y-axis values for characters
  • dx (list) – list of relative x-axis values for characters
  • dy (list) – list of relative y-axis values for characters
  • rotate (list) – list of rotation-values for characters (in degrees)

Attributes

TSpan.text

stores the text value.

Methods

TSpan.__init__(text, insert=None, x=None, y=None, dx=None, dy=None, rotate=None, **extra)
Parameters:
  • text (string) – tspan content
  • insert (2-tuple) – The insert parameter is the absolute insert point of the text, don’t use this parameter in combination with the x or the y parameter.
  • x (list) – list of absolute x-axis values for characters
  • y (list) – list of absolute y-axis values for characters
  • dx (list) – list of relative x-axis values for characters
  • dy (list) – list of relative y-axis values for characters
  • rotate (list) – list of rotation-values for characters (in degrees)

SVG Attributes

  • x<coordinate+>

    If a single <coordinate> is provided, then the value represents the new absolute X coordinate for the current text position for rendering the glyphs that correspond to the first character within this element or any of its descendants.

    If list of n <coordinates> is provided, then the values represent new absolute X coordinates for the current text position for rendering the glyphs corresponding to each of the first n characters within this element or any of its descendants.

    If more <coordinates> are provided than characters, then the extra <coordinates> will have no effect on glyph positioning.

    If more characters exist than <coordinates> , then for each of these extra characters:

    1. if an ancestor Text or TSpan element specifies an absolute X coordinate for the given character via an x attribute, then that absolute X coordinate is used as the starting X coordinate for that character (nearest ancestor has precedence), else
    2. the starting X coordinate for rendering the glyphs corresponding to the given character is the X coordinate of the resulting current text position from the most recently rendered glyph for the current Text element.

    If the attribute is not specified:

    1. if an ancestor Text or TSpan element specifies an absolute X coordinate for a given character via an x attribute, then that absolute X coordinate is used (nearest ancestor has precedence), else
    2. the starting X coordinate for rendering the glyphs corresponding to a given character is the X coordinate of the resulting current text position from the most recently rendered glyph for the current Text element.
  • y<coordinate+>

    The corresponding list of absolute Y coordinates for the glyphs corresponding to the characters within this element. The processing rules for the y attribute parallel the processing rules for the x attribute.

  • dx<length+>

    If a single <length> is provided, this value represents the new relative X coordinate for the current text position for rendering the glyphs corresponding to the first character within this element or any of its descendants. The current text position is shifted along the x-axis of the current user coordinate system by <length> before the first character’s glyphs are rendered.

    If a list of n <length> is provided, then the values represent incremental shifts along the x-axis for the current text position before rendering the glyphs corresponding to the first n characters within this element or any of its descendants. Thus, before the glyphs are rendered corresponding to each character, the current text position resulting from drawing the glyphs for the previous character within the current Text element is shifted along the X axis of the current user coordinate system by <length> .

    If more <lengths> are provided than characters, then any extra <lengths> will have no effect on glyph positioning.

    If more characters exist than <length>s, then for each of these extra characters:

    1. if an ancestor Text or TSpan element specifies a relative X coordinate for the given character via a dx attribute, then the current text position is shifted along the x-axis of the current user coordinate system by that amount (nearest ancestor has precedence), else
    2. no extra shift along the x-axis occurs.

    If the attribute is not specified:

    1. if an ancestor Text or TSpan element specifies a relative X coordinate for a given character via a dx attribute, then the current text position is shifted along the x-axis of the current user coordinate system by that amount (nearest ancestor has precedence), else
    2. no extra shift along the x-axis occurs.
  • dy<length+>

    The corresponding list of relative Y coordinates for the characters within the tspan element. The processing rules for the dy attribute parallel the processing rules for the dx attribute.

  • rotate<angle+>

    The supplemental rotation about the current text position that will be applied to all of the glyphs corresponding to each character within this element.

    If a list of <numbers> is provided, then the first <number> represents the supplemental rotation for the glyphs corresponding to the first character within this element or any of its descendants, the second <number> represents the supplemental rotation for the glyphs that correspond to the second character, and so on.

    If more <numbers> are provided than there are characters, then the extra <numbers> will be ignored.

    If more characters are provided than <numbers>, then for each of these extra characters the rotation value specified by the last number must be used.

    If the attribute is not specified and if an ancestor Text or TSpan element specifies a supplemental rotation for a given character via a rotate attribute, then the given supplemental rotation is applied to the given character (nearest ancestor has precedence). If there are more characters than <numbers> specified in the ancestor’s rotate attribute, then for each of these extra characters the rotation value specified by the last number must be used.

    This supplemental rotation has no impact on the rules by which current text position is modified as glyphs get rendered and is supplemental to any rotation due to text on a path and to glyph-orientation-horizontal or glyph-orientation-vertical.

  • classstring

    assigns one or more css-class-names to an element

  • stylestring

    allows per-element css-style rules to be specified directly on a given element

  • externalResourcesRequiredbool

    False: if document rendering can proceed even if external resources are unavailable else: True

  • textLength<length>

    The purpose of this attribute is to allow the author to achieve exact alignment, in visual rendering order after any bidirectional reordering, for the first and last rendered glyphs that correspond to this element; thus, for the last rendered character (in visual rendering order after any bidirectional reordering), any supplemental inter-character spacing beyond normal glyph advances are ignored (in most cases) when the user agent determines the appropriate amount to expand/compress the text string to fit within a length of textLength.

  • lengthAdjust'spacing | spacingAndGlyphs'

    Indicates the type of adjustments which the user agent shall make to make the rendered length of the text match the value specified on the textLength attribute.

    • 'spacing' indicates that only the advance values are adjusted. The glyphs themselves are not stretched or compressed.
    • 'spacingAndGlyphs' indicates that the advance values are adjusted and the glyphs themselves stretched or compressed in one axis (i.e., a direction parallel to the inline-progression-direction).

    If the attribute is not specified, the effect is as a value of spacing were specified.

TRef

class svgwrite.text.TRef(element, **extra)

The textual content for a Text can be either character data directly embedded within the <text> element or the character data content of a referenced element, where the referencing is specified with a TRef element.

TRef.__init__(element, **extra)
Parameters:element – create a reference this element, if element is a string its the id name of the referenced element, if element is a BaseElement the id SVG Attribute is used to create the reference.
TRef.set_href(element)

Create a reference to element.

Parameters:element – if element is a string its the id name of the referenced element, if element is a BaseElement class the id SVG Attribute is used to create the reference.

SVG Attributes

  • classstring

    assigns one or more css-class-names to an element

  • stylestring

    allows per-element css-style rules to be specified directly on a given element

  • externalResourcesRequiredbool

    False: if document rendering can proceed even if external resources are unavailable else: True

  • xlink:href<string> A IRI reference to an element whose character data content shall be used as character data for this tref element.

TextPath

class svgwrite.text.TextPath(path, text, startOffset=None, method='align', spacing='exact', **extra)

In addition to text drawn in a straight line, SVG also includes the ability to place text along the shape of a path element. To specify that a block of text is to be rendered along the shape of a path, include the given text within a textPath element which includes an xlink:href attribute with a IRI reference to a path element.

Methods

TextPath.__init__(path, text, startOffset=None, method='align', spacing='exact', **extra)
Parameters:
  • path – link to path, id string or Path object
  • text (string) – textPath content
  • startOffset (number) – text starts with offset from begin of path.
  • method (string) – align|stretch
  • spacing (string) – exact|auto

SVG Attributes

  • classstring

    assigns one or more css-class-names to an element

  • stylestring

    allows per-element css-style rules to be specified directly on a given element

  • externalResourcesRequiredbool

    False: if document rendering can proceed even if external resources are unavailable else: True

  • xlink:hrefstring

    A IRI reference to an element whose character data content shall be used as character data for this TRef element.

  • startOffset<length>

    An offset from the start of the path for the initial current text position, calculated using the user agent’s distance along the path algorithm. Value as percentage or distance along the path measured in the current user coordinate system.

  • method'align | stretch'

    Indicates the method by which text should be rendered along the path.

  • spacing'auto | exact'

    Indicates how the user agent should determine the spacing between glyphs that are to be rendered along a path.

TextArea

class svgwrite.text.TextArea(text=None, insert=None, size=None, **extra)

At this time textArea is only available for SVG 1.2 Tiny profile.

The textArea element allows simplistic wrapping of text content within a given region. The tiny profile of SVG specifies a single rectangular region. Other profiles may allow a sequence of arbitrary shapes.

Text wrapping via the textArea element is available as a lightweight and convenient facility for simple text wrapping where a complete box model layout engine is not required.

The layout of wrapped text is user agent dependent; thus, content developers need to be aware that there might be different results, particularly with regard to where line breaks occur.

The TextArea class wraps every text added by write() or writeline() as tspan element.

Methods

TextArea.write(text, **extra)

Add text as tspan elements, with extra-params for the tspan element.

Use the ‘\n’ character for line breaks.

SVG Attributes

  • x<coordinate>

    The x-axis coordinate of one corner of the rectangular region into which the text content will be placed. The lacuna value is '0'.

  • y<coordinate>

    The y-axis coordinate of one corner of the rectangular region into which the text content will be placed. The lacuna value is '0'.

  • width'auto' | <coordinate>

    The width of the rectangular region into which the text content will be placed. A value of 'auto' indicates that the width of the rectangular region is infinite. The lacuna value is 'auto'.

  • height'auto' | <coordinate>

    The height of the rectangular region into which the text content will be placed. A value of 'auto' indicates that the height of the rectangular region is infinite. The lacuna value is 'auto'.

  • editable'auto`| simple'

    This attribute indicates whether the text can be edited. See the definition of the ‘editable’ attribute.

  • focusable'true | false | auto'

    Value Description
    'true' The element is keyboard-aware and must be treated as any other UI component that can get focus.
    'false' The element is not focusable.
    'auto' The lacuna value. Equivalent to 'false'

    Exception: see http://www.w3.org/TR/SVGMobile12/interact.html#focusable-attr

  • line-increment'auto | inherit' | <number>

    The line-increment property provides limited control over the size of each line in the block-progression-direction. This property applies to the textArea element, and to child elements of the textArea element. The line-increment property must not have any effect when used on an element which is not, or does not have as an ancestor, a textArea element.

  • text-align'start | end | center | inherit'

    Alignment in the inline progression direction in flowing text is provided by the text-align property.

  • display-align'auto | before | center | after | inherit'

    The display-align property specifies the alignment, in the block-progression-direction, of the text content of the textArea element.

    Value Description
    'auto' For SVG, auto is equivalent to before.
    'before' The before-edge of the first line is aligned with the before-edge of the first region.
    'center' The lines are centered in the block-progression-direction.
    'after' The after-edge of the last line is aligned with the after-edge of the last region.

    Layout rules: see http://www.w3.org/TR/SVGMobile12/text.html#TextAreaElement