fdo#77454: fix WW8 import/export of negative image crop

The negative crop values were imported as large positive values, which
caused the image to be rendered with 1 pixel width after commit
2e5167528f7566dd9b000e50fc1610b7bf99132a.

Change-Id: I0e01b9d9a05d90e868699832085a06ba5aab7e54
diff --git a/sw/qa/extras/ww8export/data/fdo77454.doc b/sw/qa/extras/ww8export/data/fdo77454.doc
new file mode 100644
index 0000000..4e2d7b2
--- /dev/null
+++ b/sw/qa/extras/ww8export/data/fdo77454.doc
Binary files differ
diff --git a/sw/qa/extras/ww8export/ww8export.cxx b/sw/qa/extras/ww8export/ww8export.cxx
index d014dba..5857559 100644
--- a/sw/qa/extras/ww8export/ww8export.cxx
+++ b/sw/qa/extras/ww8export/ww8export.cxx
@@ -17,6 +17,7 @@
#include <com/sun/star/view/XViewSettingsSupplier.hpp>
#include <com/sun/star/table/ShadowFormat.hpp>
#include <com/sun/star/table/TableBorder2.hpp>
#include <com/sun/star/text/GraphicCrop.hpp>

class Test : public SwModelTestBase
{
@@ -150,6 +151,29 @@ DECLARE_WW8EXPORT_TEST(testCharacterBorder, "charborder.odt")
    }
}

DECLARE_WW8EXPORT_TEST(testFdo77454, "fdo77454.doc")
{
    {
        // check negative crops round-trip
        text::GraphicCrop const crop =
            getProperty<text::GraphicCrop>(getShape(1), "GraphicCrop");
        CPPUNIT_ASSERT_EQUAL(sal_Int32( -439), crop.Left);
        CPPUNIT_ASSERT_EQUAL(sal_Int32(-7040), crop.Right);
        CPPUNIT_ASSERT_EQUAL(sal_Int32( -220), crop.Top);
        CPPUNIT_ASSERT_EQUAL(sal_Int32(-7040), crop.Bottom);
    }

    {
        // check positive crops round-trip
        text::GraphicCrop const crop =
            getProperty<text::GraphicCrop>(getShape(2), "GraphicCrop");
        CPPUNIT_ASSERT_EQUAL(sal_Int32(  326), crop.Left);
        CPPUNIT_ASSERT_EQUAL(sal_Int32( 1208), crop.Right);
        CPPUNIT_ASSERT(abs(sal_Int32(1635) -  crop.Top) <= 2);
        CPPUNIT_ASSERT(abs(sal_Int32(  95) - crop.Bottom) <= 2);
    }
}

DECLARE_WW8EXPORT_TEST(testFdo59530, "fdo59530.doc")
{
    // See ooxmlexport's testFdo38244().
diff --git a/sw/source/filter/ww8/wrtw8esh.cxx b/sw/source/filter/ww8/wrtw8esh.cxx
index a5bf48a..0471950 100644
--- a/sw/source/filter/ww8/wrtw8esh.cxx
+++ b/sw/source/filter/ww8/wrtw8esh.cxx
@@ -2182,9 +2182,19 @@ sal_Int32 SwBasicEscherEx::ToFract16(sal_Int32 nVal, sal_uInt32 nMax) const
{
    if (nMax)
    {
        sal_Int32 nMSVal = (nVal / 65536) * nMax;
        nMSVal += (nVal * 65536 ) / nMax;
        return nMSVal;
        if (nVal >= 0)
        {
            sal_Int32 nMSVal = (nVal / 65536) * nMax;
            nMSVal += (nVal * 65536) / nMax;
            return nMSVal;
        } else {
            // negative fraction does not have "-0", fractional part is always
            // positive:  -0.4 represented as -1 + 0.6
            sal_Int32 const nDiv = (nVal / sal_Int32(nMax)) - 1;
            sal_uInt32 nMSVal = (nDiv << 16) & 0xffff0000;
            nMSVal += (nVal * 65536) / sal_Int32(nMax) + (-nDiv * 65536);
            return nMSVal;
        }
    }
    return 0;
}
diff --git a/sw/source/filter/ww8/ww8graf.cxx b/sw/source/filter/ww8/ww8graf.cxx
index b4f28ab..ec4bf38 100644
--- a/sw/source/filter/ww8/ww8graf.cxx
+++ b/sw/source/filter/ww8/ww8graf.cxx
@@ -1972,6 +1972,13 @@ void SwWW8ImplReader::MapWrapIntoFlyFmt(SvxMSDffImportRec* pRecord,
    }
}

static sal_Int32 lcl_ConvertCrop(sal_uInt32 const nCrop, sal_Int32 const nSize)
{
    // cast to sal_Int32 to handle negative crop properly
    return ((static_cast<sal_Int32>(nCrop) >> 16) * nSize)
              + (((nCrop & 0xffff) * nSize) >> 16) ;
}

void
SwWW8ImplReader::SetAttributesAtGrfNode(SvxMSDffImportRec const*const pRecord,
    SwFrmFmt *pFlyFmt, WW8_FSPA *pF )
@@ -1994,23 +2001,23 @@ SwWW8ImplReader::SetAttributesAtGrfNode(SvxMSDffImportRec const*const pRecord,
            pRecord->nCropFromLeft || pRecord->nCropFromRight )
        {
            SwCropGrf aCrop;            // Cropping is stored in 'fixed floats'
                                        // 16.16 (it est fraction times total
                                        // 16.16 (fraction times total
            if( pRecord->nCropFromTop ) //        image width or height resp.)
                aCrop.SetTop( static_cast< sal_Int32 >(
                (   ( (pRecord->nCropFromTop    >> 16   ) * rHeight )
                  + (((pRecord->nCropFromTop    & 0xffff) * rHeight ) >> 16) )));
            {
                aCrop.SetTop(lcl_ConvertCrop(pRecord->nCropFromTop, rHeight));
            }
            if( pRecord->nCropFromBottom )
                aCrop.SetBottom( static_cast< sal_Int32 >(
                (   ( (pRecord->nCropFromBottom >> 16   ) * rHeight )
                  + (((pRecord->nCropFromBottom & 0xffff) * rHeight ) >> 16) )));
            {
                aCrop.SetBottom(lcl_ConvertCrop(pRecord->nCropFromBottom, rHeight));
            }
            if( pRecord->nCropFromLeft )
                aCrop.SetLeft( static_cast< sal_Int32 >(
                (   ( (pRecord->nCropFromLeft   >> 16   ) * rWidth  )
                  + (((pRecord->nCropFromLeft   & 0xffff) * rWidth  ) >> 16) )));
            {
                aCrop.SetLeft(lcl_ConvertCrop(pRecord->nCropFromLeft, rWidth));
            }
            if( pRecord->nCropFromRight )
                aCrop.SetRight( static_cast< sal_Int32 >(
                (   ( (pRecord->nCropFromRight  >> 16   ) * rWidth  )
                  + (((pRecord->nCropFromRight  & 0xffff) * rWidth  ) >> 16) )));
            {
                aCrop.SetRight(lcl_ConvertCrop(pRecord->nCropFromRight,rWidth));
            }

            pGrfNd->SetAttr( aCrop );
        }