1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00
Files
Vadim Pisarevsky a3a4d4adac Merge pull request #29300 from vpisarev:add_harfbuzz
Added harfbuzz; use it instead of STB to render text #29300

Merge with https://github.com/opencv/opencv_extra/pull/1378.

This is the next big step to improve font rendering in OpenCV 5.x. See #18760, #26301. See also OpenCV 5.0 release notes, where it was pointed out that complex scripts are not rendered correctly, and HarfBuzz integration is needed to fix it. So, here it is — HarfBuzz integration:

* Removed tweaked STB engine. STB truetype rendering engine was included into OpenCV 5.0-pre/5.0 and it was extended to instantiate and render variable fonts, but the support was incomplete and immature.
* Instead, we now use [HarfBuzz](https://github.com/harfbuzz/harfbuzz) — famous font shaping library and now also font rendering library, used by many big companies and organizations.
* As a result, we now correctly render complex scripts, such as arabic or devanagari, correctly handle font ligatures (ff, fi, ft etc.)

<img width="1300" height="600" alt="text_test" src="https://github.com/user-attachments/assets/a7d2c754-0fcf-40f8-8104-578f3a14850b" />

* Potentially, we could also support color emoji, but that would be a next step.
* As with STB-based engine, glyphs are cached (the same glyph from the same font is not rendered twice), so the performance should be on par with the previous engine.
* When OpenCV is built with `-DWITH_HARFBUZZ=ON`, which is set by default, it tries to detect HarfBuzz in the system and use it. If it's not detected, OpenCV builds our own small subset of HarfBuzz.

The following table compares size of libopencv_imgproc.dylib.5.0.0 (Release mode) in different configurations:

| Configuration                          | libopencv_imgproc (stripped) | delta vs 5.0 |
|----------------------------------------|------------------------------|----------|
| imgproc without text rendering (`-DWITH_HARFBUZZ=OFF`)  | 4.27 MB  |  −2.35 MB |
| imgproc with stb-based engine (5.0)      | 6.62 MB      |  0.0 Mb |
| imgproc with HarfBuzz-based engine (this PR)     | 7.07 MB      |  +0.45 MB |

The biggest source of the size increase when OpenCV is built with text rendering support (STB- or Harfbuzz-based) is that imgproc in this case includes .ttf fonts (Rubik and 'Wen Quan Yi Micro Hei'), which are gzip-compressed, but still have noticeable size, especially 'Wen Quan Yi' (~2Mb). HarfBuzz itself, compared to STB engine, adds just 0.45Mb, or ~7% to imgproc size. On other platforms (Linux x64, Windows), where IPP or other acceleration libraries are linked into libopencv_imgproc, the harfbuzz footprint is even less noticeable.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2026-06-16 10:05:32 +03:00

71 lines
3.0 KiB
CMake

# ----------------------------------------------------------------------------
# CMake file for the bundled HarfBuzz text-shaping library. See root CMakeLists.txt
#
# OpenCV vendors a minimal subset of HarfBuzz (core + HB_HAS_RASTER) as a
# normal static library: each .cc is a separate translation unit. The subset
# is produced by hb_extract.py (see that script to update HarfBuzz).
# ----------------------------------------------------------------------------
project(${HARFBUZZ_LIBRARY} CXX)
ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")
# Every .cc copied by hb_extract.py is a real translation unit (the script
# never copies non-TU .cc files), so a plain recursive glob is exactly right.
file(GLOB_RECURSE lib_srcs src/*.cc)
file(GLOB_RECURSE lib_hdrs src/*.h src/*.hh)
# HarfBuzz is built with HB_TINY for minimum footprint. Because we request the
# software rasterizer (HB_HAS_RASTER, defined below), HB_TINY does NOT disable
# the draw/paint/color APIs nor CFF (PostScript/.otf
# outlines) -- see hb-config.hh: HB_NO_DRAW/COLOR/PAINT and the TINY->HB_NO_CFF
# rule only trigger when no HB_HAS_* backend is requested. So CFF (CJK/Indic),
# COLR/CPAL color and our rasterizer all survive HB_TINY.
#
# Two features HB_TINY (via HB_LEAN) would otherwise drop are restored through
# HarfBuzz's config-override hook (hb-opencv-config.hh): thread-safety (HB_NO_MT)
# and variable fonts (HB_NO_VAR). No platform backends (CoreText/DirectWrite/...).
add_library(${HARFBUZZ_LIBRARY} STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${lib_srcs} ${lib_hdrs})
target_compile_definitions(${HARFBUZZ_LIBRARY} PRIVATE
HB_TINY
HB_HAS_RASTER # request the software rasterizer: keeps draw/paint/color + CFF under HB_TINY
"HB_CONFIG_OVERRIDE_H=\"hb-opencv-config.hh\"")
set_target_properties(${HARFBUZZ_LIBRARY} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
ocv_warnings_disable(CMAKE_CXX_FLAGS
-Wunused-variable -Wunused-function -Wunused-parameter
-Wunused-but-set-variable # clang15/gcc
-Wshadow
-Wmissing-declarations # gcc
-Wmissing-prototypes # clang
-Wimplicit-fallthrough
-Wextra-semi # clang
-Wdeprecated-copy # gcc/clang
-Wsuggest-override
-Wcast-function-type
-Wclass-memaccess # gcc
-Wexpansion-to-defined
)
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4244 /wd4267 /wd4127 /wd4146 /wd4456 /wd4459) # MSVC
set_target_properties(${HARFBUZZ_LIBRARY}
PROPERTIES OUTPUT_NAME ${HARFBUZZ_LIBRARY}
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
COMPILE_PDB_NAME ${HARFBUZZ_LIBRARY}
COMPILE_PDB_NAME_DEBUG "${HARFBUZZ_LIBRARY}${OPENCV_DEBUG_POSTFIX}"
ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}
)
if(ENABLE_SOLUTION_FOLDERS)
set_target_properties(${HARFBUZZ_LIBRARY} PROPERTIES FOLDER "3rdparty")
endif()
if(NOT BUILD_SHARED_LIBS)
ocv_install_target(${HARFBUZZ_LIBRARY} EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev OPTIONAL)
endif()