Py5Font.width()

Get the width of a character in this font.

Examples

example picture for width()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def setup():
    font_size = 45
    font = py5.create_font('DejaVu Sans', font_size, True, 'py5')
    py5.text_font(font)

    x = 10
    py5.text('py5', x, py5.height / 2)
    py5.line(x, 0, x, py5.height)

    for c in list('py5'):
        x += font.width(c) * font_size
        py5.line(x, 0, x, py5.height)

    py5.println(font.width('x'))

Description

Get the width of a character in this font. The value is based on a font of size 1. Multiply it by the font size to get the horizontal space of the character.

This will return 0 if the character is not in the font’s character set.

Underlying Java method: PFont.width

Syntax

width(c: chr, /) -> float

Parameters

  • c: chr - single character

Updated on September 11, 2021 16:51:34pm UTC