tdf#148940 improve non Snap-to-char mode textgrid layout.

- Revert b647f9f4bcb24ed9b71c77038da6b754b8d42eb7 , i.e.
tdf#107362 don't insert kern if snap to char is not checked.

This includes:
  a) Revert to insert kern portion to align the next CJK
  text portion to the next grid cell border.

  b) Revert none snap-to-char code to proecess CJK script only
   as before. Latin and CTL scripts are processed as witnout
   text grid.

- EvalGridWidthAdd() now uses the 0th element of kern array as
  the glyph width to calculate the distance to the nearest cell
  border. This makes layout more accurate and do not generate
  extra spaces, allow testTdf107362 to pass.

- SwFont::GetTextBreak() non snap-to-char mode now uses the same
  code as snap-to-char mode to calculate the accommodable text
  length. This fix one excess character per line problem for non
  snap-to-char mode.

- Update testTdf107025 because the character in the test case
  occupies more than one text grid cells. Layout change has been
  verified.

- tdf#107446 breaks again because of the patch. It needs different
  solution for Word compatibility.

Change-Id: I7fadb065b9cce2d71aaaed39be2881c43d8e4db4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134001
Tested-by: Jenkins
Reviewed-by: Mark Hung <marklh9@gmail.com>
diff --git a/sw/qa/extras/uiwriter/uiwriter4.cxx b/sw/qa/extras/uiwriter/uiwriter4.cxx
index e3a625b..ae650c8 100644
--- a/sw/qa/extras/uiwriter/uiwriter4.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter4.cxx
@@ -1836,10 +1836,11 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest4, testTdf107025)
        return;

    CPPUNIT_ASSERT(!parseDump("(//SwLinePortion)[2]", "width").isEmpty());
    // Width of the second line is expected to be 9 times of the first.
    sal_Int32 nWidth2 = getXPath(pXmlDoc, "(//SwLinePortion)[2]", "width").toInt32();
    sal_Int32 nRatio = nWidth2 / nWidth1;

    CPPUNIT_ASSERT_EQUAL(sal_Int32(9), nWidth2 / nWidth1);
    CPPUNIT_ASSERT(nRatio >= 9); // Occupy at least 9 cells.
    CPPUNIT_ASSERT(nRatio < 18); // Occupy at most 18 cells.
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest4, testTdf107362)
diff --git a/sw/source/core/text/itrform2.cxx b/sw/source/core/text/itrform2.cxx
index f9e6f45..52d13e6 100644
--- a/sw/source/core/text/itrform2.cxx
+++ b/sw/source/core/text/itrform2.cxx
@@ -507,7 +507,7 @@ void SwTextFormatter::BuildPortions( SwTextFormatInfo &rInf )
                }
            }
        }
        else if ( bHasGrid && pGrid->IsSnapToChars() && ! pGridKernPortion && ! m_pMulti && ! pPor->InTabGrp() )
        else if ( bHasGrid && ! pGridKernPortion && ! m_pMulti && ! pPor->InTabGrp() )
        {
            // insert a grid kerning portion
            pGridKernPortion = pPor->IsKernPortion() ?
@@ -650,7 +650,7 @@ void SwTextFormatter::BuildPortions( SwTextFormatInfo &rInf )
            }
        }

        if ( bHasGrid && pGrid->IsSnapToChars() && pPor != pGridKernPortion && ! m_pMulti && ! pPor->InTabGrp() )
        if ( bHasGrid && pPor != pGridKernPortion && ! m_pMulti && ! pPor->InTabGrp() )
        {
            TextFrameIndex const nTmp = rInf.GetIdx() + pPor->GetLen();
            const SwTwips nRestWidth = rInf.Width() - rInf.X() - pPor->Width();
diff --git a/sw/source/core/txtnode/fntcache.cxx b/sw/source/core/txtnode/fntcache.cxx
index cfcf549..5623a35 100644
--- a/sw/source/core/txtnode/fntcache.cxx
+++ b/sw/source/core/txtnode/fntcache.cxx
@@ -74,21 +74,15 @@ static vcl::DeleteOnDeinit< VclPtr<OutputDevice> > s_pFntObjPixOut {};
namespace
{

tools::Long EvalGridWidthAdd( const SwTextGridItem *const pGrid, const SwDrawTextInfo &rInf )
tools::Long EvalGridWidthAdd( const SwTextGridItem *const pGrid,
        const SwDrawTextInfo &rInf, tools::Long nFontWidth )
{
    SwDocShell* pDocShell = rInf.GetShell()->GetDoc()->GetDocShell();
    SfxStyleSheetBasePool* pBasePool = pDocShell->GetStyleSheetPool();

    SfxStyleSheetBase* pStyle = pBasePool->Find(SwResId(STR_POOLCOLL_STANDARD), SfxStyleFamily::Para);
    SfxItemSet& aTmpSet = pStyle->GetItemSet();
    const SvxFontHeightItem &aDefaultFontItem = aTmpSet.Get(RES_CHRATR_CJK_FONTSIZE);

    const SwDoc* pDoc = rInf.GetShell()->GetDoc();
    const sal_uInt16 nGridWidth = GetGridWidth(*pGrid, *pDoc);
    const sal_uInt32 nFontHeight = aDefaultFontItem.GetHeight();
    const tools::Long nGridWidthAdd = nGridWidth > nFontHeight ? nGridWidth - nFontHeight : 0;
    if( SwFontScript::Latin == rInf.GetFont()->GetActual() )
        return nGridWidthAdd / 2;
    tools::Long nGridWidthAdd = nGridWidth - nFontWidth;

    while (nGridWidthAdd < 0)
        nGridWidthAdd += nGridWidth;

    return nGridWidthAdd;
}
@@ -1045,21 +1039,23 @@ void SwFntObj::DrawText( SwDrawTextInfo &rInf )
    // For text grid refactor
    // ASIAN LINE AND CHARACTER GRID MODE START: not snap to characters

    if ( rInf.GetFrame() && rInf.SnapToGrid() )
    if ( rInf.GetFrame() && rInf.SnapToGrid() && rInf.GetFont() &&
         SwFontScript::CJK == rInf.GetFont()->GetActual() )
    {
        SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame()));

        // ASIAN LINE AND CHARACTER GRID MODE - do not snap to characters
        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() )
        {
            const tools::Long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf );

            std::vector<sal_Int32> aKernArray;

            if ( m_pPrinter )
                GetTextArray(*m_pPrinter, rInf, aKernArray);
            else
                GetTextArray(rInf.GetOut(), rInf, aKernArray);

            const tools::Long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf, aKernArray[0] );

            if ( bSwitchH2V )
                rInf.GetFrame()->SwitchHorizontalToVertical( aTextOriginPos );
            if ( rInf.GetSpace() || rInf.GetKanaComp())
@@ -1846,12 +1842,12 @@ Size SwFntObj::GetTextSize( SwDrawTextInfo& rInf )
    }

    //for textgrid refactor
    if ( rInf.GetFrame() && nLn && rInf.SnapToGrid() && rInf.GetFont() )
    if ( rInf.GetFrame() && nLn && rInf.SnapToGrid() && rInf.GetFont() &&
         SwFontScript::CJK == rInf.GetFont()->GetActual() )
    {
        SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame()));
        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() )
        {
            const tools::Long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf );
            OutputDevice* pOutDev;
            if ( m_pPrinter )
            {
@@ -1861,8 +1857,11 @@ Size SwFntObj::GetTextSize( SwDrawTextInfo& rInf )
            }
            else
                pOutDev = rInf.GetpOut();
            aTextSize.setWidth(pOutDev->GetTextWidth(rInf.GetText(),
                        sal_Int32(rInf.GetIdx()), sal_Int32(nLn)));

            tools::Long nWidth = pOutDev->GetTextWidth(rInf.GetText(),
                        sal_Int32(rInf.GetIdx()), sal_Int32(nLn));
            const tools::Long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf, nWidth / sal_Int32(nLn) );
            aTextSize.setWidth(nWidth);
            aTextSize.setHeight( pOutDev->GetTextHeight() +
                                GetFontLeading( rInf.GetShell(), rInf.GetOut() ) );
            aTextSize.AdjustWidth(sal_Int32(nLn) * nGridWidthAdd);
@@ -2063,13 +2062,14 @@ TextFrameIndex SwFntObj::GetModelPositionForViewPoint(SwDrawTextInfo &rInf)
    }

    //for textgrid refactor
    if ( rInf.GetFrame() && rInf.GetLen() && rInf.SnapToGrid() )
    if ( rInf.GetFrame() && rInf.GetLen() && rInf.SnapToGrid() &&
         rInf.GetFont() && SwFontScript::CJK == rInf.GetFont()->GetActual() )
    {
        SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame()));
        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() )
        {

            const tools::Long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf );
            const tools::Long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf, aKernArray[0] );

            for (TextFrameIndex j(0); j < rInf.GetLen(); j++)
            {
@@ -2269,7 +2269,7 @@ TextFrameIndex SwFont::GetTextBreak(SwDrawTextInfo const & rInf, tools::Long nTe
         rInf.GetFont() && SwFontScript::CJK == rInf.GetFont()->GetActual() )
    {
        SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame()));
        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && pGrid->IsSnapToChars() )
        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType())
        {
            const SwDoc* pDoc = rInf.GetShell()->GetDoc();
            const sal_uInt16 nGridWidth = GetGridWidth(*pGrid, *pDoc);
@@ -2297,26 +2297,6 @@ TextFrameIndex SwFont::GetTextBreak(SwDrawTextInfo const & rInf, tools::Long nTe
        }
    }

    //for text grid enhancement
    if ( rInf.GetFrame() && nLn && rInf.SnapToGrid() )
    {
        SwTextGridItem const*const pGrid(GetGridItem(rInf.GetFrame()->FindPageFrame()));
        if ( pGrid && GRID_LINES_CHARS == pGrid->GetGridType() && !pGrid->IsSnapToChars() )
        {
            const tools::Long nGridWidthAdd = EvalGridWidthAdd( pGrid, rInf );

            std::vector<sal_Int32> aKernArray;
            GetTextArray( rInf.GetOut(), rInf.GetText(), aKernArray,
                        sal_Int32(rInf.GetIdx()), sal_Int32(rInf.GetLen()));
            tools::Long nCurrPos = aKernArray[sal_Int32(nTextBreak)] + nGridWidthAdd;
            while (++nTextBreak < rInf.GetLen() && nTextWidth >= nCurrPos)
            {
                nCurrPos = aKernArray[sal_Int32(nTextBreak)] + nGridWidthAdd * (sal_Int32(nTextBreak) + 1);
            }
            return nTextBreak + rInf.GetIdx();
        }
    }

    if( m_aSub[m_nActual].IsCapital() && nLn )
    {
        nTextBreak = GetCapitalBreak( rInf.GetShell(), rInf.GetpOut(),