mirror of
https://github.com/opencv/opencv.git
synced 2026-07-21 19:33:03 +04:00
a3a4d4adac
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
86 lines
3.7 KiB
CMake
86 lines
3.7 KiB
CMake
# HarfBuzz text shaping + rasterization for cv::putText / cv::getTextSize.
|
|
#
|
|
# Behaviour (mirrors BUILD_PNG / BUILD_ZLIB etc.):
|
|
# WITH_HARFBUZZ=ON (default)
|
|
# Try to use a system HarfBuzz; if it is missing or too old to provide the
|
|
# hb-raster software-rasterizer API we depend on, build the bundled subset
|
|
# in 3rdparty/harfbuzz.
|
|
# WITH_HARFBUZZ=ON BUILD_HARFBUZZ=ON
|
|
# Skip the system search and always build the bundled subset.
|
|
# WITH_HARFBUZZ=OFF
|
|
# Disabled entirely; BUILD_HARFBUZZ has no effect (as with BUILD_PNG etc.).
|
|
|
|
if(WITH_HARFBUZZ)
|
|
include(CheckCXXSourceCompiles)
|
|
|
|
ocv_clear_vars(HAVE_HARFBUZZ HARFBUZZ_IS_BUNDLED)
|
|
|
|
if(NOT BUILD_HARFBUZZ)
|
|
# --- look for a system HarfBuzz via pkg-config ---
|
|
ocv_clear_internal_cache_vars(HARFBUZZ_LIBRARY HARFBUZZ_LIBRARIES HARFBUZZ_INCLUDE_DIR)
|
|
ocv_check_modules(HARFBUZZ harfbuzz)
|
|
|
|
if(HAVE_HARFBUZZ)
|
|
# The hb-raster software rasterizer is a recent addition and may be
|
|
# shipped as a separate library: e.g. Homebrew/Linux split it into
|
|
# libharfbuzz-raster (with its own harfbuzz-raster.pc) while the plain
|
|
# harfbuzz.pc links only -lharfbuzz (no hb_raster_* symbols). Pull in the
|
|
# raster module when present; otherwise assume a monolithic build.
|
|
ocv_check_modules(HARFBUZZ_RASTER harfbuzz-raster)
|
|
if(HAVE_HARFBUZZ_RASTER)
|
|
# harfbuzz-raster.pc "Requires: harfbuzz", so its resolved flags already
|
|
# include the core library and include dir.
|
|
set(_hb_inc "${HARFBUZZ_RASTER_INCLUDE_DIRS}")
|
|
set(_hb_libs "${HARFBUZZ_RASTER_LIBRARIES}")
|
|
else()
|
|
set(_hb_inc "${HARFBUZZ_INCLUDE_DIRS}")
|
|
set(_hb_libs "${HARFBUZZ_LIBRARIES}")
|
|
endif()
|
|
|
|
# Verify the header and symbol actually compile and link with the chosen
|
|
# libraries; otherwise fall back to the bundled copy.
|
|
ocv_clear_vars(HAVE_HARFBUZZ)
|
|
set(CMAKE_REQUIRED_INCLUDES "${_hb_inc}")
|
|
set(CMAKE_REQUIRED_LIBRARIES "${_hb_libs}")
|
|
check_cxx_source_compiles("
|
|
#include <hb.h>
|
|
#include <hb-raster.h>
|
|
int main() {
|
|
hb_raster_draw_t* rd = hb_raster_draw_create_or_fail();
|
|
hb_raster_draw_render(rd);
|
|
return rd ? 0 : 1;
|
|
}" HARFBUZZ_HAS_RASTER)
|
|
unset(CMAKE_REQUIRED_INCLUDES)
|
|
unset(CMAKE_REQUIRED_LIBRARIES)
|
|
|
|
if(HARFBUZZ_HAS_RASTER)
|
|
set(HARFBUZZ_INCLUDE_DIR "${_hb_inc}" CACHE INTERNAL "")
|
|
set(HARFBUZZ_LIBRARIES "${_hb_libs}" CACHE INTERNAL "")
|
|
set(HAVE_HARFBUZZ 1)
|
|
else()
|
|
message(STATUS "HarfBuzz: found system version ${HARFBUZZ_VERSION} but it lacks the hb-raster API; building the bundled copy instead")
|
|
endif()
|
|
endif()
|
|
endif()
|
|
|
|
if(NOT HAVE_HARFBUZZ)
|
|
# --- build the bundled HarfBuzz subset (HB_TINY + hb-raster) ---
|
|
ocv_clear_vars(HARFBUZZ_LIBRARY HARFBUZZ_LIBRARIES HARFBUZZ_INCLUDE_DIR)
|
|
set(HARFBUZZ_LIBRARY libharfbuzz CACHE INTERNAL "")
|
|
set(HARFBUZZ_LIBRARIES ${HARFBUZZ_LIBRARY})
|
|
add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/harfbuzz")
|
|
set(HARFBUZZ_INCLUDE_DIR "${${HARFBUZZ_LIBRARY}_SOURCE_DIR}/src" CACHE INTERNAL "")
|
|
# Read the bundled version straight from the sources so it stays correct
|
|
# when HarfBuzz is updated (re-run of hb_extract.py) without touching this file.
|
|
set(_hb_version "unknown")
|
|
file(STRINGS "${OpenCV_SOURCE_DIR}/3rdparty/harfbuzz/src/hb-version.h" _hb_version_line
|
|
REGEX "^#define[ \t]+HB_VERSION_STRING[ \t]+\"[^\"]*\"")
|
|
if(_hb_version_line MATCHES "\"([^\"]*)\"")
|
|
set(_hb_version "${CMAKE_MATCH_1}")
|
|
endif()
|
|
set(HARFBUZZ_VERSION "build (${_hb_version})" CACHE INTERNAL "")
|
|
set(HARFBUZZ_IS_BUNDLED YES)
|
|
set(HAVE_HARFBUZZ 1)
|
|
endif()
|
|
endif()
|