Class: QgsTextRenderer

class qgis.core.QgsTextRenderer

Bases: sip.wrapper

Handles rendering text using rich formatting options, including drop shadows, buffers and background shapes.

Methods

drawPart

Draws a single component of rendered text using the specified settings.

drawText

Draws text within a rectangle using the specified settings.

fontMetrics

Returns the font metrics for the given text format, when rendered in the specified render context.

sizeToPixel

Calculates pixel size (considering output size should be in pixel or map units, scale factors and optionally oversampling)

textHeight

Returns the height of a text based on a given format.

textWidth

Returns the width of a text based on a given format.

Attributes

AlignCenter

AlignLeft

AlignRight

Background

Buffer

Label

Point

Rect

Shadow

Text

AlignCenter = 1
AlignLeft = 0
AlignRight = 2
Background = 2
Buffer = 1
class DrawMode

Bases: int

class HAlignment

Bases: int

Label = 2
Point = 1
Rect = 0
Shadow = 3
Text = 0
class TextPart

Bases: int

drawPart(rect: QRectF, rotation: float, alignment: QgsTextRenderer.HAlignment, textLines: Iterable[str], context: QgsRenderContext, format: QgsTextFormat, part: QgsTextRenderer.TextPart, drawAsOutlines: bool = True)

Draws a single component of rendered text using the specified settings.

Parameters
  • rect (QRectF) – destination rectangle for text

  • rotation (float) – text rotation

  • alignment (QgsTextRenderer.HAlignment) – horizontal alignment

  • textLines (Iterable[str]) – list of lines of text to draw

  • context (QgsRenderContext) – render context

  • format (QgsTextFormat) – text format

  • part (QgsTextRenderer.TextPart) – component of text to draw. Note that Shadow parts cannot be drawn individually and instead are drawn with their associated part (e.g., drawn together with the text or background parts)

  • drawAsOutlines (bool = True) – set to False to render text as text. This allows outputs to formats like SVG to maintain text as text objects, but at the cost of degraded rendering and may result in side effects like misaligned text buffers. This setting is deprecated and has no effect as of QGIS 3.4.3 and the text format should be set using QgsRenderContext.setTextRenderFormat() instead.

drawPart(origin: Union[QPointF, QPoint], rotation: float, alignment: QgsTextRenderer.HAlignment, textLines: Iterable[str], context: QgsRenderContext, format: QgsTextFormat, part: QgsTextRenderer.TextPart, drawAsOutlines: bool = True) Draws a single component of rendered text using the specified settings.

Parameters
  • origin – origin for start of text. Y coordinate will be used as baseline.

  • rotation – text rotation

  • alignment – horizontal alignment

  • textLines – list of lines of text to draw

  • context – render context

  • format – text format

  • part – component of text to draw. Note that Shadow parts cannot be drawn individually and instead are drawn with their associated part (e.g., drawn together with the text or background parts)

  • drawAsOutlines – set to False to render text as text. This allows outputs to formats like SVG to maintain text as text objects, but at the cost of degraded rendering and may result in side effects like misaligned text buffers. This setting is deprecated and has no effect as of QGIS 3.4.3 and the text format should be set using QgsRenderContext.setTextRenderFormat() instead.

drawText(rect: QRectF, rotation: float, alignment: QgsTextRenderer.HAlignment, textLines: Iterable[str], context: QgsRenderContext, format: QgsTextFormat, drawAsOutlines: bool = True)

Draws text within a rectangle using the specified settings.

Parameters
  • rect (QRectF) – destination rectangle for text

  • rotation (float) – text rotation

  • alignment (QgsTextRenderer.HAlignment) – horizontal alignment

  • textLines (Iterable[str]) – list of lines of text to draw

  • context (QgsRenderContext) – render context

  • format (QgsTextFormat) – text format

  • drawAsOutlines (bool = True) – set to False to render text as text. This allows outputs to formats like SVG to maintain text as text objects, but at the cost of degraded rendering and may result in side effects like misaligned text buffers. This setting is deprecated and has no effect as of QGIS 3.4.3 and the text format should be set using QgsRenderContext.setTextRenderFormat() instead.

drawText(point: Union[QPointF, QPoint], rotation: float, alignment: QgsTextRenderer.HAlignment, textLines: Iterable[str], context: QgsRenderContext, format: QgsTextFormat, drawAsOutlines: bool = True) Draws text at a point origin using the specified settings.

Parameters
  • point – origin of text

  • rotation – text rotation

  • alignment – horizontal alignment

  • textLines – list of lines of text to draw

  • context – render context

  • format – text format

  • drawAsOutlines – set to False to render text as text. This allows outputs to formats like SVG to maintain text as text objects, but at the cost of degraded rendering and may result in side effects like misaligned text buffers. This setting is deprecated and has no effect as of QGIS 3.4.3 and the text format should be set using QgsRenderContext.setTextRenderFormat() instead.

fontMetrics(context: QgsRenderContext, format: QgsTextFormat) → QFontMetricsF

Returns the font metrics for the given text format, when rendered in the specified render context. The font metrics will take into account all scaling required by the render context.

New in version 3.2.

Parameters
Return type

QFontMetricsF

sizeToPixel(size: float, c: QgsRenderContext, unit: QgsUnitTypes.RenderUnit, mapUnitScale: QgsMapUnitScale = QgsMapUnitScale()) → int

Calculates pixel size (considering output size should be in pixel or map units, scale factors and optionally oversampling)

Parameters
Return type

int

Returns

font pixel size

textHeight(context: QgsRenderContext, format: QgsTextFormat, textLines: Iterable[str], mode: QgsTextRenderer.DrawMode, fontMetrics: QFontMetricsF = None) → float

Returns the height of a text based on a given format.

Parameters
Return type

float

textWidth(context: QgsRenderContext, format: QgsTextFormat, textLines: Iterable[str], fontMetrics: QFontMetricsF = None) → float

Returns the width of a text based on a given format.

Parameters
  • context (QgsRenderContext) – render context

  • format (QgsTextFormat) – text format

  • textLines (Iterable[str]) – list of lines of text to calculate width from

  • fontMetrics (QFontMetricsF = None) – font metrics

Return type

float