emfio/inc/mtftools.hxx                         |    4 -
 emfio/qa/cppunit/wmf/data/TestSetTextAlign.wmf |binary
 emfio/qa/cppunit/wmf/wmfimporttest.cxx         |   91 +++++++++++++++++++++++++
 emfio/source/reader/mtftools.cxx               |   10 +-
 4 files changed, 99 insertions(+), 6 deletions(-)

New commits:
commit a2dcaacbdf9c29c89200c9905da4687e71d74ade
Author:     Bartosz Kosiorek <gan...@poczta.onet.pl>
AuthorDate: Wed May 10 22:30:20 2023 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Mon May 15 11:00:29 2023 +0200

    EMF Fix text aligning for EMR_SETTEXTALIGN for wrong values.
    
    Change-Id: I4d67eb7112d2295185905eac52ebab022a1beb78
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151670
    Tested-by: Jenkins
    Reviewed-by: Bartosz Kosiorek <gan...@poczta.onet.pl>
    (cherry picked from commit 717bdd06a7ac1a2a266f36a43d7f1e695559d056)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151772
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/emfio/inc/mtftools.hxx b/emfio/inc/mtftools.hxx
index f7a318f39d55..d2cbcf01c6e6 100644
--- a/emfio/inc/mtftools.hxx
+++ b/emfio/inc/mtftools.hxx
@@ -180,7 +180,9 @@ namespace emfio
         TA_RIGHT_CENTER = (TA_RIGHT | TA_CENTER),
         TA_TOP          = 0x0000,
         TA_BOTTOM       = 0x0008,
-        TA_BASELINE     = 0x0018,
+        // In [MS-WMF] 2.1.2.3, TA_BASELINE value is wrong.
+        // It is 0x0018 and it should be 0x0010.
+        TA_BASELINE     = 0x0010,
         TA_RTLREADING   = 0x0100
     };
 
diff --git a/emfio/qa/cppunit/wmf/data/TestSetTextAlign.wmf 
b/emfio/qa/cppunit/wmf/data/TestSetTextAlign.wmf
new file mode 100644
index 000000000000..9a3ad46f1d32
Binary files /dev/null and b/emfio/qa/cppunit/wmf/data/TestSetTextAlign.wmf 
differ
diff --git a/emfio/qa/cppunit/wmf/wmfimporttest.cxx 
b/emfio/qa/cppunit/wmf/wmfimporttest.cxx
index c5b2ddbc3205..6e79756e386b 100644
--- a/emfio/qa/cppunit/wmf/wmfimporttest.cxx
+++ b/emfio/qa/cppunit/wmf/wmfimporttest.cxx
@@ -48,6 +48,7 @@ public:
     void testNonPlaceableWmf();
     void testTdf88163NonPlaceableWmf();
     void testTdf88163PlaceableWmf();
+    void testSetTextAlignWmf();
     void testSine();
     void testEmfProblem();
     void testEmfLineStyles();
@@ -63,6 +64,7 @@ public:
     CPPUNIT_TEST(testNonPlaceableWmf);
     CPPUNIT_TEST(testTdf88163NonPlaceableWmf);
     CPPUNIT_TEST(testTdf88163PlaceableWmf);
+    CPPUNIT_TEST(testSetTextAlignWmf);
     CPPUNIT_TEST(testSine);
     CPPUNIT_TEST(testEmfProblem);
     CPPUNIT_TEST(testEmfLineStyles);
@@ -180,6 +182,95 @@ void WmfTest::testTdf88163PlaceableWmf()
     assertXPath(pDoc, "/metafile/push[2]/textarray[3]", "y", "390");
 }
 
+void WmfTest::testSetTextAlignWmf()
+{
+    OUString fileName(u"TestSetTextAlign.wmf");
+    SvFileStream aFileStream(getFullUrl(fileName), StreamMode::READ);
+    GDIMetaFile aGDIMetaFile;
+    ReadWindowMetafile(aFileStream, aGDIMetaFile);
+
+    MetafileXmlDump dumper;
+
+    xmlDocUniquePtr pDoc = dumpAndParse(dumper, aGDIMetaFile);
+
+    CPPUNIT_ASSERT(pDoc);
+
+    assertXPath(pDoc, "/metafile", "height", "20999");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[2]", "align", "top");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[2]", "x", "11642");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[2]", "y", "212");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[2]/text", 
"textalignment:default");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[3]", "align", "top");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[3]", "x", "12700");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[3]", "y", "212");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[3]/text", 
"textalignment:0x00");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[4]", "align", "top");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[4]", "x", "11999");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[4]", "y", "423");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[4]/text", 
"textalignment:0x02");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[5]", "align", "top");
+    // Fails without the fix: Expected: 11999, Actual: 12350
+    assertXPath(pDoc, "/metafile/push[2]/textarray[5]", "x", "11999");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[5]", "y", "635");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[5]/text", 
"textalignment:0x04");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[6]", "align", "top");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[6]", "x", "12350");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[6]", "y", "847");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[6]/text", 
"textalignment:0x06");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[7]", "align", "top");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[7]", "x", "12700");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[7]", "y", "1058");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[7]/text", 
"textalignment:0x08");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[8]", "align", "top");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[8]", "x", "11999");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[8]", "y", "1270");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[8]/text", 
"textalignment:0x0A");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[9]", "align", "bottom");
+    // Fails without the fix: Expected: 11999, Actual: 12350
+    assertXPath(pDoc, "/metafile/push[2]/textarray[9]", "x", "11999");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[9]", "y", "1482");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[9]/text", 
"textalignment:0x0C");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[10]", "align", "bottom");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[10]", "x", "12350");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[10]", "y", "1693");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[10]/text", 
"textalignment:0x0E");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[11]", "align", "bottom");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[11]", "x", "12700");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[11]", "y", "1905");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[11]/text", 
"textalignment:0x10");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[12]", "align", "bottom");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[12]", "x", "11999");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[12]", "y", "2117");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[12]/text", 
"textalignment:0x12");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[13]", "align", "bottom");
+    // Fails without the fix: Expected: 11999, Actual: 12350
+    assertXPath(pDoc, "/metafile/push[2]/textarray[13]", "x", "11999");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[13]", "y", "2328");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[13]/text", 
"textalignment:0x14");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[14]", "align", "bottom");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[14]", "x", "12350");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[14]", "y", "2540");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[14]/text", 
"textalignment:0x16");
+
+    assertXPath(pDoc, "/metafile/push[2]/textalign[15]", "align", "bottom");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[15]", "x", "12700");
+    assertXPath(pDoc, "/metafile/push[2]/textarray[15]", "y", "2752");
+    assertXPathContent(pDoc, "/metafile/push[2]/textarray[15]/text", 
"textalignment:0x18");
+}
+
 void WmfTest::testSine()
 {
     SvFileStream aFileStream(getFullUrl(u"sine_wave.emf"), StreamMode::READ);
diff --git a/emfio/source/reader/mtftools.cxx b/emfio/source/reader/mtftools.cxx
index 5b82c54daa21..17c679cc2a06 100644
--- a/emfio/source/reader/mtftools.cxx
+++ b/emfio/source/reader/mtftools.cxx
@@ -1680,11 +1680,11 @@ namespace emfio
             mnLatestTextLayoutMode = mnTextLayoutMode;
             mpGDIMetaFile->AddAction( new MetaLayoutModeAction( 
mnTextLayoutMode ) );
         }
-        SetGfxMode( nGfxMode );
+        SetGfxMode(nGfxMode);
         TextAlign eTextAlign;
-        if ( ( mnTextAlign & TA_BASELINE) == TA_BASELINE )
+        if (mnTextAlign & TA_BASELINE)
             eTextAlign = ALIGN_BASELINE;
-        else if( ( mnTextAlign & TA_BOTTOM) == TA_BOTTOM )
+        else if (mnTextAlign & TA_BOTTOM)
             eTextAlign = ALIGN_BOTTOM;
         else
             eTextAlign = ALIGN_TOP;
@@ -1779,9 +1779,9 @@ namespace emfio
             if( mnTextAlign & TA_UPDATECP )
                 rPosition = maActPos;
 
-            if ( mnTextAlign & TA_RIGHT_CENTER )
+            if (mnTextAlign & TA_RIGHT_CENTER)
             {
-                Point aDisplacement( ( ( mnTextAlign & TA_RIGHT_CENTER ) == 
TA_RIGHT ) ? nTextWidth : nTextWidth >> 1, 0 );
+                Point aDisplacement(((mnTextAlign & TA_RIGHT_CENTER) == 
TA_CENTER) ? nTextWidth >> 1: nTextWidth, 0);
                 Point().RotateAround(aDisplacement, maFont.GetOrientation());
                 rPosition -= aDisplacement;
             }

Reply via email to