From 0cc684aec5f86416bffe3a3fd3b6281f659e6511 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Tue, 9 Jul 2019 14:48:11 +0200 Subject: [PATCH] Truncate long citation label in the middle rather than the end. Fixes: #10769 --- src/BiblioInfo.cpp | 15 ++++++++++----- src/insets/InsetCitation.cpp | 4 ++-- src/support/lstrings.cpp | 16 ++++++++++++---- src/support/lstrings.h | 5 ++++- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/BiblioInfo.cpp b/src/BiblioInfo.cpp index 80cafeb359..b0f64bd0e3 100644 --- a/src/BiblioInfo.cpp +++ b/src/BiblioInfo.cpp @@ -1326,9 +1326,15 @@ docstring const BiblioInfo::getLabel(vector keys, LASSERT(max_size >= 16, max_size = 16); // we can't display more than 10 of these, anyway + // but since we truncate in the middle, + // we need to split into two halfs. bool const too_many_keys = keys.size() > 10; - if (too_many_keys) - keys.resize(10); + vector lkeys; + if (too_many_keys) { + lkeys.insert(lkeys.end(), keys.end() - 5, keys.end()); + keys.resize(5); + keys.insert(keys.end(), lkeys.begin(), lkeys.end()); + } CiteEngineType const engine_type = buf.params().citeEngineType(); DocumentClass const & dc = buf.params().documentClass(); @@ -1353,9 +1359,8 @@ docstring const BiblioInfo::getLabel(vector keys, ret = data.getLabel(xrefptrs, buf, ret, ci, key + 1 != ken, i == 1); } - if (too_many_keys) - ret.push_back(0x2026);//HORIZONTAL ELLIPSIS - support::truncateWithEllipsis(ret, max_size); + support::truncateWithEllipsis(ret, max_size, true); + return ret; } diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index ba45f691a3..6d5c381df8 100644 --- a/src/insets/InsetCitation.cpp +++ b/src/insets/InsetCitation.cpp @@ -458,8 +458,8 @@ void InsetCitation::updateBuffer(ParIterator const &, UpdateType) cache.recalculate = false; cache.generated_label = glabel; unsigned int const maxLabelChars = 45; - cache.screen_label = glabel.substr(0, maxLabelChars + 1); - support::truncateWithEllipsis(cache.screen_label, maxLabelChars); + cache.screen_label = glabel; + support::truncateWithEllipsis(cache.screen_label, maxLabelChars, true); } diff --git a/src/support/lstrings.cpp b/src/support/lstrings.cpp index 68eabd9d26..e2f3d9c084 100644 --- a/src/support/lstrings.cpp +++ b/src/support/lstrings.cpp @@ -1233,13 +1233,21 @@ docstring const protectArgument(docstring & arg, char const l, } -bool truncateWithEllipsis(docstring & str, size_t const len) +bool truncateWithEllipsis(docstring & str, size_t const len, bool const mid) { if (str.size() <= len) return false; - str.resize(len); - if (len > 0) - str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS + if (mid && len > 0) { + size_t const hlen = len / 2; + docstring suffix = str.substr(str.size() - hlen); + str.resize(hlen); + str[hlen - 1] = 0x2026;// HORIZONTAL ELLIPSIS + str += suffix; + } else { + str.resize(len); + if (len > 0) + str[len - 1] = 0x2026;// HORIZONTAL ELLIPSIS + } return true; } diff --git a/src/support/lstrings.h b/src/support/lstrings.h index dde06e5454..ff0ce1d207 100644 --- a/src/support/lstrings.h +++ b/src/support/lstrings.h @@ -276,6 +276,8 @@ docstring const protectArgument(docstring & arg, char const l = '[', /// Truncates a string with an ellipsis at the end. Leaves str unchanged and /// returns false if it is shorter than len. Otherwise resizes str to len, with /// U+2026 HORIZONTAL ELLIPSIS at the end, and returns true. +/// If mid is true, the ellipsis will be put to the mid of the string, and the first +/// and last half is appended/prepended. /// /// Warning (Unicode): The cases where we want to truncate the text and it does /// not end up converted into a QString for UI display must be really @@ -294,7 +296,8 @@ docstring const protectArgument(docstring & arg, char const l = '[', /// /// FIXME: apply those principles in the current code. /// -bool truncateWithEllipsis(docstring & str, size_t const len); +bool truncateWithEllipsis(docstring & str, size_t const len, + bool const mid = false); /// Word-wraps the provided docstring, returning a line-broken string /// of width no wider than width, with the string broken at spaces. -- 2.39.5