tdf#159102 sw smart justify: fix automatic hyphenation

As before with soft hyphens, automatic hyphenation
could result too much shrinking, because of calculating
with an extra non-existing space in the line.

Also try to shrink the line only if a space likely
will be available in it.

During testing, extend user dictionary temporarily
with custom automatic hyphenation to avoid of false tests
because of non-available hyphenation patterns. (Note: maybe
the other tests with non-user dictionary based automatic
hyphenations are not correct.)

Follow-up to commit d511367c102ef2ada0f73dbe81744d39865d58ba
"tdf#195085 sw smart justify: fix bad shrinking at soft hyphen".

Change-Id: I58ecb8f1ea9f55ef2457d7ff4aca6aefa59a6dd1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162199
Tested-by: Jenkins
Reviewed-by: László Németh <nemeth@numbertext.org>
(cherry picked from commit 7c1f4dd740c32050480f3ab678805ad3c4c748bb)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162219
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/sw/qa/uitest/data/tdf159102.fodt b/sw/qa/uitest/data/tdf159102.fodt
new file mode 100644
index 0000000..dfe9fc1
--- /dev/null
+++ b/sw/qa/uitest/data/tdf159102.fodt
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>

<office:document xmlns:css3t="http://www.w3.org/TR/css3-text/" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:rpt="http://openoffice.org/2005/report" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
 <office:settings>
  <config:config-item-set config:name="ooo:configuration-settings">
   <config:config-item config:name="JustifyLinesWithShrinking" config:type="boolean">true</config:config-item>
   <config:config-item config:name="PrinterIndependentLayout" config:type="string">high-resolution</config:config-item>
  </config:config-item-set>
 </office:settings>
 <office:font-face-decls>
  <style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss"/>
  <style:font-face style:name="Arial1" svg:font-family="Arial" style:font-family-generic="system" style:font-pitch="variable"/>
 </office:font-face-decls>
 <office:styles>
  <style:default-style style:family="paragraph">
   <style:paragraph-properties fo:hyphenation-ladder-count="no-limit" style:text-autospace="ideograph-alpha" style:punctuation-wrap="hanging" style:line-break="strict" style:tab-stop-distance="36pt" style:writing-mode="page"/>
   <style:text-properties style:use-window-font-color="true" loext:opacity="0%" style:font-name="Calibri" fo:font-size="10pt" fo:language="en" fo:country="US" style:letter-kerning="false" style:font-name-asian="MS Mincho" style:font-size-asian="10pt" style:language-asian="en" style:country-asian="US" style:font-name-complex="Calibri1" style:font-size-complex="10pt" style:language-complex="ar" style:country-complex="SA" fo:hyphenate="true" fo:hyphenation-remain-char-count="2" fo:hyphenation-push-char-count="2" loext:hyphenation-no-caps="false" loext:hyphenation-no-last-word="false" loext:hyphenation-word-char-count="5" loext:hyphenation-zone="no-limit"/>
  </style:default-style>
  <style:default-style style:family="table">
   <style:table-properties table:border-model="collapsing"/>
  </style:default-style>
  <style:default-style style:family="table-row">
   <style:table-row-properties fo:keep-together="auto"/>
  </style:default-style>
  <style:style style:name="Standard" style:family="paragraph" style:class="text">
   <style:paragraph-properties fo:margin-top="0pt" fo:margin-bottom="0pt" style:contextual-spacing="false" fo:text-align="start" style:justify-single-word="false" fo:orphans="2" fo:widows="2" style:writing-mode="lr-tb"/>
   <style:text-properties style:font-name="Arial" fo:font-family="Arial" style:font-family-generic="swiss" fo:font-size="12pt" fo:language="en" fo:country="GB" style:font-name-asian="Times New Roman1" style:font-family-asian="&apos;Times New Roman&apos;" style:font-family-generic-asian="system" style:font-pitch-asian="variable" style:font-size-asian="12pt" style:font-name-complex="Arial1" style:font-family-complex="Arial" style:font-family-generic-complex="system" style:font-pitch-complex="variable" style:font-size-complex="12pt"/>
  </style:style>
 </office:styles>
 <office:automatic-styles>
  <style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard">
   <style:paragraph-properties fo:text-align="justify" style:justify-single-word="false"/>
   <style:text-properties officeooo:paragraph-rsid="0018cb95"/>
  </style:style>
  <style:style style:name="T1" style:family="text">
   <style:text-properties fo:font-size="10pt" fo:language="es" fo:country="ES" style:font-size-asian="10pt" style:font-size-complex="10pt"/>
  </style:style>
  <style:style style:name="T2" style:family="text">
   <style:text-properties fo:font-size="10pt" fo:language="es" fo:country="ES" officeooo:rsid="001a9d3d" style:font-size-asian="10pt" style:font-size-complex="10pt"/>
  </style:style>
  <style:style style:name="T3" style:family="text">
   <style:text-properties fo:font-size="10pt" style:font-size-asian="10pt" style:font-size-complex="10pt"/>
  </style:style>
  <style:page-layout style:name="pm1">
   <style:page-layout-properties fo:page-width="419.56pt" fo:page-height="595.3pt" style:num-format="1" style:print-orientation="portrait" fo:margin-top="21.6pt" fo:margin-bottom="14.4pt" fo:margin-left="36pt" fo:margin-right="36pt" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="31" style:layout-grid-base-height="18pt" style:layout-grid-ruby-height="0pt" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:layout-grid-base-width="10.46pt" style:layout-grid-snap-to="true" style:footnote-max-height="0pt" loext:margin-gutter="0pt">
    <style:footnote-sep style:width="0.51pt" style:distance-before-sep="2.86pt" style:distance-after-sep="2.86pt" style:line-style="solid" style:adjustment="left" style:rel-width="25%" style:color="#000000"/>
   </style:page-layout-properties>
   <style:header-style/>
   <style:footer-style/>
  </style:page-layout>
 </office:automatic-styles>
 <office:master-styles>
  <style:master-page style:name="Standard" style:page-layout-name="pm1" draw:style-name="dp1"/>
 </office:master-styles>
 <office:body>
  <office:text>
   <text:p text:style-name="P1" loext:marker-style-name="T3"><text:span text:style-name="T1">venenatis, quis commodo dolor posuere. Curabitur dignissim sapien quis cursus egestas.</text:span></text:p>
   <text:p text:style-name="P1" loext:marker-style-name="T3"><text:span text:style-name="T1">venenatis, quis commodo dolor posuere. Curabitur dignissim sapien quis cur</text:span><text:span text:style-name="T1">sus egestas.</text:span></text:p>
  </office:text>
 </office:body>
</office:document>
diff --git a/sw/qa/uitest/writer_tests8/tdf159102.py b/sw/qa/uitest/writer_tests8/tdf159102.py
new file mode 100644
index 0000000..07152ad
--- /dev/null
+++ b/sw/qa/uitest/writer_tests8/tdf159102.py
@@ -0,0 +1,81 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from uitest.framework import UITestCase
from uitest.uihelper.common import get_state_as_dict, get_url_for_data_file
from libreoffice.uno.propertyvalue import mkPropertyValues

# handle tdf#119908 smart justify with automatic hyphenation

class tdf159102(UITestCase):

    def set_custom_hyphenation(self):
        with self.ui_test.execute_dialog_through_command(".uno:OptionsTreeDialog") as xDialog:

            xPages = xDialog.getChild("pages")
            xLanguageEntry = xPages.getChild('2')                 # Language Settings
            xLanguageEntry.executeAction("EXPAND", tuple())
            xxLanguageEntryWritingAidsEntry = xLanguageEntry.getChild('1')
            xxLanguageEntryWritingAidsEntry.executeAction("SELECT", tuple())          # Writing Aids

            # add hyphenation "cur=sus" to the custom dictionary to solve the non-accessible
            # hyphenation patterns for the test

            # Select an editable dictionary (list of Ignored words)
            dictionaries = xDialog.getChild("lingudicts")
            hasEditableDictionary = False
            for i in dictionaries.getChildren():
                entry = dictionaries.getChild(i)
                entry_label = get_state_as_dict(entry)["Text"]
                if entry_label == "List of Ignored Words [All]":
                    hasEditableDictionary = True
                    entry.executeAction("SELECT", tuple())          # an editable user dictionary
                    break

            self.assertEqual(True, hasEditableDictionary)

            # open Edit dialog window
            edit = xDialog.getChild("lingudictsedit")
            with self.ui_test.execute_blocking_action(edit.executeAction, args=('CLICK', ()), close_button="close") as xEdit:
                # write cur=sus into the input box
                inputbox = xEdit.getChild("word")
                inputbox.executeAction("TYPE", mkPropertyValues({"TEXT": "cur=sus"}))
                add = xEdit.getChild("newreplace")
                add.executeAction("CLICK", tuple())

    def test_tdf159102_smart_justify_with_automatic_hyphenation(self):
        xToolkit = self.xContext.ServiceManager.createInstance('com.sun.star.awt.Toolkit')
        with self.ui_test.load_file(get_url_for_data_file("tdf159102.fodt")) as writer_doc:
            # we must not depend on the installed hyphenation patterns,
            # so extend user dictionary temporarily with the hyphenation cur=sus
            self.set_custom_hyphenation()
            xToolkit.processEventsToIdle()
            # delete the text of the first line
            self.xUITest.executeCommand(".uno:GoToEndOfLine")
            self.xUITest.executeCommand('.uno:StartOfDocumentSel')
            self.xUITest.executeCommand('.uno:Delete')
            paragraphs = writer_doc.Text.createEnumeration()
            para1 = paragraphs.nextElement()
            # This was "stas.", i.e. too much shrinking (accept also " cursus egestas." in the
            # case of missing hyphenation patterns on some test platforms).
            try:
                self.assertEqual("sus egestas.", para1.String)
            except AssertionError:
                self.assertEqual(" cursus egestas.", para1.String)

            # check next paragraph (containing different text portions)
            self.xUITest.executeCommand(".uno:GoDown")
            self.xUITest.executeCommand(".uno:GoToEndOfLine")
            self.xUITest.executeCommand('.uno:StartOfDocumentSel')
            self.xUITest.executeCommand('.uno:Delete')
            paragraphs = writer_doc.Text.createEnumeration()
            para1 = paragraphs.nextElement()
            try:
                self.assertEqual("sus egestas.", para1.String)
            except AssertionError:
                self.assertEqual(" cursus egestas.", para1.String)
diff --git a/sw/source/core/text/portxt.cxx b/sw/source/core/text/portxt.cxx
index 85691ef..e58021e 100644
--- a/sw/source/core/text/portxt.cxx
+++ b/sw/source/core/text/portxt.cxx
@@ -325,14 +325,20 @@ bool SwTextPortion::Format_( SwTextFormatInfo &rInf )

        // call with an extra space: shrinking can result a new word in the line
        // and a new space before that, which is also a shrank space
        // (except if the line was already broken at a soft hyphen, i.e. inside a word)
        if ( aGuess.BreakPos() < TextFrameIndex(rInf.GetText().getLength()) &&
        // (except if the line was already broken inside a word with hyphenation)
        // TODO: handle the case, if the line contains extra amount of spaces
        if (
             // no automatic hyphenation
             !aGuess.HyphWord().is() &&
             // no hyphenation at soft hyphen
             aGuess.BreakPos() < TextFrameIndex(rInf.GetText().getLength()) &&
             rInf.GetText()[sal_Int32(aGuess.BreakPos())] != CHAR_SOFTHYPHEN )
        {
            ++nSpacesInLine;
        }

        bFull = !aGuess.Guess( *this, rInf, Height(), nSpacesInLine );
        if ( nSpacesInLine > 0 )
            bFull = !aGuess.Guess( *this, rInf, Height(), nSpacesInLine );
    }

    // these are the possible cases: