diff options
author | Xisco Fauli <xiscofauli@libreoffice.org> | 2023-08-03 17:47:36 +0200 |
---|---|---|
committer | Xisco Fauli <xiscofauli@libreoffice.org> | 2023-08-03 18:36:56 +0200 |
commit | 2795a230464aea3a792e67b5625fce2a0c01d547 (patch) | |
tree | 905bb5b808270eb1c43c5d796171fc1940bd98a7 /svgio/source | |
parent | 490cf5393d30dd2e9ab201286ef863b3c266a8b4 (diff) |
tdf#151103: Use the whole text line to calculate the align position
Change-Id: I7ecd41c422afbf028101924972c47a510834ba5a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155314
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
Diffstat (limited to 'svgio/source')
-rw-r--r-- | svgio/source/svgreader/svgcharacternode.cxx | 6 | ||||
-rw-r--r-- | svgio/source/svgreader/svgdocumenthandler.cxx | 91 |
2 files changed, 92 insertions, 5 deletions
diff --git a/svgio/source/svgreader/svgcharacternode.cxx b/svgio/source/svgreader/svgcharacternode.cxx index 8a6610c91d25..bfd17c6e0d58 100644 --- a/svgio/source/svgreader/svgcharacternode.cxx +++ b/svgio/source/svgreader/svgcharacternode.cxx @@ -359,17 +359,19 @@ namespace svgio::svgreader } } + // Use the whole text line to calculate the align position + double fWholeTextLineWidth(aTextLayouterDevice.getTextWidth(getWholeTextLine(), 0, getWholeTextLine().getLength())); // apply TextAlign switch(aTextAlign) { case TextAlign::right: { - aPosition.setX(aPosition.getX() - fTextWidth); + aPosition.setX(aPosition.getX() - fWholeTextLineWidth); break; } case TextAlign::center: { - aPosition.setX(aPosition.getX() - (fTextWidth * 0.5)); + aPosition.setX(aPosition.getX() - (fWholeTextLineWidth * 0.5)); break; } case TextAlign::notset: diff --git a/svgio/source/svgreader/svgdocumenthandler.cxx b/svgio/source/svgreader/svgdocumenthandler.cxx index 5c600a86dce1..b0bdfbabe7fb 100644 --- a/svgio/source/svgreader/svgdocumenthandler.cxx +++ b/svgio/source/svgreader/svgdocumenthandler.cxx @@ -145,6 +145,87 @@ namespace return pLast; } + + OUString getWholeTextLine(svgio::svgreader::SvgNode const * pNode) + { + OUString sText; + if (pNode) + { + const auto& rChilds = pNode->getChildren(); + const sal_uInt32 nCount(rChilds.size()); + + for(sal_uInt32 a(0); a < nCount; a++) + { + svgio::svgreader::SvgNode* pCandidate = rChilds[a].get(); + + if(pCandidate) + { + switch(pCandidate->getType()) + { + case SVGToken::Character: + { + svgio::svgreader::SvgCharacterNode* pCharNode = static_cast< svgio::svgreader::SvgCharacterNode* >(pCandidate); + sText += pCharNode->getText(); + break; + } + case SVGToken::Tspan: + case SVGToken::TextPath: + case SVGToken::Tref: + { + sText += getWholeTextLine(pCandidate); + break; + } + default: + { + OSL_ENSURE(false, "Unexpected token inside SVGTokenText (!)"); + break; + } + } + } + } + } + return sText; + } + + void setWholeTextLine(svgio::svgreader::SvgNode const * pNode, const OUString& rText) + { + if (pNode) + { + const auto& rChilds = pNode->getChildren(); + const sal_uInt32 nCount(rChilds.size()); + + for(sal_uInt32 a(0); a < nCount; a++) + { + svgio::svgreader::SvgNode* pCandidate = rChilds[a].get(); + + if(pCandidate) + { + switch(pCandidate->getType()) + { + case SVGToken::Character: + { + svgio::svgreader::SvgCharacterNode* pCharNode = static_cast< svgio::svgreader::SvgCharacterNode* >(pCandidate); + pCharNode->setWholeTextLine(rText); + break; + } + case SVGToken::Tspan: + case SVGToken::TextPath: + case SVGToken::Tref: + { + setWholeTextLine(pCandidate, rText); + break; + } + default: + { + OSL_ENSURE(false, "Unexpected token inside SVGTokenText (!)"); + break; + } + } + } + } + } + } + } // end anonymous namespace SvgDocHdl::SvgDocHdl(const OUString& aAbsolutePath) @@ -468,7 +549,7 @@ namespace return; const SVGToken aSVGToken(StrToSVGToken(aName, false)); - SvgNode* pWhitespaceCheck(SVGToken::Text == aSVGToken ? mpTarget : nullptr); + SvgNode* pTextNode(SVGToken::Text == aSVGToken ? mpTarget : nullptr); SvgStyleNode* pCssStyle(SVGToken::Style == aSVGToken ? static_cast< SvgStyleNode* >(mpTarget) : nullptr); SvgTitleDescNode* pSvgTitleDescNode(SVGToken::Title == aSVGToken || SVGToken::Desc == aSVGToken ? static_cast< SvgTitleDescNode* >(mpTarget) : nullptr); @@ -597,10 +678,14 @@ namespace } } - if(pWhitespaceCheck) + if(pTextNode) { // cleanup read strings - whiteSpaceHandling(pWhitespaceCheck, nullptr); + whiteSpaceHandling(pTextNode, nullptr); + + // Iterate over all the nodes in this text element to get the whole text line + OUString sWholeTextLine = getWholeTextLine(pTextNode); + setWholeTextLine(pTextNode, sWholeTextLine); } } |