bump product version to 7.2.5.1
[LibreOffice.git] / svgio / source / svgreader / svgtextnode.cxx
blobee096712969a391efb263a1586191996111fae27
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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,
32 SvgNode* pParent)
33 : SvgNode(SVGToken::Text, rDocument, pParent),
34 maSvgStyleAttributes(*this),
35 maSvgTextPositions()
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)
50 // call parent
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);
59 // parse own
60 switch(aSVGToken)
62 case SVGToken::Style:
64 readLocalCssStyle(aContent);
65 break;
67 case SVGToken::Transform:
69 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
71 if(!aMatrix.isIdentity())
73 setTransform(&aMatrix);
75 break;
77 default:
79 break;
84 void SvgTextNode::addTextPrimitives(
85 const SvgNode& rCandidate,
86 drawinglayer::primitive2d::Primitive2DContainer& rTarget,
87 drawinglayer::primitive2d::Primitive2DContainer const & rSource)
89 if(rSource.empty())
90 return;
92 const SvgStyleAttributes* pAttributes = rCandidate.getSvgStyleAttributes();
94 if(pAttributes)
96 // add text with taking all Fill/Stroke attributes into account
97 pAttributes->add_text(rTarget, rSource);
99 else
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);
116 break;
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);
140 aNewTarget.clear();
142 // dismantle TextPrimitives and map them on curve/path
143 rSvgTextPathNode.decomposePathNode(aPathContent, aNewTarget, aTextStart);
146 if(!aNewTarget.empty())
148 addTextPrimitives(rCandidate, rTarget, aNewTarget);
152 break;
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());
161 if(nCount)
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);
178 break;
180 case SVGToken::Tref:
182 const SvgTrefNode& rSvgTrefNode = static_cast< const SvgTrefNode& >(rCandidate);
183 const SvgTextNode* pRefText = rSvgTrefNode.getReferencedSvgTextNode();
185 if(pRefText)
187 const auto& rChildren = pRefText->getChildren();
188 const sal_uInt32 nCount(rChildren.size());
189 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
191 if(nCount)
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);
209 break;
211 default:
213 OSL_ENSURE(false, "Unexpected node in text token (!)");
214 break;
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())
226 return;
228 const double fOpacity(pStyle->getOpacity().getNumber());
230 if(fOpacity <= 0.0)
231 return;
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: */