LibreOffice Module svgio (master) 1
svgtextnode.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <svgtextnode.hxx>
21#include <svgcharacternode.hxx>
23#include <svgtrefnode.hxx>
24#include <svgtextpathnode.hxx>
25#include <svgtspannode.hxx>
26#include <osl/diagnose.h>
27
28namespace svgio::svgreader
29{
31 SvgDocument& rDocument,
32 SvgNode* pParent)
33 : SvgNode(SVGToken::Text, rDocument, pParent),
34 maSvgStyleAttributes(*this)
35 {
36 }
37
39 {
40 }
41
43 {
45 }
46
47 void SvgTextNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
48 {
49 // call parent
50 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
51
52 // read style attributes
53 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
54
55 // read text position attributes
57
58 // parse own
59 switch(aSVGToken)
60 {
61 case SVGToken::Style:
62 {
63 readLocalCssStyle(aContent);
64 break;
65 }
67 {
68 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
69
70 if(!aMatrix.isIdentity())
71 {
72 setTransform(aMatrix);
73 }
74 break;
75 }
76 default:
77 {
78 break;
79 }
80 }
81 }
82
84 const SvgNode& rCandidate,
87 {
88 if(rSource.empty())
89 return;
90
91 const SvgStyleAttributes* pAttributes = rCandidate.getSvgStyleAttributes();
92
93 if(pAttributes)
94 {
95 // add text with taking all Fill/Stroke attributes into account
96 pAttributes->add_text(rTarget, std::move(rSource));
97 }
98 else
99 {
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));
103 }
104 }
105
107 {
108 switch(rCandidate.getType())
109 {
111 {
112 // direct SvgTextPathNode derivates, decompose them
113 const SvgCharacterNode& rSvgCharacterNode = static_cast< const SvgCharacterNode& >(rCandidate);
114 rSvgCharacterNode.decomposeText(rTarget, rSvgTextPosition);
115 break;
116 }
118 {
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());
123
124 if(nCount && rSvgTextPathNode.isValid())
125 {
126 // remember original TextStart to later detect hor/ver offsets
127 const basegfx::B2DPoint aTextStart(rSvgTextPosition.getPosition());
129
130 // decompose to regular TextPrimitives
131 for(sal_uInt32 a(0); a < nCount; a++)
132 {
133 DecomposeChild(*rChildren[a], aNewTarget, rSvgTextPosition);
134 }
135
136 if(!aNewTarget.empty())
137 {
138 const drawinglayer::primitive2d::Primitive2DContainer aPathContent(aNewTarget);
139 aNewTarget.clear();
140
141 // dismantle TextPrimitives and map them on curve/path
142 rSvgTextPathNode.decomposePathNode(aPathContent, aNewTarget, aTextStart);
143 }
144
145 if(!aNewTarget.empty())
146 {
147 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
148 }
149 }
150
151 break;
152 }
153 case SVGToken::Tspan:
154 {
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());
159
160 if(nCount)
161 {
162 SvgTextPosition aSvgTextPosition(&rSvgTextPosition, rSvgTspanNode, rSvgTspanNode.getSvgTextPositions());
164
165 for(sal_uInt32 a(0); a < nCount; a++)
166 {
167 DecomposeChild(*rChildren[a], aNewTarget, aSvgTextPosition);
168 }
169
170 rSvgTextPosition.setPosition(aSvgTextPosition.getPosition());
171
172 if(!aNewTarget.empty())
173 {
174 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
175 }
176 }
177 break;
178 }
179 case SVGToken::Tref:
180 {
181 const SvgTrefNode& rSvgTrefNode = static_cast< const SvgTrefNode& >(rCandidate);
182 const SvgTextNode* pRefText = rSvgTrefNode.getReferencedSvgTextNode();
183
184 if(pRefText)
185 {
186 const auto& rChildren = pRefText->getChildren();
187 const sal_uInt32 nCount(rChildren.size());
189
190 if(nCount)
191 {
192 for(sal_uInt32 a(0); a < nCount; a++)
193 {
194 const SvgNode& rChildCandidate = *rChildren[a];
195 const_cast< SvgNode& >(rChildCandidate).setAlternativeParent(this);
196
197 DecomposeChild(rChildCandidate, aNewTarget, rSvgTextPosition);
198 const_cast< SvgNode& >(rChildCandidate).setAlternativeParent();
199 }
200
201 if(!aNewTarget.empty())
202 {
203 addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
204 }
205 }
206 }
207
208 break;
209 }
210 default:
211 {
212 OSL_ENSURE(false, "Unexpected node in text token (!)");
213 break;
214 }
215 }
216 }
217
219 {
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
223
224 if(!pStyle || getChildren().empty())
225 return;
226
227 const double fOpacity(pStyle->getOpacity().getNumber());
228
229 if(fOpacity <= 0.0)
230 return;
231
232 SvgTextPosition aSvgTextPosition(nullptr, *this, maSvgTextPositions);
234 const auto& rChildren = getChildren();
235 const sal_uInt32 nCount(rChildren.size());
236
237 for(sal_uInt32 a(0); a < nCount; a++)
238 {
239 const SvgNode& rCandidate = *rChildren[a];
240
241 DecomposeChild(rCandidate, aNewTarget, aSvgTextPosition);
242 }
243
244 if(!aNewTarget.empty())
245 {
247
248 addTextPrimitives(*this, aNewTarget2, std::move(aNewTarget));
249 aNewTarget = aNewTarget2;
250 }
251
252 if(!aNewTarget.empty())
253 {
254 pStyle->add_postProcess(rTarget, std::move(aNewTarget), getTransform());
255 }
256 }
257
258} // end of namespace svgio::svgreader
259
260/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool isIdentity() const
void decomposeText(drawinglayer::primitive2d::Primitive2DContainer &rTarget, SvgTextPosition &rSvgTextPosition) const
void setAlternativeParent(const SvgNode *pAlternativeParent=nullptr)
alternative parent
Definition: svgnode.hxx:186
const std::vector< std::unique_ptr< SvgNode > > & getChildren() const
Definition: svgnode.hxx:159
SVGToken getType() const
basic data read access
Definition: svgnode.hxx:156
const SvgStyleAttributes * checkForCssStyle(const SvgStyleAttributes &rOriginal) const
helper to evtl. link to css style
Definition: svgnode.cxx:335
virtual void parseAttribute(const OUString &rTokenName, SVGToken aSVGToken, const OUString &aContent)
Definition: svgnode.cxx:534
void readLocalCssStyle(std::u16string_view aContent)
scan helper to read and interpret a local CssStyle to mpLocalCssStyle
Definition: svgnode.cxx:412
virtual const SvgStyleAttributes * getSvgStyleAttributes() const
Definition: svgnode.cxx:37
double getNumber() const
Definition: SvgNumber.hxx:85
SvgNumber getOpacity() const
Opacity content.
void add_postProcess(drawinglayer::primitive2d::Primitive2DContainer &rTarget, drawinglayer::primitive2d::Primitive2DContainer &&rSource, const std::optional< basegfx::B2DHomMatrix > &pTransform) const
void parseStyleAttribute(SVGToken aSVGToken, const OUString &rContent)
local attribute scanner
void add_text(drawinglayer::primitive2d::Primitive2DContainer &rTarget, drawinglayer::primitive2d::Primitive2DContainer &&rSource) const
helper which does the necessary with a given path
static void addTextPrimitives(const SvgNode &rCandidate, drawinglayer::primitive2d::Primitive2DContainer &rTarget, drawinglayer::primitive2d::Primitive2DContainer &&rSource)
Definition: svgtextnode.cxx:83
virtual void decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer &rTarget, bool bReferenced) const override
virtual ~SvgTextNode() override
Definition: svgtextnode.cxx:38
void DecomposeChild(const SvgNode &rCandidate, drawinglayer::primitive2d::Primitive2DContainer &rTarget, SvgTextPosition &rSvgTextPosition) const
local helpers
void setTransform(const std::optional< basegfx::B2DHomMatrix > &pMatrix)
Definition: svgtextnode.hxx:62
SvgTextNode(SvgDocument &rDocument, SvgNode *pParent)
Definition: svgtextnode.cxx:30
SvgTextPositions maSvgTextPositions
Definition: svgtextnode.hxx:38
virtual const SvgStyleAttributes * getSvgStyleAttributes() const override
Definition: svgtextnode.cxx:42
SvgStyleAttributes maSvgStyleAttributes
use styles
Definition: svgtextnode.hxx:33
virtual void parseAttribute(const OUString &rTokenName, SVGToken aSVGToken, const OUString &aContent) override
Definition: svgtextnode.cxx:47
const std::optional< basegfx::B2DHomMatrix > & getTransform() const
transform content, set if found in current context
Definition: svgtextnode.hxx:61
void decomposePathNode(const drawinglayer::primitive2d::Primitive2DContainer &rPathContent, drawinglayer::primitive2d::Primitive2DContainer &rTarget, const basegfx::B2DPoint &rTextStart) const
void setPosition(const basegfx::B2DPoint &rNew)
const basegfx::B2DPoint & getPosition() const
void parseTextPositionAttributes(SVGToken aSVGToken, std::u16string_view aContent)
const SvgTextNode * getReferencedSvgTextNode() const
access to referenced SvgTextNode
Definition: svgtrefnode.cxx:71
const SvgTextPositions & getSvgTextPositions() const
access to SvgTextPositions
int nCount
FilterGroup & rTarget
uno_Any a
basegfx::B2DHomMatrix readTransform(std::u16string_view rCandidate, InfoProvider const &rInfoProvider)
Definition: svgtools.cxx:871