sw/qa/extras/htmlexport/htmlexport.cxx  |   34 +++++++++++++++++++++
 sw/source/filter/html/htmlflywriter.cxx |   50 ++++++++++++++++++++++++++++----
 2 files changed, 78 insertions(+), 6 deletions(-)

New commits:
commit 961c0da0503c94f5a86c962acd4932ffa8967db1
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Tue May 17 16:11:25 2022 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Mon May 23 10:58:13 2022 +0200

    sw HTML export: avoid pixel height when height is scale and width is 
relative
    
    Commit b17180a84cb4561b8a7bbf9e2281c91fffd56f87 (write out image size in
    html export for 'keep ratio' images, 2021-06-29) changed the sw HTML
    export to write the layout size of images in case one dimension is "keep
    ratio" and the other is some more concrete value.
    
    This is useful in case that other dimension is a fixed value, because
    "keep ratio" on the UI only means to keep the ratio as the size changes,
    it does not mean that the ratio will be the original ratio of the
    bitmap. However, it's problematic to write this layout size of the "keep
    ratio" dimension when the other dimension is relative, as this will mean
    the image's aspect ratio will change if the user resizes the browser
    window.
    
    Fix the problem by extending the way we write the "height" and "width"
    of fly frames:
    
    1) Write a percentage in case of relative sizes
    
    2) Write an explicit "auto" (or just omit the attribute in XHTML mode)
       in case the size is "keep ratio" and the other dimension is a
       relative size
    
    3) Write the layout size in other cases (fixed size or "keep ratio", but
       the other dimension is a fixed size)
    
    Note that HTML itself has no concept of relative sizes where 100% is not
    the parent's size (e.g. page, not paragraph) and also has no concept of
    keeping an aspect ratio which is not the aspect ratio of the bitmap, so
    those cases remain unchanged.
    
    Change-Id: Ic5c7dc4d697160eff81e960a2f7d335fb78ab7c0
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134482
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins
    (cherry picked from commit 9e3eee88338c45424b24040f731083f9f59cfbe2)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134450
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>
    (cherry picked from commit 4bced2b78b3e22730c12d9729f5633ae1a3a35c8)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134754
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>

diff --git a/sw/qa/extras/htmlexport/htmlexport.cxx 
b/sw/qa/extras/htmlexport/htmlexport.cxx
index 3ee58613903a..997537012bb8 100644
--- a/sw/qa/extras/htmlexport/htmlexport.cxx
+++ b/sw/qa/extras/htmlexport/htmlexport.cxx
@@ -2200,6 +2200,40 @@ CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, 
testTableBackground)
     assertXPathNoAttribute(pXmlDoc, 
"//reqif-xhtml:table[2]/reqif-xhtml:tr[1]", "bgcolor");
 }
 
+CPPUNIT_TEST_FIXTURE(HtmlExportTest, testImageKeepRatio)
+{
+    // Given a document with an image: width is relative, height is "keep 
ratio":
+    createSwDoc();
+    uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<beans::XPropertySet> xTextGraphic(
+        xFactory->createInstance("com.sun.star.text.TextGraphicObject"), 
uno::UNO_QUERY);
+    xTextGraphic->setPropertyValue("AnchorType",
+                                   
uno::Any(text::TextContentAnchorType_AS_CHARACTER));
+    xTextGraphic->setPropertyValue("RelativeWidth", 
uno::Any(static_cast<sal_Int16>(42)));
+    xTextGraphic->setPropertyValue("IsSyncHeightToWidth", uno::Any(true));
+    uno::Reference<text::XTextDocument> xTextDocument(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<text::XText> xBodyText = xTextDocument->getText();
+    uno::Reference<text::XTextCursor> xCursor(xBodyText->createTextCursor());
+    uno::Reference<text::XTextContent> xTextContent(xTextGraphic, 
uno::UNO_QUERY);
+    xBodyText->insertTextContent(xCursor, xTextContent, false);
+
+    // When exporting to HTML:
+    uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+    uno::Sequence<beans::PropertyValue> aStoreProperties = {
+        comphelper::makePropertyValue("FilterName", OUString("HTML 
(StarWriter)")),
+    };
+    xStorable->storeToURL(maTempFile.GetURL(), aStoreProperties);
+
+    // Then make sure that the width is not a fixed size, that would break on 
resizing the browser
+    // window:
+    htmlDocUniquePtr pDoc = parseHtml(maTempFile);
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: auto
+    // - Actual  : 2
+    // i.e. a static (CSS pixel) height was written.
+    assertXPath(pDoc, "/html/body/p/img", "height", "auto");
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/filter/html/htmlflywriter.cxx 
b/sw/source/filter/html/htmlflywriter.cxx
index 2aee659fc6e8..5eff1441760f 100644
--- a/sw/source/filter/html/htmlflywriter.cxx
+++ b/sw/source/filter/html/htmlflywriter.cxx
@@ -987,22 +987,60 @@ void SwHTMLWriter::writeFrameFormatOptions(HtmlWriter& 
aHtml, const SwFrameForma
             ((nPercentWidth && nPercentWidth!=255) || aPixelSz.Width()) )
         {
             OString sWidth;
-            if (nPercentWidth && nPercentWidth != 255)
-                sWidth = 
OString::number(static_cast<sal_Int32>(nPercentWidth)) + "%";
+            if (nPercentWidth)
+            {
+                if (nPercentWidth == 255)
+                {
+                    if (nPercentHeight)
+                    {
+                        sWidth = "auto";
+                    }
+                    else
+                    {
+                        sWidth = 
OString::number(static_cast<sal_Int32>(aPixelSz.Width()));
+                    }
+                }
+                else
+                {
+                    sWidth = 
OString::number(static_cast<sal_Int32>(nPercentWidth)) + "%";
+                }
+            }
             else
                 sWidth = 
OString::number(static_cast<sal_Int32>(aPixelSz.Width()));
-            aHtml.attribute(OOO_STRING_SVTOOLS_HTML_O_width, sWidth);
+            if (!mbXHTML || sWidth != "auto")
+            {
+                aHtml.attribute(OOO_STRING_SVTOOLS_HTML_O_width, sWidth);
+            }
         }
 
         if( (nFrameOptions & HtmlFrmOpts::Height) &&
             ((nPercentHeight && nPercentHeight!=255) || aPixelSz.Height()) )
         {
             OString sHeight;
-            if (nPercentHeight && nPercentHeight != 255)
-                sHeight = 
OString::number(static_cast<sal_Int32>(nPercentHeight)) + "%";
+            if (nPercentHeight)
+            {
+                if (nPercentHeight == 255)
+                {
+                    if (nPercentWidth)
+                    {
+                        sHeight = "auto";
+                    }
+                    else
+                    {
+                        sHeight = 
OString::number(static_cast<sal_Int32>(aPixelSz.Height()));
+                    }
+                }
+                else
+                {
+                    sHeight = 
OString::number(static_cast<sal_Int32>(nPercentHeight)) + "%";
+                }
+            }
             else
                 sHeight = 
OString::number(static_cast<sal_Int32>(aPixelSz.Height()));
-            aHtml.attribute(OOO_STRING_SVTOOLS_HTML_O_height, sHeight);
+            if (!mbXHTML || sHeight != "auto")
+            {
+                aHtml.attribute(OOO_STRING_SVTOOLS_HTML_O_height, sHeight);
+            }
         }
     }
 

Reply via email to