Files
opencv-MIRROR/modules/imgproc/test/test_main.cpp
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

29 lines
1.1 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#if defined(HAVE_HPX)
#include <hpx/hpx_main.hpp>
#endif
#include "opencv2/core/utils/filesystem.hpp"
// The drawing tests (and their reference pictures) were made with the "italic"
// (Rubik Italic) and "uni" (WenQuanYi Micro Hei) fonts compiled into OpenCV. Both
// are optional now (WITH_ITALICFONT / WITH_UNIFONT, OFF by default), so provide
// them as external files from opencv_extra when they are available.
static void registerExternalTestFonts()
{
const std::string dir = cvtest::TS::ptr()->get_data_path() + "../highgui/drawing/";
const char* fonts[][2] = {{"italic", "Rubik-Italic.ttf.gz"}, {"uni", "WenQuanYiMicroHei.ttf.gz"}};
for (const auto& f : fonts)
{
const std::string path = dir + f[1];
if (cv::utils::fs::exists(path))
cv::FontFace::setBuiltinFont(f[0], path);
}
}
CV_TEST_MAIN("cv", registerExternalTestFonts())