How Pango Handles Bidirectional Text in GIMP
This article examines how the open-source text layout engine Pango enables the GNU Image Manipulation Program (GIMP) to process complex typography. By integrating Pango, GIMP gains full support for bidirectional text layouts, accurate glyph shaping for diverse world scripts, dynamic font fallback, and span-level text formatting within native text layers.
The Role of Pango in GIMP
Pango is the core text-rendering framework used across GTK-based applications, including GIMP. While GIMP provides the graphical canvas, user interface, and layer management, it delegates the actual calculation of text metrics, line breaking, and character positioning directly to Pango. When a user inputs text into a GIMP text layer, Pango processes the raw Unicode strings into positioned visual glyphs before GIMP rasterizes them onto the canvas.
Bidirectional (BiDi) Text Processing
The most critical role Pango serves in multilingual environments is its implementation of the Unicode Bidirectional Algorithm (UBA). Certain languages—such as Arabic, Hebrew, Persian, and Urdu—are read from right to left (RTL), while languages like English or Russian are read from left to right (LTR).
When text contains a mixture of RTL and LTR scripts (such as an English product name inside an Arabic sentence, or numbers within Hebrew text), Pango automatically:
- Determines Base Direction: Resolves the overall paragraph direction based on context or user-defined layer properties.
- Segments Text Runs: Splices the input into uniform directional runs so each script segment is handled properly.
- Reorders Glyphs Visually: Reorders the logical sequence of characters (the order typed or stored in memory) into the correct visual sequence required for display and printing.
- Maintains Insertion Points: Correctly places the cursor and handles text selection highlights across directional boundaries in GIMP's active on-canvas text editor.
Complex Script Shaping via HarfBuzz
Bidirectional scripts often require contextual shaping, where the visual form of a letter changes depending on its position within a word (isolated, initial, medial, or final). Pango integrates with HarfBuzz, a specialized shaping engine, to translate sequences of Unicode codepoints into context-appropriate OpenType glyphs, handle mandatory ligatures, and correctly attach diacritics and vowel marks.
Inline Formatting and Pango Markup
GIMP's modern text tool allows users to apply localized formatting—such as bolding a single word, altering color per character, or mixing font families within one layer. Pango makes this possible through an internal attribute list similar to Pango Markup.
Instead of treating a text layer as uniform, Pango applies styling attributes across specific character offsets. When directional text shifts or reflows, these formatting attributes travel with their corresponding characters rather than staying anchored to static visual coordinates.
Font Fallback Handling
If a chosen font lacks the necessary glyphs to display an RTL script, Pango automatically executes font fallback. It queries system fonts to locate a suitable typeface containing the missing characters, ensuring that mixed-script text layers render without displaying broken glyph boxes ("tofu"), all while preserving the original layout structure within GIMP.