text - text rendering and meshing
This file is to render floating texts and mesh texts
The current implementation of floating texts uses a bitmap font texture baked at program start. It’s following the first technique described in this tutorial: https://learnopengl.com/In-Practice/Text-Rendering
For mesh texts, freetype (https://freetype.org/) is used to read the font files and extract curve primitives from it. The primitives are then discretized and triangulated using the madcad functions.
When specified by their name, fonts are searched in the system font directories plus the directories in module variable fontpath
-
fontpath
list of paths where to look for fonts files
-
text
(text: str, font: Optional[str] = None, size: float = 1, spacing=dvec2(0.05, 0.2), fill=True, align=(0, 0), resolution=None)[source] return a Mesh/Web containing the given text written using the given font
The meshed font is cached so long texts are still fast to mesh
Parameters: - text – a multiline string to represent
- font – the string name of a font such as
'NotoMono-Bold'
or the path to a.ttf
font file - size – the character size (metric unit)
- spacing – spacing ratio between characters
vec2(horizontal, vertical)
- fill – if True, the characters are triangulated and the function returns a
Mesh
, else it returns its outline as aWeb
- align –
text alignment, the tuple items can be:
- ’left’ or ‘top’
- ’center’
- ’right’ or ‘bottom’
- any float, as an offset on the position
- resolution – discretisation setting for the character primitives
Example
>>> part = text('Hello everyone.\nthis is a great font !!', ... font='NotoSans-Regular', ... align=('left', 0), ... fill=True)