sc/source/ui/inc/output.hxx   |    1 
 sc/source/ui/view/output.cxx  |    9 ----
 sc/source/ui/view/output2.cxx |   83 +++++++++++++++++++++++-------------------
 3 files changed, 47 insertions(+), 46 deletions(-)

New commits:
commit 8747e45b5f651d39682299a84af86aa41fef5be7
Author:     Tibor Nagy <tibor.nagy.ext...@allotropia.de>
AuthorDate: Thu May 22 02:45:36 2025 +0200
Commit:     Nagy Tibor <tibor.nagy.ext...@allotropia.de>
CommitDate: Thu May 22 12:52:17 2025 +0200

    tdf#166233 PDF: Optimize Tagged PDF export to reduce file size in Calc
    
    Let’s avoid exporting empty cells into the tagged PDF, because even if
    a cell doesn’t contain any value, it still gets 3 tags.(<TR>,<TD>,<P>)
    These unnecessary tags significantly increase the size of the tagged PDF
    
    Change-Id: I9302f6daebb03c1502dded5835e07a8953279aea
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/185636
    Reviewed-by: Nagy Tibor <tibor.nagy.ext...@allotropia.de>
    Tested-by: Jenkins

diff --git a/sc/source/ui/inc/output.hxx b/sc/source/ui/inc/output.hxx
index 3c6ebf6cf765..22527d3addc7 100644
--- a/sc/source/ui/inc/output.hxx
+++ b/sc/source/ui/inc/output.hxx
@@ -68,7 +68,6 @@ typedef std::map<std::pair<SCROW, SCCOL>, sal_Int32> 
TableDataIdMap;
 struct ScEnhancedPDFState
 {
     sal_Int32 m_WorksheetId = -1;
-    sal_Int32 m_TableId = -1;
     TableRowIdMap m_TableRowMap;
     TableDataIdMap m_TableDataMap;
     ScEnhancedPDFState(){};
diff --git a/sc/source/ui/view/output.cxx b/sc/source/ui/view/output.cxx
index e12aabaff1c6..062a3d71cdf9 100644
--- a/sc/source/ui/view/output.cxx
+++ b/sc/source/ui/view/output.cxx
@@ -313,15 +313,6 @@ bool 
ScOutputData::ReopenPDFStructureElement(vcl::pdf::StructElement aType, SCRO
                 bReopenTag = true;
             }
         }
-        else if (aType == vcl::pdf::StructElement::Table)
-        {
-            if (pPDF->GetScPDFState()->m_TableId != -1)
-            {
-                sal_Int32 nId = pPDF->GetScPDFState()->m_TableId;
-                pPDF->BeginStructureElement(nId);
-                bReopenTag = true;
-            }
-        }
         else if (aType == vcl::pdf::StructElement::TableRow)
         {
             const auto aIter = pPDF->GetScPDFState()->m_TableRowMap.find(nRow);
diff --git a/sc/source/ui/view/output2.cxx b/sc/source/ui/view/output2.cxx
index 839e36e805b3..ea9501d0b86f 100644
--- a/sc/source/ui/view/output2.cxx
+++ b/sc/source/ui/view/output2.cxx
@@ -1493,14 +1493,8 @@ void ScOutputData::LayoutStrings(bool bPixelToLogic)
     bool bTaggedPDF = pPDF && pPDF->GetIsExportTaggedPDF();
     if (bTaggedPDF)
     {
-        bool bReopenTag = 
ReopenPDFStructureElement(vcl::pdf::StructElement::Table);
-        if (!bReopenTag)
-        {
-            sal_Int32 nId = pPDF->EnsureStructureElement(nullptr);
-            pPDF->InitStructureElement(nId, vcl::pdf::StructElement::Table, 
u"Table"_ustr);
-            pPDF->BeginStructureElement(nId);
-            pPDF->GetScPDFState()->m_TableId = nId;
-        }
+        pPDF->WrapBeginStructureElement(vcl::pdf::StructElement::Table, 
u"Table"_ustr);
+        pPDF->GetScPDFState()->m_TableRowMap.clear();
     }
 
     bool bOrigIsInLayoutStrings = mpDoc->IsInLayoutStrings();
@@ -1565,31 +1559,12 @@ void ScOutputData::LayoutStrings(bool bPixelToLogic)
         SCROW nY = pThisRowInfo->nRowNo;
         if (pThisRowInfo->bChanged)
         {
-            if (bTaggedPDF)
-            {
-                bool bReopenTag = false;
-                if (nLoopStartX != 0)
-                {
-                    bReopenTag
-                        = 
ReopenPDFStructureElement(vcl::pdf::StructElement::TableRow, nY);
-                }
-                if (!bReopenTag)
-                {
-                    sal_Int32 nId = pPDF->EnsureStructureElement(nullptr);
-                    pPDF->InitStructureElement(nId, 
vcl::pdf::StructElement::TableRow, u"TR"_ustr);
-                    pPDF->BeginStructureElement(nId);
-                    pPDF->GetScPDFState()->m_TableRowMap.emplace(nY, nId);
-                }
-            }
-
+            bool bReopenRowTag = false;
             tools::Long nPosX = nInitPosX;
             if ( nLoopStartX < nX1 )
                 nPosX -= pRowInfo[0].basicCellInfo(nLoopStartX).nWidth * 
nLayoutSign;
             for (SCCOL nX=nLoopStartX; nX<=nX2; nX++)
             {
-                if (bTaggedPDF)
-                    
pPDF->WrapBeginStructureElement(vcl::pdf::StructElement::TableData, u"TD"_ustr);
-
                 bool bMergeEmpty = false;
                 const ScCellInfo* pInfo = &pThisRowInfo->cellInfo(nX);
                 bool bEmpty = nX < nX1 || 
pThisRowInfo->basicCellInfo(nX).bEmptyCellText;
@@ -1922,8 +1897,26 @@ void ScOutputData::LayoutStrings(bool bPixelToLogic)
                     // Mark the tagged "TD" structure element to be drawn in 
DrawEdit
                     if (bTaggedPDF)
                     {
+                        if (bReopenRowTag)
+                            
ReopenPDFStructureElement(vcl::pdf::StructElement::TableRow, nY);
+                        else
+                        {
+                            sal_Int32 nId = 
pPDF->EnsureStructureElement(nullptr);
+                            pPDF->InitStructureElement(nId, 
vcl::pdf::StructElement::TableRow,
+                                                       u"TR"_ustr);
+                            pPDF->BeginStructureElement(nId);
+                            pPDF->GetScPDFState()->m_TableRowMap.emplace(nY, 
nId);
+                            bReopenRowTag = true;
+                        }
+
+                        
pPDF->WrapBeginStructureElement(vcl::pdf::StructElement::TableData,
+                                                        u"TD"_ustr);
+
                         sal_Int32 nId = pPDF->GetCurrentStructureElement();
-                        pPDF->GetScPDFState()->m_TableDataMap[{nY, nX}] = nId;
+                        pPDF->GetScPDFState()->m_TableDataMap[{ nY, nX }] = 
nId;
+
+                        pPDF->EndStructureElement(); // TableData
+                        pPDF->EndStructureElement(); // TableRow
                     }
                 }
                 if ( bDoCell )
@@ -2134,7 +2127,25 @@ void ScOutputData::LayoutStrings(bool bPixelToLogic)
                         if (!aString.isEmpty())
                         {
                             if (bTaggedPDF)
-                                
pPDF->WrapBeginStructureElement(vcl::pdf::StructElement::Paragraph, u"P"_ustr);
+                            {
+                                if (bReopenRowTag)
+                                    
ReopenPDFStructureElement(vcl::pdf::StructElement::TableRow,
+                                                              nY);
+                                else
+                                {
+                                    sal_Int32 nId = 
pPDF->EnsureStructureElement(nullptr);
+                                    pPDF->InitStructureElement(
+                                        nId, 
vcl::pdf::StructElement::TableRow, u"TR"_ustr);
+                                    pPDF->BeginStructureElement(nId);
+                                    
pPDF->GetScPDFState()->m_TableRowMap.emplace(nY, nId);
+                                    bReopenRowTag = true;
+                                }
+
+                                
pPDF->WrapBeginStructureElement(vcl::pdf::StructElement::TableData,
+                                                                u"TD"_ustr);
+                                
pPDF->WrapBeginStructureElement(vcl::pdf::StructElement::Paragraph,
+                                                                u"P"_ustr);
+                            }
 
                             // If the string is clipped, make it shorter for
                             // better performance since drawing by HarfBuzz is
@@ -2203,7 +2214,11 @@ void ScOutputData::LayoutStrings(bool bPixelToLogic)
                                     aVars.GetLayoutGlyphs(aShort));
                             }
                             if (bTaggedPDF)
-                                pPDF->EndStructureElement();
+                            {
+                                pPDF->EndStructureElement(); // Paragraph
+                                pPDF->EndStructureElement(); // TableData
+                                pPDF->EndStructureElement(); // TableRow
+                            }
                         }
 
                         if ( bHClip || bVClip )
@@ -2224,16 +2239,12 @@ void ScOutputData::LayoutStrings(bool bPixelToLogic)
                     }
                 }
                 nPosX += pRowInfo[0].basicCellInfo(nX).nWidth * nLayoutSign;
-                if (bTaggedPDF)
-                    pPDF->EndStructureElement();
             }
-            if (bTaggedPDF)
-                pPDF->EndStructureElement();
         }
         nPosY += pRowInfo[nArrY].nHeight;
     }
     if (bTaggedPDF)
-        pPDF->EndStructureElement();
+        pPDF->EndStructureElement(); // Table
 
     if ( bProgress )
         ScProgress::DeleteInterpretProgress();

Reply via email to