sw/inc/ToxTextGenerator.hxx             |    2 +-
 sw/source/core/tox/ToxTextGenerator.cxx |   16 ++++++++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)

New commits:
commit b7a5ac03e03066d36e05da786669a9243ad0116f
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Tue Feb 28 16:09:32 2023 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Tue Feb 28 19:12:34 2023 +0000

    tdf#114773: only add space between entry number and text
    
    Modify the hack from commit ce95e39f8e952159844e9dc04a1df402bb103634
    (tdf#44282 fix missing space for numbered lists in TOC, 2016-08-16),
    which added the space after entry numbers unconditionally.
    
    There are other possibilities, like custom separators between the entry
    number and text (e.g., [E#][T][E][T][#]); or the entry number (maybe
    with limited depth) used with page number (like in [E#][E][T][E#]-[#]).
    Generally, when entry number is not immediately followed by entry text,
    the space is not needed.
    
    Additionally, the space is not needed when the number text already ends
    with a space character.
    
    Change-Id: Ifa6c474574bfb74466ab721eca49f421a3750942
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147997
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sw/inc/ToxTextGenerator.hxx b/sw/inc/ToxTextGenerator.hxx
index 30581e110a1e..35eefd0fb7ab 100644
--- a/sw/inc/ToxTextGenerator.hxx
+++ b/sw/inc/ToxTextGenerator.hxx
@@ -143,7 +143,7 @@ private:
      */
     static OUString
     GetNumStringOfFirstNode(const SwTOXSortTabBase& rBase, bool bUsePrefix,
-            sal_uInt8 nLevel, SwRootFrame const* pLayout);
+            sal_uInt8 nLevel, SwRootFrame const* pLayout, bool bAddSpace = 
true);
 
     /** Handle a chapter token.
      */
diff --git a/sw/source/core/tox/ToxTextGenerator.cxx 
b/sw/source/core/tox/ToxTextGenerator.cxx
index 97ca39666391..1ef2a59d55bf 100644
--- a/sw/source/core/tox/ToxTextGenerator.cxx
+++ b/sw/source/core/tox/ToxTextGenerator.cxx
@@ -61,6 +61,9 @@ bool sortTabHasNoToxSourcesOrFirstToxSourceHasNoNode(const 
SwTOXSortTabBase& sor
     return false;
 }
 
+// Similar to rtl::isAsciiWhiteSpace, but applicable to ToC entry number
+bool isWhiteSpace(sal_Unicode ch) { return ch == ' ' || ch == '\t'; }
+
 } // end anonymous namespace
 
 namespace sw {
@@ -68,7 +71,7 @@ namespace sw {
 OUString
 ToxTextGenerator::GetNumStringOfFirstNode(const SwTOXSortTabBase& rBase,
         bool bUsePrefix, sal_uInt8 nLevel,
-        SwRootFrame const*const pLayout)
+        SwRootFrame const*const pLayout, bool bAddSpace)
 {
     if (sortTabHasNoToxSourcesOrFirstToxSourceHasNoNode(rBase)) {
         return OUString();
@@ -97,7 +100,7 @@ ToxTextGenerator::GetNumStringOfFirstNode(const 
SwTOXSortTabBase& rBase,
         sRet = pNd->GetNumString(bUsePrefix, nLevel, pLayout);
     }
 
-    if (!sRet.isEmpty()) {
+    if (bAddSpace && !sRet.isEmpty() && !isWhiteSpace(sRet[sRet.getLength() - 
1])) {
         sRet += " ";// Makes sure spacing is done only when there is outline 
numbering
     }
 
@@ -190,17 +193,22 @@ ToxTextGenerator::GenerateText(SwDoc* pDoc,
         // #i21237#
         SwFormTokens aPattern = mToxForm.GetPattern(nLvl);
         // remove text from node
-        for(const auto& aToken : aPattern) // #i21237#
+        for (size_t i = 0; i < aPattern.size(); ++i) // #i21237#
         {
+            const auto& aToken = aPattern[i];
             sal_Int32 nStartCharStyle = rText.getLength();
             OUString aCharStyleName = aToken.sCharStyleName;
             switch( aToken.eTokenType )
             {
             case TOKEN_ENTRY_NO:
                 // for TOC numbering
+                // Only add space when there is outline numbering, and also 
when the next token
+                // is the entry text: it can also be e.g. a tab, or the entry 
number can be used
+                // in page number area like "2-15" for chapter 2, page 15.
                 rText += GetNumStringOfFirstNode(rBase,
                     aToken.nChapterFormat == CF_NUMBER,
-                    static_cast<sal_uInt8>(aToken.nOutlineLevel - 1), pLayout);
+                    static_cast<sal_uInt8>(aToken.nOutlineLevel - 1), pLayout,
+                    i < aPattern.size() - 1 && aPattern[i + 1].eTokenType == 
TOKEN_ENTRY_TEXT);
                 break;
 
             case TOKEN_ENTRY_TEXT: {

Reply via email to