diff options
author | Xisco Fauli <xiscofauli@libreoffice.org> | 2023-08-14 14:52:41 +0200 |
---|---|---|
committer | Xisco Fauli <xiscofauli@libreoffice.org> | 2023-08-15 03:53:06 +0200 |
commit | 5cfd31e505b4d1b4f9d2e21b0f9f8aac22539f47 (patch) | |
tree | b037f5d6ce2cfc412a75382db71a55249ba49b0c /svgio/source/svgreader | |
parent | 850b66f969834e61f3cb1a1ccd4bfc15f23d558f (diff) |
tdf#93583: use getTextWidth to calculate line's width
Since every character in the line might use different styles
Change-Id: I2ce079d4308f4acde42a8366838749a7c20331b4
Change-Id: I01f51f157caa667cebc8860ae37d4458fac2d511
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155666
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
Diffstat (limited to 'svgio/source/svgreader')
-rw-r--r-- | svgio/source/svgreader/svgcharacternode.cxx | 70 | ||||
-rw-r--r-- | svgio/source/svgreader/svgdocumenthandler.cxx | 24 | ||||
-rw-r--r-- | svgio/source/svgreader/svgtspannode.cxx | 3 |
3 files changed, 62 insertions, 35 deletions
diff --git a/svgio/source/svgreader/svgcharacternode.cxx b/svgio/source/svgreader/svgcharacternode.cxx index 9ba70ffb3ef5..ebc317c3a445 100644 --- a/svgio/source/svgreader/svgcharacternode.cxx +++ b/svgio/source/svgreader/svgcharacternode.cxx @@ -19,7 +19,6 @@ #include <svgcharacternode.hxx> #include <svgstyleattributes.hxx> -#include <drawinglayer/attribute/fontattribute.hxx> #include <drawinglayer/primitive2d/textprimitive2d.hxx> #include <drawinglayer/primitive2d/textlayoutdevice.hxx> #include <drawinglayer/primitive2d/textbreakuphelper.hxx> @@ -100,6 +99,39 @@ namespace svgio::svgreader } } + drawinglayer::attribute::FontAttribute SvgCharacterNode::getFontAttribute( + const SvgStyleAttributes& rSvgStyleAttributes) + { + const SvgStringVector& rFontFamilyVector = rSvgStyleAttributes.getFontFamily(); + OUString aFontFamily("Times New Roman"); + if(!rFontFamilyVector.empty()) + aFontFamily=rFontFamilyVector[0]; + + // #i122324# if the FontFamily name ends on ' embedded' it is probably a re-import + // of a SVG export with font embedding. Remove this to make font matching work. This + // is pretty safe since there should be no font family names ending on ' embedded'. + // Remove again when FontEmbedding is implemented in SVG import + if(aFontFamily.endsWith(" embedded")) + { + aFontFamily = aFontFamily.copy(0, aFontFamily.getLength() - 9); + } + + const ::FontWeight nFontWeight(getVclFontWeight(rSvgStyleAttributes.getFontWeight())); + bool bItalic(FontStyle::italic == rSvgStyleAttributes.getFontStyle() || FontStyle::oblique == rSvgStyleAttributes.getFontStyle()); + + return drawinglayer::attribute::FontAttribute( + aFontFamily, + OUString(), + nFontWeight, + false/*bSymbol*/, + false/*bVertical*/, + bItalic, + false/*bMonospaced*/, + false/*bOutline*/, + false/*bRTL*/, + false/*bBiDiStrong*/); + } + rtl::Reference<BasePrimitive2D> SvgCharacterNode::createSimpleTextPrimitive( SvgTextPosition& rSvgTextPosition, const SvgStyleAttributes& rSvgStyleAttributes) const @@ -111,35 +143,9 @@ namespace svgio::svgreader if(nLength) { sal_uInt32 nIndex(0); - // prepare FontAttribute - const SvgStringVector& rFontFamilyVector = rSvgStyleAttributes.getFontFamily(); - OUString aFontFamily("Times New Roman"); - if(!rFontFamilyVector.empty()) - aFontFamily=rFontFamilyVector[0]; - - // #i122324# if the FontFamily name ends on ' embedded' it is probably a re-import - // of a SVG export with font embedding. Remove this to make font matching work. This - // is pretty safe since there should be no font family names ending on ' embedded'. - // Remove again when FontEmbedding is implemented in SVG import - if(aFontFamily.endsWith(" embedded")) - { - aFontFamily = aFontFamily.copy(0, aFontFamily.getLength() - 9); - } - const ::FontWeight nFontWeight(getVclFontWeight(rSvgStyleAttributes.getFontWeight())); - bool bItalic(FontStyle::italic == rSvgStyleAttributes.getFontStyle() || FontStyle::oblique == rSvgStyleAttributes.getFontStyle()); - - const drawinglayer::attribute::FontAttribute aFontAttribute( - aFontFamily, - OUString(), - nFontWeight, - false/*bSymbol*/, - false/*bVertical*/, - bItalic, - false/*bMonospaced*/, - false/*bOutline*/, - false/*bRTL*/, - false/*bBiDiStrong*/); + // prepare FontAttribute + const drawinglayer::attribute::FontAttribute aFontAttribute(getFontAttribute(rSvgStyleAttributes)); // prepare FontSizeNumber double fFontWidth(rSvgStyleAttributes.getFontSizeNumber().solve(*this)); @@ -250,19 +256,17 @@ namespace svgio::svgreader } } - // Use the whole text line to calculate the align position - double fWholeTextLineWidth(aTextLayouterDevice.getTextWidth(mpParentLine->getTextLine(), 0, mpParentLine->getTextLine().getLength())); // apply TextAlign switch(aTextAlign) { case TextAlign::right: { - aPosition.setX(aPosition.getX() - fWholeTextLineWidth); + aPosition.setX(aPosition.getX() - mpParentLine->getTextLineWith()); break; } case TextAlign::center: { - aPosition.setX(aPosition.getX() - (fWholeTextLineWidth * 0.5)); + aPosition.setX(aPosition.getX() - (mpParentLine->getTextLineWith() * 0.5)); break; } case TextAlign::notset: diff --git a/svgio/source/svgreader/svgdocumenthandler.cxx b/svgio/source/svgreader/svgdocumenthandler.cxx index ea70f3c5cbd6..0b66a4bb44da 100644 --- a/svgio/source/svgreader/svgdocumenthandler.cxx +++ b/svgio/source/svgreader/svgdocumenthandler.cxx @@ -55,6 +55,9 @@ #include <sal/log.hxx> #include <osl/diagnose.h> +#include <com/sun/star/lang/Locale.hpp> +#include <drawinglayer/primitive2d/textlayoutdevice.hxx> + using namespace com::sun::star; namespace svgio::svgreader @@ -87,7 +90,26 @@ namespace pCharNode->whiteSpaceHandling(); pLast = pCharNode->addGap(pLast); - pParentLine->concatenateTextLine(pCharNode->getText()); + double fTextWidth(0.0); + + const SvgStyleAttributes* pSvgStyleAttributes = pCharNode->getSvgStyleAttributes(); + + if(pSvgStyleAttributes) + { + const drawinglayer::attribute::FontAttribute aFontAttribute( + svgio::svgreader::SvgCharacterNode::getFontAttribute(*pSvgStyleAttributes)); + + double fFontWidth(pSvgStyleAttributes->getFontSizeNumber().solve(*pCharNode)); + double fFontHeight(fFontWidth); + + css::lang::Locale aLocale; + + drawinglayer::primitive2d::TextLayouterDevice aTextLayouterDevice; + aTextLayouterDevice.setFontAttribute(aFontAttribute, fFontWidth, fFontHeight, aLocale); + fTextWidth = aTextLayouterDevice.getTextWidth(pCharNode->getText(), 0.0, pCharNode->getText().getLength()); + } + + pParentLine->concatenateTextLineWidth(fTextWidth); break; } case SVGToken::Tspan: diff --git a/svgio/source/svgreader/svgtspannode.cxx b/svgio/source/svgreader/svgtspannode.cxx index 4472b88ab3ad..2ac7ee1118ed 100644 --- a/svgio/source/svgreader/svgtspannode.cxx +++ b/svgio/source/svgreader/svgtspannode.cxx @@ -28,7 +28,8 @@ namespace svgio::svgreader SvgNode* pParent) : SvgNode(aType, rDocument, pParent), maSvgStyleAttributes(*this), - mbLengthAdjust(true) + mbLengthAdjust(true), + mnTextLineWidth(0.0) { } |