Shaders

Font images

Turn a font into an image that any shader can read, and draw text in a shader, in the studio and in an OpenFX plug-in.

A shader has no fonts. Font Image turns a font into a picture: a grid of multi-channel distance fields, one cell for each character, that a shader reads from an Imported image to draw text at any size, sharp. What a shader needs to lay the text out (the width of each character, its line height) is written in the image itself, so the image is all you carry around: it works in the preview, in a pipeline, in a video export and in an OpenFX plug-in.

Font Image is a Pro feature.

Make the image

  1. Open a library, then choose File, New, Font Image… (or New…, Font Image… in the context menu of the explorer, or Font Image… on a font file).
  2. Choose a font of the computer, or Choose a file… for a .ttf or .otf font.
  3. Choose the characters (an image holds 256 consecutive characters: the first page is the Latin alphabet), the cell (the size of a character in the image) and the range.
  4. Optionally, type a text and a name for it: the dialog then also makes the small image that holds that text, and gives the code that draws it.
  5. Press Create. The images are written in the folder that was in front in the explorer (the one you right-clicked, the one of the font file, the library itself when none), which you can change in the Folder field. The explorer opens it, and Show the image opens the picture.

A larger cell is sharper when the text is drawn big, and makes a larger image. The Wide range gives the effects (outline, glow, shadow) more room around the letters.

The image is a copy of the outlines of the font. Check that the license of the font allows it, above all if you deliver the result (a plug-in, a video). The dialog tells when a font says that it cannot be embedded.

Read it in a shader

Add the image as an Imported input (+), then use the snippets of the Text & sprites family:

SnippetWhat it gives
Font CharHow much of a pixel a character covers, from 0 to 1.
Font TextThe same for a whole text held in a text image, up to 256 characters. A line break goes to the next line.
Font Char Distance, Font Text DistanceThe signed distance in pixels to the letters, for outlines and glows.
Font Advance, Font Line HeightThe metrics of the font, to lay text out yourself.
Sprite CellThe cell of a sprite sheet, for any grid of pictures.

The text reaches the shader in one of two ways: as code that the dialog gives (the text is written in a comment above it), or in a small text image, one pixel for each character, that you can swap without touching the shader.

Draw text in a pipeline

In a pipeline, the Text category has a Draw Text node. It takes the string that is wired to its text input, and draws it with the font image wired to font (an imported image): p is the pixel in pixels from the start of the first line, size the em in pixels, spacing and lineSpacing what is added to each character and to each line, as a fraction of size (0, the default, is the font's own: 0.1 opens the characters a little, a negative number closes them), result the coverage from 0 to 1 and sdf the signed distance in pixels, for outlines and glows.

aabbMin and aabbMax give the box of the text, in the same pixels as p (from the start of the first line, y upwards): from the start of the lines to the end of the longest one, and from the descender of the last line to the ascender of the first. It is the box of the lines, not of the ink: spaces count, and every glyph is measured by its advance. Use it to centre or align a text, or to draw a background behind it.

The string comes from a Text, Comment or TODO node of the same category: each has a string output. Write the text in the node, wire it to Draw Text. The text is written in the shader as code, one call for each character, so the shader has no string and no extra image to carry: it is the Font Text snippets' unrolled form. A text holds up to 256 characters, and a line break goes to the next line. A string cannot cross the edge of a group, a loop or a function: keep Text and Draw Text in the same graph.

Type a text, one character at a time

The Typewriter node (same category) takes the same string, the same font image and the same p, size, spacing and lineSpacing as Draw Text, and says how much of the text is written at a given time: time is the seconds since it starts and speed the characters a second. Its result is 1 where the text is written and 0 elsewhere, with an edge one pixel wide at the pen: multiply it by the result of Draw Text. It adds up the width of each character, with the font, as Draw Text places them, so the edge always falls between two letters, whatever the font. A line break counts as a character; the lines before the one being written are all there.

It also gives characters, the length of the text, and penEnd, where the pen ends, in the same pixels as p. index is the number of the character under the pixel (-1 where there is none) and cell where the pixel lies in its cell: x from 0 to 1 across its advance, y in em above the baseline. They give each letter a time of its own: take the clock of the typewriter away by index and each letter starts after the one before. With cell round the middle of the letter, and the sdf of Draw Text (positive inside: pass its opposite), the Draw On snippet traces the outline of each letter by turning round it, then fills it, and Staggered Progress gives each letter its own time (the Titles - Trace example of ISF Studio does it). To type a second text, maybe in another font, after the first: take penEnd away from p for the second Draw Text and the second Typewriter, and start the clock of the second late by characters / speed. To make it start again, wrap the time (mod) by the length of the loop you want.

For a text held in an image (made by the Font Image tool) the snippets do the same: Font Text Length, Typewriter Count and Font Text Typed. Soft Wipe is the other way to write a text on: a mask with a soft edge that goes along a width, whatever the text and the font.

Limits

  • The distance is exact within half the range on each side of the edge of a letter; further, it only says "outside". A wide effect needs the Wide range.
  • An image holds 256 characters. For another alphabet, make another image and choose its characters.
  • A text image holds up to 256 characters.