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 <svgtextnode.hxx>
21 #include <svgcharacternode.hxx>
22 #include <svgstyleattributes.hxx>
23 #include <svgtrefnode.hxx>
24 #include <svgtextpathnode.hxx>
25 #include <svgtspannode.hxx>
26 #include <osl/diagnose.h>
28 namespace svgio::svgreader
30 SvgTextNode::SvgTextNode(
31 SvgDocument
& rDocument
,
33 : SvgNode(SVGToken::Text
, rDocument
, pParent
),
34 maSvgStyleAttributes(*this),
39 SvgTextNode::~SvgTextNode()
43 const SvgStyleAttributes
* SvgTextNode::getSvgStyleAttributes() const
45 return checkForCssStyle("text", maSvgStyleAttributes
);
48 void SvgTextNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
51 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
53 // read style attributes
54 maSvgStyleAttributes
.parseStyleAttribute(aSVGToken
, aContent
, false);
56 // read text position attributes
57 maSvgTextPositions
.parseTextPositionAttributes(aSVGToken
, aContent
);
64 readLocalCssStyle(aContent
);
67 case SVGToken::Transform
:
69 const basegfx::B2DHomMatrix
aMatrix(readTransform(aContent
, *this));
71 if(!aMatrix
.isIdentity())
73 setTransform(&aMatrix
);
84 void SvgTextNode::addTextPrimitives(
85 const SvgNode
& rCandidate
,
86 drawinglayer::primitive2d::Primitive2DContainer
& rTarget
,
87 drawinglayer::primitive2d::Primitive2DContainer
const & rSource
)
92 const SvgStyleAttributes
* pAttributes
= rCandidate
.getSvgStyleAttributes();
96 // add text with taking all Fill/Stroke attributes into account
97 pAttributes
->add_text(rTarget
, rSource
);
101 // should not happen, every subnode from SvgTextNode will at least
102 // return the attributes from SvgTextNode. Nonetheless, add text
103 rTarget
.append(rSource
);
107 void SvgTextNode::DecomposeChild(const SvgNode
& rCandidate
, drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, SvgTextPosition
& rSvgTextPosition
) const
109 switch(rCandidate
.getType())
111 case SVGToken::Character
:
113 // direct SvgTextPathNode derivates, decompose them
114 const SvgCharacterNode
& rSvgCharacterNode
= static_cast< const SvgCharacterNode
& >(rCandidate
);
115 rSvgCharacterNode
.decomposeText(rTarget
, rSvgTextPosition
);
118 case SVGToken::TextPath
:
120 // direct TextPath decompose
121 const SvgTextPathNode
& rSvgTextPathNode
= static_cast< const SvgTextPathNode
& >(rCandidate
);
122 const auto& rChildren
= rSvgTextPathNode
.getChildren();
123 const sal_uInt32
nCount(rChildren
.size());
125 if(nCount
&& rSvgTextPathNode
.isValid())
127 // remember original TextStart to later detect hor/ver offsets
128 const basegfx::B2DPoint
aTextStart(rSvgTextPosition
.getPosition());
129 drawinglayer::primitive2d::Primitive2DContainer aNewTarget
;
131 // decompose to regular TextPrimitives
132 for(sal_uInt32
a(0); a
< nCount
; a
++)
134 DecomposeChild(*rChildren
[a
], aNewTarget
, rSvgTextPosition
);
137 if(!aNewTarget
.empty())
139 const drawinglayer::primitive2d::Primitive2DContainer
aPathContent(aNewTarget
);
142 // dismantle TextPrimitives and map them on curve/path
143 rSvgTextPathNode
.decomposePathNode(aPathContent
, aNewTarget
, aTextStart
);
146 if(!aNewTarget
.empty())
148 addTextPrimitives(rCandidate
, rTarget
, aNewTarget
);
154 case SVGToken::Tspan
:
156 // Tspan may have children, call recursively
157 const SvgTspanNode
& rSvgTspanNode
= static_cast< const SvgTspanNode
& >(rCandidate
);
158 const auto& rChildren
= rSvgTspanNode
.getChildren();
159 const sal_uInt32
nCount(rChildren
.size());
163 SvgTextPosition
aSvgTextPosition(&rSvgTextPosition
, rSvgTspanNode
, rSvgTspanNode
.getSvgTextPositions());
164 drawinglayer::primitive2d::Primitive2DContainer aNewTarget
;
166 for(sal_uInt32
a(0); a
< nCount
; a
++)
168 DecomposeChild(*rChildren
[a
], aNewTarget
, aSvgTextPosition
);
171 rSvgTextPosition
.setPosition(aSvgTextPosition
.getPosition());
173 if(!aNewTarget
.empty())
175 addTextPrimitives(rCandidate
, rTarget
, aNewTarget
);
182 const SvgTrefNode
& rSvgTrefNode
= static_cast< const SvgTrefNode
& >(rCandidate
);
183 const SvgTextNode
* pRefText
= rSvgTrefNode
.getReferencedSvgTextNode();
187 const auto& rChildren
= pRefText
->getChildren();
188 const sal_uInt32
nCount(rChildren
.size());
189 drawinglayer::primitive2d::Primitive2DContainer aNewTarget
;
193 for(sal_uInt32
a(0); a
< nCount
; a
++)
195 const SvgNode
& rChildCandidate
= *rChildren
[a
];
196 const_cast< SvgNode
& >(rChildCandidate
).setAlternativeParent(this);
198 DecomposeChild(rChildCandidate
, aNewTarget
, rSvgTextPosition
);
199 const_cast< SvgNode
& >(rChildCandidate
).setAlternativeParent();
202 if(!aNewTarget
.empty())
204 addTextPrimitives(rCandidate
, rTarget
, aNewTarget
);
213 OSL_ENSURE(false, "Unexpected node in text token (!)");
219 void SvgTextNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, bool /*bReferenced`*/) const
221 // text has a group of child nodes, allowed are SVGToken::Character, SVGToken::Tspan,
222 // SVGToken::Tref and SVGToken::TextPath. These increase a given current text position
223 const SvgStyleAttributes
* pStyle
= getSvgStyleAttributes();
225 if(!pStyle
|| getChildren().empty())
228 const double fOpacity(pStyle
->getOpacity().getNumber());
233 SvgTextPosition
aSvgTextPosition(nullptr, *this, maSvgTextPositions
);
234 drawinglayer::primitive2d::Primitive2DContainer aNewTarget
;
235 const auto& rChildren
= getChildren();
236 const sal_uInt32
nCount(rChildren
.size());
238 for(sal_uInt32
a(0); a
< nCount
; a
++)
240 const SvgNode
& rCandidate
= *rChildren
[a
];
242 DecomposeChild(rCandidate
, aNewTarget
, aSvgTextPosition
);
245 if(!aNewTarget
.empty())
247 drawinglayer::primitive2d::Primitive2DContainer aNewTarget2
;
249 addTextPrimitives(*this, aNewTarget2
, aNewTarget
);
250 aNewTarget
= aNewTarget2
;
253 if(!aNewTarget
.empty())
255 pStyle
->add_postProcess(rTarget
, aNewTarget
, getTransform());
259 double SvgTextNode::getCurrentFontSize() const
261 return getCurrentFontSizeInherited();
263 } // end of namespace svgio::svgreader
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */