pyuno: call target only with internal python
[LibreOffice.git] / svgio / source / svgreader / svgtextnode.cxx
blob38f80a86e0f389ff8b398bbb15217353316294d5
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 : SvgTspanNode(SVGToken::Text, rDocument, pParent)
37 SvgTextNode::~SvgTextNode()
41 void SvgTextNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent)
43 // call parent
44 SvgTspanNode::parseAttribute(aSVGToken, aContent);
46 // parse own
47 switch(aSVGToken)
49 case SVGToken::Transform:
51 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
53 if(!aMatrix.isIdentity())
55 setTransform(aMatrix);
57 break;
59 default:
61 break;
66 void SvgTextNode::addTextPrimitives(
67 const SvgNode& rCandidate,
68 drawinglayer::primitive2d::Primitive2DContainer& rTarget,
69 drawinglayer::primitive2d::Primitive2DContainer&& rSource)
71 if(rSource.empty())
72 return;
74 const SvgStyleAttributes* pAttributes = rCandidate.getSvgStyleAttributes();
76 if(pAttributes)
78 // add text with taking all Fill/Stroke attributes into account
79 pAttributes->add_text(rTarget, std::move(rSource));
81 else
83 // should not happen, every subnode from SvgTextNode will at least
84 // return the attributes from SvgTextNode. Nonetheless, add text
85 rTarget.append(std::move(rSource));
89 void SvgTextNode::DecomposeChild(const SvgNode& rCandidate, drawinglayer::primitive2d::Primitive2DContainer& rTarget, SvgTextPosition& rSvgTextPosition) const
91 switch(rCandidate.getType())
93 case SVGToken::Character:
95 // direct SvgTextPathNode derivates, decompose them
96 const SvgCharacterNode& rSvgCharacterNode = static_cast< const SvgCharacterNode& >(rCandidate);
97 rSvgCharacterNode.decomposeText(rTarget, rSvgTextPosition);
98 break;
100 case SVGToken::TextPath:
102 // direct TextPath decompose
103 const SvgTextPathNode& rSvgTextPathNode = static_cast< const SvgTextPathNode& >(rCandidate);
104 const auto& rChildren = rSvgTextPathNode.getChildren();
105 const sal_uInt32 nCount(rChildren.size());
107 if(nCount && rSvgTextPathNode.isValid())
109 // remember original TextStart to later detect hor/ver offsets
110 const basegfx::B2DPoint aTextStart(rSvgTextPosition.getPosition());
111 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
113 // decompose to regular TextPrimitives
114 for(sal_uInt32 a(0); a < nCount; a++)
116 DecomposeChild(*rChildren[a], aNewTarget, rSvgTextPosition);
119 if(!aNewTarget.empty())
121 const drawinglayer::primitive2d::Primitive2DContainer aPathContent(aNewTarget);
122 aNewTarget.clear();
124 // dismantle TextPrimitives and map them on curve/path
125 rSvgTextPathNode.decomposePathNode(aPathContent, aNewTarget, aTextStart);
128 if(!aNewTarget.empty())
130 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
134 break;
136 case SVGToken::Tspan:
138 // Tspan may have children, call recursively
139 const SvgTspanNode& rSvgTspanNode = static_cast< const SvgTspanNode& >(rCandidate);
140 const auto& rChildren = rSvgTspanNode.getChildren();
141 const sal_uInt32 nCount(rChildren.size());
143 if(nCount)
145 SvgTextPosition aSvgTextPosition(&rSvgTextPosition, rSvgTspanNode);
146 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
148 for(sal_uInt32 a(0); a < nCount; a++)
150 DecomposeChild(*rChildren[a], aNewTarget, aSvgTextPosition);
153 rSvgTextPosition.setPosition(aSvgTextPosition.getPosition());
155 if(!aNewTarget.empty())
157 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
160 break;
162 case SVGToken::Tref:
164 const SvgTrefNode& rSvgTrefNode = static_cast< const SvgTrefNode& >(rCandidate);
165 const SvgTextNode* pRefText = rSvgTrefNode.getReferencedSvgTextNode();
167 if(pRefText)
169 const auto& rChildren = pRefText->getChildren();
170 const sal_uInt32 nCount(rChildren.size());
171 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
173 if(nCount)
175 for(sal_uInt32 a(0); a < nCount; a++)
177 const SvgNode& rChildCandidate = *rChildren[a];
178 const_cast< SvgNode& >(rChildCandidate).setAlternativeParent(this);
180 DecomposeChild(rChildCandidate, aNewTarget, rSvgTextPosition);
181 const_cast< SvgNode& >(rChildCandidate).setAlternativeParent();
184 if(!aNewTarget.empty())
186 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
191 break;
193 default:
195 OSL_ENSURE(false, "Unexpected node in text token (!)");
196 break;
201 void SvgTextNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool /*bReferenced`*/) const
203 // text has a group of child nodes, allowed are SVGToken::Character, SVGToken::Tspan,
204 // SVGToken::Tref and SVGToken::TextPath. These increase a given current text position
205 const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
207 if(!pStyle || getChildren().empty())
208 return;
210 const double fOpacity(pStyle->getOpacity().getNumber());
212 if(fOpacity <= 0.0)
213 return;
215 SvgTextPosition aSvgTextPosition(nullptr, *this);
216 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
217 const auto& rChildren = getChildren();
218 const sal_uInt32 nCount(rChildren.size());
220 for(sal_uInt32 a(0); a < nCount; a++)
222 const SvgNode& rCandidate = *rChildren[a];
224 DecomposeChild(rCandidate, aNewTarget, aSvgTextPosition);
227 if(!aNewTarget.empty())
229 drawinglayer::primitive2d::Primitive2DContainer aNewTarget2;
231 addTextPrimitives(*this, aNewTarget2, std::move(aNewTarget));
232 aNewTarget = std::move(aNewTarget2);
235 if(!aNewTarget.empty())
237 pStyle->add_postProcess(rTarget, std::move(aNewTarget), getTransform());
241 } // end of namespace svgio::svgreader
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */