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
2026-09-17 11:51:55 +03:00
2026-07-28 20:27:29 +05:30
2018-10-11 17:57:51 +00:00
2025-08-07 10:35:20 +03:00

OpenCV: Open Source Computer Vision Library

Resources

Contributing

Please read the contribution guidelines before starting work on a pull request.

Summary of the guidelines:

  • One pull request per issue;
  • Choose the right base branch;
  • Include tests and documentation;
  • Clean up "oops" commits before submitting;
  • Follow the coding style guide.

Additional Resources

Description
Languages
C++ 87.7%
C 3.1%
Python 2.9%
CMake 2%
Java 1.5%
Other 2.6%