[vector] Write the Porter-Duff composite modes in PDF

PDF has the blend modes and none of the Porter-Duff operators, so
pop_group painted PLUS as Screen, XOR as Difference, and the other
ten modes as Normal.  composite_CLEAR in test_glyphs-glyf_colr_1.ttf
came out with both of the squares that it should have erased, and
the 28 glyphs of that font that composite in DEST_OVER came out with
their two layers the wrong way up.

Now that groups are Form XObjects, a group can be a soft mask as well
as be painted, and that is all these modes take:

  CLEAR, SRC, DEST, DEST_OVER   neither, one, or both the other way up
  SRC_IN, DEST_IN               one group, through the other's alpha
  SRC_OUT, DEST_OUT             one, through the inverse of it
  SRC_ATOP                      the backdrop, then SRC_IN
  DEST_ATOP                     the source, then DEST_IN
  XOR                           DEST_OUT, then SRC_OUT in Screen

The inverse of a group's alpha is the luminosity of a group that is
white with black painted over it through the alpha mask.  A transfer
function on the alpha mask would say that in fewer objects, but
poppler's cairo backend does not apply one.

The last three, and PLUS, are sums of two layers, and PDF has no way
to add: the upper layer hides some of the lower where both are partly
transparent.  As written, SRC_ATOP is exact where the backdrop is
opaque or absent, DEST_ATOP where the source is, and XOR where either
is.  PLUS stays Screen.  The API documentation now says which modes
are approximate, in PDF and in SVG.

As in the SVG writer, the backdrop is everything the enclosing group
has painted: transforms and clips that are open in it are closed
around the backdrop, repeated around the source, and opened again;
pops that do not match their pushes leave the content streams
balanced; and what a document refers to more than once counts against
the document cap.  Something painted with a blend mode in the root
group would blend with the background too, so a root that has one is
painted as a group of its own.  render now fails if the root's
content stream was cut short, as it does for a missing object.

The graphics states that are kept for sizing sweep gradients are
popped where a Q is written, so that they go on describing the
content stream when a pop has nothing to close.

Testing: hb-vector's PDF for the thirteen composite_* glyphs from
CLEAR to PLUS, and for modes.ttf from How HarfBuzz Works, has the
pixels hb-raster gives them in poppler (Splash and cairo),
Ghostscript, pdfium and MuPDF, antialiased edges aside.  Over the
whole of test_glyphs-glyf_colr_1.ttf, 30 more glyphs now match
hb-raster in PDF, and with the two SVG changes before this one, 41
in SVG; none fewer.  Glyphs without a composite are byte for byte
what they were.  Replaying the same paint calls on hb-raster and
hb-vector for 300 random scenes of nested groups and translucent
fills, with groups popped under open transforms and clips and at the
root, the nine exact modes agree in poppler, pdfium and MuPDF on
every scene.  Ghostscript 10.05 differs on six.  The one that was
reduced comes down to a group that sets a soft mask being painted
through another, inside the group of a third, which Ghostscript also
gets wrong in a PDF written by hand.

The vector fuzzer and these scenes were also run under ASan and
UBSan, with and without failing allocations.  The composite test
gains the PDF counterparts of its SVG checks, and the thirteen glyphs
a second expected output.  Full test suite passes.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6829
Fixes: https://github.com/harfbuzz/harfbuzz/issues/5951

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
6 files changed
tree: ec39dc6c0ef52c2c9239bade11f6a5ace1218ccd
  1. .ci/
  2. .github/
  3. docs/
  4. perf/
  5. src/
  6. subprojects/
  7. test/
  8. util/
  9. .clang-format
  10. .codecov.yml
  11. .editorconfig
  12. AGENTS.md
  13. AUTHORS
  14. AVAR2-INSTANCING.md
  15. BUILD.md
  16. CMakeLists.txt
  17. CODE_OF_AI_CONDUCT.md
  18. CONFIG.md
  19. COPYING
  20. harfbuzz.doap
  21. HarfBuzz.png
  22. meson.build
  23. meson_options.txt
  24. NEWS
  25. README.md
  26. README.mingw.md
  27. README.python.md
  28. RELEASING.md
  29. replace-enum-strings.cmake
  30. SECURITY.md
  31. TESTING.md
  32. THANKS
  33. xkcd.png
README.md

HarfBuzz

Linux CI Status macoOS CI Status Windows CI Status OSS-Fuzz Status Coverity Scan Build Status Packaging status OpenSSF Scorecard

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:

Core libraries

LibraryDescription
libharfbuzzText 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-subsetFont subsetting and variable-font instancing.

Auxiliary libraries

LibraryDescription
libharfbuzz-icuICU Unicode integration.
libharfbuzz-cairoCairo rendering integration.
libharfbuzz-gobjectGObject/GI bindings.

Rendering libraries

LibraryDescription
libharfbuzz-rasterGlyph rasterization to bitmaps, including color fonts. Uses hb-draw and hb-paint.
libharfbuzz-vectorGlyph output to vector formats (SVG and PDF), including color fonts. Uses hb-draw and hb-paint.
libharfbuzz-gpuEncodes 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.

Command-line tools

ToolDescription
hb-shapeShape text and display glyph output.
hb-viewRender shaped text to an image.
hb-subsetSubset and optimize fonts.
hb-infoDisplay font metadata.
hb-rasterRender glyphs to bitmap images.
hb-vectorRender glyphs to vector formats (SVG).
hb-gpuInteractive 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.

API stability

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.

Documentation

For user manual as well as API documentation, check: https://harfbuzz.github.io

Download

Tarball releases and Win32/Win64 binary bundles are available on the github releases page.

Development

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.

Developer documents

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:

Name

HarfBuzz /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.

Users

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.

Distribution

Packaging status