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

@@ -134,29 +134,80 @@ namespace detail
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)
::SIZE size;
if(::GetTextExtentPoint32(dw->context, text, static_cast<int>(len), &size))
return nana::size(size.cx, size.cy);
::SIZE size;
if (::GetTextExtentPoint32(dw->context, text, static_cast<int>(len), &size))
return nana::size(size.cx, size.cy);
#elif defined(NANA_X11)
#if defined(NANA_USE_XFT)
std::string utf8str = to_utf8(std::wstring(text, len));
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*>(utf8str.c_str())), utf8str.size(), &ext);
return nana::size(ext.xOff, fs->ascent + fs->descent);
#else
XRectangle ink;
XRectangle logic;
::XmbTextExtents(reinterpret_cast<XFontSet>(dw->font->native_handle()), text, len, &ink, &logic);
return nana::size(logic.width, logic.height);
#endif
std::string utf8str = to_utf8(std::wstring(text, len));
#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*>(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
return nana::size();
#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);
#else
XRectangle ink;
XRectangle logic;
::XmbTextExtents(reinterpret_cast<XFontSet>(dw->font->native_handle()), text, len, &ink, &logic);
return nana::size(logic.width, logic.height);
#endif
#endif
}
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)
@@ -164,7 +215,7 @@ namespace detail
if (nullptr == dw || nullptr == text || 0 == len)
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;
int tabs = 0;

View File

@@ -411,8 +411,8 @@ namespace paint
impl_->handle = dw.get();
impl_->size = sz;
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;
}
}
@@ -438,8 +438,9 @@ namespace paint
#if defined(NANA_WINDOWS)
::SelectObject(impl_->handle->context, reinterpret_cast<HFONT>(f.impl_->real_font->native_handle()));
#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)
impl_->changed = true;
@@ -453,6 +454,20 @@ namespace paint
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
{
throw_not_utf8(text);
@@ -483,6 +498,7 @@ namespace paint
{
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
{
@@ -562,7 +578,11 @@ namespace paint
auto const reordered = unicode_reorder(str.c_str(), str.size());
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);
#endif
sz.width += t.width;
if(sz.height < t.height)
sz.height = t.height;
@@ -1011,7 +1031,11 @@ namespace paint
for (auto & i : reordered)
{
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);
#endif
}
return static_cast<unsigned>(moved_pos.x - pos.x);
}
@@ -1076,7 +1100,7 @@ namespace paint
{
//Render a part that does not contains a tab
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;

View File

@@ -52,6 +52,7 @@ namespace nana
{
std::size_t len = ent.end - ent.begin;
nana::size ts = detail::text_extent_size(dw, ent.begin, len);
if(ts.height > pixels) pixels = ts.height;
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)
: 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);
#endif
if (endpos - x > static_cast<int>(omitted_pixels))
this->endpos -= omitted_pixels;
else
@@ -629,7 +634,11 @@ namespace nana
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;
#endif
std::unique_ptr<unsigned[]> pixels(new unsigned[text.size()]);
graph_.glyph_pixels(text.c_str(), text.size(), pixels.get());