tdf#139495 doc import: prevent negative Int forced into uInt

Don't cast a negative number into a sal_uInt16
and expect good results. (ww8import test)

But for bug 139495 (and ww8export3), the problem area is also
related to pHdFormat->SetFormatAttr(SwFormatFrameSize...
which takes an awt::Size which consists of Longs.
So they should accept negative numbers.
Likely the result is being treated as a uInt somewhere.
And a negative height doesn't mean very much anyway.

There seems to be a fundamental difference between
Word and LO here. Word sets the page margins,
but that is irrelevant to the header/footer,
which have their own distance from the edge.

In LO, the header starts at the page margin,
and the body text must follow the header,
(In Word, the body text can start above the header,
as seen in the ww8export3 unit test.)

[FYI: DOC format (since forever) seems to ignore the header size,
allowing the content to at least overlap with the body text,
but DOCX doesn't have that.
Likely this is #112727# using floating frames hack.
So I'd better not keep messing around in here.]

Change-Id: Ie7bffec8a3de25958cf1f311ff2c8338d4870d99
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113681
Tested-by: Jenkins
Reviewed-by: Justin Luth <justin_luth@sil.org>
diff --git a/sw/qa/extras/ww8export/data/tdf139495_tinyHeader.doc b/sw/qa/extras/ww8export/data/tdf139495_tinyHeader.doc
new file mode 100644
index 0000000..c45a6c2
--- /dev/null
+++ b/sw/qa/extras/ww8export/data/tdf139495_tinyHeader.doc
Binary files differ
diff --git a/sw/qa/extras/ww8export/ww8export3.cxx b/sw/qa/extras/ww8export/ww8export3.cxx
index 508636e..05211e4 100644
--- a/sw/qa/extras/ww8export/ww8export3.cxx
+++ b/sw/qa/extras/ww8export/ww8export3.cxx
@@ -256,6 +256,12 @@ DECLARE_WW8EXPORT_TEST(testTdf122460_header, "tdf122460_header.odt")
    CPPUNIT_ASSERT(headerIsOn);
}

DECLARE_WW8EXPORT_TEST(testTdf139495_tinyHeader, "tdf139495_tinyHeader.doc")
{
    // In Word 2003, this is one page, but definitely not six pages.
    CPPUNIT_ASSERT(getPages() < 3);
}

DECLARE_WW8EXPORT_TEST(testFdo53985, "fdo53985.doc")
{
    uno::Reference<text::XTextTablesSupplier> xTablesSupplier(mxComponent, uno::UNO_QUERY);
diff --git a/sw/qa/extras/ww8import/ww8import.cxx b/sw/qa/extras/ww8import/ww8import.cxx
index 7fca28b..a9b7558 100644
--- a/sw/qa/extras/ww8import/ww8import.cxx
+++ b/sw/qa/extras/ww8import/ww8import.cxx
@@ -245,6 +245,10 @@ DECLARE_WW8IMPORT_TEST(testTdf122425_1, "tdf122425_1.doc")
            CPPUNIT_ASSERT(!pBox->GetLine(eLine));
        }
    }

    //tdf#139495: without the fix, a negative number was converted into a uInt16, overflowing to 115501
    auto nDist = getProperty<sal_uInt32>(getStyles("PageStyles")->getByName("Standard"), "HeaderBodyDistance");
    CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), nDist);
}

DECLARE_WW8IMPORT_TEST(testTdf110987, "tdf110987")
diff --git a/sw/source/filter/ww8/ww8par6.cxx b/sw/source/filter/ww8/ww8par6.cxx
index 4753ca2..bf9569c 100644
--- a/sw/source/filter/ww8/ww8par6.cxx
+++ b/sw/source/filter/ww8/ww8par6.cxx
@@ -669,8 +669,10 @@ void wwSectionManager::SetPageULSpaceItems(SwFrameFormat &rFormat,
            }
            else
            {
                // Hack alert: these calculations are based on
                // #112727# import negative height headers/footers as floating frames inside fixed height headers/footer
                // #i48832# - set correct spacing between header and body.
                const sal_Int32 nHdLowerSpace( std::abs(rSection.maSep.dyaTop) - rData.nSwUp - rData.nSwHLo );
                const sal_Int32 nHdLowerSpace(std::max<sal_Int32>(0, std::abs(rSection.maSep.dyaTop) - rData.nSwUp - rData.nSwHLo));
                pHdFormat->SetFormatAttr(SwFormatFrameSize(SwFrameSize::Fixed, 0, rData.nSwHLo + nHdLowerSpace));
                aHdUL.SetLower( static_cast< sal_uInt16 >(nHdLowerSpace) );
                pHdFormat->SetFormatAttr(SwHeaderAndFooterEatSpacingItem(
@@ -697,7 +699,7 @@ void wwSectionManager::SetPageULSpaceItems(SwFrameFormat &rFormat,
            else
            {
                // #i48832# - set correct spacing between footer and body.
                const SwTwips nFtUpperSpace( std::abs(rSection.maSep.dyaBottom) - rData.nSwLo - rData.nSwFUp );
                const sal_Int32 nFtUpperSpace(std::max<sal_Int32>(0, std::abs(rSection.maSep.dyaBottom) - rData.nSwLo - rData.nSwFUp));
                pFtFormat->SetFormatAttr(SwFormatFrameSize(SwFrameSize::Fixed, 0, rData.nSwFUp + nFtUpperSpace));
                aFtUL.SetUpper( static_cast< sal_uInt16 >(nFtUpperSpace) );
                pFtFormat->SetFormatAttr(SwHeaderAndFooterEatSpacingItem(