1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svgcharacternode.hxx>
21 #include <svgstyleattributes.hxx>
22 #include <drawinglayer/attribute/fontattribute.hxx>
23 #include <drawinglayer/primitive2d/textprimitive2d.hxx>
24 #include <drawinglayer/primitive2d/textlayoutdevice.hxx>
25 #include <drawinglayer/primitive2d/textbreakuphelper.hxx>
26 #include <drawinglayer/primitive2d/textdecoratedprimitive2d.hxx>
28 namespace svgio::svgreader
30 SvgTextPositions::SvgTextPositions()
41 void SvgTextPositions::parseTextPositionAttributes(SVGToken aSVGToken
, const OUString
& aContent
)
48 if(!aContent
.isEmpty())
50 SvgNumberVector aVector
;
52 if(readSvgNumberVector(aContent
, aVector
))
61 if(!aContent
.isEmpty())
63 SvgNumberVector aVector
;
65 if(readSvgNumberVector(aContent
, aVector
))
74 if(!aContent
.isEmpty())
76 SvgNumberVector aVector
;
78 if(readSvgNumberVector(aContent
, aVector
))
87 if(!aContent
.isEmpty())
89 SvgNumberVector aVector
;
91 if(readSvgNumberVector(aContent
, aVector
))
100 if(!aContent
.isEmpty())
102 SvgNumberVector aVector
;
104 if(readSvgNumberVector(aContent
, aVector
))
111 case SVGTokenTextLength
:
115 if(readSingleNumber(aContent
, aNum
))
117 if(aNum
.isPositive())
124 case SVGTokenLengthAdjust
:
126 if(!aContent
.isEmpty())
128 if(aContent
.startsWith("spacing"))
130 setLengthAdjust(true);
132 else if(aContent
.startsWith("spacingAndGlyphs"))
134 setLengthAdjust(false);
146 } // end of namespace svgio::svgreader
149 namespace svgio::svgreader
153 class localTextBreakupHelper
: public drawinglayer::primitive2d::TextBreakupHelper
156 SvgTextPosition
& mrSvgTextPosition
;
159 /// allow user callback to allow changes to the new TextTransformation. Default
161 virtual bool allowChange(sal_uInt32 nCount
, basegfx::B2DHomMatrix
& rNewTransform
, sal_uInt32 nIndex
, sal_uInt32 nLength
) override
;
164 localTextBreakupHelper(
165 const drawinglayer::primitive2d::TextSimplePortionPrimitive2D
& rSource
,
166 SvgTextPosition
& rSvgTextPosition
)
167 : drawinglayer::primitive2d::TextBreakupHelper(rSource
),
168 mrSvgTextPosition(rSvgTextPosition
)
175 bool localTextBreakupHelper::allowChange(sal_uInt32
/*nCount*/, basegfx::B2DHomMatrix
& rNewTransform
, sal_uInt32
/*nIndex*/, sal_uInt32
/*nLength*/)
177 const double fRotation(mrSvgTextPosition
.consumeRotation());
181 const basegfx::B2DPoint
aBasePoint(rNewTransform
* basegfx::B2DPoint(0.0, 0.0));
183 rNewTransform
.translate(-aBasePoint
.getX(), -aBasePoint
.getY());
184 rNewTransform
.rotate(fRotation
);
185 rNewTransform
.translate(aBasePoint
.getX(), aBasePoint
.getY());
191 } // end of namespace svgio::svgreader
194 namespace svgio::svgreader
196 SvgCharacterNode::SvgCharacterNode(
197 SvgDocument
& rDocument
,
199 const OUString
& rText
)
200 : SvgNode(SVGTokenCharacter
, rDocument
, pParent
),
205 SvgCharacterNode::~SvgCharacterNode()
209 const SvgStyleAttributes
* SvgCharacterNode::getSvgStyleAttributes() const
211 // no own style, use parent's
214 return getParent()->getSvgStyleAttributes();
222 drawinglayer::primitive2d::TextSimplePortionPrimitive2D
* SvgCharacterNode::createSimpleTextPrimitive(
223 SvgTextPosition
& rSvgTextPosition
,
224 const SvgStyleAttributes
& rSvgStyleAttributes
) const
226 // prepare retval, index and length
227 drawinglayer::primitive2d::TextSimplePortionPrimitive2D
* pRetval
= nullptr;
228 sal_uInt32
nLength(getText().getLength());
232 sal_uInt32
nIndex(0);
233 // prepare FontAttribute
234 const SvgStringVector
& rFontFamilyVector
= rSvgStyleAttributes
.getFontFamily();
235 OUString aFontFamily
= rFontFamilyVector
.empty() ?
236 OUString("Times New Roman") :
237 rFontFamilyVector
[0];
239 // #i122324# if the FontFamily name ends on ' embedded' it is probably a re-import
240 // of a SVG export with font embedding. Remove this to make font matching work. This
241 // is pretty safe since there should be no font family names ending on ' embedded'.
242 // Remove again when FontEmbedding is implemented in SVG import
243 if(aFontFamily
.endsWith(" embedded"))
245 aFontFamily
= aFontFamily
.copy(0, aFontFamily
.getLength() - 9);
248 const ::FontWeight
nFontWeight(getVclFontWeight(rSvgStyleAttributes
.getFontWeight()));
249 bool bItalic(FontStyle_italic
== rSvgStyleAttributes
.getFontStyle() || FontStyle_oblique
== rSvgStyleAttributes
.getFontStyle());
251 const drawinglayer::attribute::FontAttribute
aFontAttribute(
258 false/*bMonospaced*/,
261 false/*bBiDiStrong*/);
263 // prepare FontSizeNumber
264 double fFontWidth(rSvgStyleAttributes
.getFontSizeNumber().solve(*this));
265 double fFontHeight(fFontWidth
);
268 css::lang::Locale aLocale
;
270 // prepare TextLayouterDevice
271 drawinglayer::primitive2d::TextLayouterDevice aTextLayouterDevice
;
272 aTextLayouterDevice
.setFontAttribute(aFontAttribute
, fFontWidth
, fFontHeight
, aLocale
);
275 ::std::vector
< double > aTextArray(rSvgTextPosition
.getX());
277 if(!aTextArray
.empty() && aTextArray
.size() < nLength
)
279 const sal_uInt32
nArray(aTextArray
.size());
285 if(rSvgTextPosition
.getParent() && rSvgTextPosition
.getParent()->getAbsoluteX())
287 fStartX
= rSvgTextPosition
.getParent()->getPosition().getX();
291 fStartX
= aTextArray
[nArray
- 1];
294 ::std::vector
< double > aExtendArray(aTextLayouterDevice
.getTextArray(getText(), nArray
, nLength
- nArray
));
295 aTextArray
.reserve(nLength
);
297 for(size_t a(0); a
< aExtendArray
.size(); a
++)
299 aTextArray
.push_back(aExtendArray
[a
] + fStartX
);
304 // get current TextPosition and TextWidth in units
305 basegfx::B2DPoint
aPosition(rSvgTextPosition
.getPosition());
306 double fTextWidth(aTextLayouterDevice
.getTextWidth(getText(), nIndex
, nLength
));
308 // check for user-given TextLength
309 if(0.0 != rSvgTextPosition
.getTextLength()
310 && !basegfx::fTools::equal(fTextWidth
, rSvgTextPosition
.getTextLength()))
312 const double fFactor(rSvgTextPosition
.getTextLength() / fTextWidth
);
314 if(rSvgTextPosition
.getLengthAdjust())
316 // spacing, need to create and expand TextArray
317 if(aTextArray
.empty())
319 aTextArray
= aTextLayouterDevice
.getTextArray(getText(), nIndex
, nLength
);
322 for(size_t a(0); a
< aTextArray
.size(); a
++)
324 aTextArray
[a
] *= fFactor
;
329 // spacing and glyphs, just apply to FontWidth
330 fFontWidth
*= fFactor
;
333 fTextWidth
= rSvgTextPosition
.getTextLength();
337 TextAlign
aTextAlign(rSvgStyleAttributes
.getTextAlign());
339 // map TextAnchor to TextAlign, there seems not to be a difference
340 if(TextAnchor_notset
!= rSvgStyleAttributes
.getTextAnchor())
342 switch(rSvgStyleAttributes
.getTextAnchor())
344 case TextAnchor_start
:
346 aTextAlign
= TextAlign_left
;
349 case TextAnchor_middle
:
351 aTextAlign
= TextAlign_center
;
356 aTextAlign
= TextAlign_right
;
369 case TextAlign_right
:
371 aPosition
.setX(aPosition
.getX() - fTextWidth
);
374 case TextAlign_center
:
376 aPosition
.setX(aPosition
.getX() - (fTextWidth
* 0.5));
379 case TextAlign_notset
:
381 case TextAlign_justify
:
383 // TextAlign_notset, TextAlign_left: nothing to do
384 // TextAlign_justify is not clear currently; handle as TextAlign_left
390 const BaselineShift
aBaselineShift(rSvgStyleAttributes
.getBaselineShift());
392 // apply BaselineShift
393 switch(aBaselineShift
)
395 case BaselineShift_Sub
:
397 aPosition
.setY(aPosition
.getY() + aTextLayouterDevice
.getUnderlineOffset());
400 case BaselineShift_Super
:
402 aPosition
.setY(aPosition
.getY() + aTextLayouterDevice
.getOverlineOffset());
405 case BaselineShift_Percentage
:
406 case BaselineShift_Length
:
408 const SvgNumber
aNumber(rSvgStyleAttributes
.getBaselineShiftNumber());
409 const double mfBaselineShift(aNumber
.solve(*this));
411 aPosition
.setY(aPosition
.getY() + mfBaselineShift
);
414 default: // BaselineShift_Baseline
422 const basegfx::BColor
aFill(rSvgStyleAttributes
.getFill()
423 ? *rSvgStyleAttributes
.getFill()
424 : basegfx::BColor(0.0, 0.0, 0.0));
426 // prepare TextTransformation
427 basegfx::B2DHomMatrix aTextTransform
;
429 aTextTransform
.scale(fFontWidth
, fFontHeight
);
430 aTextTransform
.translate(aPosition
.getX(), aPosition
.getY());
432 // check TextDecoration and if TextDecoratedPortionPrimitive2D is needed
433 const TextDecoration
aDeco(rSvgStyleAttributes
.getTextDecoration());
435 if(TextDecoration_underline
== aDeco
436 || TextDecoration_overline
== aDeco
437 || TextDecoration_line_through
== aDeco
)
439 // get the fill for decoration as described by SVG. We cannot
440 // have different stroke colors/definitions for those, though
441 const SvgStyleAttributes
* pDecoDef
= rSvgStyleAttributes
.getTextDecorationDefiningSvgStyleAttributes();
442 const basegfx::BColor
aDecoColor(pDecoDef
&& pDecoDef
->getFill() ? *pDecoDef
->getFill() : aFill
);
444 // create decorated text primitive
445 pRetval
= new drawinglayer::primitive2d::TextDecoratedPortionPrimitive2D(
456 // extra props for decorated
459 TextDecoration_overline
== aDeco
? drawinglayer::primitive2d::TEXT_LINE_SINGLE
: drawinglayer::primitive2d::TEXT_LINE_NONE
,
460 TextDecoration_underline
== aDeco
? drawinglayer::primitive2d::TEXT_LINE_SINGLE
: drawinglayer::primitive2d::TEXT_LINE_NONE
,
462 TextDecoration_line_through
== aDeco
? drawinglayer::primitive2d::TEXT_STRIKEOUT_SINGLE
: drawinglayer::primitive2d::TEXT_STRIKEOUT_NONE
,
464 drawinglayer::primitive2d::TEXT_FONT_EMPHASIS_MARK_NONE
,
467 drawinglayer::primitive2d::TEXT_RELIEF_NONE
,
472 // create text primitive
473 pRetval
= new drawinglayer::primitive2d::TextSimplePortionPrimitive2D(
484 // advance current TextPosition
485 rSvgTextPosition
.setPosition(rSvgTextPosition
.getPosition() + basegfx::B2DVector(fTextWidth
, 0.0));
491 void SvgCharacterNode::decomposeTextWithStyle(
492 drawinglayer::primitive2d::Primitive2DContainer
& rTarget
,
493 SvgTextPosition
& rSvgTextPosition
,
494 const SvgStyleAttributes
& rSvgStyleAttributes
) const
496 const drawinglayer::primitive2d::Primitive2DReference
xRef(
497 createSimpleTextPrimitive(
499 rSvgStyleAttributes
));
501 if(!(xRef
.is() && (Visibility_visible
== rSvgStyleAttributes
.getVisibility())))
504 if(!rSvgTextPosition
.isRotated())
506 rTarget
.push_back(xRef
);
510 // need to apply rotations to each character as given
511 const drawinglayer::primitive2d::TextSimplePortionPrimitive2D
* pCandidate
=
512 dynamic_cast< const drawinglayer::primitive2d::TextSimplePortionPrimitive2D
* >(xRef
.get());
516 const localTextBreakupHelper
alocalTextBreakupHelper(*pCandidate
, rSvgTextPosition
);
517 const drawinglayer::primitive2d::Primitive2DContainer
& aResult(
518 alocalTextBreakupHelper
.getResult());
522 rTarget
.append(aResult
);
525 // also consume for the implied single space
526 rSvgTextPosition
.consumeRotation();
530 OSL_ENSURE(false, "Used primitive is not a text primitive (!)");
535 void SvgCharacterNode::whiteSpaceHandling()
537 if(XmlSpace_default
== getXmlSpace())
539 maText
= whiteSpaceHandlingDefault(maText
);
543 maText
= whiteSpaceHandlingPreserve(maText
);
547 void SvgCharacterNode::addGap()
552 void SvgCharacterNode::concatenate(const OUString
& rText
)
557 void SvgCharacterNode::decomposeText(drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, SvgTextPosition
& rSvgTextPosition
) const
559 if(!getText().isEmpty())
561 const SvgStyleAttributes
* pSvgStyleAttributes
= getSvgStyleAttributes();
563 if(pSvgStyleAttributes
)
565 decomposeTextWithStyle(rTarget
, rSvgTextPosition
, *pSvgStyleAttributes
);
570 } // end of namespace svgio::svgreader
573 namespace svgio::svgreader
575 SvgTextPosition::SvgTextPosition(
576 SvgTextPosition
* pParent
,
577 const InfoProvider
& rInfoProvider
,
578 const SvgTextPositions
& rSvgTextPositions
)
580 maX(), // computed below
581 maY(), // computed below
582 maRotate(solveSvgNumberVector(rSvgTextPositions
.getRotate(), rInfoProvider
)),
584 maPosition(), // computed below
586 mbLengthAdjust(rSvgTextPositions
.getLengthAdjust()),
589 // get TextLength if provided
590 if(rSvgTextPositions
.getTextLength().isSet())
592 mfTextLength
= rSvgTextPositions
.getTextLength().solve(rInfoProvider
);
595 // SVG does not really define in which units a \91rotate\92 for Text/TSpan is given,
596 // but it seems to be degrees. Convert here to radians
597 if(!maRotate
.empty())
599 for (double& f
: maRotate
)
601 f
= basegfx::deg2rad(f
);
605 // get text positions X
606 const sal_uInt32
nSizeX(rSvgTextPositions
.getX().size());
610 // we have absolute positions, get first one as current text position X
611 maPosition
.setX(rSvgTextPositions
.getX()[0].solve(rInfoProvider
, xcoordinate
));
616 // fill deltas to maX
619 for(sal_uInt32
a(1); a
< nSizeX
; a
++)
621 maX
.push_back(rSvgTextPositions
.getX()[a
].solve(rInfoProvider
, xcoordinate
) - maPosition
.getX());
627 // no absolute position, get from parent
630 maPosition
.setX(pParent
->getPosition().getX());
633 const sal_uInt32
nSizeDx(rSvgTextPositions
.getDx().size());
637 // relative positions given, translate position derived from parent
638 maPosition
.setX(maPosition
.getX() + rSvgTextPositions
.getDx()[0].solve(rInfoProvider
, xcoordinate
));
642 // fill deltas to maX
643 maX
.reserve(nSizeDx
);
645 for(sal_uInt32
a(1); a
< nSizeDx
; a
++)
647 maX
.push_back(rSvgTextPositions
.getDx()[a
].solve(rInfoProvider
, xcoordinate
));
653 // get text positions Y
654 const sal_uInt32
nSizeY(rSvgTextPositions
.getY().size());
658 // we have absolute positions, get first one as current text position Y
659 maPosition
.setY(rSvgTextPositions
.getY()[0].solve(rInfoProvider
, ycoordinate
));
664 // fill deltas to maY
667 for(sal_uInt32
a(1); a
< nSizeY
; a
++)
669 maY
.push_back(rSvgTextPositions
.getY()[a
].solve(rInfoProvider
, ycoordinate
) - maPosition
.getY());
675 // no absolute position, get from parent
678 maPosition
.setY(pParent
->getPosition().getY());
681 const sal_uInt32
nSizeDy(rSvgTextPositions
.getDy().size());
685 // relative positions given, translate position derived from parent
686 maPosition
.setY(maPosition
.getY() + rSvgTextPositions
.getDy()[0].solve(rInfoProvider
, ycoordinate
));
690 // fill deltas to maY
691 maY
.reserve(nSizeDy
);
693 for(sal_uInt32
a(1); a
< nSizeDy
; a
++)
695 maY
.push_back(rSvgTextPositions
.getDy()[a
].solve(rInfoProvider
, ycoordinate
));
702 bool SvgTextPosition::isRotated() const
708 return getParent()->isRotated();
721 double SvgTextPosition::consumeRotation()
729 fRetval
= mpParent
->consumeRotation();
738 const sal_uInt32
nSize(maRotate
.size());
740 if(mnRotationIndex
< nSize
)
742 fRetval
= maRotate
[mnRotationIndex
++];
746 fRetval
= maRotate
[nSize
- 1];
753 } // end of namespace svgio
755 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */