Text & Text Boxes

Text can be rendered via draw_text() or draw_text_box().

Text Positioning

To position text on the canvas, you simply need a reference position (i.e. \(x`\) and \(y\) coordinate) and an Anchor. The following example shows text boxes along with the corresponding anchor and reference positions. These reference positions are highlighted via draw_marker():

Text anchors

Corresponding Python code:

 1x = offset_x
 2y = offset_y
 3padding = (6, 6)
 4for anchor in viren2d.Anchor.list_all():
 5    pos = (x, y)
 6    if anchor != viren2d.Anchor.Center:
 7        painter.draw_marker(pos, marker_style)
 8
 9    painter.draw_text_box(
10        [str(anchor)], pos, anchor=anchor, text_style=text_style,
11        padding=padding, rotation=0, line_style=viren2d.LineStyle.Invalid,
12        fill_color='azure!90', radius=0.15)
13
14    x += delta_x
15    if x >= canvas_width:
16        x = offset_x
17        y += delta_y

Multi-Line Text

The following example shows the different alignment options of multi-line text boxes. These lines are an excerpt from Revelation, a Visual Poem.

Multi-line Text Alignment

This visualization has been created with the following Python code:

 1poem = [
 2    'This nature frames my world!',
 3    '',
 4    "It's a Leitmotiv,",
 5    'a compass, an anchor,',
 6    'a constant companion,',
 7    'a steady hand...'
 8]
 9padding = (14, 6)
10fixed_box_size = (220, 170)
11color = 'tangerine!90'
12
13line_style = viren2d.LineStyle.Invalid
14
15text_style = viren2d.TextStyle(family='xkcd', size=14)
16text_style.halign = 'left'
17text_style.valign = 'top'
18painter.draw_text_box(
19    poem, (5, 5), anchor='top-left', text_style=text_style,
20    padding=padding, rotation=0, line_style=line_style,
21    fill_color=color, radius=0.1, fixed_size=fixed_box_size)
22
23text_style.halign = 'center'
24text_style.valign = 'center'
25painter.draw_text_box(
26    poem, (canvas_width / 2, 5), anchor='top', text_style=text_style,
27    padding=padding, rotation=0, line_style=line_style,
28    fill_color=color, radius=0.1, fixed_size=fixed_box_size)
29
30text_style.halign = 'right'
31text_style.valign = 'bottom'
32painter.draw_text_box(
33    poem, (canvas_width - 5, 5), anchor='top-right', text_style=text_style,
34    padding=padding, rotation=0, line_style=line_style,
35    fill_color=color, radius=0.1, fixed_size=fixed_box_size)