mirror of
https://github.com/opencv/opencv.git
synced 2026-11-09 18:04:29 -06:00
d5c56723d162f48825f725d276717b2fde112302
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>
OpenCV: Open Source Computer Vision Library
Resources
- Homepage: https://opencv.org
- Courses: https://opencv.org/courses
- Docs: https://docs.opencv.org/5.x/
- Q&A forum: https://forum.opencv.org
- previous forum (read only): http://answers.opencv.org
- Issue tracking: https://github.com/opencv/opencv/issues
- Additional OpenCV functionality: https://github.com/opencv/opencv_contrib
- Donate to OpenCV: https://opencv.org/support/
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
- Submit your OpenCV-based project for inclusion in Community Friday on opencv.org
- Subscribe to the OpenCV YouTube Channel featuring OpenCV Live, an hour-long streaming show
- Follow OpenCV on LinkedIn for daily posts showing the state-of-the-art in computer vision & AI
- Apply to be an OpenCV Volunteer to help organize events and online campaigns as well as amplify them
- Follow OpenCV on Mastodon in the Fediverse
- Follow OpenCV on X
- OpenCV.ai: Computer Vision and AI development services from the OpenCV team.
Description
Languages
C++
87.7%
C
3.1%
Python
2.9%
CMake
2%
Java
1.5%
Other
2.6%