related tdf#99602 ww8/rtfoutput: fix incorrect rounding on subscripts

Adding .5 is a poor mans version of rounding which works fine with
unsigned numbers, but not with negative numbers. Perhaps use the
exotic round() function instead?

In addition, the font size isn't necessarily an integer,
so that should have been a float.

The result of bad rounding was losing a percentage of the subscript
every round-trip.

Change-Id: I9093f7bfcd0b87249b42562668e45480dcb59f53
Reviewed-on: https://gerrit.libreoffice.org/80218
Tested-by: Jenkins
Reviewed-by: Justin Luth <justin_luth@sil.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
diff --git a/sw/qa/extras/ww8export/ww8export3.cxx b/sw/qa/extras/ww8export/ww8export3.cxx
index cb4caa3..7a4dc17 100644
--- a/sw/qa/extras/ww8export/ww8export3.cxx
+++ b/sw/qa/extras/ww8export/ww8export3.cxx
@@ -176,8 +176,12 @@ DECLARE_WW8EXPORT_TEST(testTdf120225_textControlCrossRef, "tdf120225_textControl
DECLARE_WW8EXPORT_TEST(testTdf127316_autoEscapement, "tdf127316_autoEscapement.odt")
{
    uno::Reference<text::XTextRange> xPara = getParagraph(2);
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.f, getProperty<float>(getRun(xPara, 1), "CharEscapement"), 0);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(-33.f, getProperty<float>(getRun(xPara, 2), "CharEscapement"), 3);
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.f, getProperty<float>(getRun(xPara, 1, "Normal text "), "CharEscapement"), 0);
    // Automatic escapement SHOULD BE limited by the font bottom line(?)
    // and so the calculations ought to be different. There is room for a lot of export improvement here.
    // Negative escapements (subscripts) were decreasing by 1% every round-trip due to bad manual rounding.
    // The actual number of 33% isn't so important here, but test that it is stable across multiple round-trips.
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Did you fix or break me?", -33.f, getProperty<float>(getRun(xPara, 2), "CharEscapement"), 1);
}

DECLARE_WW8EXPORT_TEST(testTdf121111_fillStyleNone, "tdf121111_fillStyleNone.docx")
diff --git a/sw/source/filter/ww8/rtfattributeoutput.cxx b/sw/source/filter/ww8/rtfattributeoutput.cxx
index 078f9dc..70ba0dd 100644
--- a/sw/source/filter/ww8/rtfattributeoutput.cxx
+++ b/sw/source/filter/ww8/rtfattributeoutput.cxx
@@ -2306,14 +2306,14 @@ void RtfAttributeOutput::CharEscapement(const SvxEscapementItem& rEscapement)

    const char* pUpDn;

    SwTwips nH = m_rExport.GetItem(RES_CHRATR_FONTSIZE).GetHeight();
    double fHeight = m_rExport.GetItem(RES_CHRATR_FONTSIZE).GetHeight();

    if (0 < rEscapement.GetEsc())
        pUpDn = OOO_STRING_SVTOOLS_RTF_UP;
    else if (0 > rEscapement.GetEsc())
    {
        pUpDn = OOO_STRING_SVTOOLS_RTF_DN;
        nH = -nH;
        fHeight = -fHeight;
    }
    else
        return;
@@ -2344,9 +2344,7 @@ void RtfAttributeOutput::CharEscapement(const SvxEscapementItem& rEscapement)
     *                    -----------------------  = ------------
     *                      100%                       Escapement
     */

    m_aStyles.append(static_cast<sal_Int32>((long(nEsc) * nH) + 500) / 1000);
    // 500 to round !!
    m_aStyles.append(static_cast<sal_Int32>(round(fHeight * nEsc / 1000)));
}

void RtfAttributeOutput::CharFont(const SvxFontItem& rFont)
diff --git a/sw/source/filter/ww8/ww8atr.cxx b/sw/source/filter/ww8/ww8atr.cxx
index 040153b..17780b0 100644
--- a/sw/source/filter/ww8/ww8atr.cxx
+++ b/sw/source/filter/ww8/ww8atr.cxx
@@ -1396,6 +1396,7 @@ void WW8AttributeOutput::CharEscapement( const SvxEscapementItem& rEscapement )
        else if ( DFLT_ESC_SUPER == nEsc || DFLT_ESC_AUTO_SUPER == nEsc )
            b = 1;
    }
    // FIXME: these need a better formula. See rtfAttributeOutput
    else if ( DFLT_ESC_AUTO_SUPER == nEsc )
        nEsc = DFLT_ESC_SUPER;
    else if ( DFLT_ESC_AUTO_SUB == nEsc )
@@ -1410,17 +1411,15 @@ void WW8AttributeOutput::CharEscapement( const SvxEscapementItem& rEscapement )

    if ( 0 == b || 0xFF == b )
    {
        long nHeight = m_rWW8Export.GetItem( RES_CHRATR_FONTSIZE ).GetHeight();
        double fHeight = m_rWW8Export.GetItem( RES_CHRATR_FONTSIZE ).GetHeight();
        m_rWW8Export.InsUInt16( NS_sprm::sprmCHpsPos );

        m_rWW8Export.InsUInt16( static_cast<short>(( nHeight * nEsc + 500 ) / 1000 ));
        m_rWW8Export.InsUInt16(static_cast<short>( round(fHeight * nEsc / 1000) ));

        if( 100 != nProp || !b )
        {
            m_rWW8Export.InsUInt16( NS_sprm::sprmCHps );

            m_rWW8Export.InsUInt16(
                msword_cast<sal_uInt16>((nHeight * nProp + 500 ) / 1000));
            m_rWW8Export.InsUInt16(msword_cast<sal_uInt16>( round(fHeight * nProp / 1000) ));
        }
    }
}