i18nutil/source/utility/paper.cxx |   21 +++++++++++++++------
 include/i18nutil/paper.hxx        |    2 +-
 vcl/source/gdi/print.cxx          |   20 +++++++++++---------
 vcl/win/gdi/salprn.cxx            |    4 ++--
 4 files changed, 29 insertions(+), 18 deletions(-)

New commits:
commit 9e54023fe79f6dc800050f0d8f133606c7f3157d
Author:     Mike Kaganski <[email protected]>
AuthorDate: Fri Feb 20 15:49:02 2026 +0500
Commit:     Xisco Fauli <[email protected]>
CommitDate: Fri Feb 20 19:58:38 2026 +0100

    tdf#162488: do not assume printer drivers only providing portrait paper
    
    This assumption is false e.g. for Microsoft XPS Document Writer, where
    both portrait and landscape paper are in the list. The orientation and
    the size of the paper must be detected from the dimensions.
    
    Change-Id: Ibcfc68f64243ec144743a385f73586ca67f0cbe5
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/199824
    Reviewed-by: Mike Kaganski <[email protected]>
    Tested-by: Jenkins
    (cherry picked from commit 6171ccd8e6a4ee668fa526f8351e1b4e4fa0a0e2)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/199877
    Reviewed-by: Xisco Fauli <[email protected]>

diff --git a/i18nutil/source/utility/paper.cxx 
b/i18nutil/source/utility/paper.cxx
index f774b1998554..ddad0b26d202 100644
--- a/i18nutil/source/utility/paper.cxx
+++ b/i18nutil/source/utility/paper.cxx
@@ -376,20 +376,29 @@ PaperInfo::PaperInfo(Paper eType) : m_eType(eType)
     m_nPaperHeight = aDinTab[m_eType].m_nHeight;
 }
 
-PaperInfo::PaperInfo(tools::Long nPaperWidth, tools::Long nPaperHeight)
+PaperInfo::PaperInfo(tools::Long nPaperWidth, tools::Long nPaperHeight, bool 
bAlsoTryRotated)
     : m_eType(PAPER_USER),
       m_nPaperWidth(nPaperWidth),
       m_nPaperHeight(nPaperHeight)
 {
     for ( size_t i = 0; i < nTabSize; ++i )
     {
-        if (
-             (nPaperWidth == aDinTab[i].m_nWidth) &&
-             (nPaperHeight == aDinTab[i].m_nHeight)
-           )
+        if (nPaperWidth == aDinTab[i].m_nWidth && nPaperHeight == 
aDinTab[i].m_nHeight)
         {
             m_eType = static_cast<Paper>(i);
-            break;
+            return;
+        }
+    }
+    if (!bAlsoTryRotated)
+        return;
+    // Only check rotated dimensions after completing the normal pass, because 
there are pairs like
+    // Tabloid (11x17) / Ledger (17x11) in aDinTab
+    for ( size_t i = 0; i < nTabSize; ++i )
+    {
+        if (nPaperHeight == aDinTab[i].m_nWidth && nPaperWidth == 
aDinTab[i].m_nHeight)
+        {
+            m_eType = static_cast<Paper>(i);
+            return;
         }
     }
 }
diff --git a/include/i18nutil/paper.hxx b/include/i18nutil/paper.hxx
index c097727cfa7c..84be74be1ee3 100644
--- a/include/i18nutil/paper.hxx
+++ b/include/i18nutil/paper.hxx
@@ -136,7 +136,7 @@ class I18NUTIL_DLLPUBLIC PaperInfo
     tools::Long m_nPaperHeight; // height in 100thMM
 public:
     PaperInfo(Paper eType);
-    PaperInfo(tools::Long nPaperWidth, tools::Long nPaperHeight);
+    PaperInfo(tools::Long nPaperWidth, tools::Long nPaperHeight, bool 
bAlsoTryRotated = false);
 
     Paper getPaper() const { return m_eType; }
     tools::Long getWidth() const { return m_nPaperWidth; }
diff --git a/vcl/source/gdi/print.cxx b/vcl/source/gdi/print.cxx
index dec236fcc378..914d44f9c8bb 100644
--- a/vcl/source/gdi/print.cxx
+++ b/vcl/source/gdi/print.cxx
@@ -57,10 +57,10 @@ int nImplSysDialog = 0;
 
 namespace
 {
-    Paper ImplGetPaperFormat( tools::Long nWidth100thMM, tools::Long 
nHeight100thMM )
+    Paper ImplGetPaperFormat( tools::Long nWidth100thMM, tools::Long 
nHeight100thMM, bool bAlsoTryRotated = false )
     {
         PaperInfo aInfo(nWidth100thMM, nHeight100thMM);
-        aInfo.doSloppyFit();
+        aInfo.doSloppyFit(bAlsoTryRotated);
         return aInfo.getPaper();
     }
 
@@ -1221,15 +1221,16 @@ void Printer::ImplFindPaperFormatForUserSize( JobSetup& 
aJobSetup )
         if ( aInfo.sloppyEqual(rPaperInfo) )
         {
             rData.SetPaperFormat(
-                ImplGetPaperFormat( rPaperInfo.getWidth(),
-                    rPaperInfo.getHeight() ));
-            rData.SetOrientation( Orientation::Portrait );
+                ImplGetPaperFormat(rPaperInfo.getWidth(), 
rPaperInfo.getHeight(), true));
+            rData.SetOrientation(rPaperInfo.getWidth() <= 
rPaperInfo.getHeight()
+                                     ? Orientation::Portrait
+                                     : Orientation::Landscape);
             return;
         }
     }
 
     // If the printer supports landscape orientation, check paper sizes again
-    // with landscape orientation. This is necessary as a printer driver 
provides
+    // with landscape orientation. This is necessary as many printer drivers 
provide
     // all paper sizes with portrait orientation only!!
     if ( !(rData.GetPaperFormat() == PAPER_USER &&
          nLandscapeAngle != 0 &&
@@ -1247,9 +1248,10 @@ void Printer::ImplFindPaperFormatForUserSize( JobSetup& 
aJobSetup )
         if ( aRotatedInfo.sloppyEqual( rPaperInfo ) )
         {
             rData.SetPaperFormat(
-                ImplGetPaperFormat( rPaperInfo.getWidth(),
-                    rPaperInfo.getHeight() ));
-            rData.SetOrientation( Orientation::Landscape );
+                ImplGetPaperFormat(rPaperInfo.getWidth(), 
rPaperInfo.getHeight(), true));
+            rData.SetOrientation(rPaperInfo.getWidth() < rPaperInfo.getHeight()
+                                     ? Orientation::Landscape
+                                     : Orientation::Portrait);
             return;
         }
     }
diff --git a/vcl/win/gdi/salprn.cxx b/vcl/win/gdi/salprn.cxx
index 07c5666a46fe..519d62f861c7 100644
--- a/vcl/win/gdi/salprn.cxx
+++ b/vcl/win/gdi/salprn.cxx
@@ -910,7 +910,7 @@ static void ImplJobSetupToDevMode( WinSalInfoPrinter const 
* pPrinter, const Imp
                     }
 
                     // If the printer supports landscape orientation, check 
paper sizes again
-                    // with landscape orientation. This is necessary as a 
printer driver provides
+                    // with landscape orientation. This is necessary as many 
printer drivers provide
                     // all paper sizes with portrait orientation only!!
                     if ( !nPaper && nLandscapeAngle != 0 )
                     {
@@ -1105,7 +1105,7 @@ void WinSalInfoPrinter::InitPaperFormats( const 
ImplJobSetup* pSetupData )
 
         for( DWORD i = 0; i < nCount; ++i )
         {
-            PaperInfo aInfo(pPaperSizes[i].x * 10, pPaperSizes[i].y * 10);
+            PaperInfo aInfo(pPaperSizes[i].x * 10, pPaperSizes[i].y * 10, 
true);
             m_aPaperFormats.push_back( aInfo );
         }
         std::free( pNamesBuffer );

Reply via email to