vispy.visuals.text.text module

class vispy.visuals.text.text.FontManager(method='cpu')[source]

Bases: object

Helper to create TextureFont instances and reuse them when possible

get_font(face, bold=False, italic=False)[source]

Get a font described by face and size

class vispy.visuals.text.text.SDFRendererCPU[source]

Bases: object

Render SDFs using the CPU.

render_to_texture(data, texture, offset, size)[source]
class vispy.visuals.text.text.TextVisual(text=None, color='black', bold=False, italic=False, face='OpenSans', font_size=12, pos=[0, 0, 0], rotation=0.0, anchor_x='center', anchor_y='center', method='cpu', font_manager=None)[source]

Bases: vispy.visuals.visual.Visual

Visual that displays text

Note: SDF GPU is not currently supported in WebGL without additional

extensions (see comments in fragment shader below).

textstr | list of str

Text to display. Can also be a list of strings. Note: support for list of str might be removed soon in favor of text collections.

colorinstance of Color

Color to use.


Bold face.


Italic face.


Font face to use.


Point size to use.

postuple | list of tuple

Position (x, y) or (x, y, z) of the text. Can also be a list of tuple if text is a list.


Rotation (in degrees) of the text clockwise.


Horizontal text anchor.


Vertical text anchor.


Rendering method for text characters. Either ‘cpu’ (default) or ‘gpu’. The ‘cpu’ method should perform better on remote backends like those based on WebGL. The ‘gpu’ method should produce higher quality results.

font_managerobject | None

Font manager to use (can be shared if the GLContext is shared).

FRAGMENT_SHADER = '\n        // Extensions for WebGL\n        #extension GL_OES_standard_derivatives : enable\n        #extension GL_OES_element_index_uint : enable\n        #include "misc/spatial-filters.frag"\n        // Adapted from glumpy with permission\n        const float M_SQRT1_2 = 0.707106781186547524400844362104849039;\n\n        uniform sampler2D u_font_atlas;\n        uniform vec2 u_font_atlas_shape;\n        varying vec4 v_color;\n        uniform float u_npix;\n\n        varying vec2 v_texcoord;\n        const float center = 0.5;\n\n        float contour(in float d, in float w)\n        {\n            return smoothstep(center - w, center + w, d);\n        }\n\n        float sample(sampler2D texture, vec2 uv, float w)\n        {\n            return contour(texture2D(texture, uv).r, w);\n        }\n\n        void main(void) {\n            vec2 uv = v_texcoord.xy;\n            vec4 rgb;\n\n            // Use interpolation at high font sizes\n            if(u_npix >= 50.0)\n                rgb = CatRom(u_font_atlas, u_font_atlas_shape, uv);\n            else\n                rgb = texture2D(u_font_atlas, uv);\n            float distance = rgb.r;\n\n            // GLSL\'s fwidth = abs(dFdx(uv)) + abs(dFdy(uv))\n            float width = 0.5 * fwidth(distance);  // sharpens a bit\n\n            // Regular SDF\n            float alpha = contour(distance, width);\n\n            if (u_npix < 30.) {\n                // Supersample, 4 extra points\n                // Half of 1/sqrt2; you can play with this\n                float dscale = 0.5 * M_SQRT1_2;\n                vec2 duv = dscale * (dFdx(v_texcoord) + dFdy(v_texcoord));\n                vec4 box = vec4(v_texcoord-duv, v_texcoord+duv);\n                float asum = sample(u_font_atlas, box.xy, width)\n                           + sample(u_font_atlas,, width)\n                           + sample(u_font_atlas, box.xw, width)\n                           + sample(u_font_atlas, box.zy, width);\n                // weighted average, with 4 extra points having 0.5 weight\n                // each, so 1 + 0.5*4 = 3 is the divisor\n                alpha = (alpha + 0.5 * asum) / 3.0;\n            }\n\n            gl_FragColor = vec4(v_color.rgb, v_color.a * alpha);\n        }\n        '
VERTEX_SHADER = '\n        attribute float a_rotation;  // rotation in rad\n        attribute vec2 a_position; // in point units\n        attribute vec2 a_texcoord;\n        attribute vec3 a_pos;  // anchor position\n        varying vec2 v_texcoord;\n        varying vec4 v_color;\n\n        void main(void) {\n            // Eventually "rot" should be handled by SRTTransform or so...\n            mat4 rot = mat4(cos(a_rotation), -sin(a_rotation), 0, 0,\n                            sin(a_rotation), cos(a_rotation), 0, 0,\n                            0, 0, 1, 0, 0, 0, 0, 1);\n            vec4 pos = $transform(vec4(a_pos, 1.0)) +\n                       vec4($text_scale(rot * vec4(a_position, 0.0, 1.0)).xyz, 0.0);\n            gl_Position = pos;\n            v_texcoord = a_texcoord;\n            v_color = $color;\n        }\n        '
property anchors
property bold
property color

The color of the text

property face
property font_size

The font size (in points) of the text

property italic
property pos

The position of the text anchor in the local coordinate frame

property rotation

The rotation of the text (clockwise, in degrees)

property text

The text string

class vispy.visuals.text.text.TextureFont(font, renderer)[source]

Bases: object

Gather a set of glyphs relative to a given font name and size

This currently stores characters in a TextureAtlas object which uses a 2D RGB texture to store unsigned 8-bit integer data. In the future this could be changed to a GL_R8 texture instead of RGB when OpenGL ES 3.0+ is standard. Since VisPy tries to stay compatible with OpenGL ES 2.0 we are using an RGB texture. Using a single channel texture should improve performance by requiring less data to be sent to the GPU and to remote backends (jupyter notebook).


Dict with entries “face”, “size”, “bold”, “italic”.

rendererinstance of SDFRenderer

SDF renderer to use.

property ratio

Ratio of the initial high-res to final stored low-res glyph

property slop

Extra space along each glyph edge due to SDF borders