drawinglayer/source/primitive2d/textlayoutdevice.cxx  |   58 ++++++++----
 drawinglayer/source/processor2d/vclprocessor2d.cxx    |   28 +++++
 include/drawinglayer/primitive2d/textlayoutdevice.hxx |    2 
 include/vcl/outdev.hxx                                |    7 +
 include/vcl/vcllayout.hxx                             |    5 -
 svgio/qa/cppunit/SvgImportTest.cxx                    |   85 +++++++++---------
 svgio/source/svgreader/svgcharacternode.cxx           |   28 +++--
 vcl/skia/gdiimpl.cxx                                  |    2 
 vcl/source/gdi/sallayout.cxx                          |   33 +++---
 vcl/source/outdev/map.cxx                             |    8 +
 vcl/source/outdev/text.cxx                            |   38 ++++++--
 vcl/win/gdi/DWriteTextRenderer.cxx                    |   11 +-
 12 files changed, 207 insertions(+), 98 deletions(-)

New commits:
commit 5a25899a9c5c0c4e6fb92ca355ea1e24cec3a747
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Tue Apr 16 10:48:06 2024 +0500
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Fri May 3 13:32:31 2024 +0200

    tdf#160702: improve text positioning
    
    Opening an SVG with text in different elements (e.g., tspans) in the
    same text element performs calculations to position the parts properly
    (i.e., the next part will start where the previous part ended, unless
    the position in overridden explicitly). These calculations require to
    know the text widths. The first problem leas here: the text width was
    calculated for a typically small text size (numerically equal to the
    pixel size defined in the SVG), but these calculations aren't truly
    linear, because font rendering may change depending on font height.
    Additionally, the rounding gives much higher error in smaller sizes
    than in larger. There was already a workaround for a similar problem
    in ViewRedirector::createRedirectedPrimitive2DSequence, where a large
    font (with 100 times greater height) was used to increase correctness.
    This was also used here, with the same large height (50000) used as a
    reference.
    
    Then, at the time of wrawing the text at different zoom levels, the
    code in VclProcessor2D::RenderTextSimpleOrDecoratedPortionPrimitive2D
    creates a font of a calculated size, and uses it to output the text.
    But the font is always created with an integral height, which means,
    that for a wanted height of 2.5 (in a zoomed out view), the really
    used height will be 3, which is 20% larger; or for wanted height of
    2.4, the actual height will be 2 (20% smaller). This resulted in odd
    jumps of the text widths, when the text may overlap the following
    part, or conversely, create a big gap before the next gap. To try to
    mitigate that, the function now takes the difference between the
    wanted and the actual font size into account, and adjusts the MapMode
    accordingly. This doesn't fix the jumping completely (e.g., because
    of the mentioned special handling of small font sizes in the fonts
    thenselves, like hinting), but still makes the calculations much more
    stable, decreasing the amount of jumping. Similar changes are made in
    TextLayouterDevice.
    
    Use of the functions that return text size as a double, not rounded
    value, should additionally help improving stability.
    
    Change-Id: I455845d8ca43ee9c06a0fc980947f35d8a25797a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166238
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166468

diff --git a/drawinglayer/source/primitive2d/textlayoutdevice.cxx 
b/drawinglayer/source/primitive2d/textlayoutdevice.cxx
index 5145d84ed2b6..afc0663aa4b0 100644
--- a/drawinglayer/source/primitive2d/textlayoutdevice.cxx
+++ b/drawinglayer/source/primitive2d/textlayoutdevice.cxx
@@ -22,6 +22,8 @@
 #include <algorithm>
 
 #include <com/sun/star/uno/XComponentContext.hpp>
+
+#include <basegfx/matrix/b2dhommatrixtools.hxx>
 #include <drawinglayer/attribute/fontattribute.hxx>
 #include <drawinglayer/primitive2d/textlayoutdevice.hxx>
 #include <comphelper/processfactory.hxx>
@@ -162,56 +164,69 @@ TextLayouterDevice::TextLayouterDevice()
 
 TextLayouterDevice::~TextLayouterDevice() COVERITY_NOEXCEPT_FALSE { 
releaseGlobalVirtualDevice(); }
 
-void TextLayouterDevice::setFont(const vcl::Font& rFont) { 
mrDevice.SetFont(rFont); }
+void TextLayouterDevice::setFont(const vcl::Font& rFont)
+{
+    mrDevice.SetFont(rFont);
+    mnFontScalingFixX = 1.0;
+    mnFontScalingFixY = 1.0;
+}
 
 void TextLayouterDevice::setFontAttribute(const attribute::FontAttribute& 
rFontAttribute,
                                           double fFontScaleX, double 
fFontScaleY,
                                           const css::lang::Locale& rLocale)
 {
-    setFont(getVclFontFromFontAttribute(rFontAttribute, fFontScaleX, 
fFontScaleY, 0.0, rLocale));
+    vcl::Font aFont
+        = getVclFontFromFontAttribute(rFontAttribute, fFontScaleX, 
fFontScaleY, 0.0, rLocale);
+    setFont(aFont);
+    Size aFontSize = aFont.GetFontSize();
+    mnFontScalingFixY = fFontScaleY / aFontSize.Height();
+    mnFontScalingFixX = fFontScaleX / (aFontSize.Width() ? aFontSize.Width() : 
aFontSize.Height());
 }
 
 double TextLayouterDevice::getOverlineOffset() const
 {
     const ::FontMetric& rMetric = mrDevice.GetFontMetric();
     double fRet = (rMetric.GetInternalLeading() / 2.0) - rMetric.GetAscent();
-    return fRet;
+    return fRet * mnFontScalingFixY;
 }
 
 double TextLayouterDevice::getUnderlineOffset() const
 {
     const ::FontMetric& rMetric = mrDevice.GetFontMetric();
     double fRet = rMetric.GetDescent() / 2.0;
-    return fRet;
+    return fRet * mnFontScalingFixY;
 }
 
 double TextLayouterDevice::getStrikeoutOffset() const
 {
     const ::FontMetric& rMetric = mrDevice.GetFontMetric();
     double fRet = (rMetric.GetAscent() - rMetric.GetInternalLeading()) / 3.0;
-    return fRet;
+    return fRet * mnFontScalingFixY;
 }
 
 double TextLayouterDevice::getOverlineHeight() const
 {
     const ::FontMetric& rMetric = mrDevice.GetFontMetric();
     double fRet = rMetric.GetInternalLeading() / 2.5;
-    return fRet;
+    return fRet * mnFontScalingFixY;
 }
 
 double TextLayouterDevice::getUnderlineHeight() const
 {
     const ::FontMetric& rMetric = mrDevice.GetFontMetric();
     double fRet = rMetric.GetDescent() / 4.0;
-    return fRet;
+    return fRet * mnFontScalingFixY;
 }
 
-double TextLayouterDevice::getTextHeight() const { return 
mrDevice.GetTextHeight(); }
+double TextLayouterDevice::getTextHeight() const
+{
+    return mrDevice.GetTextHeight() * mnFontScalingFixY;
+}
 
 double TextLayouterDevice::getTextWidth(const OUString& rText, sal_uInt32 
nIndex,
                                         sal_uInt32 nLength) const
 {
-    return mrDevice.GetTextWidth(rText, nIndex, nLength);
+    return mrDevice.GetTextWidth(rText, nIndex, nLength) * mnFontScalingFixX;
 }
 
 void TextLayouterDevice::getTextOutlines(basegfx::B2DPolyPolygonVector& 
rB2DPolyPolyVector,
@@ -245,6 +260,13 @@ void 
TextLayouterDevice::getTextOutlines(basegfx::B2DPolyPolygonVector& rB2DPoly
     {
         mrDevice.GetTextOutlines(rB2DPolyPolyVector, rText, nIndex, nIndex, 
nLength);
     }
+    if (!rtl_math_approxEqual(mnFontScalingFixY, 1.0)
+        || !rtl_math_approxEqual(mnFontScalingFixX, 1.0))
+    {
+        auto scale = 
basegfx::utils::createScaleB2DHomMatrix(mnFontScalingFixX, mnFontScalingFixY);
+        for (auto& poly : rB2DPolyPolyVector)
+            poly.transform(scale);
+    }
 }
 
 basegfx::B2DRange TextLayouterDevice::getTextBoundRect(const OUString& rText, 
sal_uInt32 nIndex,
@@ -262,6 +284,12 @@ basegfx::B2DRange 
TextLayouterDevice::getTextBoundRect(const OUString& rText, sa
     {
         basegfx::B2DRange aRect;
         mrDevice.GetTextBoundRect(aRect, rText, nIndex, nIndex, nLength);
+        if (!rtl_math_approxEqual(mnFontScalingFixY, 1.0)
+            || !rtl_math_approxEqual(mnFontScalingFixX, 1.0))
+        {
+            aRect.transform(
+                basegfx::utils::createScaleB2DHomMatrix(mnFontScalingFixX, 
mnFontScalingFixY));
+        }
         return aRect;
     }
 
@@ -271,13 +299,13 @@ basegfx::B2DRange 
TextLayouterDevice::getTextBoundRect(const OUString& rText, sa
 double TextLayouterDevice::getFontAscent() const
 {
     const ::FontMetric& rMetric = mrDevice.GetFontMetric();
-    return rMetric.GetAscent();
+    return rMetric.GetAscent() * mnFontScalingFixY;
 }
 
 double TextLayouterDevice::getFontDescent() const
 {
     const ::FontMetric& rMetric = mrDevice.GetFontMetric();
-    return rMetric.GetDescent();
+    return rMetric.GetDescent() * mnFontScalingFixY;
 }
 
 void TextLayouterDevice::addTextRectActions(const ::tools::Rectangle& 
rRectangle,
@@ -305,7 +333,7 @@ std::vector<double> TextLayouterDevice::getTextArray(const 
OUString& rText, sal_
         mrDevice.GetTextArray(rText, &aArray, nIndex, nTextLength, bCaret);
         aRetval.reserve(aArray.size());
         for (size_t i = 0, nEnd = aArray.size(); i < nEnd; ++i)
-            aRetval.push_back(aArray[i]);
+            aRetval.push_back(aArray[i] * mnFontScalingFixX);
     }
 
     return aRetval;
diff --git a/drawinglayer/source/processor2d/vclprocessor2d.cxx 
b/drawinglayer/source/processor2d/vclprocessor2d.cxx
index 2c3521ace0dd..a1c23c91dbc9 100644
--- a/drawinglayer/source/processor2d/vclprocessor2d.cxx
+++ b/drawinglayer/source/processor2d/vclprocessor2d.cxx
@@ -182,7 +182,8 @@ void 
VclProcessor2D::RenderTextSimpleOrDecoratedPortionPrimitive2D(
             }
 
             // Don't draw fonts without height
-            if (aFont.GetFontHeight() <= 0)
+            Size aResultFontSize = aFont.GetFontSize();
+            if (aResultFontSize.Height() <= 0)
                 return;
 
             // set FillColor Attribute
@@ -408,7 +409,30 @@ void 
VclProcessor2D::RenderTextSimpleOrDecoratedPortionPrimitive2D(
             else
             {
                 const basegfx::B2DPoint aPoint(aLocalTransform * 
basegfx::B2DPoint(0.0, 0.0));
-                aStartPoint = Point(basegfx::fround(aPoint.getX()), 
basegfx::fround(aPoint.getY()));
+                double aPointX = aPoint.getX(), aPointY = aPoint.getY();
+
+                // aFont has an integer size; we must scale a bit for precision
+                double nFontScalingFixY = aFontScaling.getY() / 
aResultFontSize.Height();
+                double nFontScalingFixX = aFontScaling.getX()
+                                          / (aResultFontSize.Width() ? 
aResultFontSize.Width()
+                                                                     : 
aResultFontSize.Height());
+
+                if (!rtl_math_approxEqual(nFontScalingFixY, 1.0)
+                    || !rtl_math_approxEqual(nFontScalingFixX, 1.0))
+                {
+                    MapMode aMapMode = mpOutputDevice->GetMapMode();
+                    aMapMode.SetScaleX(aMapMode.GetScaleX() * 
nFontScalingFixX);
+                    aMapMode.SetScaleY(aMapMode.GetScaleY() * 
nFontScalingFixY);
+
+                    mpOutputDevice->Push(vcl::PushFlags::MAPMODE);
+                    mpOutputDevice->SetRelativeMapMode(aMapMode);
+                    bChangeMapMode = true;
+
+                    aPointX /= nFontScalingFixX;
+                    aPointY /= nFontScalingFixY;
+                }
+
+                aStartPoint = Point(basegfx::fround(aPointX), 
basegfx::fround(aPointY));
             }
 
             // tdf#152990 set the font after the MapMode is (potentially) set 
so canvas uses the desired
diff --git a/include/drawinglayer/primitive2d/textlayoutdevice.hxx 
b/include/drawinglayer/primitive2d/textlayoutdevice.hxx
index 6f98b50a4f06..db019494a8c0 100644
--- a/include/drawinglayer/primitive2d/textlayoutdevice.hxx
+++ b/include/drawinglayer/primitive2d/textlayoutdevice.hxx
@@ -63,6 +63,8 @@ class DRAWINGLAYER_DLLPUBLIC TextLayouterDevice
     /// internally used VirtualDevice
     SolarMutexGuard maSolarGuard;
     VirtualDevice& mrDevice;
+    double mnFontScalingFixX = 1.0;
+    double mnFontScalingFixY = 1.0;
 
 public:
     /// constructor/destructor
diff --git a/svgio/qa/cppunit/SvgImportTest.cxx 
b/svgio/qa/cppunit/SvgImportTest.cxx
index 43910078e59f..ed3fa02e1c22 100644
--- a/svgio/qa/cppunit/SvgImportTest.cxx
+++ b/svgio/qa/cppunit/SvgImportTest.cxx
@@ -905,12 +905,12 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf93583)
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"width"_ostr, "16");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"height"_ostr, "16");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"text"_ostr, " first");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"x"_ostr, "127");
+    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"x"_ostr, "128");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"y"_ostr, "303");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"width"_ostr, "32");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"height"_ostr, "32");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"text"_ostr, " line");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"x"_ostr, "187");
+    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"x"_ostr, "188");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"y"_ostr, "303");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"width"_ostr, "16");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"height"_ostr, "16");
@@ -930,7 +930,7 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156616)
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"x"_ostr, "114");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"y"_ostr, "103");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"text"_ostr, " line");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"x"_ostr, "142");
+    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"x"_ostr, "143");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"y"_ostr, "103");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"text"_ostr, "Second line");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"x"_ostr, "114");
@@ -939,7 +939,7 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156616)
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[4]"_ostr, 
"x"_ostr, "86");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[4]"_ostr, 
"y"_ostr, "153");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[5]"_ostr, 
"text"_ostr, " line");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[5]"_ostr, 
"x"_ostr, "114");
+    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[5]"_ostr, 
"x"_ostr, "115");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[5]"_ostr, 
"y"_ostr, "153");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[6]"_ostr, 
"text"_ostr, "Second line");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[6]"_ostr, 
"x"_ostr, "77");
@@ -948,7 +948,7 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156616)
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[7]"_ostr, 
"x"_ostr, "59");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[7]"_ostr, 
"y"_ostr, "203");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[8]"_ostr, 
"text"_ostr, " line");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[8]"_ostr, 
"x"_ostr, "87");
+    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[8]"_ostr, 
"x"_ostr, "88");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[8]"_ostr, 
"y"_ostr, "203");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[9]"_ostr, 
"text"_ostr, "Second line");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[9]"_ostr, 
"x"_ostr, "40");
@@ -1729,11 +1729,11 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf95400)
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"x"_ostr, "30");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"y"_ostr, "20");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"text"_ostr, "ABC");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx0"_ostr, "36");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx1"_ostr, "69");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx2"_ostr, "102");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx0"_ostr, 36, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx1"_ostr, 69, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx2"_ostr, 102, 0.5);
 
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"width"_ostr, "48");
+    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"width"_ostr, "49");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"height"_ostr, "16");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"x"_ostr, "30");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"y"_ostr, "30");
@@ -1791,11 +1791,11 @@ CPPUNIT_TEST_FIXTURE(Test, testTextAnchor)
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[10]"_ostr, "y"_ostr, "40");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[10]"_ostr, "text"_ostr, "A");
 
-    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[11]"_ostr, "x"_ostr, "72");
+    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[11]"_ostr, "x"_ostr, "71");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[11]"_ostr, "y"_ostr, "40");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[11]"_ostr, "text"_ostr, "B");
 
-    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[12]"_ostr, "x"_ostr, "83");
+    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[12]"_ostr, "x"_ostr, "82");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[12]"_ostr, "y"_ostr, "40");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[12]"_ostr, "text"_ostr, "C");
 
@@ -1806,11 +1806,11 @@ CPPUNIT_TEST_FIXTURE(Test, testTextAnchor)
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[13]"_ostr, "y"_ostr, "50");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[13]"_ostr, "text"_ostr, "A");
 
-    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[14]"_ostr, "x"_ostr, "55");
+    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[14]"_ostr, "x"_ostr, "54");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[14]"_ostr, "y"_ostr, "50");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[14]"_ostr, "text"_ostr, "B");
 
-    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[15]"_ostr, "x"_ostr, "66");
+    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[15]"_ostr, "x"_ostr, "65");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[15]"_ostr, "y"_ostr, "50");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[15]"_ostr, "text"_ostr, "C");
 
@@ -1818,11 +1818,11 @@ CPPUNIT_TEST_FIXTURE(Test, testTextAnchor)
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[16]"_ostr, "y"_ostr, "60");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[16]"_ostr, "text"_ostr, "A");
 
-    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[17]"_ostr, "x"_ostr, "38");
+    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[17]"_ostr, "x"_ostr, "37");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[17]"_ostr, "y"_ostr, "60");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[17]"_ostr, "text"_ostr, "B");
 
-    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[18]"_ostr, "x"_ostr, "49");
+    assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[18]"_ostr, "x"_ostr, "48");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[18]"_ostr, "y"_ostr, "60");
     assertXPath(pDocument, 
"/primitive2D/transform/textsimpleportion[18]"_ostr, "text"_ostr, "C");
 }
@@ -1853,9 +1853,9 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156577)
     // Without the fix in place, this test would have failed with
     // - Expected: 22
     // - Actual  : 52
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx0"_ostr, "22");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx1"_ostr, "53");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx2"_ostr, "94");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[2]"_ostr, "dx0"_ostr, 22, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[2]"_ostr, "dx1"_ostr, 52, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[2]"_ostr, "dx2"_ostr, 93, 0.5);
 }
 
 CPPUNIT_TEST_FIXTURE(Test, testTdf156283)
@@ -1873,9 +1873,9 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156283)
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"x"_ostr, "30");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"y"_ostr, "20");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"text"_ostr, "ABC");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx0"_ostr, "41");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx1"_ostr, "52");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx2"_ostr, "63");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx0"_ostr, 41, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx1"_ostr, 52, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx2"_ostr, 62, 0.5);
 
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"width"_ostr, "16");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"height"_ostr, "16");
@@ -1886,9 +1886,9 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156283)
     // Without the fix in place, this test would have failed with
     // - Expected: 41
     // - Actual  : 12
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx0"_ostr, "41");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx1"_ostr, "52");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx2"_ostr, "63");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[2]"_ostr, "dx0"_ostr, 41, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[2]"_ostr, "dx1"_ostr, 51, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[2]"_ostr, "dx2"_ostr, 62, 0.5);
 }
 
 CPPUNIT_TEST_FIXTURE(Test, testTdf156569)
@@ -1906,22 +1906,22 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156569)
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"x"_ostr, "0");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"y"_ostr, "20");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"text"_ostr, "ABC");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx0"_ostr, "40");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx1"_ostr, "80");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]"_ostr, 
"dx2"_ostr, "91");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx0"_ostr, 40, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx1"_ostr, 80, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[1]"_ostr, "dx2"_ostr, 91, 0.5);
 
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"width"_ostr, "16");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"height"_ostr, "16");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"x"_ostr, "0");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"y"_ostr, "30");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"text"_ostr, "ABC");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx0"_ostr, "40");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[2]"_ostr, "dx0"_ostr, 40, 0.5);
 
     // Without the fix in place, this test would have failed with
     // - Expected: 80
     // - Actual  : 51
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx1"_ostr, "80");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]"_ostr, 
"dx2"_ostr, "91");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/textsimpleportion[2]"_ostr, "dx2"_ostr, 91, 0.5);
 }
 
 CPPUNIT_TEST_FIXTURE(Test, testTdf156837)
@@ -1964,8 +1964,8 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156271)
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[1]"_ostr, "x"_ostr, "40");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[1]"_ostr, "y"_ostr, "10");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[1]"_ostr, "text"_ostr, "AB");
-    assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[1]"_ostr, "dx0"_ostr, "-30");
-    assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[1]"_ostr, "dx1"_ostr, "-19");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[1]"_ostr, "dx0"_ostr, -30, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[1]"_ostr, "dx1"_ostr, -19, 0.5);
 
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[2]"_ostr, "width"_ostr, "16");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[2]"_ostr, "height"_ostr, "16");
@@ -1976,24 +1976,24 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156271)
     // Without the fix in place, this test would have failed with
     // - Expected: -30
     // - Actual  : 0
-    assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[2]"_ostr, "dx0"_ostr, "-30");
-    assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[2]"_ostr, "dx1"_ostr, "-19");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[2]"_ostr, "dx0"_ostr, -30, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[2]"_ostr, "dx1"_ostr, -19, 0.5);
 
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "width"_ostr, "16");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "height"_ostr, "16");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "x"_ostr, "40");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "y"_ostr, "30");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "text"_ostr, "AB");
-    assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "dx0"_ostr, "-30");
-    assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "dx1"_ostr, "-19");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "dx0"_ostr, -30, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[3]"_ostr, "dx1"_ostr, -19, 0.5);
 
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "width"_ostr, "16");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "height"_ostr, "16");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "x"_ostr, "40");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "y"_ostr, "40");
     assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "text"_ostr, "AB");
-    assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "dx0"_ostr, "12");
-    assertXPath(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "dx1"_ostr, "23");
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "dx0"_ostr, 12, 0.5);
+    assertXPathDoubleValue(pDocument, 
"/primitive2D/transform/mask/textsimpleportion[4]"_ostr, "dx1"_ostr, 22, 0.5);
 }
 
 CPPUNIT_TEST_FIXTURE(Test, testTdf159968)
diff --git a/svgio/source/svgreader/svgcharacternode.cxx 
b/svgio/source/svgreader/svgcharacternode.cxx
index 02bc3911a3b8..7fdac9e92bc5 100644
--- a/svgio/source/svgreader/svgcharacternode.cxx
+++ b/svgio/source/svgreader/svgcharacternode.cxx
@@ -138,11 +138,11 @@ namespace svgio::svgreader
         {
             // prepare retval, index and length
             rtl::Reference<BasePrimitive2D> pRetval;
-            sal_uInt32 nLength(getText().getLength());
+            const sal_uInt32 nLength(getText().getLength());
 
             if(nLength)
             {
-                sal_uInt32 nIndex(0);
+                const sal_uInt32 nIndex(0);
 
                 // prepare FontAttribute
                 const drawinglayer::attribute::FontAttribute 
aFontAttribute(getFontAttribute(rSvgStyleAttributes));
@@ -154,9 +154,12 @@ namespace svgio::svgreader
                 // prepare locale
                 css::lang::Locale aLocale;
 
-                // prepare TextLayouterDevice
+                // prepare TextLayouterDevice; use a larger font size for more 
linear size
+                // calculations. Similar to nTextSizeFactor in 
sd/source/ui/view/sdview.cxx
+                // (ViewRedirector::createRedirectedPrimitive2DSequence).
+                const double sizeFactor = fFontHeight < 50000 ? 50000 / 
fFontHeight : 1.0;
                 TextLayouterDevice aTextLayouterDevice;
-                aTextLayouterDevice.setFontAttribute(aFontAttribute, 
fFontWidth, fFontHeight, aLocale);
+                aTextLayouterDevice.setFontAttribute(aFontAttribute, 
fFontWidth * sizeFactor, fFontHeight * sizeFactor, aLocale);
 
                 // prepare TextArray
                 ::std::vector< double > aTextArray(rSvgTextPosition.getX());
@@ -190,13 +193,13 @@ namespace svgio::svgreader
                         {
                             fComulativeDx += aDxArray[a];
                         }
-                        aTextArray.push_back(aExtendArray[a] + fStartX + 
fComulativeDx);
+                        aTextArray.push_back(aExtendArray[a] / sizeFactor + 
fStartX + fComulativeDx);
                     }
                 }
 
                 // get current TextPosition and TextWidth in units
                 basegfx::B2DPoint aPosition(rSvgTextPosition.getPosition());
-                double fTextWidth(aTextLayouterDevice.getTextWidth(getText(), 
nIndex, nLength));
+                double fTextWidth(aTextLayouterDevice.getTextWidth(getText(), 
nIndex, nLength) / sizeFactor);
 
                 // check for user-given TextLength
                 if(0.0 != rSvgTextPosition.getTextLength()
@@ -209,7 +212,10 @@ namespace svgio::svgreader
                         // spacing, need to create and expand TextArray
                         if(aTextArray.empty())
                         {
-                            aTextArray = 
aTextLayouterDevice.getTextArray(getText(), nIndex, nLength);
+                            auto 
aExtendArray(aTextLayouterDevice.getTextArray(getText(), nIndex, nLength));
+                            aTextArray.reserve(aExtendArray.size());
+                            for (auto n : aExtendArray)
+                                aTextArray.push_back(n / sizeFactor);
                         }
 
                         for(auto &a : aTextArray)
@@ -289,12 +295,12 @@ namespace svgio::svgreader
                     case DominantBaseline::Middle:
                     case DominantBaseline::Central:
                     {
-                        aPosition.setY(aPosition.getY() - aRange.getCenterY());
+                        aPosition.setY(aPosition.getY() - aRange.getCenterY() 
/ sizeFactor);
                         break;
                     }
                     case DominantBaseline::Hanging:
                     {
-                        aPosition.setY(aPosition.getY() - aRange.getMinY());
+                        aPosition.setY(aPosition.getY() - aRange.getMinY() / 
sizeFactor);
                         break;
                     }
                     default: // DominantBaseline::Auto
@@ -312,12 +318,12 @@ namespace svgio::svgreader
                 {
                     case BaselineShift::Sub:
                     {
-                        aPosition.setY(aPosition.getY() + 
aTextLayouterDevice.getUnderlineOffset());
+                        aPosition.setY(aPosition.getY() + 
aTextLayouterDevice.getUnderlineOffset() / sizeFactor);
                         break;
                     }
                     case BaselineShift::Super:
                     {
-                        aPosition.setY(aPosition.getY() + 
aTextLayouterDevice.getOverlineOffset());
+                        aPosition.setY(aPosition.getY() + 
aTextLayouterDevice.getOverlineOffset() / sizeFactor);
                         break;
                     }
                     case BaselineShift::Percentage:
commit aa2df54f37926cf95261eefade1b87da3229cfd7
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Apr 10 12:15:55 2024 +0500
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Fri May 3 13:32:20 2024 +0200

    tdf#160622: Let SalLayout::GetBoundRect return basegfx::B2DRectangle
    
    This avoids premature rounding in TextLayouterDevice::getTextBoundRect.
    The box in D2DWriteTextOutRenderer::performRender needs to be expanded
    to allow room for the line width (which now will be guaranteed on all
    sides; previously, the rounding could happen to give no room on some
    side, even prior to commit 8962141a12c966b2d891829925e6203bf8d51852).
    Fixes some lines partially cut off in smaller text (or zoomed out).
    
    Change-Id: I07335136021f894cf045363b4d736bfab06c64d4
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166236
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167014

diff --git a/drawinglayer/source/primitive2d/textlayoutdevice.cxx 
b/drawinglayer/source/primitive2d/textlayoutdevice.cxx
index 1c551ce01363..5145d84ed2b6 100644
--- a/drawinglayer/source/primitive2d/textlayoutdevice.cxx
+++ b/drawinglayer/source/primitive2d/textlayoutdevice.cxx
@@ -260,15 +260,9 @@ basegfx::B2DRange 
TextLayouterDevice::getTextBoundRect(const OUString& rText, sa
 
     if (nTextLength)
     {
-        ::tools::Rectangle aRect;
-
+        basegfx::B2DRange aRect;
         mrDevice.GetTextBoundRect(aRect, rText, nIndex, nIndex, nLength);
-
-        // #i104432#, #i102556# take empty results into account
-        if (!aRect.IsEmpty())
-        {
-            return vcl::unotools::b2DRectangleFromRectangle(aRect);
-        }
+        return aRect;
     }
 
     return basegfx::B2DRange();
diff --git a/include/vcl/outdev.hxx b/include/vcl/outdev.hxx
index 329364559108..ea49f505066f 100644
--- a/include/vcl/outdev.hxx
+++ b/include/vcl/outdev.hxx
@@ -52,6 +52,7 @@
 #include <vcl/settings.hxx>
 #include <vcl/vclreferencebase.hxx>
 
+#include <basegfx/range/b2drectangle.hxx>
 #include <basegfx/numeric/ftools.hxx>
 #include <basegfx/point/b2dpoint.hxx>
 #include <basegfx/vector/b2enums.hxx>
@@ -961,6 +962,11 @@ public:
                                                   sal_uLong nLayoutWidth = 0, 
KernArraySpan aDXArray = KernArraySpan(),
                                                   std::span<const sal_Bool> 
pKashidaArray = {},
                                                   const SalLayoutGlyphs* 
pGlyphs = nullptr ) const;
+    bool                        GetTextBoundRect( basegfx::B2DRectangle& rRect,
+                                                  const OUString& rStr, 
sal_Int32 nBase = 0, sal_Int32 nIndex = 0, sal_Int32 nLen = -1,
+                                                  sal_uLong nLayoutWidth = 0, 
KernArraySpan aDXArray = KernArraySpan(),
+                                                  std::span<const sal_Bool> 
pKashidaArray = {},
+                                                  const SalLayoutGlyphs* 
pGlyphs = nullptr ) const;
 
     tools::Rectangle            ImplGetTextBoundRect( const SalLayout& ) const;
 
@@ -1614,6 +1620,7 @@ public:
     SAL_WARN_UNUSED_RESULT tools::Polygon PixelToLogic(const tools::Polygon& 
rDevicePoly) const;
     SAL_WARN_UNUSED_RESULT tools::PolyPolygon PixelToLogic(const 
tools::PolyPolygon& rDevicePolyPoly) const;
     SAL_WARN_UNUSED_RESULT basegfx::B2DPolyPolygon PixelToLogic(const 
basegfx::B2DPolyPolygon& rDevicePolyPoly) const;
+    SAL_WARN_UNUSED_RESULT basegfx::B2DRectangle PixelToLogic(const 
basegfx::B2DRectangle& rDeviceRect) const;
     SAL_WARN_UNUSED_RESULT vcl::Region PixelToLogic(const vcl::Region& 
rDeviceRegion) const;
     SAL_WARN_UNUSED_RESULT Point PixelToLogic(const Point& rDevicePt, const 
MapMode& rMapMode) const;
     SAL_WARN_UNUSED_RESULT Size PixelToLogic(const Size& rDeviceSize, const 
MapMode& rMapMode) const;
diff --git a/include/vcl/vcllayout.hxx b/include/vcl/vcllayout.hxx
index 7c7a179b9b05..c946f6f67884 100644
--- a/include/vcl/vcllayout.hxx
+++ b/include/vcl/vcllayout.hxx
@@ -21,6 +21,7 @@
 
 #include <basegfx/point/b2dpoint.hxx>
 #include <basegfx/polygon/b2dpolypolygon.hxx>
+#include <basegfx/range/b2drectangle.hxx>
 #include <i18nlangtag/languagetag.hxx>
 #include <tools/gen.hxx>
 #include <tools/degree.hxx>
@@ -103,7 +104,9 @@ public:
     virtual bool    GetNextGlyph(const GlyphItem** pGlyph, basegfx::B2DPoint& 
rPos, int& nStart,
                                  const LogicalFontInstance** ppGlyphFont = 
nullptr) const = 0;
     virtual bool GetOutline(basegfx::B2DPolyPolygonVector&) const;
-    bool GetBoundRect(tools::Rectangle&) const;
+    bool GetBoundRect(basegfx::B2DRectangle&) const;
+
+    static tools::Rectangle BoundRect2Rectangle(basegfx::B2DRectangle&);
 
     virtual SalLayoutGlyphs GetGlyphs() const;
 
diff --git a/svgio/qa/cppunit/SvgImportTest.cxx 
b/svgio/qa/cppunit/SvgImportTest.cxx
index f56966a4423c..43910078e59f 100644
--- a/svgio/qa/cppunit/SvgImportTest.cxx
+++ b/svgio/qa/cppunit/SvgImportTest.cxx
@@ -39,6 +39,8 @@ protected:
 
     Primitive2DSequence parseSvg(std::u16string_view aSource);
     xmlDocUniquePtr dumpAndParseSvg(std::u16string_view aSource);
+    void assertXPathDouble(const xmlDocUniquePtr& pXmlDoc, const OString& 
rXPath,
+                           const OString& rAttribute, double nExpectedValue, 
double delta);
 };
 
 Primitive2DSequence Test::parseSvg(std::u16string_view aSource)
@@ -91,6 +93,13 @@ void Test::checkRectPrimitive(Primitive2DSequence const & 
rPrimitive)
 
 }
 
+void Test::assertXPathDouble(const xmlDocUniquePtr& pXmlDoc, const OString& 
rXPath,
+                             const OString& rAttribute, double nExpectedValue, 
double delta)
+{
+    auto sVal = getXPath(pXmlDoc, rXPath, rAttribute);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(nExpectedValue, sVal.toDouble(), delta);
+}
+
 namespace
 {
 bool arePrimitive2DSequencesEqual(const Primitive2DSequence& rA, const 
Primitive2DSequence& rB)
@@ -812,7 +821,7 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156834)
 
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"text"_ostr, "Hanging");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"x"_ostr, "30");
-    assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]"_ostr, 
"y"_ostr, "94");
+    assertXPathDouble(pDocument, 
"/primitive2D/transform/textsimpleportion[3]"_ostr, "y"_ostr, 93.5, 0.5);
 
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[4]"_ostr, 
"text"_ostr, "Central");
     assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[4]"_ostr, 
"x"_ostr, "30");
diff --git a/vcl/skia/gdiimpl.cxx b/vcl/skia/gdiimpl.cxx
index d7028b959842..f4ad57f715b0 100644
--- a/vcl/skia/gdiimpl.cxx
+++ b/vcl/skia/gdiimpl.cxx
@@ -2154,7 +2154,7 @@ void SkiaSalGraphicsImpl::drawGenericLayout(const 
GenericSalLayout& layout, Colo
 
     preDraw();
     auto getBoundRect = [&layout]() {
-        tools::Rectangle rect;
+        basegfx::B2DRectangle rect;
         layout.GetBoundRect(rect);
         return rect;
     };
diff --git a/vcl/source/gdi/sallayout.cxx b/vcl/source/gdi/sallayout.cxx
index 28138c3f61fd..3b1279dd1f66 100644
--- a/vcl/source/gdi/sallayout.cxx
+++ b/vcl/source/gdi/sallayout.cxx
@@ -220,11 +220,10 @@ static double trimInsignificant(double n)
     return std::abs(n) >= 0x1p53 ? n : std::round(n * 1e5) / 1e5;
 }
 
-bool SalLayout::GetBoundRect(tools::Rectangle& rRect) const
+bool SalLayout::GetBoundRect(basegfx::B2DRectangle& rRect) const
 {
     bool bRet = false;
-
-    basegfx::B2DRectangle aUnion;
+    rRect.reset();
     basegfx::B2DRectangle aRectangle;
 
     basegfx::B2DPoint aPos;
@@ -240,28 +239,28 @@ bool SalLayout::GetBoundRect(tools::Rectangle& rRect) 
const
             {
                 
aRectangle.transform(basegfx::utils::createTranslateB2DHomMatrix(aPos));
                 // merge rectangle
-                aUnion.expand(aRectangle);
+                rRect.expand(aRectangle);
             }
             bRet = true;
         }
     }
-    if (aUnion.isEmpty())
-    {
-        rRect = {};
-    }
-    else
-    {
-        double l = rtl::math::approxFloor(trimInsignificant(aUnion.getMinX())),
-               t = rtl::math::approxFloor(trimInsignificant(aUnion.getMinY())),
-               r = rtl::math::approxCeil(trimInsignificant(aUnion.getMaxX())),
-               b = rtl::math::approxCeil(trimInsignificant(aUnion.getMaxY()));
-        assert(std::isfinite(l) && std::isfinite(t) && std::isfinite(r) && 
std::isfinite(b));
-        rRect = tools::Rectangle(l, t, r, b);
-    }
 
     return bRet;
 }
 
+tools::Rectangle SalLayout::BoundRect2Rectangle(basegfx::B2DRectangle& rRect)
+{
+    if (rRect.isEmpty())
+        return {};
+
+    double l = rtl::math::approxFloor(trimInsignificant(rRect.getMinX())),
+           t = rtl::math::approxFloor(trimInsignificant(rRect.getMinY())),
+           r = rtl::math::approxCeil(trimInsignificant(rRect.getMaxX())),
+           b = rtl::math::approxCeil(trimInsignificant(rRect.getMaxY()));
+    assert(std::isfinite(l) && std::isfinite(t) && std::isfinite(r) && 
std::isfinite(b));
+    return tools::Rectangle(l, t, r, b);
+}
+
 SalLayoutGlyphs SalLayout::GetGlyphs() const
 {
     return SalLayoutGlyphs(); // invalid
diff --git a/vcl/source/outdev/map.cxx b/vcl/source/outdev/map.cxx
index 23c68a238551..8707805eb1c2 100644
--- a/vcl/source/outdev/map.cxx
+++ b/vcl/source/outdev/map.cxx
@@ -1217,6 +1217,14 @@ basegfx::B2DPolyPolygon OutputDevice::PixelToLogic( 
const basegfx::B2DPolyPolygo
     return aTransformedPoly;
 }
 
+basegfx::B2DRectangle OutputDevice::PixelToLogic(const basegfx::B2DRectangle& 
rDeviceRect) const
+{
+    basegfx::B2DRectangle aTransformedRect = rDeviceRect;
+    const basegfx::B2DHomMatrix& rTransformationMatrix = 
GetInverseViewTransformation();
+    aTransformedRect.transform(rTransformationMatrix);
+    return aTransformedRect;
+}
+
 vcl::Region OutputDevice::PixelToLogic( const vcl::Region& rDeviceRegion ) 
const
 {
 
diff --git a/vcl/source/outdev/text.cxx b/vcl/source/outdev/text.cxx
index 1b40a1b2de28..16fdf7b073d0 100644
--- a/vcl/source/outdev/text.cxx
+++ b/vcl/source/outdev/text.cxx
@@ -21,6 +21,7 @@
 
 #include <sal/log.hxx>
 #include <basegfx/matrix/b2dhommatrix.hxx>
+#include <basegfx/matrix/b2dhommatrixtools.hxx>
 #include <tools/lineend.hxx>
 #include <tools/debug.hxx>
 #include <unotools/configmgr.hxx>
@@ -214,7 +215,11 @@ bool OutputDevice::ImplDrawRotateText( SalLayout& 
rSalLayout )
     tools::Rectangle aBoundRect;
     rSalLayout.DrawBase() = basegfx::B2DPoint( 0, 0 );
     rSalLayout.DrawOffset() = Point( 0, 0 );
-    if (!rSalLayout.GetBoundRect(aBoundRect))
+    if (basegfx::B2DRectangle r; rSalLayout.GetBoundRect(r))
+    {
+        aBoundRect = SalLayout::BoundRect2Rectangle(r);
+    }
+    else
     {
         // guess vertical text extents if GetBoundRect failed
         double nRight = rSalLayout.GetTextWidth();
@@ -1921,9 +1926,22 @@ bool OutputDevice::GetTextBoundRect( tools::Rectangle& 
rRect,
                                          sal_uLong nLayoutWidth, KernArraySpan 
pDXAry,
                                          std::span<const sal_Bool> pKashidaAry,
                                          const SalLayoutGlyphs* pGlyphs ) const
+{
+    basegfx::B2DRectangle aRect;
+    bool bRet = GetTextBoundRect(aRect, rStr, nBase, nIndex, nLen, 
nLayoutWidth, pDXAry,
+                                 pKashidaAry, pGlyphs);
+    rRect = SalLayout::BoundRect2Rectangle(aRect);
+    return bRet;
+}
+
+bool OutputDevice::GetTextBoundRect(basegfx::B2DRectangle& rRect, const 
OUString& rStr,
+                                    sal_Int32 nBase, sal_Int32 nIndex, 
sal_Int32 nLen,
+                                    sal_uLong nLayoutWidth, KernArraySpan 
pDXAry,
+                                    std::span<const sal_Bool> pKashidaAry,
+                                    const SalLayoutGlyphs* pGlyphs) const
 {
     bool bRet = false;
-    rRect.SetEmpty();
+    rRect.reset();
 
     std::unique_ptr<SalLayout> pSalLayout;
     const Point aPoint;
@@ -1947,18 +1965,22 @@ bool OutputDevice::GetTextBoundRect( tools::Rectangle& 
rRect,
                             nullptr, pGlyphs);
     if( pSalLayout )
     {
-        tools::Rectangle aPixelRect;
+        basegfx::B2DRectangle aPixelRect;
         bRet = pSalLayout->GetBoundRect(aPixelRect);
 
         if( bRet )
         {
-            Point aRotatedOfs( mnTextOffX, mnTextOffY );
             basegfx::B2DPoint aPos = 
pSalLayout->GetDrawPosition(basegfx::B2DPoint(nXOffset, 0));
-            aRotatedOfs -= Point(aPos.getX(), aPos.getY());
-            aPixelRect += aRotatedOfs;
+            auto m = basegfx::utils::createTranslateB2DHomMatrix(mnTextOffX - 
aPos.getX(),
+                                                                 mnTextOffY - 
aPos.getY());
+            aPixelRect.transform(m);
             rRect = PixelToLogic( aPixelRect );
-            if( mbMap )
-                rRect += Point( maMapRes.mnMapOfsX, maMapRes.mnMapOfsY );
+            if (mbMap)
+            {
+                m = 
basegfx::utils::createTranslateB2DHomMatrix(maMapRes.mnMapOfsX,
+                                                                
maMapRes.mnMapOfsY);
+                rRect.transform(m);
+            }
         }
     }
 
diff --git a/vcl/win/gdi/DWriteTextRenderer.cxx 
b/vcl/win/gdi/DWriteTextRenderer.cxx
index 3d3dac83c6dd..82508061b62a 100644
--- a/vcl/win/gdi/DWriteTextRenderer.cxx
+++ b/vcl/win/gdi/DWriteTextRenderer.cxx
@@ -225,8 +225,15 @@ bool 
D2DWriteTextOutRenderer::performRender(GenericSalLayout const & rLayout, Sa
     if (!pFontFace)
         return false;
 
-    tools::Rectangle bounds;
-    bool succeeded = rLayout.GetBoundRect(bounds);
+    auto [succeeded, bounds] = [&rLayout]()
+    {
+        basegfx::B2DRectangle r;
+        bool result = rLayout.GetBoundRect(r);
+        if (result)
+            r.grow(1); // plus 1 pixel to the tight range
+        return std::make_pair(result, SalLayout::BoundRect2Rectangle(r));
+    }();
+
     if (succeeded)
     {
         hr = BindDC(hDC, bounds);   // Update the bounding rect.

Reply via email to