144. opengl.fonttexture — Textures for rendering text fonts.

This module defines the FontTexture class which is a Texture specialized for rendering fonts. It forms the basis for text drawing on the OpenGL canvas. uses textures on quads to render text on an OpenGL canvas. It is dependent on freetype and the Python bindings freetype-py. These bindings come included with pyFormex.

144.1. Classes defined in module opengl.fonttexture

class opengl.fonttexture.FontTexture(filename=None, size=24, *, charset=None, saveto=None)[source]

A Texture class for text rendering.

The FontTexture class is a Texture containing a bitmap of the important characters in a font. The Texture can then be used to draw text on the OpenGL canvas or on some geometry. The FontTexture can be initialized from one of two sources:

  • A monospace .ttf font file. Additionally a characterset may be defined to limit the FontTexture to the important characters. The created FontTexture can then be saved in a .png image file, for faster reload.

  • A .png image file containing a saved image of a FontTexture. pyFormex comes with a large set of pregenerated .png files containing all ASCII characters for most monospace fonts on typical Linux installations.

The FontTexture class supports the full unicode character sets available in the .ttf files, but is currently limited to monospace fonts.

Parameters:
  • filename (path_like) – The path of the font file to be used. This should be either an existing monospace .ttf font or a texture font .png file generated by pyFormex.

  • size (float) – Intended font height. The actual height might differ a bit. Different texture font heights can be generated from a single .ttf font file. A higher font height provides a higher resolution and nicer text, at a larger cost.

  • charset (str, optional) – Only useful if filename is a .ttf file: the set of characters to be included in the FontTexture. If not provided, the set of ASCII characters 32..128 is used. PNG texture font files contain the character set used when they were generated.

  • saveto (path_like, optional) – The name of a .png file where the texture image will be stored, so it can subsequently be reloaded with the need to generate it from the TTF font.

activate(mode=None)[source]

Bind the texture and make it ready for use.

Returns the texture id.

texCoords(s)[source]

Return the texture coordinates for the character in a string.

Parameters:

char (str) – 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).

Returns:

array – The texture coordinates needed to display the given string on a grid of quad4 elements. It is a float array with shape (len(s), 4, 2). For each character from s, this contains 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, because the FontTextures are stored from top to bottom as in images files.

classmethod default(size=24)[source]

Set and return the default FontTexture.

144.2. Functions defined in module opengl.fonttexture

opengl.fonttexture.font2texture(filename, size=24, charset=None, saveto=None)[source]

Create a texture image from a ttf font file

Parameters:
  • filename (path_like) – The path to a TTF font file

  • size (int) – The intended font size

  • charset (string) – The set of characters to include in the texture.

  • saveto (path_like, optional) – The name of a .png file where the texture image will be stored, so it can subsequently be reloaded with the need to generate it from the TTF font.

Returns:

ndarray – A texture image with the rendered (ASCII) characters from the font.

Examples

>>> font = pf.utils.defaultMonoFont()
>>> pngfile = pf.cfg['tmpdir'] / 'testfont.png'
>>> im, info = font2texture(font, 24, charset='abcé°€', saveto=pngfile)
>>> rim = Image.open(pngfile)
>>> print(rim.text)
{'size': '24', 'nrows': '1', 'ncols': '6', 'charset': 'abcé°€'}
opengl.fonttexture.read_texturefont(filename)[source]

Read a pyFormex Texture Font File

Parameters:

filename (path_like) – The path of a pyFormex Texture Font file. This is a .png file with a FontTexture in PNG image format and containing the appropriate metadata as generated by the FontTexture class.