sw/source/filter/md/mdcallbcks.cxx |   19 +++++++++++++----
 sw/source/filter/md/swmd.cxx       |   40 ++++++++++++++++++++-----------------
 sw/source/filter/md/swmd.hxx       |   31 ++++++++++++++++++++++++----
 3 files changed, 64 insertions(+), 26 deletions(-)

New commits:
commit 20710fd58c441c928fcbb32ee0d58b92ac2974a5
Author:     Ujjawal Kumar <[email protected]>
AuthorDate: Mon Sep 15 14:00:08 2025 +0530
Commit:     Thorsten Behrens <[email protected]>
CommitDate: Wed Sep 17 03:45:55 2025 +0200

    tdf#162153 Refactor: Improved Image Handling
    
    Change-Id: If7f56cea743bbb250c7e0318d86dbc6a555b3056
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190953
    Reviewed-by: Thorsten Behrens <[email protected]>
    Tested-by: Jenkins

diff --git a/sw/source/filter/md/mdcallbcks.cxx 
b/sw/source/filter/md/mdcallbcks.cxx
index 655f95bd344d..108f9844f2f2 100644
--- a/sw/source/filter/md/mdcallbcks.cxx
+++ b/sw/source/filter/md/mdcallbcks.cxx
@@ -190,7 +190,15 @@ int SwMarkdownParser::enter_span_callback(MD_SPANTYPE 
type, void* detail, void*
                 pINetFormat = dynamic_cast<const 
SwFormatINetFormat*>(pTopItem);
             }
 
-            parser->InsertImage(aURL, aTitle, pINetFormat);
+            MDImage& rImg = parser->m_aImg;
+            rImg.url = std::move(aURL);
+            rImg.title = std::move(aTitle);
+
+            if (pINetFormat)
+            {
+                rImg.link = pINetFormat->GetValue();
+            }
+
             parser->m_bInsideImage = true;
             break;
         }
@@ -235,6 +243,7 @@ int SwMarkdownParser::leave_span_callback(MD_SPANTYPE type, 
void* /*detail*/, vo
     switch (type)
     {
         case MD_SPAN_IMG:
+            parser->InsertCurImage();
             parser->m_bInsideImage = false;
             break;
         case MD_SPAN_EM:
@@ -267,11 +276,13 @@ int SwMarkdownParser::text_callback(MD_TEXTTYPE type, 
const MD_CHAR* text, MD_SI
         case MD_TEXT_CODE:
         case MD_TEXT_NORMAL:
         {
+            OUString aText = OStringToOUString({ text, size }, 
RTL_TEXTENCODING_UTF8);
+
             if (!parser->m_bInsideImage)
-            {
-                OUString aText = OStringToOUString({ text, size }, 
RTL_TEXTENCODING_UTF8);
                 parser->InsertText(aText);
-            }
+            else
+                parser->m_aImg.altText = std::move(aText);
+
             break;
         }
         case MD_TEXT_BR:
diff --git a/sw/source/filter/md/swmd.cxx b/sw/source/filter/md/swmd.cxx
index 1b46ae46494b..82f96647f686 100644
--- a/sw/source/filter/md/swmd.cxx
+++ b/sw/source/filter/md/swmd.cxx
@@ -593,10 +593,9 @@ void SwMarkdownParser::SetAttrs(SwPaM& rRange)
 
 void SwMarkdownParser::ClearAttrs() { m_xDoc->ResetAttrs(*m_pPam, true); }
 
-void SwMarkdownParser::InsertImage(const OUString& aURL, const OUString& 
rTitle,
-                                   const SwFormatINetFormat* pINetFormat)
+void SwMarkdownParser::InsertImage(const MDImage& rImg)
 {
-    OUString sGrfNm = INetURLObject::GetAbsURL(m_sBaseURL, aURL);
+    OUString sGrfNm = INetURLObject::GetAbsURL(m_sBaseURL, rImg.url);
 
     Graphic aGraphic;
     INetURLObject aGraphicURL(sGrfNm);
@@ -615,26 +614,30 @@ void SwMarkdownParser::InsertImage(const OUString& aURL, 
const OUString& rTitle,
                 = o3tl::convert(aDescriptor.GetSizePixel(), o3tl::Length::px, 
o3tl::Length::twip);
     }
 
-    tools::Long nWidth = aGrfSz.getWidth();
-    tools::Long nHeight = aGrfSz.getHeight();
-
-    if (nWidth > 0 && nHeight > 0)
+    tools::Long nWidth = 0;
+    tools::Long nHeight = 0;
+    if (!aGrfSz.IsEmpty())
     {
+        nWidth = aGrfSz.getWidth();
+        nHeight = aGrfSz.getHeight();
         if (nWidth > MD_MAX_IMAGE_WIDTH_IN_TWIPS || nHeight > 
MD_MAX_IMAGE_HEIGHT_IN_TWIPS)
         {
             double fScaleX = static_cast<double>(MD_MAX_IMAGE_WIDTH_IN_TWIPS) 
/ nWidth;
             double fScaleY = static_cast<double>(MD_MAX_IMAGE_HEIGHT_IN_TWIPS) 
/ nHeight;
             double fScale = std::min(fScaleX, fScaleY);
-
             nWidth = static_cast<tools::Long>(nWidth * fScale);
             nHeight = static_cast<tools::Long>(nHeight * fScale);
         }
+        if (nWidth < MD_MIN_IMAGE_WIDTH_IN_TWIPS && nWidth < 
MD_MIN_IMAGE_HEIGHT_IN_TWIPS)
+        {
+            nWidth = MD_MIN_IMAGE_WIDTH_IN_TWIPS;
+            nHeight = MD_MIN_IMAGE_HEIGHT_IN_TWIPS;
+        }
     }
-
-    if (nWidth < MINFLY || nHeight < MINFLY)
+    else
     {
-        nWidth = MINFLY;
-        nHeight = MINFLY;
+        nWidth = MD_MIN_IMAGE_WIDTH_IN_TWIPS;
+        nHeight = MD_MIN_IMAGE_HEIGHT_IN_TWIPS;
     }
 
     SfxItemSet aFlySet(
@@ -646,11 +649,11 @@ void SwMarkdownParser::InsertImage(const OUString& aURL, 
const OUString& rTitle,
     aFlySet.Put(
         SwFormatVertOrient(0, text::VertOrientation::CHAR_CENTER, 
text::RelOrientation::CHAR));
 
-    if (pINetFormat)
+    if (!rImg.link.isEmpty())
     {
         // Have a link, set that on the image.
         SwFormatURL aFormatURL;
-        aFormatURL.SetURL(pINetFormat->GetValue(), /*bServerMap=*/false);
+        aFormatURL.SetURL(rImg.link, /*bServerMap=*/false);
         aFlySet.Put(aFormatURL);
     }
 
@@ -662,10 +665,11 @@ void SwMarkdownParser::InsertImage(const OUString& aURL, 
const OUString& rTitle,
     SwGrfNode* pGrfNd = 
m_xDoc->GetNodes()[pFlyFormat->GetContent().GetContentIdx()->GetIndex() + 1]
                             ->GetGrfNode();
 
-    if (pGrfNd && !rTitle.isEmpty())
-    {
-        pGrfNd->SetTitle(rTitle);
-    }
+    if (!rImg.title.isEmpty())
+        pFlyFormat->SetFormatName(UIName(rImg.title));
+
+    if (pGrfNd && !rImg.altText.isEmpty())
+        pGrfNd->SetTitle(rImg.altText);
 
     m_bNoParSpace = true;
 }
diff --git a/sw/source/filter/md/swmd.hxx b/sw/source/filter/md/swmd.hxx
index d4646ccb507c..fc57b95e8472 100644
--- a/sw/source/filter/md/swmd.hxx
+++ b/sw/source/filter/md/swmd.hxx
@@ -33,9 +33,27 @@
 
 class MDTable;
 
+struct MDImage
+{
+    OUString url;
+    OUString title;
+    OUString altText;
+    OUString link;
+
+    void Reset()
+    {
+        url.clear();
+        title.clear();
+        altText.clear();
+        link.clear();
+    }
+};
+
 constexpr tools::Long MD_PARSPACE = o3tl::toTwips(5, o3tl::Length::mm);
-constexpr tools::Long MD_MAX_IMAGE_WIDTH_IN_TWIPS = 2500;
-constexpr tools::Long MD_MAX_IMAGE_HEIGHT_IN_TWIPS = 2500;
+constexpr tools::Long MD_MAX_IMAGE_WIDTH_IN_TWIPS = 5000;
+constexpr tools::Long MD_MAX_IMAGE_HEIGHT_IN_TWIPS = 5000;
+constexpr tools::Long MD_MIN_IMAGE_WIDTH_IN_TWIPS = 500;
+constexpr tools::Long MD_MIN_IMAGE_HEIGHT_IN_TWIPS = 500;
 
 constexpr frozen::unordered_map<MD_ALIGN, SvxAdjust, 4> adjustMap
     = { { MD_ALIGN_DEFAULT, SvxAdjust::Left },
@@ -78,6 +96,7 @@ class SwMarkdownParser
     bool m_bNoParSpace = true;
     bool m_bInsideImage = false;
 
+    MDImage m_aImg;
     std::vector<MDTable*> m_aTables;
     std::shared_ptr<MDTable> m_xTable;
 
@@ -118,8 +137,12 @@ class SwMarkdownParser
     void SetAttrs(SwPaM& rRange);
     void ClearAttrs();
 
-    void InsertImage(const OUString& aURL, const OUString& rTitle,
-                     const SwFormatINetFormat* pINetFormat);
+    void InsertCurImage()
+    {
+        InsertImage(m_aImg);
+        m_aImg.Reset();
+    }
+    void InsertImage(const MDImage& rImg);
 
     void StartTable(sal_Int32 nRow, sal_Int32 nCol);
     void EndTable();

Reply via email to