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