filter/source/msfilter/util.cxx       |   58 ++++++++++++++++------------------
 include/filter/msfilter/util.hxx      |    8 ----
 sc/source/filter/oox/pagesettings.cxx |    5 +-
 sc/source/ui/vba/vbapagesetup.cxx     |    5 --
 4 files changed, 32 insertions(+), 44 deletions(-)

New commits:
commit b0a24d949ea1fbfd208adfc975c94221022259ee
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Mon Jan 13 14:48:11 2025 +0500
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Feb 4 15:31:30 2025 +0100

    Simplify and improve getMSPaperSizeIndex
    
    Previously, it required for some reason, that in order to be found,
    the new paper size must be better than the previous-best, in *both*
    dimensions. Thus, if the previous-best e.g. matched one dimention
    perfectly, but not the other, no better match could be found.
    
    Change-Id: Ia8cd4fd98f324c8450577859dbdd21831b66e2e3
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/180164
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Tested-by: Jenkins
    (cherry picked from commit 6bfe8335c83fd9e5fd1b8c02a7d31650caa56279)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/180186
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/filter/source/msfilter/util.cxx b/filter/source/msfilter/util.cxx
index 911b4cfc010a..0d9ffbc7b5ab 100644
--- a/filter/source/msfilter/util.cxx
+++ b/filter/source/msfilter/util.cxx
@@ -167,11 +167,19 @@ OUString ConvertColorOU( const Color &rColor )
     return OUString( pBuffer );
 }
 
-#define IN2MM100( v )    static_cast< sal_Int32 >( (v) * 2540.0 + 0.5 )
-#define MM2MM100( v )    static_cast< sal_Int32 >( (v) * 100.0 + 0.5 )
+namespace
+{
+struct ApiPaperSize
+{
+    sal_Int32 mnWidth;
+    sal_Int32 mnHeight;
+};
+
+constexpr sal_Int32 IN2MM100(double v) { return o3tl::convert(v, 
o3tl::Length::in, o3tl::Length::mm100) + 0.5; }
+constexpr sal_Int32 MM2MM100(double v) { return o3tl::convert(v, 
o3tl::Length::mm, o3tl::Length::mm100) + 0.5; }
 
 // see XclPaperSize pPaperSizeTable in calc and aDinTab in i18nutil
-const ApiPaperSize spPaperSizeTable[] =
+constexpr ApiPaperSize spPaperSizeTable[] =
 {
     { 0, 0 },                                                //  0 - 
(undefined)
     { IN2MM100( 8.5 ),       IN2MM100( 11 )      },          //  1 - Letter 
paper
@@ -248,7 +256,7 @@ const ApiPaperSize spPaperSizeTable[] =
     { MM2MM100( 297 ),       MM2MM100( 420 )     },          // 67 - A3 
transverse paper
     { MM2MM100( 322 ),       MM2MM100( 445 )     },          // 68 - A3 extra 
transverse paper
     { MM2MM100( 200 ),       MM2MM100( 148 )     },          // 69 - Japanese 
double postcard
-    { MM2MM100( 105 ),       MM2MM100( 148 ),    },          // 70 - A6 paper
+    { MM2MM100( 105 ),       MM2MM100( 148 )     },          // 70 - A6 paper
     { 0, 0 },                                                // 71 - Japanese 
Envelope Kaku #2
     { 0, 0 },                                                // 72 - Japanese 
Envelope Kaku #3
     { 0, 0 },                                                // 73 - Japanese 
Envelope Chou #3
@@ -268,48 +276,38 @@ const ApiPaperSize spPaperSizeTable[] =
     { 0, 0 },                                                // 87 - Japanese 
Envelope Chou #4 Rotated
     { MM2MM100( 128 ),       MM2MM100( 182 )     },          // 88 - B6 (JIS)
     { MM2MM100( 182 ),       MM2MM100( 128 )     },          // 89 - B6 (JIS) 
Rotated
-    { IN2MM100( 12 ),        IN2MM100( 11 )      }           // 90 - 12x11
+    { IN2MM100( 12 ),        IN2MM100( 11 )      },          // 90 - 12x11
 };
+} // unnamed namespace
 
 sal_Int32 PaperSizeConv::getMSPaperSizeIndex( const css::awt::Size& rSize )
 {
     // Need to find the best match for current size
-    sal_Int32 nDeltaWidth = 0;
-    sal_Int32 nDeltaHeight = 0;
+    sal_Int32 nDeltaWidth = rSize.Width;
+    sal_Int32 nDeltaHeight = rSize.Height;
+    sal_Int32 nTol = 10; // hmm not sure is this the best way
 
     sal_Int32 nPaperSizeIndex = 0; // Undefined
-    const ApiPaperSize* pItem = spPaperSizeTable;
-    const ApiPaperSize* pEnd =  spPaperSizeTable + std::size( spPaperSizeTable 
);
-    for ( ; pItem != pEnd; ++pItem )
+    for (size_t i = 1; i < std::size(spPaperSizeTable); ++i)
     {
-        sal_Int32 nCurDeltaHeight = std::abs( pItem->mnHeight - rSize.Height );
-        sal_Int32 nCurDeltaWidth = std::abs( pItem->mnWidth - rSize.Width );
-        if ( pItem == spPaperSizeTable ) // initialize delta with first item
+        sal_Int32 nCurDeltaHeight = std::abs(spPaperSizeTable[i].mnHeight - 
rSize.Height);
+        sal_Int32 nCurDeltaWidth = std::abs(spPaperSizeTable[i].mnWidth - 
rSize.Width);
+        if (nCurDeltaWidth <= nTol && nCurDeltaHeight <= nTol
+            && nCurDeltaWidth + nCurDeltaHeight < nDeltaWidth + nDeltaHeight)
         {
             nDeltaWidth = nCurDeltaWidth;
             nDeltaHeight = nCurDeltaHeight;
-        }
-        else
-        {
-            if ( nCurDeltaWidth < nDeltaWidth && nCurDeltaHeight < 
nDeltaHeight )
-            {
-                nDeltaWidth = nCurDeltaWidth;
-                nDeltaHeight = nCurDeltaHeight;
-                nPaperSizeIndex = (pItem - spPaperSizeTable);
-            }
+            nPaperSizeIndex = i;
         }
     }
-    sal_Int32 nTol = 10; // hmm not sure is this the best way
-    if ( nDeltaWidth <= nTol && nDeltaHeight <= nTol )
-        return nPaperSizeIndex;
-    return 0;
+    return nPaperSizeIndex;
 }
 
-const ApiPaperSize& PaperSizeConv::getApiSizeForMSPaperSizeIndex( sal_Int32 
nMSOPaperIndex )
+css::awt::Size PaperSizeConv::getApiSizeForMSPaperSizeIndex(sal_Int32 
nMSOPaperIndex)
 {
-    if ( nMSOPaperIndex  < 0 || nMSOPaperIndex > std::ssize( spPaperSizeTable 
) - 1 )
-        return spPaperSizeTable[ 0 ];
-    return spPaperSizeTable[ nMSOPaperIndex ];
+    if (nMSOPaperIndex < 0 || nMSOPaperIndex >= std::ssize(spPaperSizeTable))
+        nMSOPaperIndex = 0;
+    return { spPaperSizeTable[nMSOPaperIndex].mnWidth, 
spPaperSizeTable[nMSOPaperIndex].mnHeight };
 }
 
 OUString CreateDOCXStyleId(std::u16string_view const aName)
diff --git a/include/filter/msfilter/util.hxx b/include/filter/msfilter/util.hxx
index 495c8ed13581..6157af1b3175 100644
--- a/include/filter/msfilter/util.hxx
+++ b/include/filter/msfilter/util.hxx
@@ -66,17 +66,11 @@ MSFILTER_DLLPUBLIC OUString ConvertColorOU( const Color 
&rColor );
 
 
 /** Paper size in 1/100 millimeters. */
-struct MSFILTER_DLLPUBLIC ApiPaperSize
-{
-    sal_Int32           mnWidth;
-    sal_Int32           mnHeight;
-};
-
 class MSFILTER_DLLPUBLIC PaperSizeConv
 {
 public:
     static sal_Int32 getMSPaperSizeIndex( const css::awt::Size& rSize );
-    static const ApiPaperSize& getApiSizeForMSPaperSizeIndex( sal_Int32 
nMSOPaperIndex );
+    static css::awt::Size getApiSizeForMSPaperSizeIndex(sal_Int32 
nMSOPaperIndex);
 };
 
 MSFILTER_DLLPUBLIC OUString CreateDOCXStyleId(std::u16string_view aName);
diff --git a/sc/source/filter/oox/pagesettings.cxx 
b/sc/source/filter/oox/pagesettings.cxx
index 825389d37ad0..34062acd893f 100644
--- a/sc/source/filter/oox/pagesettings.cxx
+++ b/sc/source/filter/oox/pagesettings.cxx
@@ -939,9 +939,8 @@ void PageSettingsConverter::writePageSettingsProperties(
 
         if( 0 < rModel.mnPaperSize )
         {
-            const msfilter::util::ApiPaperSize& rPaperSize = 
msfilter::util::PaperSizeConv::getApiSizeForMSPaperSizeIndex(  
rModel.mnPaperSize );
-            aSize = awt::Size( rPaperSize.mnWidth, rPaperSize.mnHeight );
-            bValid = ( rPaperSize.mnWidth != 0 && rPaperSize.mnHeight != 0 );
+            aSize = 
msfilter::util::PaperSizeConv::getApiSizeForMSPaperSizeIndex(rModel.mnPaperSize);
+            bValid = (aSize.Width != 0 && aSize.Height != 0);
         }
         if( rModel.mnPaperWidth > 0 && rModel.mnPaperHeight > 0 )
         {
diff --git a/sc/source/ui/vba/vbapagesetup.cxx 
b/sc/source/ui/vba/vbapagesetup.cxx
index 009570e32dca..94430e9da6a5 100644
--- a/sc/source/ui/vba/vbapagesetup.cxx
+++ b/sc/source/ui/vba/vbapagesetup.cxx
@@ -606,10 +606,7 @@ void SAL_CALL ScVbaPageSetup::setPaperSize( sal_Int32 
papersize )
 {
     if ( papersize != excel::XlPaperSize::xlPaperUser )
     {
-        awt::Size aPaperSize;
-        const msfilter::util::ApiPaperSize& rConvertedSize = 
msfilter::util::PaperSizeConv::getApiSizeForMSPaperSizeIndex( papersize );
-        aPaperSize.Height = rConvertedSize.mnHeight;
-        aPaperSize.Width = rConvertedSize.mnWidth;
+        awt::Size aPaperSize = 
msfilter::util::PaperSizeConv::getApiSizeForMSPaperSizeIndex( papersize );
         if ( mbIsLandscape )
             ::std::swap( aPaperSize.Width, aPaperSize.Height );
         mxPageProps->setPropertyValue( u"Size"_ustr, uno::Any( aPaperSize ) );

Reply via email to