diff options
author | Kevin Suo <suokunlong@126.com> | 2022-10-11 10:04:16 +0800 |
---|---|---|
committer | Thorsten Behrens <thorsten.behrens@allotropia.de> | 2022-10-13 21:38:12 +0200 |
commit | 69e9925ded584113e52f84ef0ed7c224079fa061 (patch) | |
tree | 1af5c8e9bedbceedd7b4ddefb67c37a79462aaa0 /sdext/source/pdfimport/test | |
parent | 3442a46995f967dbd4c99797f7f13794912f0f58 (diff) |
sdext.pdfimport: resolves tdf#104597: RTL script text runs are reversed
For the simple Arabic string: ٱلسَّلَامُ عَلَيْك, the xpdfimport binary generates the
follwing (drawchar) sequences:
كَ
يْ
لَ
عَ
مُ
ا
لَ
سَّ
ل
ٱ
(i.e., in reversed order, one character by one character).
Before this patch, after pdfimport the text shows up as لَسَّلٱ كَيْلَعَ مُا, which is reversed.
It was surposed to combine these characters into text frames in
DrawXmlOptimizer::optimizeTextElements(Element& rParent) (sdext/source/pdfimport/\
tree/drawtreevisiting.cxx:677), but actually it was not combined successfully there.
The single characters were then passed to sdext/source/pdfimport/tree/drawtreevisiting\
.cxx:105, one by one, in the hope that the strings could be mirrored. The mirroring
failed, because one single character, even after mirroring, always equals itself.
The DrawXmlOptimizer::optimizeTextElements failed to combine the characters into
one text frame, because the condition:
(rCurGC.Transformation == rNextGC.Transformation || notTransformed(rNextGC))
would never be true, as at least its horizontal position is different. A further analysis
indicates that we do not need to check the transformation here at all, as this is an
optimizer for a TextElement and in case a character is transformed then it would already
be in a different draw element (thus will never be combined with this TextElement).
After the fix of DrawXmlOptimizer::optimizeTextElements which now successfully
combines the characters, there is another issue in the old PDFIProcessor::mirrorString
function. It seems to mirror the characters within a word, but if a string contains
two words, then the two words are not successfully switched (e.g. for string "abc def"
it produces "cba fed" rather than "fed cba"), which is not suitable for
the case of RTL which requires all the characters been reversed. Fix this by using
comphelper::string::reverseString.
Change-Id: Ifa210836f1d6666dd56205ff0d243adfb4114794
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/141231
Tested-by: Jenkins
Reviewed-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
Diffstat (limited to 'sdext/source/pdfimport/test')
-rw-r--r-- | sdext/source/pdfimport/test/testdocs/tdf104597_textrun.pdf | bin | 0 -> 21042 bytes | |||
-rw-r--r-- | sdext/source/pdfimport/test/tests.cxx | 44 |
2 files changed, 44 insertions, 0 deletions
diff --git a/sdext/source/pdfimport/test/testdocs/tdf104597_textrun.pdf b/sdext/source/pdfimport/test/testdocs/tdf104597_textrun.pdf Binary files differnew file mode 100644 index 000000000000..dcee96aa3169 --- /dev/null +++ b/sdext/source/pdfimport/test/testdocs/tdf104597_textrun.pdf diff --git a/sdext/source/pdfimport/test/tests.cxx b/sdext/source/pdfimport/test/tests.cxx index 193ec2227620..25c12a23901c 100644 --- a/sdext/source/pdfimport/test/tests.cxx +++ b/sdext/source/pdfimport/test/tests.cxx @@ -29,6 +29,7 @@ #include <rtl/math.hxx> #include <osl/file.hxx> #include <comphelper/sequence.hxx> +#include <comphelper/string.hxx> #include <cppunit/TestAssert.h> #include <cppunit/extensions/HelperMacros.h> @@ -786,6 +787,48 @@ namespace #endif } + void testTdf104597_textrun() + { +#if HAVE_FEATURE_POPPLER + rtl::Reference<pdfi::PDFIRawAdaptor> xAdaptor(new pdfi::PDFIRawAdaptor(OUString(), getComponentContext())); + xAdaptor->setTreeVisitorFactory(createDrawTreeVisitorFactory()); + + OString aOutput; + CPPUNIT_ASSERT_MESSAGE("Converting PDF to ODF XML", + xAdaptor->odfConvert(m_directories.getURLFromSrc(u"/sdext/source/pdfimport/test/testdocs/tdf104597_textrun.pdf"), + new OutputWrapString(aOutput), + nullptr)); + + // std::cout << aOutput << std::endl; + xmlDocUniquePtr pXmlDoc(xmlParseDoc(reinterpret_cast<xmlChar const *>(aOutput.getStr()))); + + // Test for امُ عَلَيْكَ + // TODO: How to get the "عَلَيْكَ" in xpath, as shown after the <text:s> tag? + OString xpath = "//draw:frame[@draw:transform='matrix(917.222222222222 0 0 917.222222222222 14821.9583333333 2159.23861112778)']/draw:text-box/text:p/text:span"; + OUString sContent = getXPathContent(pXmlDoc, xpath); // u"\nا\nُ\nم\n" + CPPUNIT_ASSERT_EQUAL(OUString(u"اُم"), sContent.replaceAll("\n", "")); + + // Test for ٱلَّسَل . It appears in the 3rd frame, i.e. after the امُ عَلَيْكَ which is in the 2nd frame (from left to right) + // thus these two frames together appear as ٱلَّسَل امُ عَلَيْكَ in Draw. + xpath = "//draw:frame[@draw:transform='matrix(917.222222222222 0 0 917.222222222222 17420.1666666667 2159.23861112778)']/draw:text-box/text:p/text:span"; + sContent = getXPathContent(pXmlDoc, xpath); + CPPUNIT_ASSERT_EQUAL(OUString(u"ٱلَّسَل"), sContent.replaceAll("\n", "")); + + // Test for "LibreOffice LTR" + // TODO: How to get the "LTR" as shown after the <text:s> tag? + xpath = "//draw:frame[@draw:transform='matrix(917.222222222222 0 0 917.222222222222 12779.375 5121.79583335)']/draw:text-box/text:p/text:span"; + sContent = getXPathContent(pXmlDoc, xpath); + CPPUNIT_ASSERT_EQUAL(OUString(u"LibreOffice"), sContent.replaceAll("\n", "")); + + /* Test for Chinese characters */ + // Use last() instead of matrix below, because the matrix may be different on different OS due to fallback of Chinese fonts. + xpath = "//draw:frame[last()]/draw:text-box/text:p/text:span"; + sContent = getXPathContent(pXmlDoc, xpath); + CPPUNIT_ASSERT_EQUAL(OUString(u"中文测试,中文"), sContent.replaceAll("\n", "")); +#endif + } + + CPPUNIT_TEST_SUITE(PDFITest); CPPUNIT_TEST(testXPDFParser); CPPUNIT_TEST(testOdfWriterExport); @@ -797,6 +840,7 @@ namespace CPPUNIT_TEST(testTdf78427_FontFeatures); CPPUNIT_TEST(testTdf78427_FontWeight_MyraidProSemibold); CPPUNIT_TEST(testTdf143959_nameFromFontFile); + CPPUNIT_TEST(testTdf104597_textrun); CPPUNIT_TEST_SUITE_END(); }; |