std::string_view

This commit is contained in:
Jinhao 2018-06-07 03:41:08 +08:00
parent 549b3d4353
commit 3427181405
12 changed files with 251 additions and 44 deletions

View File

@ -1,7 +1,7 @@
/* /*
* The Deploy Implementation * The Deploy Implementation
* Nana C++ Library(http://www.nanapro.org) * Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2017 Jinhao(cnjinhao@hotmail.com) * Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com)
* *
* Distributed under the Boost Software License, Version 1.0. * Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at * (See accompanying file LICENSE_1_0.txt or copy at
@ -23,6 +23,10 @@
#include <stdexcept> #include <stdexcept>
#ifdef _nana_std_has_string_view
#include <string_view>
#endif
namespace nana namespace nana
{ {
/// move to *.h ?? /// move to *.h ??
@ -44,10 +48,15 @@ namespace nana
/// Checks whether a specified text is utf8 encoding /// Checks whether a specified text is utf8 encoding
#ifdef _nana_std_has_string_view
bool is_utf8(std::string_view str);
void throw_not_utf8(std::string_view str);
#else
bool is_utf8(const char* str, std::size_t len); bool is_utf8(const char* str, std::size_t len);
void throw_not_utf8(const std::string& text); void throw_not_utf8(const std::string& text);
void throw_not_utf8(const char*, std::size_t len); void throw_not_utf8(const char*, std::size_t len);
void throw_not_utf8(const char*); void throw_not_utf8(const char*);
#endif
/// this text needed change, it needed review ?? /// this text needed change, it needed review ??
bool review_utf8(const std::string& text); bool review_utf8(const std::string& text);
@ -58,7 +67,12 @@ namespace nana
const std::string& to_utf8(const std::string&); const std::string& to_utf8(const std::string&);
std::string to_utf8(const std::wstring&); std::string to_utf8(const std::wstring&);
#ifdef _nana_std_has_string_view
std::wstring to_wstring(std::string_view utf8_str);
#else
std::wstring to_wstring(const std::string& utf8_str); std::wstring to_wstring(const std::string& utf8_str);
#endif
const std::wstring& to_wstring(const std::wstring& wstr); const std::wstring& to_wstring(const std::wstring& wstr);
std::wstring&& to_wstring(std::wstring&& wstr); std::wstring&& to_wstring(std::wstring&& wstr);

View File

@ -1,6 +1,6 @@
/* /*
* Platform Implementation * Platform Implementation
* Copyright(C) 2003-2013 Jinhao(cnjinhao@hotmail.com) * Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com)
* *
* Distributed under the Boost Software License, Version 1.0. * Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at * (See accompanying file LICENSE_1_0.txt or copy at
@ -32,8 +32,11 @@ namespace detail
//dw color = dw color * fade_rate + bdcolor * (1 - fade_rate) //dw color = dw color * fade_rate + bdcolor * (1 - fade_rate)
void blend(drawable_type dw, const nana::rectangle& r, pixel_color_t bdcolor, double fade_rate); void blend(drawable_type dw, const nana::rectangle& r, pixel_color_t bdcolor, double fade_rate);
nana::size raw_text_extent_size(drawable_type, const wchar_t*, std::size_t len); nana::size real_text_extent_size(drawable_type, const char*, std::size_t len);
nana::size real_text_extent_size(drawable_type, const wchar_t*, std::size_t len);
nana::size text_extent_size(drawable_type, const char*, std::size_t len);
nana::size text_extent_size(drawable_type, const wchar_t*, std::size_t len); nana::size text_extent_size(drawable_type, const wchar_t*, std::size_t len);
void draw_string(drawable_type, const nana::point&, const wchar_t *, std::size_t len); void draw_string(drawable_type, const nana::point&, const wchar_t *, std::size_t len);
}//end namespace detail }//end namespace detail
}//end namespace paint }//end namespace paint

View File

@ -21,6 +21,10 @@
#include "detail/ptdefs.hpp" #include "detail/ptdefs.hpp"
#ifdef _nana_std_has_string_view
#include <string_view>
#endif
namespace nana namespace nana
{ {
namespace paint namespace paint
@ -108,12 +112,20 @@ namespace nana
void resize(const ::nana::size&); void resize(const ::nana::size&);
void typeface(const font&); ///< Selects a specified font type into the graphics object. void typeface(const font&); ///< Selects a specified font type into the graphics object.
font typeface() const; font typeface() const;
#ifdef _nana_std_has_string_view
::nana::size text_extent_size(std::string_view text) const;
::nana::size text_extent_size(std::wstring_view text) const;
#else
::nana::size text_extent_size(const ::std::string&) const; ::nana::size text_extent_size(const ::std::string&) const;
::nana::size text_extent_size(const char*, std::size_t len) const; ::nana::size text_extent_size(const char*, std::size_t len) const;
::nana::size text_extent_size(const wchar_t*) const; ///< Computes the width and height of the specified string of text. ::nana::size text_extent_size(const wchar_t*) const; ///< Computes the width and height of the specified string of text.
::nana::size text_extent_size(const ::std::wstring&) const; ///< Computes the width and height of the specified string of text. ::nana::size text_extent_size(const ::std::wstring&) const; ///< Computes the width and height of the specified string of text.
::nana::size text_extent_size(const wchar_t*, std::size_t length) const; ///< Computes the width and height of the specified string of text with the specified length. ::nana::size text_extent_size(const wchar_t*, std::size_t length) const; ///< Computes the width and height of the specified string of text with the specified length.
::nana::size text_extent_size(const ::std::wstring&, std::size_t length) const; ///< Computes the width and height of the specified string of text with the specified length. ::nana::size text_extent_size(const ::std::wstring&, std::size_t length) const; ///< Computes the width and height of the specified string of text with the specified length.
#endif
::nana::size glyph_extent_size(const wchar_t*, std::size_t length, std::size_t begin, std::size_t end) const; ::nana::size glyph_extent_size(const wchar_t*, std::size_t length, std::size_t begin, std::size_t end) const;
::nana::size glyph_extent_size(const ::std::wstring&, std::size_t length, std::size_t begin, std::size_t end) const; ::nana::size glyph_extent_size(const ::std::wstring&, std::size_t length, std::size_t begin, std::size_t end) const;
bool glyph_pixels(const wchar_t *, std::size_t length, unsigned* pxbuf) const; bool glyph_pixels(const wchar_t *, std::size_t length, unsigned* pxbuf) const;

View File

@ -28,6 +28,42 @@
namespace nana namespace nana
{ {
#ifdef _nana_std_has_string_view
bool is_utf8(std::string_view str)
{
auto ustr = reinterpret_cast<const unsigned char*>(str.data());
auto end = ustr + str.size();
while (ustr < end)
{
const auto uv = *ustr;
if (uv < 0x80)
{
++ustr;
continue;
}
if (uv < 0xC0)
return false;
if ((uv < 0xE0) && (end - ustr > 1))
ustr += 2;
else if ((uv < 0xF0) && (end - ustr > 2))
ustr += 3;
else if ((uv < 0x1F) && (end - ustr > 3))
ustr += 4;
else
return false;
}
return true;
}
void throw_not_utf8(std::string_view str)
{
if (!is_utf8(str))
return utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + std::string(str.data(), str.size())).emit();
}
#else
bool is_utf8(const char* str, std::size_t len) bool is_utf8(const char* str, std::size_t len)
{ {
auto ustr = reinterpret_cast<const unsigned char*>(str); auto ustr = reinterpret_cast<const unsigned char*>(str);
@ -57,6 +93,23 @@ namespace nana
return true; return true;
} }
void throw_not_utf8(const std::string& text)
{
throw_not_utf8(text.c_str(), text.size());
}
void throw_not_utf8(const char* text)
{
throw_not_utf8(text, std::strlen(text));
}
void throw_not_utf8(const char* text, std::size_t len)
{
if (!is_utf8(text, len))
return utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + std::string(text, len)).emit();
}
#endif
//class utf8_Error //class utf8_Error
#if defined(_MSC_VER) #if defined(_MSC_VER)
@ -79,22 +132,6 @@ namespace nana
bool utf8_Error::use_throw{ false }; bool utf8_Error::use_throw{ false };
//end class utf8_Error //end class utf8_Error
void throw_not_utf8(const std::string& text)
{
throw_not_utf8(text.c_str(), text.size());
}
void throw_not_utf8(const char* text)
{
throw_not_utf8(text, std::strlen(text));
}
void throw_not_utf8(const char* text, std::size_t len)
{
if (!is_utf8(text, len))
return utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + std::string(text, len) ).emit();
}
std::string recode_to_utf8(std::string no_utf8) std::string recode_to_utf8(std::string no_utf8)
{ {
return nana::charset(std::move(no_utf8)).to_bytes(nana::unicode::utf8); return nana::charset(std::move(no_utf8)).to_bytes(nana::unicode::utf8);
@ -103,7 +140,11 @@ namespace nana
/// this text needed change, it needed review ?? /// this text needed change, it needed review ??
bool review_utf8(const std::string& text) bool review_utf8(const std::string& text)
{ {
#ifdef _nana_std_has_string_view
if (!is_utf8(text))
#else
if (!is_utf8(text.c_str(), text.length())) if (!is_utf8(text.c_str(), text.length()))
#endif
{ {
utf8_Error(std::string("\nThe const text is not encoded in UTF8: ") + text).emit(); utf8_Error(std::string("\nThe const text is not encoded in UTF8: ") + text).emit();
return true; /// it needed change, it needed review !! return true; /// it needed change, it needed review !!
@ -115,7 +156,11 @@ namespace nana
/// this text needed change, it needed review ?? /// this text needed change, it needed review ??
bool review_utf8(std::string& text) bool review_utf8(std::string& text)
{ {
#ifdef _nana_std_has_string_view
if(!is_utf8(text))
#else
if (!is_utf8(text.c_str(), text.length())) if (!is_utf8(text.c_str(), text.length()))
#endif
{ {
utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + text).emit(); utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + text).emit();
text=recode_to_utf8(text); text=recode_to_utf8(text);
@ -135,10 +180,21 @@ namespace nana
return ::nana::charset(text).to_bytes(::nana::unicode::utf8); return ::nana::charset(text).to_bytes(::nana::unicode::utf8);
} }
#ifdef _nana_std_has_string_view
std::wstring to_wstring(std::string_view utf8_str)
{
if (utf8_str.empty())
return{};
return ::nana::charset(std::string{ utf8_str.data(), utf8_str.size() }, unicode::utf8);
}
#else
std::wstring to_wstring(const std::string& utf8_str) std::wstring to_wstring(const std::string& utf8_str)
{ {
return ::nana::charset(utf8_str, ::nana::unicode::utf8); return ::nana::charset(utf8_str, ::nana::unicode::utf8);
} }
#endif
const std::wstring& to_wstring(const std::wstring& wstr) const std::wstring& to_wstring(const std::wstring& wstr)
{ {

View File

@ -393,8 +393,13 @@ namespace API
{ {
if (shortkey) if (shortkey)
{ {
#ifdef _nana_std_has_string_view
auto off_x = (shortkey_position ? graph.text_extent_size(std::string_view{ text.c_str(), shortkey_position }).width : 0);
auto key_px = static_cast<int>(graph.text_extent_size(std::wstring_view{ &shortkey, 1 }).width);
#else
auto off_x = (shortkey_position ? graph.text_extent_size(text.c_str(), shortkey_position).width : 0); auto off_x = (shortkey_position ? graph.text_extent_size(text.c_str(), shortkey_position).width : 0);
auto key_px = static_cast<int>(graph.text_extent_size(&shortkey, 1).width); auto key_px = static_cast<int>(graph.text_extent_size(&shortkey, 1).width);
#endif
unsigned ascent, descent, inleading; unsigned ascent, descent, inleading;
graph.text_metrics(ascent, descent, inleading); graph.text_metrics(ascent, descent, inleading);

View File

@ -78,7 +78,11 @@ namespace nana{ namespace drawerbase
} }
//draw crook //draw crook
#ifdef _nana_std_has_string_view
auto txt_px = graph.text_extent_size(std::wstring_view( L"jN", 2 )).height + 2;
#else
auto txt_px = graph.text_extent_size(L"jN", 2).height + 2; auto txt_px = graph.text_extent_size(L"jN", 2).height + 2;
#endif
impl_->crook.draw(graph, wdg->bgcolor(), wdg->fgcolor(), rectangle(0, txt_px > 16 ? (txt_px - 16) / 2 : 0, 16, 16), API::element_state(*wdg)); impl_->crook.draw(graph, wdg->bgcolor(), wdg->fgcolor(), rectangle(0, txt_px > 16 ? (txt_px - 16) / 2 : 0, 16, 16), API::element_state(*wdg));
} }

View File

@ -83,7 +83,11 @@ namespace nana
auto pre_font = graph.typeface(); //used for restoring the font auto pre_font = graph.typeface(); //used for restoring the font
#ifdef _nana_std_has_string_view
const unsigned def_line_pixels = graph.text_extent_size(std::wstring_view{ L" ", 1 }).height;
#else
const unsigned def_line_pixels = graph.text_extent_size(L" ", 1).height; const unsigned def_line_pixels = graph.text_extent_size(L" ", 1).height;
#endif
font_ = pre_font; font_ = pre_font;
fblock_ = nullptr; fblock_ = nullptr;
@ -170,7 +174,11 @@ namespace nana
auto ft = graph.typeface(); //used for restoring the font auto ft = graph.typeface(); //used for restoring the font
#ifdef _nana_std_has_string_view
const unsigned def_line_pixels = graph.text_extent_size(std::wstring_view(L" ", 1)).height;
#else
const unsigned def_line_pixels = graph.text_extent_size(L" ", 1).height; const unsigned def_line_pixels = graph.text_extent_size(L" ", 1).height;
#endif
font_ = ft; font_ = ft;
fblock_ = nullptr; fblock_ = nullptr;

View File

@ -3147,7 +3147,11 @@ namespace nana{ namespace widgets
maskstr.append(n, mask_char_); maskstr.append(n, mask_char_);
return graph_.text_extent_size(maskstr); return graph_.text_extent_size(maskstr);
} }
#ifdef _nana_std_has_string_view
return graph_.text_extent_size(std::basic_string_view<char_type>(str, n));
#else
return graph_.text_extent_size(str, static_cast<unsigned>(n)); return graph_.text_extent_size(str, static_cast<unsigned>(n));
#endif
} }
bool text_editor::_m_adjust_view() bool text_editor::_m_adjust_view()
@ -3500,7 +3504,12 @@ namespace nana{ namespace widgets
for (auto & ent : reordered) for (auto & ent : reordered)
{ {
std::size_t len = ent.end - ent.begin; std::size_t len = ent.end - ent.begin;
#ifdef _nana_std_has_string_view
unsigned str_w = graph_.text_extent_size(std::wstring_view{ ent.begin, len }).width;
#else
unsigned str_w = graph_.text_extent_size(ent.begin, len).width; unsigned str_w = graph_.text_extent_size(ent.begin, len).width;
#endif
if ((text_draw_pos.x + static_cast<int>(str_w) > text_area_.area.x) && (text_draw_pos.x < text_right)) if ((text_draw_pos.x + static_cast<int>(str_w) > text_area_.area.x) && (text_draw_pos.x < text_right))
{ {
@ -3519,7 +3528,11 @@ namespace nana{ namespace widgets
for (auto & ent : reordered) for (auto & ent : reordered)
{ {
const auto len = ent.end - ent.begin; const auto len = ent.end - ent.begin;
#ifdef _nana_std_has_string_view
auto ent_px = graph_.text_extent_size(std::wstring_view(ent.begin, len)).width;
#else
auto ent_px = graph_.text_extent_size(ent.begin, len).width; auto ent_px = graph_.text_extent_size(ent.begin, len).width;
#endif
extra_space = false; extra_space = false;
@ -3589,7 +3602,11 @@ namespace nana{ namespace widgets
auto pos = text_coord.x + text_len; auto pos = text_coord.x + text_len;
if (b.x != pos || text_coord.y != b.y) if (b.x != pos || text_coord.y != b.y)
{ {
#ifdef _nana_std_has_string_view
auto whitespace_w = graph_.text_extent_size(std::wstring_view{ L" ", 1 }).width;
#else
auto whitespace_w = graph_.text_extent_size(L" ", 1).width; auto whitespace_w = graph_.text_extent_size(L" ", 1).width;
#endif
graph_.rectangle(::nana::rectangle{ text_draw_pos, { whitespace_w, line_h_pixels } }, true); graph_.rectangle(::nana::rectangle{ text_draw_pos, { whitespace_w, line_h_pixels } }, true);
} }
} }

View File

@ -1163,7 +1163,11 @@ namespace nana
virtual unsigned item_height(graph_reference graph) const override virtual unsigned item_height(graph_reference graph) const override
{ {
#ifdef _nana_std_has_string_view
return graph.text_extent_size(std::wstring_view{ L"jH{", 3 }).height + 8;
#else
return graph.text_extent_size(L"jH{", 3).height + 8; return graph.text_extent_size(L"jH{", 3).height + 8;
#endif
} }
virtual unsigned item_width(graph_reference graph, const item_attribute_t& attr) const override virtual unsigned item_width(graph_reference graph, const item_attribute_t& attr) const override

View File

@ -134,20 +134,50 @@ namespace detail
pixbuf.paste(nana::rectangle(r.x, 0, r.width, r.height), dw, point{r.x, r.y}); pixbuf.paste(nana::rectangle(r.x, 0, r.width, r.height), dw, point{r.x, r.y});
} }
nana::size raw_text_extent_size(drawable_type dw, const wchar_t* text, std::size_t len) nana::size real_text_extent_size(drawable_type dw, const wchar_t* text, std::size_t len)
{ {
if(nullptr == dw || nullptr == text || 0 == len) return nana::size(); if (dw && text && len)
{
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
::SIZE size; ::SIZE size;
if (::GetTextExtentPoint32(dw->context, text, static_cast<int>(len), &size)) if (::GetTextExtentPoint32(dw->context, text, static_cast<int>(len), &size))
return nana::size(size.cx, size.cy); return nana::size(size.cx, size.cy);
#elif defined(NANA_X11) #elif defined(NANA_X11)
#if defined(NANA_USE_XFT)
std::string utf8str = to_utf8(std::wstring(text, len)); std::string utf8str = to_utf8(std::wstring(text, len));
#if defined(NANA_USE_XFT)
XGlyphInfo ext; XGlyphInfo ext;
XftFont * fs = reinterpret_cast<XftFont*>(dw->font->native_handle()); XftFont * fs = reinterpret_cast<XftFont*>(dw->font->native_handle());
::XftTextExtentsUtf8(nana::detail::platform_spec::instance().open_display(), fs, ::XftTextExtentsUtf8(nana::detail::platform_spec::instance().open_display(), fs,
reinterpret_cast<XftChar8*>(const_cast<char*>(utf8str.c_str())), utf8str.size(), &ext); reinterpret_cast<XftChar8*>(const_cast<char*>(utf8text.data())), utf8text.size(), &ext);
return nana::size(ext.xOff, fs->ascent + fs->descent);
#else
XRectangle ink;
XRectangle logic;
::XmbTextExtents(reinterpret_cast<XFontSet>(dw->font->native_handle()), utf8str.c_str(), utf8str.size(), &ink, &logic);
return nana::size(logic.width, logic.height);
#endif
#endif
}
return {};
}
nana::size real_text_extent_size(drawable_type dw, const char* text, std::size_t len)
{
if (dw && text && len)
{
#if defined(NANA_WINDOWS)
auto wstr = to_wstring(std::string(text,len));
::SIZE size;
if (::GetTextExtentPoint32(dw->context, wstr.c_str(), static_cast<int>(wstr.size()), &size))
return nana::size(size.cx, size.cy);
#elif defined(NANA_X11)
#if defined(NANA_USE_XFT)
XGlyphInfo ext;
XftFont * fs = reinterpret_cast<XftFont*>(dw->font->native_handle());
::XftTextExtentsUtf8(nana::detail::platform_spec::instance().open_display(), fs,
reinterpret_cast<XftChar8*>(const_cast<char*>(text)), len, &ext);
return nana::size(ext.xOff, fs->ascent + fs->descent); return nana::size(ext.xOff, fs->ascent + fs->descent);
#else #else
XRectangle ink; XRectangle ink;
@ -156,7 +186,28 @@ namespace detail
return nana::size(logic.width, logic.height); return nana::size(logic.width, logic.height);
#endif #endif
#endif #endif
return nana::size(); }
return {};
}
nana::size text_extent_size(drawable_type dw, const char * text, std::size_t len)
{
if (nullptr == dw || nullptr == text || 0 == len)
return{};
nana::size extents = real_text_extent_size(dw, text, len);
auto const end = text + len;
int tabs = 0;
for (; text != end; ++text)
{
if (*text == '\t')
++tabs;
}
if (tabs)
extents.width = static_cast<int>(extents.width) - tabs * static_cast<int>(dw->string.tab_pixels - dw->string.whitespace_pixels * dw->string.tab_length);
return extents;
} }
nana::size text_extent_size(drawable_type dw, const wchar_t * text, std::size_t len) nana::size text_extent_size(drawable_type dw, const wchar_t * text, std::size_t len)
@ -164,7 +215,7 @@ namespace detail
if (nullptr == dw || nullptr == text || 0 == len) if (nullptr == dw || nullptr == text || 0 == len)
return{}; return{};
nana::size extents = raw_text_extent_size(dw, text, len); nana::size extents = real_text_extent_size(dw, text, len);
const wchar_t* const end = text + len; const wchar_t* const end = text + len;
int tabs = 0; int tabs = 0;

View File

@ -411,8 +411,8 @@ namespace paint
impl_->handle = dw.get(); impl_->handle = dw.get();
impl_->size = sz; impl_->size = sz;
impl_->handle->string.tab_pixels = detail::raw_text_extent_size(impl_->handle, L"\t", 1).width; impl_->handle->string.tab_pixels = detail::real_text_extent_size(impl_->handle, L"\t", 1).width;
impl_->handle->string.whitespace_pixels = detail::raw_text_extent_size(impl_->handle, L" ", 1).width; impl_->handle->string.whitespace_pixels = detail::real_text_extent_size(impl_->handle, L" ", 1).width;
} }
} }
@ -438,8 +438,9 @@ namespace paint
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
::SelectObject(impl_->handle->context, reinterpret_cast<HFONT>(f.impl_->real_font->native_handle())); ::SelectObject(impl_->handle->context, reinterpret_cast<HFONT>(f.impl_->real_font->native_handle()));
#endif #endif
impl_->handle->string.tab_pixels = detail::raw_text_extent_size(impl_->handle, L"\t", 1).width;
impl_->handle->string.whitespace_pixels = detail::raw_text_extent_size(impl_->handle, L" ", 1).width; impl_->handle->string.tab_pixels = detail::real_text_extent_size(impl_->handle, L"\t", 1).width;
impl_->handle->string.whitespace_pixels = detail::real_text_extent_size(impl_->handle, L" ", 1).width;
if (impl_->changed == false) if (impl_->changed == false)
impl_->changed = true; impl_->changed = true;
@ -453,6 +454,20 @@ namespace paint
return (impl_->handle ? font(impl_->handle) : impl_->font_shadow); return (impl_->handle ? font(impl_->handle) : impl_->font_shadow);
} }
#ifdef _nana_std_has_string_view
size graphics::text_extent_size(std::string_view text) const
{
throw_not_utf8(text);
return detail::text_extent_size(impl_->handle, text.data(), text.length());
}
size graphics::text_extent_size(std::wstring_view text) const
{
return detail::text_extent_size(impl_->handle, text.data(), text.length());
}
#else
::nana::size graphics::text_extent_size(const ::std::string& text) const ::nana::size graphics::text_extent_size(const ::std::string& text) const
{ {
throw_not_utf8(text); throw_not_utf8(text);
@ -483,6 +498,7 @@ namespace paint
{ {
return detail::text_extent_size(impl_->handle, str.c_str(), len); return detail::text_extent_size(impl_->handle, str.c_str(), len);
} }
#endif
nana::size graphics::glyph_extent_size(const wchar_t * str, std::size_t len, std::size_t begin, std::size_t end) const nana::size graphics::glyph_extent_size(const wchar_t * str, std::size_t len, std::size_t begin, std::size_t end) const
{ {
@ -562,7 +578,11 @@ namespace paint
auto const reordered = unicode_reorder(str.c_str(), str.size()); auto const reordered = unicode_reorder(str.c_str(), str.size());
for(auto & i: reordered) for(auto & i: reordered)
{ {
#ifdef _nana_std_has_string_view
nana::size t = text_extent_size(std::wstring_view( i.begin, i.end - i.begin ));
#else
nana::size t = text_extent_size(i.begin, i.end - i.begin); nana::size t = text_extent_size(i.begin, i.end - i.begin);
#endif
sz.width += t.width; sz.width += t.width;
if(sz.height < t.height) if(sz.height < t.height)
sz.height = t.height; sz.height = t.height;
@ -1011,7 +1031,11 @@ namespace paint
for (auto & i : reordered) for (auto & i : reordered)
{ {
string(moved_pos, i.begin, i.end - i.begin); string(moved_pos, i.begin, i.end - i.begin);
#ifdef _nana_std_has_string_view
moved_pos.x += static_cast<int>(text_extent_size(std::wstring_view( i.begin, i.end - i.begin )).width);
#else
moved_pos.x += static_cast<int>(text_extent_size(i.begin, i.end - i.begin).width); moved_pos.x += static_cast<int>(text_extent_size(i.begin, i.end - i.begin).width);
#endif
} }
return static_cast<unsigned>(moved_pos.x - pos.x); return static_cast<unsigned>(moved_pos.x - pos.x);
} }
@ -1076,7 +1100,7 @@ namespace paint
{ {
//Render a part that does not contains a tab //Render a part that does not contains a tab
detail::draw_string(impl_->handle, pos, str, len); detail::draw_string(impl_->handle, pos, str, len);
pos.x += detail::raw_text_extent_size(impl_->handle, str, len).width; pos.x += detail::real_text_extent_size(impl_->handle, str, len).width;
} }
str = i; str = i;

View File

@ -52,6 +52,7 @@ namespace nana
{ {
std::size_t len = ent.end - ent.begin; std::size_t len = ent.end - ent.begin;
nana::size ts = detail::text_extent_size(dw, ent.begin, len); nana::size ts = detail::text_extent_size(dw, ent.begin, len);
if(ts.height > pixels) pixels = ts.height; if(ts.height > pixels) pixels = ts.height;
if(pos.x + static_cast<int>(ts.width) > 0) if(pos.x + static_cast<int>(ts.width) > 0)
@ -128,7 +129,11 @@ namespace nana
draw_string_omitted(graphics& graph, int x, int endpos, bool omitted) draw_string_omitted(graphics& graph, int x, int endpos, bool omitted)
: graph(graph), x(x), endpos(endpos) : graph(graph), x(x), endpos(endpos)
{ {
#ifdef _nana_std_has_string_view
omitted_pixels = (omitted ? graph.text_extent_size(std::string_view{ "...", 3 }).width : 0);
#else
omitted_pixels = (omitted ? graph.text_extent_size("...", 3).width : 0); omitted_pixels = (omitted ? graph.text_extent_size("...", 3).width : 0);
#endif
if (endpos - x > static_cast<int>(omitted_pixels)) if (endpos - x > static_cast<int>(omitted_pixels))
this->endpos -= omitted_pixels; this->endpos -= omitted_pixels;
else else
@ -629,7 +634,11 @@ namespace nana
return; return;
} }
#ifdef _nana_std_has_string_view
const auto ellipsis = graph_.text_extent_size(std::string_view{ "...", 3 }).width;
#else
const auto ellipsis = graph_.text_extent_size("...", 3).width; const auto ellipsis = graph_.text_extent_size("...", 3).width;
#endif
std::unique_ptr<unsigned[]> pixels(new unsigned[text.size()]); std::unique_ptr<unsigned[]> pixels(new unsigned[text.size()]);
graph_.glyph_pixels(text.c_str(), text.size(), pixels.get()); graph_.glyph_pixels(text.c_str(), text.size(), pixels.get());