harfbuzz-world.cc

Your one-stop HarfBuzz shop

Try:
Drop a font file

Welcome to HarfBuzz World

harfbuzz-world.cc builds HarfBuzz as a single C++ translation unit for use in C or C++ projects. Explore its shaping, subsetting, and rendering tools in your browser:

Each tab includes code examples and further reading.

Embedding

Add HarfBuzz to your project as a submodule or vendor a checkout alongside your code. Compile src/harfbuzz-world.cc, the amalgamation that includes the library's source files in a single C++ translation unit:

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

Link harfbuzz-world.o with the rest of your program and call the public C API as usual:

#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 compile-time #defines. Pass them on the command line (-DHB_TINY) or via a small wrapper header that #defines them and 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 Mac system fonts.

Building for the web (Emscripten)

The amalgamation also compiles to WebAssembly with em++. This site 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:

  • uharfbuzz — Python bindings, used by FontTools.
  • harfbuzzjs — JavaScript and WebAssembly bindings for browsers and Node.js.
  • harfrust — a Rust port with no C dependency.

Powered by HarfBuzz
the ffmpeg of text rendering
Built (cdd500c8)
Feedback