[vector] Pad a PDF gradient in its last color where that comes late
A color line that pads goes on after its last stop in that stop's
color. pdfium takes 256 samples of a shading's function, at 0, 1/256
and so on to 255/256, and goes on after the shading in the last of
them: in what the function gives at 255/256, not at 1. For a color
line of two stops that is within a level of the last color. Where
the last two stops are close together it is not: with red at 0, green
at 0.9895 and white at 1, pdfium padded in (160, 208, 160), the color
63% of the way from green to white, where it should be white.
Two stops at the last offset already get the color line lengthened
by a stretch in the last color. Do that as well where the color
line is more than two levels short of its last color at 255/256, in
alpha or in what shows of red, green or blue, which is that times
alpha. The stretch for it is the last 128th of the lengthened line,
which has pdfium's last sample in it; two stops at one offset keep
their sixteenth, which puts them at a sample. A radial gradient
that ends at a radius of zero paints nothing after it and is left
alone there. It no longer counts as lengthened at that end when the
stretch at its start is sized, so two stops at its start are a
sixteenth in as well.
Testing: test_glyphs-glyf_colr_1_variable.ttf has those stops at
COL1=0.496,COL3=-0.885. At 160 pixels per em, the pixels of its
three glyphs with gradients that pad that pdfium has more than 24
levels from hb-raster's:
before after
linear_gradient_extend_mode_pad 57% none
radial_contained_gradient_extend_mode_pad 64% none
radial_horizontal_gradient_extend_mode_pad 17% 0.15%
MuPDF, poppler, Ghostscript, Chrome and librsvg have them as before:
none, and 0.12% for the last one.
Replaying the same paint calls on hb-raster and hb-vector for 300
random scenes of linear and radial gradients with a stop from 0.001
to 0.2 of the length in from an end, how many have the same pixels:
before after
pdfium 223 300
MuPDF 300 300
poppler cairo 300 300
poppler Splash 300 300
Ghostscript 290 288
Chrome (SVG) 300 300
librsvg (SVG) 300 300
The two that Ghostscript newly differs on, by 0.4% and 0.2% of the
pixels, both reduce to a linear gradient under a rotation, as do the
two largest of the ten that it differed on before.
Scenes of other gradients, 1800 of them, are written byte for byte
as before, those with two stops at an end among them. Of the 1553
mapped glyphs of the COLR test fonts the PDF of four changes, the H
of BitcountInk and the three glyphs of KalniaGlaze; their pictures
in pdfium, MuPDF, poppler and Ghostscript stay as they were to within
0.03% of the pixels. No SVG changes.
The test for padding has more cases, and fails without the change.
It is clean as C99 with -pedantic under gcc and clang, and the vector
tests pass under ASan, UBSan and MSan. hb-vector-fuzzer, built with
coverage instrumentation and run from the COLR test fonts with
failing allocations, made 289591 runs in three minutes on eight jobs
without a finding. Full test suite passes.
Found comparing every glyph of the variable test font at random
locations in its design space.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
HarfBuzz started as a text shaping engine but has grown into a full font platform — the ffmpeg of text shaping. It primarily supports OpenType, but also Apple Advanced Typography.
HarfBuzz shapes the majority of text on modern screens.
HarfBuzz is optimized for robustness, correctness, and performance — in that order. Achieve all.
Try it live at harfbuzz-world.cc — an interactive playground for shaping, subsetting, rasterization, vector output, and GPU rendering, all running in your browser.
Here is a quick map of its components:
| Library | Description |
|---|---|
| libharfbuzz | Text shaping, draw API, paint API. Highly configurable (see CONFIG.md). Optional integration backends compiled in: hb-ft (FreeType), hb-coretext (macOS), hb-uniscribe (Windows), hb-directwrite (Windows), hb-gdi (Windows), hb-glib, hb-graphite2. |
| libharfbuzz-subset | Font subsetting and variable-font instancing. |
| Library | Description |
|---|---|
| libharfbuzz-icu | ICU Unicode integration. |
| libharfbuzz-cairo | Cairo rendering integration. |
| libharfbuzz-gobject | GObject/GI bindings. |
| Library | Description |
|---|---|
| libharfbuzz-raster | Glyph rasterization to bitmaps, including color fonts. Uses hb-draw and hb-paint. |
| libharfbuzz-vector | Glyph output to vector formats (SVG and PDF), including color fonts. Uses hb-draw and hb-paint. |
| libharfbuzz-gpu | Encodes glyph outlines for GPU rasterization (Slug algorithm). Provides shader sources in GLSL, WGSL, MSL, and HLSL. Live demo. |
Notable missing feature: font hinting (including autohinting) is not implemented. For hinted rasterization, use FreeType or Skrifa.
For simplified builds, amalgamated sources are available: harfbuzz.cc (just libharfbuzz), harfbuzz-subset.cc (just libharfbuzz-subset), or harfbuzz-world.cc (everything, driven by a custom hb-features.h). For a live in-browser playground plus a worked example of the world.cc single-file build, see harfbuzz-world.cc.
| Tool | Description |
|---|---|
| hb-shape | Shape text and display glyph output. |
| hb-view | Render shaped text to an image. |
| hb-subset | Subset and optimize fonts. |
| hb-info | Display font metadata. |
| hb-raster | Render glyphs to bitmap images. |
| hb-vector | Render glyphs to vector formats (SVG). |
| hb-gpu | Interactive GPU text rendering. |
The canonical source tree and bug trackers are available on github. Both development and user support discussion around HarfBuzz happen on github as well.
For license information, see COPYING.
The API that comes with hb.h will not change incompatibly. Other, peripheral, headers are more likely to go through minor modifications, but again, we do our best to never change API in an incompatible way. We will never break the ABI.
The API and ABI are stable even across major version number jumps. In fact, current HarfBuzz is API/ABI compatible all the way back to the 0.9.x series. If one day we need to break the API/ABI, that would be called a new library.
As such, we bump the major version number only when we add major new features, the minor version when there is new API, and the micro version when there are bug fixes.
For user manual as well as API documentation, check: https://harfbuzz.github.io
Tarball releases and Win32/Win64 binary bundles are available on the github releases page.
For build information, see BUILD.md.
For custom configurations, see CONFIG.md.
For testing and profiling, see TESTING.md.
For using with Python, see README.python.md. There is also uharfbuzz.
For cross-compiling to Windows from Linux or macOS, see README.mingw.md.
To report bugs or submit patches please use github issues and pull-requests.
To get a better idea of where HarfBuzz stands in the text rendering stack you may want to read State of Text Rendering 2024. Here are a few presentation slides about HarfBuzz over the years:
More presentations and papers are available on behdad's website. In particular, the following studies are relevant to HarfBuzz development:
hb-decyclerhb-iterhb-ft >h_advance functionHarfBuzz /hærfˈbɒːz/
From Persian حرف (Harf: letter) and باز (Buzz: open). Transliteration of the Persian calque for OpenType.
As a noun: The Open Source text shaping engine.
As an adjective: Insincerely talkative; glib. A nod to the GNOME project where HarfBuzz originates from.
The logo shows حرفباز in the IranNastaliq font, on a Damascus steel background.
Background: Originally there was this font format called TrueType. People and companies started calling their type engines all things ending in Type: FreeType, CoolType, ClearType, etc. And then came OpenType, which is the successor of TrueType. So, for my OpenType implementation, I decided to stick with the concept but use the Persian translation. Which is fitting given that Persian is written in the Arabic script, and OpenType is an extension of TrueType that adds support for complex script rendering, and HarfBuzz is an implementation of OpenType text shaping.
HarfBuzz is used in Android, Chrome, ChromeOS, Firefox, Flutter, GNOME, GTK+, KDE, Qt, LibreOffice, OpenJDK, XeTeX, Adobe Photoshop, Illustrator, InDesign, Microsoft Edge, Amazon Kindle, PlayStation, Godot Engine, Unreal Engine, Figma, Canva, QuarkXPress, Scribus, smart TVs, car displays, and many other places.