Update widgets and draw_string on images to use convert_to_utf32 (#2769)

* Remove convert_to_utf32 from imglab by updating widgets methods

* Use convert_to_utf32 in draw_string for images

* Add an overload of draw_string for string literals
This commit is contained in:
Adrià Arrufat 2023-04-13 21:48:49 +09:00 committed by GitHub
parent 07f7425170
commit c21c9c92b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 24 deletions

View File

@ -190,7 +190,7 @@ namespace dlib
const std::string& name const std::string& name
) )
{ {
set_name(convert_mbstring_to_wstring(name)); set_name(convert_to_utf32(name));
} }
void toggle_button:: void toggle_button::
@ -327,7 +327,7 @@ namespace dlib
const std::string& text const std::string& text
) )
{ {
set_text(convert_mbstring_to_wstring(text)); set_text(convert_to_utf32(text));
} }
void label:: void label::
@ -675,7 +675,7 @@ namespace dlib
const std::string& text const std::string& text
) )
{ {
set_text(convert_mbstring_to_wstring(text)); set_text(convert_to_utf32(text));
} }
void text_field:: void text_field::
@ -1522,7 +1522,7 @@ namespace dlib
const std::string& new_name const std::string& new_name
) )
{ {
set_tab_name(idx, convert_mbstring_to_wstring(new_name)); set_tab_name(idx, convert_to_utf32(new_name));
} }
void tabbed_display:: void tabbed_display::
@ -1929,7 +1929,7 @@ namespace dlib
const std::string& name const std::string& name
) )
{ {
set_name(convert_mbstring_to_wstring(name)); set_name(convert_to_utf32(name));
} }
void named_rectangle:: void named_rectangle::
@ -3360,7 +3360,7 @@ namespace dlib
char underline_ch char underline_ch
) )
{ {
set_menu_name(idx, convert_mbstring_to_wstring(name), underline_ch); set_menu_name(idx, convert_to_utf32(name), underline_ch);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
@ -3945,7 +3945,7 @@ namespace dlib
const std::string& str const std::string& str
) )
{ {
set_text(row, col, convert_mbstring_to_wstring(str)); set_text(row, col, convert_to_utf32(str));
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
@ -4962,7 +4962,7 @@ namespace dlib
const std::string& text const std::string& text
) )
{ {
set_text(convert_mbstring_to_wstring(text)); set_text(convert_to_utf32(text));
} }
void text_box:: void text_box::

View File

@ -309,7 +309,7 @@ namespace dlib
<< "\n\tstr.size(): " << (unsigned long)str.size()); << "\n\tstr.size(): " << (unsigned long)str.size());
if (last == string::npos) if (last == string::npos)
last = str.size()-1; last = str.size();
const rectangle rect(p, p); const rectangle rect(p, p);
const font& f = *f_ptr; const font& f = *f_ptr;
@ -317,51 +317,51 @@ namespace dlib
long y_offset = rect.top() + f.ascender() - 1; long y_offset = rect.top() + f.ascender() - 1;
long pos = rect.left()+f.left_overflow(); long pos = rect.left()+f.left_overflow();
for (typename string::size_type i = first; i <= last; ++i) convert_to_utf32(str.begin() + first, str.begin() + last, [&](unichar ch)
{ {
// ignore the '\r' character // ignore the '\r' character
if (str[i] == '\r') if (ch == '\r')
continue; return;
// A combining character should be applied to the previous character, and we // A combining character should be applied to the previous character, and we
// therefore make one step back. If a combining comes right after a newline, // therefore make one step back. If a combining comes right after a newline,
// then there must be some kind of error in the string, and we don't combine. // then there must be some kind of error in the string, and we don't combine.
if(is_combining_char(str[i]) && if(is_combining_char(ch) &&
pos > rect.left() + static_cast<long>(f.left_overflow())) pos > rect.left() + static_cast<long>(f.left_overflow()))
{ {
pos -= f[str[i]].width(); pos -= f[ch].width();
} }
if (str[i] == '\n') if (ch == '\n')
{ {
y_offset += f.height(); y_offset += f.height();
pos = rect.left()+f.left_overflow(); pos = rect.left()+f.left_overflow();
continue; return;
} }
// only look at letters in the intersection area // only look at letters in the intersection area
if (c.nr() + static_cast<long>(f.height()) < y_offset) if (c.nr() + static_cast<long>(f.height()) < y_offset)
{ {
// the string is now below our rectangle so we are done // the string is now below our rectangle so we are done
break; return;
} }
else if (0 > pos - static_cast<long>(f.left_overflow()) && else if (0 > pos - static_cast<long>(f.left_overflow()) &&
pos + static_cast<long>(f[str[i]].width() + f.right_overflow()) < 0) pos + static_cast<long>(f[ch].width() + f.right_overflow()) < 0)
{ {
pos += f[str[i]].width(); pos += f[ch].width();
continue; return;
} }
else if (c.nc() + static_cast<long>(f.right_overflow()) < pos) else if (c.nc() + static_cast<long>(f.right_overflow()) < pos)
{ {
// keep looking because there might be a '\n' in the string that // keep looking because there might be a '\n' in the string that
// will wrap us around and put us back into our rectangle. // will wrap us around and put us back into our rectangle.
continue; return;
} }
// at this point in the loop we know that f[str[i]] overlaps // at this point in the loop we know that f[str[i]] overlaps
// horizontally with the intersection rectangle area. // horizontally with the intersection rectangle area.
const letter& l = f[str[i]]; const letter& l = f[ch];
for (unsigned short i = 0; i < l.num_of_points(); ++i) for (unsigned short i = 0; i < l.num_of_points(); ++i)
{ {
const long x = l[i].x + pos; const long x = l[i].x + pos;
@ -375,8 +375,25 @@ namespace dlib
} }
pos += l.width(); pos += l.width();
});
} }
template <
typename image_type,
typename pixel_type
>
void draw_string (
image_type& c,
const dlib::point& p,
const char* str,
const pixel_type& color,
const std::shared_ptr<font>& f_ptr = default_font::get_font(),
typename std::string::size_type first = 0,
typename std::string::size_type last = std::string::npos
)
{
draw_string(c, p, std::string(str), color, f_ptr, first, last);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------

View File

@ -656,7 +656,7 @@ on_overlay_rect_selected(
const image_display::overlay_rect& orect const image_display::overlay_rect& orect
) )
{ {
overlay_label.set_text(convert_to_utf32(orect.label)); overlay_label.set_text(orect.label);
display.set_default_overlay_rect_label(orect.label); display.set_default_overlay_rect_label(orect.label);
display.set_default_overlay_rect_color(string_to_color(orect.label)); display.set_default_overlay_rect_color(string_to_color(orect.label));
} }