calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / svgio / source / svgreader / svgtextnode.cxx
blobf8c0694b88265aade46bce9c55f7512700e3217e
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)
38 SvgTextNode::~SvgTextNode()
42 const SvgStyleAttributes* SvgTextNode::getSvgStyleAttributes() const
44 return checkForCssStyle("text", maSvgStyleAttributes);
47 void SvgTextNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
49 // call parent
50 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
52 // read style attributes
53 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
55 // read text position attributes
56 maSvgTextPositions.parseTextPositionAttributes(aSVGToken, aContent);
58 // parse own
59 switch(aSVGToken)
61 case SVGToken::Style:
63 readLocalCssStyle(aContent);
64 break;
66 case SVGToken::Transform:
68 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
70 if(!aMatrix.isIdentity())
72 setTransform(aMatrix);
74 break;
76 default:
78 break;
83 void SvgTextNode::addTextPrimitives(
84 const SvgNode& rCandidate,
85 drawinglayer::primitive2d::Primitive2DContainer& rTarget,
86 drawinglayer::primitive2d::Primitive2DContainer&& rSource)
88 if(rSource.empty())
89 return;
91 const SvgStyleAttributes* pAttributes = rCandidate.getSvgStyleAttributes();
93 if(pAttributes)
95 // add text with taking all Fill/Stroke attributes into account
96 pAttributes->add_text(rTarget, std::move(rSource));
98 else
100 // should not happen, every subnode from SvgTextNode will at least
101 // return the attributes from SvgTextNode. Nonetheless, add text
102 rTarget.append(std::move(rSource));
106 void SvgTextNode::DecomposeChild(const SvgNode& rCandidate, drawinglayer::primitive2d::Primitive2DContainer& rTarget, SvgTextPosition& rSvgTextPosition) const
108 switch(rCandidate.getType())
110 case SVGToken::Character:
112 // direct SvgTextPathNode derivates, decompose them
113 const SvgCharacterNode& rSvgCharacterNode = static_cast< const SvgCharacterNode& >(rCandidate);
114 rSvgCharacterNode.decomposeText(rTarget, rSvgTextPosition);
115 break;
117 case SVGToken::TextPath:
119 // direct TextPath decompose
120 const SvgTextPathNode& rSvgTextPathNode = static_cast< const SvgTextPathNode& >(rCandidate);
121 const auto& rChildren = rSvgTextPathNode.getChildren();
122 const sal_uInt32 nCount(rChildren.size());
124 if(nCount && rSvgTextPathNode.isValid())
126 // remember original TextStart to later detect hor/ver offsets
127 const basegfx::B2DPoint aTextStart(rSvgTextPosition.getPosition());
128 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
130 // decompose to regular TextPrimitives
131 for(sal_uInt32 a(0); a < nCount; a++)
133 DecomposeChild(*rChildren[a], aNewTarget, rSvgTextPosition);
136 if(!aNewTarget.empty())
138 const drawinglayer::primitive2d::Primitive2DContainer aPathContent(aNewTarget);
139 aNewTarget.clear();
141 // dismantle TextPrimitives and map them on curve/path
142 rSvgTextPathNode.decomposePathNode(aPathContent, aNewTarget, aTextStart);
145 if(!aNewTarget.empty())
147 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
151 break;
153 case SVGToken::Tspan:
155 // Tspan may have children, call recursively
156 const SvgTspanNode& rSvgTspanNode = static_cast< const SvgTspanNode& >(rCandidate);
157 const auto& rChildren = rSvgTspanNode.getChildren();
158 const sal_uInt32 nCount(rChildren.size());
160 if(nCount)
162 SvgTextPosition aSvgTextPosition(&rSvgTextPosition, rSvgTspanNode, rSvgTspanNode.getSvgTextPositions());
163 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
165 for(sal_uInt32 a(0); a < nCount; a++)
167 DecomposeChild(*rChildren[a], aNewTarget, aSvgTextPosition);
170 rSvgTextPosition.setPosition(aSvgTextPosition.getPosition());
172 if(!aNewTarget.empty())
174 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
177 break;
179 case SVGToken::Tref:
181 const SvgTrefNode& rSvgTrefNode = static_cast< const SvgTrefNode& >(rCandidate);
182 const SvgTextNode* pRefText = rSvgTrefNode.getReferencedSvgTextNode();
184 if(pRefText)
186 const auto& rChildren = pRefText->getChildren();
187 const sal_uInt32 nCount(rChildren.size());
188 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
190 if(nCount)
192 for(sal_uInt32 a(0); a < nCount; a++)
194 const SvgNode& rChildCandidate = *rChildren[a];
195 const_cast< SvgNode& >(rChildCandidate).setAlternativeParent(this);
197 DecomposeChild(rChildCandidate, aNewTarget, rSvgTextPosition);
198 const_cast< SvgNode& >(rChildCandidate).setAlternativeParent();
201 if(!aNewTarget.empty())
203 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
208 break;
210 default:
212 OSL_ENSURE(false, "Unexpected node in text token (!)");
213 break;
218 void SvgTextNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool /*bReferenced`*/) const
220 // text has a group of child nodes, allowed are SVGToken::Character, SVGToken::Tspan,
221 // SVGToken::Tref and SVGToken::TextPath. These increase a given current text position
222 const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
224 if(!pStyle || getChildren().empty())
225 return;
227 const double fOpacity(pStyle->getOpacity().getNumber());
229 if(fOpacity <= 0.0)
230 return;
232 SvgTextPosition aSvgTextPosition(nullptr, *this, maSvgTextPositions);
233 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
234 const auto& rChildren = getChildren();
235 const sal_uInt32 nCount(rChildren.size());
237 for(sal_uInt32 a(0); a < nCount; a++)
239 const SvgNode& rCandidate = *rChildren[a];
241 DecomposeChild(rCandidate, aNewTarget, aSvgTextPosition);
244 if(!aNewTarget.empty())
246 drawinglayer::primitive2d::Primitive2DContainer aNewTarget2;
248 addTextPrimitives(*this, aNewTarget2, std::move(aNewTarget));
249 aNewTarget = aNewTarget2;
252 if(!aNewTarget.empty())
254 pStyle->add_postProcess(rTarget, std::move(aNewTarget), getTransform());
258 } // end of namespace svgio::svgreader
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */