78. opengl.textext — Text rendering on the OpenGL canvas. — pyFormex 1.0.5 documentation

# 78. `opengl.textext` — Text rendering on the OpenGL canvas.

This module uses textures on quads to render text on an OpenGL canvas. It is dependent on freetype and the Python bindings freetype-py.

## 78.1. Classes defined in module opengl.textext

class `opengl.textext.` `FontTexture`(filename, size)

  A Texture class for text rendering.

  The FontTexture class is a texture containing the most important characters of a font. This texture can then be used to draw text on geometry. In the current implementation only the characters with ASCII ordinal in the range 32..127 are put in the texture.

  Parameters:

  - filename: path string. The font to be used. It should be the full file path of an existing monospace font on the system.
  - size: float: intended font heigth. The actual height might differ a bit.
  `activate`(mode=None)

    Bind the texture and make it ready for use.

    Returns the texture id.

  `texCoords`(char)

    Return the texture coordinates for a character or string.

    Parameters:

    - char: integer or ascii string. If an integer, it should be in the range 32..127 (printable ASCII characters). If a string, all its characters should be ASCII printable characters (have an ordinal value in the range 32..127).

    If char is an integer, returns a tuple with the texture coordinates in the FontTexture corresponding with the specified character. This is a sequence of four (x,y) pairs corresponding respectively with the lower left, lower right, upper right, upper left corners of the character in the texture. Note that values for the lower corners are higher than those for the upper corners. This is because the FontTextures are (currently) stored from top to bottom, while opengl coordinates are from bottom to top.

    If char is a string of length ntext, returns a float array with shape (ntext,4,2) holding the texture coordinates needed to display the given text on a grid of quad4 elements.

  classmethod `default`(size=18)

    Set and return the default FontTexture.

class `opengl.textext.` `Text`(text, pos, gravity=None, size=18, width=None, font=None, lineskip=1.0, grid=None, texmode=4, **kargs)

  A text drawn at a 2D or 3D position.

  Parameters:

  - text: string: the text to display. If not a string, the string representation of the object will be drawn. Newlines in the string are supported. After a newline, the remainder of the string is continued from a lower vertical position and the initial horizontal position. The vertical line offset is determined from the font.
  - pos: a 2D or 3D position. If 2D, the values are measured in pixels. If 3D, it is a point in global 3D space. The text is drawn in 2D, inserted at the specified position.
  - gravity: a string that determines the adjusting of the text with respect to the insert position. It can be a combination of one of the characters ‘N or ‘S’ to specify the vertical positon, and ‘W’ or ‘E’ for the horizontal. The default(empty) string will center the text.
  - size: float: size (height) of the font. This is the displayed height. The used font can have a different height and is scaled accordingly.
  - width: float: width of the font. This is the displayed width o a single character (currently only monospace fonts are supported). The default is set from the size and the aspect ratio of the font. Setting this to a different value allows the creation of condensed and expanded font types. Condensed fonts are often used to save space.
  - font: `FontTexture` or string. The font to be used. If a string, it is the filename of an existing monospace font on the system.
  - lineskip: float: distance in pixels between subsequent baselines in case of multi-line text. Multi-line text results when the input text contains newlines.
  - grid: raster geometry for the text. This is the geometry where the generate text will be rendered on as a texture. The default is a grid of rectangles of size (width,`size`) which are juxtaposed horizontally. Each rectangle will be rendered with a single character on it.

class `opengl.textext.` `TextArray`(val, pos, prefix='', **kargs)

  An array of texts drawn at a 2D or 3D positions.

  The text is drawn in 2D, inserted at the specified (2D or 3D) position, with alignment specified by the gravity (see class `Text`).

  Parameters:

  - text: a list of N strings: the texts to display. If an item is not a string, the string representation of the object will be drawn.
  - pos: either an [N,2] or [N,3] shaped array of 2D or 3D positions. If 2D, the values are measured in pixels. If 3D, it is a point in global 3D space.
  - prefix: string. If specified, it is prepended to all drawn strings.

  Other parameters can be passed to the `Text` class.

class `opengl.textext.` `Mark`(pos, tex, size, opak=False, ontop=True, **kargs)

  A 2D drawing inserted at a 3D position of the scene.

  The minimum attributes and methods are:

  - pos : 3D point where the mark will be drawn