harfbuzz-world.cc

Your one-stop HarfBuzz shop

Try:
Drop a font file

Welcome to HarfBuzz World

Explore HarfBuzz shaping, subsetting, and rendering in your browser. This site also demonstrates how to use harfbuzz-world.cc, a single-file amalgamation for C and C++ projects.

Each tab includes code examples and further reading.

Embedding

Add HarfBuzz as a submodule or vendored checkout, then compile src/harfbuzz-world.cc as C++. The amalgamation combines the library's source files into a single translation unit:

c++ -std=c++11 -O2 \
    -c third_party/harfbuzz/src/harfbuzz-world.cc \
    -o harfbuzz-world.o

Link the resulting object into your application and use HarfBuzz's public C API:

#include <hb.h>
#include <hb-ot.h>

hb_blob_t  *blob = hb_blob_create_from_file ("MyFont.ttf");
hb_face_t  *face = hb_face_create (blob, 0);
hb_font_t  *font = hb_font_create (face);
hb_buffer_t *buf = hb_buffer_create ();

hb_buffer_add_utf8 (buf, "Hello", -1, 0, -1);
hb_buffer_guess_segment_properties (buf);  /* toy: real apps set script/lang/dir explicitly */
hb_shape (font, buf, NULL, 0);

unsigned len;
hb_glyph_info_t      *info = hb_buffer_get_glyph_infos     (buf, &len);
hb_glyph_position_t  *pos  = hb_buffer_get_glyph_positions (buf, NULL);
/* …draw the glyphs… */

Customizing the build

Configure the amalgamation with preprocessor definitions. Pass them on the command line (-DHB_TINY) or define them in a small wrapper header that then includes harfbuzz-world.cc.

  • HB_TINY — enable a compact build by disabling optional features, including deprecated APIs, variable fonts, AAT, legacy fallbacks, hinting, glyph names, math, name-table access, and metrics. It also selects compiler optimizations for size. All script shapers remain available. Use the HB_HAS_* flags below to include the subsystems you need.
  • HB_HAS_SUBSET — include hb-subset for font subsetting.
  • HB_HAS_RASTER — include hb-raster for software rendering.
  • HB_HAS_VECTOR — include hb-vector for SVG and PDF output.
  • HB_HAS_GPU — include hb-gpu for GPU rendering with the Slug algorithm.
  • HB_HAS_FREETYPE, HB_HAS_GLIB, HB_HAS_ICU, HB_HAS_GRAPHITE, HB_HAS_CAIRO, and others — enable the corresponding library integrations.

To restore a feature disabled by HB_TINY, undefine its HB_NO_* flag in your override header. For example, undefine HB_NO_AAT to enable shaping with Apple Advanced Typography tables such as morx, kerx, and trak, used by some macOS system fonts.

Building for the web (Emscripten)

The amalgamation also compiles to WebAssembly with em++. This site's build enables shaping, subsetting, raster rendering, vector output, and AAT support. The GPU tab embeds a separate hb-gpu demo.

em++ -std=c++11 -Oz -flto \
     -I third_party/harfbuzz/src \
     -DHB_TINY -DHB_HAS_SUBSET -DHB_HAS_RASTER \
     -DHB_HAS_VECTOR -DHB_HAS_GPU \
     bindings.cc third_party/harfbuzz/src/harfbuzz-world.cc \
     -sMODULARIZE=1 -sALLOW_MEMORY_GROWTH=1 \
     -sEXPORTED_FUNCTIONS='["_my_shape_export","_malloc","_free"]' \
     -o myhb.js

See this site's scripts/build.sh and src/bindings.cc for a complete working example.

Resources

Other languages

HarfBuzz is also available through these bindings and ports:

  • HarfRust — a fully safe Rust port of the HarfBuzz shaper that also exposes a C API.
  • uharfbuzz — Python bindings, used by FontTools.
  • harfbuzzjs — JavaScript and WebAssembly bindings for browsers and Node.js.

Powered by HarfBuzz
the FFmpeg of text rendering
Built (1f6af007)
Feedback