Skip to content

Commit

Permalink
Fix rendering utf-8 string
Browse files Browse the repository at this point in the history
  • Loading branch information
progweb committed Jul 31, 2023
1 parent 6e60347 commit a0115a2
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/libOpenImageIO/imagebufalgo_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
// https://github.com/OpenImageIO/oiio

#include <string>
#include <codecvt>
#include <locale>
#include <algorithm>
#include <cmath>
#include <limits>
Expand Down Expand Up @@ -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<uint32_t> utext, FT_Face face, int fontsize)
text_size_from_unicode(std::u32string& utext, FT_Face face, int fontsize)
{
int y = 0;
int x = 0;
Expand Down Expand Up @@ -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<std::codecvt_utf8<char32_t>, char32_t> conv_utf8_utf32;

std::string font;
bool ok = resolve_font(font_, font);
if (!ok) {
Expand All @@ -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<uint32_t> 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);
Expand Down Expand Up @@ -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<std::codecvt_utf8<char32_t>, char32_t> conv_utf8_utf32;

std::string font;
bool ok = resolve_font(font_, font);
if (!ok) {
Expand Down Expand Up @@ -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<uint32_t> 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);
Expand Down

0 comments on commit a0115a2

Please sign in to comment.