1 xtk_shape
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_shape.h

Text shaping: the glyphs a font gives for a run of characters.

Xft draws one glyph for each character. Many fonts ask for more: "f" and "i" become one glyph, an Arabic letter takes a different form at the start, in the middle and at the end of a word, and the characters of an emoji sequence become one picture. The font lists these substitutions in its OpenType GSUB table. This module reads that table and gives the glyphs to draw, so a program can draw them with XftDrawGlyphs().

Use. Call xtk_shape_run() with the characters of a run that is drawn in one font, in reading order. It writes one XtkShapeGlyph for each glyph to draw, in the order of the characters. The cluster field of each glyph is the index of the first character it stands for, so the caller can find where a ligature starts and which characters it covers (all characters up to the next glyph's cluster). Before it shapes a run, a caller can call xtk_shape_font_has() to learn if the font has any substitution at all; if it has none, the plain one glyph for each character path is correct. xtk_shape_joins() tells if a character is a joining letter, which needs XTK_SHAPE_JOINING.

Memory. The first call for a font copies its GSUB and GDEF tables and keeps them until the program exits. They are found again by the font's file and face index, so one font file opened at several sizes shares one copy, and a font that is closed does no harm. The caller owns the output array.

What it does. The features ccmp and rlig (XTK_SHAPE_COMPOSITION), liga, clig and calt (XTK_SHAPE_LIGATURES), and for runs that contain a joining letter isol, init, medi and fina (XTK_SHAPE_JOINING). The lookup types single, multiple, ligature, context, chained context and extension, and the "ignore marks" lookup flag (with the glyph classes from GDEF). The script is "arab" for a run with a joining letter and "latn" for others, with "DFLT" as the fallback; the language is the script's default one.

Example.

// display, font (an XftFont), draw (an XftDraw) and color (an XftColor) are the program's
unsigned int text[] = {'o', 'f', 'f', 'i', 'c', 'e'};
XtkShapeGlyph glyphs[64];
int count = xtk_shape_run(display, font, text, 6, XTK_SHAPE_ALL, glyphs, 64);
XftGlyphSpec specs[64];
int x = 10;
for (int index = 0; index < count; index++) {
    XGlyphInfo info;
    XftGlyphExtents(display, font, &glyphs[index].glyph, 1, &info);
    specs[index].glyph = glyphs[index].glyph;
    specs[index].x = (short)x;
    specs[index].y = (short)(20 + font->ascent);
    x += info.xOff;
}
XftDrawGlyphSpec(draw, &color, font, specs, count);
 * 

Limitations.

  • There is no glyph positioning (GPOS): no kerning and no placing of a mark over its letter. Each glyph advances by its own width.
  • Alternate substitution (type 3), reverse chained substitution (type 8) and features of a language other than the script's default are not done.
  • A run is shaped up to 1024 characters (and grows to at most 1024 glyphs); the characters after that are left out. At most 32 font files have their tables kept: fonts after that are drawn with one glyph for each character.
#include "xtk_shape.h"

Types: XTK_SHAPE_COMPOSITION and the other values, XtkShapeGlyph

Functions: xtk_shape_font_has, xtk_shape_joins, xtk_shape_run

Types

XTK_SHAPE_COMPOSITION and the other values

Bits that choose which substitutions xtk_shape_run() and xtk_shape_font_has() look at.

Name Value Description
XTK_SHAPE_COMPOSITION 1 << 0 Glyph composition (ccmp) and required ligatures (rlig): what a font needs to show its script, and emoji sequences.
XTK_SHAPE_LIGATURES 1 << 1 Standard and contextual ligatures and alternates (liga, clig, calt): "fi", and the arrows of a programming font.
XTK_SHAPE_JOINING 1 << 2 The forms of letters that join (isol, init, medi, fina): Arabic.
XTK_SHAPE_ALL 7 All of them.

XtkShapeGlyph

One glyph of a shaped run, as xtk_shape_run() gives it.

Field Type Description
glyph FT_UInt The glyph's index in the font, as XftDrawGlyphs() and XftGlyphExtents() take it.
cluster int The index in the input of the first character this glyph stands for. Clusters never decrease along the output.

Functions

xtk_shape_font_has

int xtk_shape_font_has(Display *display, XftFont *font, int features);

Tells if a font has substitutions of the kinds asked for.

The test is made for text with no joining letters, so the XTK_SHAPE_JOINING bit is ignored. When this returns 0, a caller can draw text that has no joining letters in this font with one glyph for each character and skip xtk_shape_run(). The first call for a font loads its tables (see the memory note above).

Parameter Description
display The display the font was opened on.
font The font to test.
features XTK_SHAPE_* bits: the kinds of substitution to look for.

Returns: 1 when the font's GSUB table has at least one lookup for the features asked for; 0 when it has none, when the font has no GSUB table, or when display or font is NULL.

xtk_shape_joins

int xtk_shape_joins(unsigned int codepoint);

Tells if a character is a letter that joins to its neighbors.

Joining letters are those of Arabic, Syriac and similar scripts (Unicode joining types dual, right and left). A run that contains one needs XTK_SHAPE_JOINING to get the correct letter forms. Marks and the zero width joiner are not letters and give 0.

Parameter Description
codepoint The character, as a Unicode code point.

Returns: 1 when the character is a joining letter, 0 when it is not.

xtk_shape_run

int xtk_shape_run(
    Display *display,
    XftFont *font,
    const unsigned int *codepoints,
    int count,
    int features,
    XtkShapeGlyph *glyphs,
    int limit);

Gives the glyphs to draw for a run of characters in one font.

The characters are given in reading order: for right-to-left text the first character read is the first one in the array. The glyphs come out in the same order, so a caller that draws right-to-left text reverses them itself. A character the font does not have gives the font's "missing" glyph (index 0). When the font has no lookup for the features asked for, the result is one glyph for each character, with cluster equal to the character's index.

A substitution can make the run longer (one character becomes several glyphs) or shorter (a ligature). When there are more glyphs than limit, only the first limit are written.

Parameter Description
display The display the font was opened on.
font The font that the whole run is drawn in.
codepoints The characters, as Unicode code points.
count The number of characters. At most 1024 are shaped; the characters after that are ignored.
features XTK_SHAPE_* bits: which substitutions to do.
glyphs Receives the glyphs.
limit The number of entries in glyphs.

Returns: The number of glyphs written to glyphs. 0 when display, font, codepoints or glyphs is NULL, when count or limit is less than 1, or when memory runs out.