From 76f527f6d2832ced636bbe44ef7ac38aefd7aeb7 Mon Sep 17 00:00:00 2001 From: Nicolas VIVIEN Date: Mon, 31 Jul 2023 21:45:28 +0200 Subject: [PATCH] Fix rendering utf-8 string Signed-off-by: Nicolas VIVIEN --- src/libOpenImageIO/imagebufalgo_draw.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/libOpenImageIO/imagebufalgo_draw.cpp b/src/libOpenImageIO/imagebufalgo_draw.cpp index a88aacd182..9eef064f97 100644 --- a/src/libOpenImageIO/imagebufalgo_draw.cpp +++ b/src/libOpenImageIO/imagebufalgo_draw.cpp @@ -2,6 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 // https://github.com/OpenImageIO/oiio +#include +#include +#include #include #include #include @@ -887,7 +890,7 @@ static bool ft_broken = false; // Helper: given unicode and a font face, compute its size static ROI -text_size_from_unicode(cspan utext, FT_Face face, int fontsize) +text_size_from_unicode(std::u32string& utext, FT_Face face, int fontsize) { int y = 0; int x = 0; @@ -989,6 +992,8 @@ ImageBufAlgo::text_size(string_view text, int fontsize, string_view font_) // Thread safety lock_guard ft_lock(ft_mutex); + std::wstring_convert, char32_t> conv_utf8_utf32; + std::string font; bool ok = resolve_font(font_, font); if (!ok) { @@ -1009,9 +1014,8 @@ ImageBufAlgo::text_size(string_view text, int fontsize, string_view font_) return size; // couldn't set the character size } - std::vector utext; - utext.reserve(text.size()); - Strutil::utf8_to_unicode(text, utext); + // Convert the UTF to 32 bit unicode + std::u32string utext = conv_utf8_utf32.from_bytes(text); size = text_size_from_unicode(utext, face, fontsize); FT_Done_Face(face); @@ -1039,6 +1043,8 @@ ImageBufAlgo::render_text(ImageBuf& R, int x, int y, string_view text, // Thread safety lock_guard ft_lock(ft_mutex); + std::wstring_convert, char32_t> conv_utf8_utf32; + std::string font; bool ok = resolve_font(font_, font); if (!ok) { @@ -1081,9 +1087,7 @@ ImageBufAlgo::render_text(ImageBuf& R, int x, int y, string_view text, } // Convert the UTF to 32 bit unicode - std::vector utext; - utext.reserve(text.size()); - Strutil::utf8_to_unicode(text, utext); + std::u32string utext = conv_utf8_utf32.from_bytes(text); // Compute the size that the text will render as, into an ROI ROI textroi = text_size_from_unicode(utext, face, fontsize);