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 6171ccd8e6a4ee668fa526f8351e1b4e4fa0a0e2
Author:     Mike Kaganski <[email protected]>
AuthorDate: Fri Feb 20 15:49:02 2026 +0500
Commit:     Mike Kaganski <[email protected]>
CommitDate: Fri Feb 20 14:39:44 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

diff --git a/i18nutil/source/utility/paper.cxx 
b/i18nutil/source/utility/paper.cxx
index 792b04fb3b4d..80fd4633c93e 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 a5c6f9f64328..0cd6103e5360 100644
--- a/vcl/source/gdi/print.cxx
+++ b/vcl/source/gdi/print.cxx
@@ -62,10 +62,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();
     }
 
@@ -1210,15 +1210,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 &&
@@ -1236,9 +1237,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 9520e33ba133..bb6e3a068f8b 100644
--- a/vcl/win/gdi/salprn.cxx
+++ b/vcl/win/gdi/salprn.cxx
@@ -912,7 +912,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( pPaperSizes );

Reply via email to