Line data Source code
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 <svgio/svgreader/svgtextnode.hxx>
21 : #include <svgio/svgreader/svgcharacternode.hxx>
22 : #include <svgio/svgreader/svgstyleattributes.hxx>
23 : #include <svgio/svgreader/svgtrefnode.hxx>
24 : #include <svgio/svgreader/svgtextpathnode.hxx>
25 : #include <svgio/svgreader/svgtspannode.hxx>
26 : #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
27 : #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
28 :
29 : //////////////////////////////////////////////////////////////////////////////
30 :
31 : namespace svgio
32 : {
33 : namespace svgreader
34 : {
35 0 : SvgTextNode::SvgTextNode(
36 : SvgDocument& rDocument,
37 : SvgNode* pParent)
38 : : SvgNode(SVGTokenText, rDocument, pParent),
39 : maSvgStyleAttributes(*this),
40 : mpaTransform(0),
41 0 : maSvgTextPositions()
42 : {
43 0 : }
44 :
45 0 : SvgTextNode::~SvgTextNode()
46 : {
47 0 : if(mpaTransform) delete mpaTransform;
48 0 : }
49 :
50 0 : const SvgStyleAttributes* SvgTextNode::getSvgStyleAttributes() const
51 : {
52 0 : static rtl::OUString aClassStr(rtl::OUString::createFromAscii("text"));
53 0 : maSvgStyleAttributes.checkForCssStyle(aClassStr);
54 :
55 0 : return &maSvgStyleAttributes;
56 : }
57 :
58 0 : void SvgTextNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
59 : {
60 : // call parent
61 0 : SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
62 :
63 : // read style attributes
64 0 : maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
65 :
66 : // read text position attributes
67 0 : maSvgTextPositions.parseTextPositionAttributes(rTokenName, aSVGToken, aContent);
68 :
69 : // parse own
70 0 : switch(aSVGToken)
71 : {
72 : case SVGTokenStyle:
73 : {
74 0 : maSvgStyleAttributes.readStyle(aContent);
75 0 : break;
76 : }
77 : case SVGTokenTransform:
78 : {
79 0 : const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
80 :
81 0 : if(!aMatrix.isIdentity())
82 : {
83 0 : setTransform(&aMatrix);
84 : }
85 0 : break;
86 : }
87 : default:
88 : {
89 0 : break;
90 : }
91 : }
92 0 : }
93 :
94 0 : void SvgTextNode::addTextPrimitives(
95 : const SvgNode& rCandidate,
96 : drawinglayer::primitive2d::Primitive2DSequence& rTarget,
97 : drawinglayer::primitive2d::Primitive2DSequence& rSource) const
98 : {
99 0 : if(rSource.hasElements())
100 : {
101 0 : const SvgStyleAttributes* pAttributes = rCandidate.getSvgStyleAttributes();
102 :
103 0 : if(pAttributes)
104 : {
105 : // add text with taking all Fill/Stroke attributes into account
106 0 : pAttributes->add_text(rTarget, rSource);
107 : }
108 : else
109 : {
110 : // should not happen, every subnode from SvgTextNode will at least
111 : // return the attributes from SvgTextNode. Nonetheless, add text
112 0 : drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, rSource);
113 : }
114 : }
115 0 : }
116 :
117 0 : void SvgTextNode::DecomposeChild(const SvgNode& rCandidate, drawinglayer::primitive2d::Primitive2DSequence& rTarget, SvgTextPosition& rSvgTextPosition) const
118 : {
119 0 : switch(rCandidate.getType())
120 : {
121 : case SVGTokenCharacter:
122 : {
123 : // direct SvgTextPathNode derivates, decompose them
124 0 : const SvgCharacterNode& rSvgCharacterNode = static_cast< const SvgCharacterNode& >(rCandidate);
125 0 : rSvgCharacterNode.decomposeText(rTarget, rSvgTextPosition);
126 0 : break;
127 : }
128 : case SVGTokenTextPath:
129 : {
130 : // direct TextPath decompose
131 0 : const SvgTextPathNode& rSvgTextPathNode = static_cast< const SvgTextPathNode& >(rCandidate);
132 0 : const SvgNodeVector& rChildren = rSvgTextPathNode.getChildren();
133 0 : const sal_uInt32 nCount(rChildren.size());
134 :
135 0 : if(nCount && rSvgTextPathNode.isValid())
136 : {
137 : // remember original TextStart to later detect hor/ver offsets
138 0 : const basegfx::B2DPoint aTextStart(rSvgTextPosition.getPosition());
139 0 : drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
140 :
141 : // decompose to regular TextPrimitives
142 0 : for(sal_uInt32 a(0); a < nCount; a++)
143 : {
144 0 : DecomposeChild(*rChildren[a], aNewTarget, rSvgTextPosition);
145 : }
146 :
147 0 : if(aNewTarget.hasElements())
148 : {
149 0 : const drawinglayer::primitive2d::Primitive2DSequence aPathContent(aNewTarget);
150 0 : aNewTarget.realloc(0);
151 :
152 : // dismantle TextPrimitives and map them on curve/path
153 0 : rSvgTextPathNode.decomposePathNode(aPathContent, aNewTarget, aTextStart);
154 : }
155 :
156 0 : if(aNewTarget.hasElements())
157 : {
158 0 : addTextPrimitives(rCandidate, rTarget, aNewTarget);
159 0 : }
160 : }
161 :
162 0 : break;
163 : }
164 : case SVGTokenTspan:
165 : {
166 : // Tspan may have children, call recursively
167 0 : const SvgTspanNode& rSvgTspanNode = static_cast< const SvgTspanNode& >(rCandidate);
168 0 : const SvgNodeVector& rChildren = rSvgTspanNode.getChildren();
169 0 : const sal_uInt32 nCount(rChildren.size());
170 :
171 0 : if(nCount)
172 : {
173 0 : SvgTextPosition aSvgTextPosition(&rSvgTextPosition, rSvgTspanNode, rSvgTspanNode.getSvgTextPositions());
174 0 : drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
175 :
176 0 : for(sal_uInt32 a(0); a < nCount; a++)
177 : {
178 0 : DecomposeChild(*rChildren[a], aNewTarget, aSvgTextPosition);
179 : }
180 :
181 0 : rSvgTextPosition.setPosition(aSvgTextPosition.getPosition());
182 :
183 0 : if(aNewTarget.hasElements())
184 : {
185 0 : addTextPrimitives(rCandidate, rTarget, aNewTarget);
186 0 : }
187 : }
188 0 : break;
189 : }
190 : case SVGTokenTref:
191 : {
192 0 : const SvgTrefNode& rSvgTrefNode = static_cast< const SvgTrefNode& >(rCandidate);
193 0 : const SvgTextNode* pRefText = rSvgTrefNode.getReferencedSvgTextNode();
194 :
195 0 : if(pRefText)
196 : {
197 0 : const SvgNodeVector& rChildren = pRefText->getChildren();
198 0 : const sal_uInt32 nCount(rChildren.size());
199 0 : drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
200 :
201 0 : if(nCount)
202 : {
203 0 : for(sal_uInt32 a(0); a < nCount; a++)
204 : {
205 0 : const SvgNode& rChildCandidate = *rChildren[a];
206 0 : const_cast< SvgNode& >(rChildCandidate).setAlternativeParent(this);
207 :
208 0 : DecomposeChild(rChildCandidate, aNewTarget, rSvgTextPosition);
209 0 : const_cast< SvgNode& >(rChildCandidate).setAlternativeParent(0);
210 : }
211 :
212 0 : if(aNewTarget.hasElements())
213 : {
214 0 : addTextPrimitives(rCandidate, rTarget, aNewTarget);
215 : }
216 0 : }
217 : }
218 :
219 0 : break;
220 : }
221 : default:
222 : {
223 : OSL_ENSURE(false, "Unexpected node in text token (!)");
224 0 : break;
225 : }
226 : }
227 0 : }
228 :
229 0 : void SvgTextNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool /*bReferenced`*/) const
230 : {
231 : // text has a group of child nodes, allowed are SVGTokenCharacter, SVGTokenTspan,
232 : // SVGTokenTref and SVGTokenTextPath. These increase a given current text position
233 0 : const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
234 :
235 0 : if(pStyle && !getChildren().empty())
236 : {
237 0 : const double fOpacity(pStyle->getOpacity().getNumber());
238 :
239 0 : if(fOpacity > 0.0)
240 : {
241 0 : SvgTextPosition aSvgTextPosition(0, *this, getSvgTextPositions());
242 0 : drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
243 0 : const SvgNodeVector& rChildren = getChildren();
244 0 : const sal_uInt32 nCount(rChildren.size());
245 :
246 0 : for(sal_uInt32 a(0); a < nCount; a++)
247 : {
248 0 : const SvgNode& rCandidate = *rChildren[a];
249 :
250 0 : DecomposeChild(rCandidate, aNewTarget, aSvgTextPosition);
251 : }
252 :
253 0 : if(aNewTarget.hasElements())
254 : {
255 0 : drawinglayer::primitive2d::Primitive2DSequence aNewTarget2;
256 :
257 0 : addTextPrimitives(*this, aNewTarget2, aNewTarget);
258 0 : aNewTarget = aNewTarget2;
259 : }
260 :
261 0 : if(aNewTarget.hasElements())
262 : {
263 0 : pStyle->add_postProcess(rTarget, aNewTarget, getTransform());
264 0 : }
265 : }
266 : }
267 0 : }
268 : } // end of namespace svgreader
269 : } // end of namespace svgio
270 :
271 : //////////////////////////////////////////////////////////////////////////////
272 : // eof
273 :
274 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|