mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 07:43: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
140 lines
4.0 KiB
C++
140 lines
4.0 KiB
C++
#ifndef OT_LAYOUT_GSUB_ALTERNATESET_HH
|
|
#define OT_LAYOUT_GSUB_ALTERNATESET_HH
|
|
|
|
#include "Common.hh"
|
|
|
|
namespace OT {
|
|
namespace Layout {
|
|
namespace GSUB_impl {
|
|
|
|
template <typename Types>
|
|
struct AlternateSet
|
|
{
|
|
protected:
|
|
Array16Of<typename Types::HBGlyphID>
|
|
alternates; /* Array of alternate GlyphIDs--in
|
|
* arbitrary order */
|
|
public:
|
|
DEFINE_SIZE_ARRAY (2, alternates);
|
|
|
|
bool sanitize (hb_sanitize_context_t *c) const
|
|
{
|
|
TRACE_SANITIZE (this);
|
|
return_trace (alternates.sanitize (c));
|
|
}
|
|
|
|
bool intersects (const hb_set_t *glyphs) const
|
|
{ return hb_any (alternates, glyphs); }
|
|
|
|
void closure (hb_closure_context_t *c) const
|
|
{ c->output->add_array (alternates.arrayZ, alternates.len); }
|
|
|
|
void collect_glyphs (hb_collect_glyphs_context_t *c) const
|
|
{ c->output->add_array (alternates.arrayZ, alternates.len); }
|
|
|
|
bool apply (hb_ot_apply_context_t *c) const
|
|
{
|
|
TRACE_APPLY (this);
|
|
unsigned int count = alternates.len;
|
|
|
|
if (unlikely (!count)) return_trace (false);
|
|
|
|
hb_mask_t glyph_mask = c->buffer->cur().mask;
|
|
hb_mask_t lookup_mask = c->lookup_mask;
|
|
|
|
/* Note: This breaks badly if two features enabled this lookup together. */
|
|
unsigned int shift = hb_ctz (lookup_mask);
|
|
unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
|
|
|
|
/* If alt_index is MAX_VALUE, randomize feature if it is the rand feature. */
|
|
if (alt_index == HB_OT_MAP_MAX_VALUE && c->random)
|
|
{
|
|
/* Maybe we can do better than unsafe-to-break all; but since we are
|
|
* changing random state, it would be hard to track that. Good 'nough. */
|
|
c->buffer->unsafe_to_break (0, c->buffer->len);
|
|
alt_index = c->random_number () % count + 1;
|
|
}
|
|
|
|
if (unlikely (alt_index > count || alt_index == 0)) return_trace (false);
|
|
|
|
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
|
|
{
|
|
c->buffer->sync_so_far ();
|
|
c->buffer->message (c->font,
|
|
"replacing glyph at %u (alternate substitution)",
|
|
c->buffer->idx);
|
|
}
|
|
|
|
c->replace_glyph (alternates[alt_index - 1]);
|
|
|
|
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
|
|
{
|
|
c->buffer->message (c->font,
|
|
"replaced glyph at %u (alternate substitution)",
|
|
c->buffer->idx - 1u);
|
|
}
|
|
|
|
return_trace (true);
|
|
}
|
|
|
|
unsigned
|
|
get_alternates (unsigned start_offset,
|
|
unsigned *alternate_count /* IN/OUT. May be NULL. */,
|
|
hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const
|
|
{
|
|
if (alternates.len && alternate_count)
|
|
{
|
|
+ alternates.as_array ().sub_array (start_offset, alternate_count)
|
|
| hb_sink (hb_array (alternate_glyphs, *alternate_count))
|
|
;
|
|
}
|
|
return alternates.len;
|
|
}
|
|
|
|
void
|
|
collect_alternates (hb_codepoint_t gid,
|
|
hb_map_t *alternate_count /* IN/OUT */,
|
|
hb_map_t *alternate_glyphs /* IN/OUT */) const
|
|
{
|
|
+ hb_enumerate (alternates)
|
|
| hb_map ([gid] (hb_pair_t<unsigned, hb_codepoint_t> _) { return hb_pair (gid + (_.first << 24), _.second); })
|
|
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> &p) -> void
|
|
{ _hb_collect_glyph_alternates_add (p.first, p.second,
|
|
alternate_count, alternate_glyphs); })
|
|
;
|
|
}
|
|
|
|
template <typename Iterator,
|
|
hb_requires (hb_is_source_of (Iterator, hb_codepoint_t))>
|
|
bool serialize (hb_serialize_context_t *c,
|
|
Iterator alts)
|
|
{
|
|
TRACE_SERIALIZE (this);
|
|
return_trace (alternates.serialize (c, alts));
|
|
}
|
|
|
|
bool subset (hb_subset_context_t *c) const
|
|
{
|
|
TRACE_SUBSET (this);
|
|
const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
|
|
const hb_map_t &glyph_map = *c->plan->glyph_map;
|
|
|
|
auto it =
|
|
+ hb_iter (alternates)
|
|
| hb_filter (glyphset)
|
|
| hb_map (glyph_map)
|
|
;
|
|
|
|
auto *out = c->serializer->start_embed (*this);
|
|
return_trace (out->serialize (c->serializer, it) &&
|
|
out->alternates);
|
|
}
|
|
};
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
#endif /* OT_LAYOUT_GSUB_ALTERNATESET_HH */
|