sw/source/filter/md/mdcallbcks.cxx | 19 ++++++++++++--- sw/source/filter/md/swmd.cxx | 44 ++++++++++++++++++++----------------- sw/source/filter/md/swmd.hxx | 31 ++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 28 deletions(-)
New commits: commit 534d1935d21aeaaa82cf16fc6dea49e67521bd16 Author: Ujjawal Kumar <[email protected]> AuthorDate: Mon Sep 15 14:00:08 2025 +0530 Commit: Miklos Vajna <[email protected]> CommitDate: Wed Sep 17 11:31:57 2025 +0200 tdf#162153 Refactor: Improved Image Handling (cherry picked from commit 20710fd58c441c928fcbb32ee0d58b92ac2974a5) Change-Id: If7f56cea743bbb250c7e0318d86dbc6a555b3056 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191067 Tested-by: Jenkins CollaboraOffice <[email protected]> Reviewed-by: Miklos Vajna <[email protected]> diff --git a/sw/source/filter/md/mdcallbcks.cxx b/sw/source/filter/md/mdcallbcks.cxx index 77878e387975..dfc79b2b6be4 100644 --- a/sw/source/filter/md/mdcallbcks.cxx +++ b/sw/source/filter/md/mdcallbcks.cxx @@ -187,7 +187,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; } @@ -232,6 +240,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: @@ -263,11 +272,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 1528909cdb7f..e1a20a667d78 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(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 3aa54a6477c6..a8da96dbe485 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 }, @@ -77,6 +95,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; @@ -117,8 +136,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(); commit 4fb7a571ba69a7f82c2a92dc73dce8dcddc101ba Author: Andrea Gelmini <[email protected]> AuthorDate: Mon Jul 7 11:00:36 2025 +0200 Commit: Miklos Vajna <[email protected]> CommitDate: Wed Sep 17 11:31:43 2025 +0200 Fix typo in code (cherry picked from commit 2d054c92e11cc9f66a867c77b36a92a1b75c6d51) Change-Id: I70e6b4257a18e51a94972159754526fb1292bca4 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191066 Tested-by: Jenkins CollaboraOffice <[email protected]> Reviewed-by: Miklos Vajna <[email protected]> diff --git a/sw/source/filter/md/swmd.cxx b/sw/source/filter/md/swmd.cxx index 8b2c829989f9..1528909cdb7f 100644 --- a/sw/source/filter/md/swmd.cxx +++ b/sw/source/filter/md/swmd.cxx @@ -620,10 +620,10 @@ void SwMarkdownParser::InsertImage(const OUString& aURL, const OUString& rTitle, if (nWidth > 0 && nHeight > 0) { - if (nWidth > MD_MAX_IMAGE_WIDTH_IN_TWIPS || nHeight > MD_MAX_IMAGE_HEIGH_IN_TWIPS) + 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_HEIGH_IN_TWIPS) / nHeight; + 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); diff --git a/sw/source/filter/md/swmd.hxx b/sw/source/filter/md/swmd.hxx index a2930de1feae..3aa54a6477c6 100644 --- a/sw/source/filter/md/swmd.hxx +++ b/sw/source/filter/md/swmd.hxx @@ -35,7 +35,7 @@ class MDTable; 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_HEIGH_IN_TWIPS = 2500; +constexpr tools::Long MD_MAX_IMAGE_HEIGHT_IN_TWIPS = 2500; constexpr frozen::unordered_map<MD_ALIGN, SvxAdjust, 4> adjustMap = { { MD_ALIGN_DEFAULT, SvxAdjust::Left },
