Files
opencv-MIRROR/3rdparty/harfbuzz
Vadim Pisarevsky d5c56723d1 imgproc: shrink the text rendering footprint (HarfBuzz -Os, optional built-in fonts)
libopencv_imgproc grew noticeably with the HarfBuzz-based text engine. Measured on
macOS/arm64 (clang, Release, stripped) the text-related part of the library was
~830 KB: HarfBuzz 486 KB, the two Rubik fonts 296 KB, the engine ~25 KB, plus ~2 MB
of WenQuanYi when WITH_UNIFONT=ON (the previous default). With GCC and an unstripped
.so on Linux the HarfBuzz part alone is 700+ KB.

This patch brings it down to ~550 KB by default (HarfBuzz 377 KB + Rubik 146 KB +
engine 30 KB) without dropping functionality:

* 3rdparty/harfbuzz: build with -Os -fno-exceptions -fno-rtti -fno-threadsafe-statics
  (like HarfBuzz's own build; shaping speed is irrelevant thanks to the glyph cache)
  and HB_NO_VAR_COMPOSITES (VARC table, a 2024 format with no real fonts yet).
  Variable-font support (wght axis etc.) is untouched. -22% of HarfBuzz code.

* Only the "sans" font (Rubik) is compiled into imgproc by default. Rubik Italic and
  WenQuanYi Micro Hei became optional: WITH_ITALICFONT (new) and WITH_UNIFONT are
  both OFF now. The build summary lists the built-in fonts.

* New API cv::FontFace::setBuiltinFont(name, path) fills or replaces any of the
  three built-in slots ("sans", "italic", "uni") with an external font file. The
  registered font takes part in the automatic fallback chain exactly like a
  compiled-in one, so users can get CJK text or a true italic without rebuilding
  OpenCV. The registry is process-wide; each thread's engine reloads its slots
  lazily (generation counter), and the glyph cache is dropped on reload since
  hb_font pointers are part of its key.

* "italic" resolves to "sans" when no italic font is built in or registered, so
  FONT_ITALIC / FONT_HERSHEY_SCRIPT_* in the legacy putText keep working (upright).

* Fix: a character not covered by any font used to be drawn as '?' taken from the
  last built-in slot, which can be empty now; the '?' is looked up like any other
  character (user font first).

* Tests: opencv_test_imgproc registers Rubik-Italic.ttf.gz and
  WenQuanYiMicroHei.ttf.gz from opencv_extra (testdata/highgui/drawing) as the
  "italic"/"uni" slots, so Drawing.ttf_text reproduces the reference picture 1:1
  with the default build and the legacy hershey-font tests are unchanged.
  Drawing.builtin_font_slots covers the new API. Tests skip if the fonts are absent.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-29 23:03:04 +03:00
..

HarfBuzz

HarfBuzz Logo

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

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.

Auxiliary libraries

Library Description
libharfbuzz-icu ICU Unicode integration.
libharfbuzz-cairo Cairo rendering integration.
libharfbuzz-gobject GObject/GI bindings.

Experimental libraries

Library Description
libharfbuzz-raster Glyph rasterization to bitmaps, including color fonts. Uses hb-draw and hb-paint.
libharfbuzz-vector Glyph output to vector formats (currently SVG), 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.

Command-line tools

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.

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.

xkcd-derived image

Distribution

Packaging status of HarfBuzz

Packaging status