svgio: handle addGap internally inside SvgCharacterNode
Also add the gap at the beginning of the current node,
not at the end of the previous one
Change-Id: I6583059b4a7418010ac2af459e00fb0d02d39605
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155552
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/svgio/inc/svgcharacternode.hxx b/svgio/inc/svgcharacternode.hxx
index f44d754..e0e353a 100644
--- a/svgio/inc/svgcharacternode.hxx
+++ b/svgio/inc/svgcharacternode.hxx
@@ -104,14 +104,12 @@ namespace svgio::svgreader
void decomposeText(drawinglayer::primitive2d::Primitive2DContainer& rTarget, SvgTextPosition& rSvgTextPosition) const;
void whiteSpaceHandling();
void addGap();
SvgCharacterNode* addGap(SvgCharacterNode* pPreviousCharacterNode);
void concatenate(std::u16string_view rText);
/// Text content
const OUString& getText() const { return maText; }
const OUString& getTextBeforeSpaceHandling() const { return maTextBeforeSpaceHandling; }
void setWholeTextLine(const OUString& rWholeTextLine) { maWholeTextLine = rWholeTextLine; }
const OUString& getWholeTextLine() const { return maWholeTextLine; }
diff --git a/svgio/qa/cppunit/SvgImportTest.cxx b/svgio/qa/cppunit/SvgImportTest.cxx
index 80234e8b..cf66e5b 100644
--- a/svgio/qa/cppunit/SvgImportTest.cxx
+++ b/svgio/qa/cppunit/SvgImportTest.cxx
@@ -737,11 +737,11 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf85770)
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]", "height", "11");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]", "familyname", "Times New Roman");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]", "fontcolor", "#000000");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]", "text", "Start ");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]", "text", "Start");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]", "height", "11");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]", "familyname", "Times New Roman");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]", "fontcolor", "#000000");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]", "text", "End");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]", "text", " End");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]", "height", "11");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]", "familyname", "Times New Roman");
@@ -1163,12 +1163,12 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf156251)
// Without the fix in place, this test would have failed with
// - Expected: 'You are '
// - Actual : 'You are'
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]", "text", "You are ");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]", "text", "not ");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]", "text", "a banana!");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[4]", "text", "You are ");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[5]", "text", "not ");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[6]", "text", "a banana!");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[1]", "text", "You are");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[2]", "text", " not");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[3]", "text", " a banana!");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[4]", "text", "You are");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[5]", "text", " not");
assertXPath(pDocument, "/primitive2D/transform/textsimpleportion[6]", "text", " a banana!");
}
CPPUNIT_TEST_FIXTURE(Test, testMaskText)
diff --git a/svgio/source/svgreader/svgcharacternode.cxx b/svgio/source/svgreader/svgcharacternode.cxx
index c953c5f..e014cd6 100644
--- a/svgio/source/svgreader/svgcharacternode.cxx
+++ b/svgio/source/svgreader/svgcharacternode.cxx
@@ -26,6 +26,7 @@
#include <drawinglayer/primitive2d/textdecoratedprimitive2d.hxx>
#include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
#include <utility>
#include <o3tl/string_view.hxx>
#include <osl/diagnose.h>
using namespace drawinglayer::primitive2d;
@@ -464,9 +465,46 @@ namespace svgio::svgreader
}
}
void SvgCharacterNode::addGap()
SvgCharacterNode* SvgCharacterNode::addGap(SvgCharacterNode* pPreviousCharacterNode)
{
maText += " ";
// maText may have lost all text. If that's the case, ignore as invalid character node
// Also ignore if maTextBeforeSpaceHandling just have spaces
if(!maText.isEmpty() && !o3tl::trim(maTextBeforeSpaceHandling).empty())
{
if(pPreviousCharacterNode)
{
bool bAddGap(true);
// Do not add a gap if last node doesn't end with a space and
// current note doesn't start with a space
const sal_uInt32 nLastLength(pPreviousCharacterNode->maTextBeforeSpaceHandling.getLength());
if(pPreviousCharacterNode->maTextBeforeSpaceHandling[nLastLength - 1] != ' ' && maTextBeforeSpaceHandling[0] != ' ')
bAddGap = false;
// With this option a baseline shift between two char parts ('words')
// will not add a space 'gap' to the end of the (non-last) word. This
// seems to be the standard behaviour, see last bugdoc attached #122524#
const SvgStyleAttributes* pStyleLast = pPreviousCharacterNode->getSvgStyleAttributes();
const SvgStyleAttributes* pStyleCurrent = getSvgStyleAttributes();
if(pStyleLast && pStyleCurrent && pStyleLast->getBaselineShift() != pStyleCurrent->getBaselineShift())
{
bAddGap = false;
}
// add in-between whitespace (single space) to last
// known character node
if(bAddGap)
{
maText = " " + maText;
}
}
// this becomes the previous character node
return this;
}
return pPreviousCharacterNode;
}
void SvgCharacterNode::concatenate(std::u16string_view rText)
diff --git a/svgio/source/svgreader/svgdocumenthandler.cxx b/svgio/source/svgreader/svgdocumenthandler.cxx
index fe942d5..793539d 100644
--- a/svgio/source/svgreader/svgdocumenthandler.cxx
+++ b/svgio/source/svgreader/svgdocumenthandler.cxx
@@ -54,7 +54,6 @@
#include <svgtitledescnode.hxx>
#include <sal/log.hxx>
#include <osl/diagnose.h>
#include <o3tl/string_view.hxx>
using namespace com::sun::star;
@@ -84,45 +83,7 @@ namespace
svgio::svgreader::SvgCharacterNode* pCharNode = static_cast< svgio::svgreader::SvgCharacterNode* >(pCandidate);
pCharNode->whiteSpaceHandling();
// pCharNode may have lost all text. If that's the case, ignore
// as invalid character node
// Also ignore if textBeforeSpaceHandling just have spaces
if(!pCharNode->getText().isEmpty() && !o3tl::trim(pCharNode->getTextBeforeSpaceHandling()).empty())
{
if(pLast)
{
bool bAddGap(true);
// Do not add a gap if last node doesn't end with a space and
// current note doesn't start with a space
const sal_uInt32 nLastLength(pLast->getTextBeforeSpaceHandling().getLength());
if(pLast->getTextBeforeSpaceHandling()[nLastLength - 1] != ' ' && pCharNode->getTextBeforeSpaceHandling()[0] != ' ')
bAddGap = false;
// With this option a baseline shift between two char parts ('words')
// will not add a space 'gap' to the end of the (non-last) word. This
// seems to be the standard behaviour, see last bugdoc attached #122524#
const svgio::svgreader::SvgStyleAttributes* pStyleLast = pLast->getSvgStyleAttributes();
const svgio::svgreader::SvgStyleAttributes* pStyleCurrent = pCandidate->getSvgStyleAttributes();
if(pStyleLast && pStyleCurrent && pStyleLast->getBaselineShift() != pStyleCurrent->getBaselineShift())
{
bAddGap = false;
}
// add in-between whitespace (single space) to last
// known character node
if(bAddGap)
{
pLast->addGap();
}
}
// remember new last corrected character node
pLast = pCharNode;
}
pLast = pCharNode->addGap(pLast);
break;
}
case SVGToken::Tspan: