Subgroup: Text

Class: QgsTextRenderer

class qgis.core.QgsTextRenderer

Bases: sip.wrapper

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

New in version 3.0: Methods

drawPart Draws a single component of rendered text using the specified settings.
drawText Draws text within a rectangle using the specified settings.
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.

Signals

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 – destination rectangle for text
  • 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) :param 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.

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) :param 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.

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 – destination rectangle for 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.

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.

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:
  • size – size to convert
  • c – rendercontext
  • unit – size units
  • mapUnitScale – a mapUnitScale clamper
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:
  • context – render context
  • format – text format
  • textLines – list of lines of text to calculate width from
  • mode – draw mode
  • fontMetrics – font metrics
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 – render context
  • format – text format
  • textLines – list of lines of text to calculate width from
  • fontMetrics – font metrics