Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / svgio / source / svgreader / svgpatternnode.cxx
blob6aa7a7cd63a3b8174461876f942485bcafa18f1c
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 <svgio/svgreader/svgpatternnode.hxx>
21 #include <svgio/svgreader/svgdocument.hxx>
23 //////////////////////////////////////////////////////////////////////////////
25 namespace svgio
27 namespace svgreader
29 void SvgPatternNode::tryToFindLink()
31 if(!mpXLink && maXLink.getLength())
33 mpXLink = dynamic_cast< const SvgPatternNode* >(getDocument().findSvgNodeById(maXLink));
37 SvgPatternNode::SvgPatternNode(
38 SvgDocument& rDocument,
39 SvgNode* pParent)
40 : SvgNode(SVGTokenPattern, rDocument, pParent),
41 aPrimitives(),
42 maSvgStyleAttributes(*this),
43 mpViewBox(0),
44 maSvgAspectRatio(),
45 maX(),
46 maY(),
47 maWidth(),
48 maHeight(),
49 mpPatternUnits(0),
50 mpPatternContentUnits(0),
51 mpaPatternTransform(0),
52 maXLink(),
53 mpXLink(0)
57 SvgPatternNode::~SvgPatternNode()
59 if(mpViewBox) delete mpViewBox;
60 if(mpaPatternTransform) delete mpaPatternTransform;
61 if(mpPatternUnits) delete mpPatternUnits;
62 if(mpPatternContentUnits) delete mpPatternContentUnits;
65 const SvgStyleAttributes* SvgPatternNode::getSvgStyleAttributes() const
67 static rtl::OUString aClassStr(rtl::OUString::createFromAscii("pattern"));
68 maSvgStyleAttributes.checkForCssStyle(aClassStr);
70 return &maSvgStyleAttributes;
73 void SvgPatternNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
75 // call parent
76 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
78 // read style attributes
79 maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
81 // parse own
82 switch(aSVGToken)
84 case SVGTokenStyle:
86 maSvgStyleAttributes.readStyle(aContent);
87 break;
89 case SVGTokenViewBox:
91 const basegfx::B2DRange aRange(readViewBox(aContent, *this));
93 if(!aRange.isEmpty())
95 setViewBox(&aRange);
97 break;
99 case SVGTokenPreserveAspectRatio:
101 setSvgAspectRatio(readSvgAspectRatio(aContent));
102 break;
104 case SVGTokenX:
106 SvgNumber aNum;
108 if(readSingleNumber(aContent, aNum))
110 setX(aNum);
112 break;
114 case SVGTokenY:
116 SvgNumber aNum;
118 if(readSingleNumber(aContent, aNum))
120 setY(aNum);
122 break;
124 case SVGTokenWidth:
126 SvgNumber aNum;
128 if(readSingleNumber(aContent, aNum))
130 if(aNum.isPositive())
132 setWidth(aNum);
135 break;
137 case SVGTokenHeight:
139 SvgNumber aNum;
141 if(readSingleNumber(aContent, aNum))
143 if(aNum.isPositive())
145 setHeight(aNum);
148 break;
150 case SVGTokenPatternUnits:
152 if(aContent.getLength())
154 if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
156 setPatternUnits(userSpaceOnUse);
158 else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
160 setPatternUnits(objectBoundingBox);
163 break;
165 case SVGTokenPatternContentUnits:
167 if(aContent.getLength())
169 if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
171 setPatternContentUnits(userSpaceOnUse);
173 else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
175 setPatternContentUnits(objectBoundingBox);
178 break;
180 case SVGTokenPatternTransform:
182 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
184 if(!aMatrix.isIdentity())
186 setPatternTransform(&aMatrix);
188 break;
190 case SVGTokenXlinkHref:
192 const sal_Int32 nLen(aContent.getLength());
194 if(nLen && sal_Unicode('#') == aContent[0])
196 maXLink = aContent.copy(1);
197 tryToFindLink();
199 break;
201 default:
203 break;
208 void SvgPatternNode::getValuesRelative(double& rfX, double& rfY, double& rfW, double& rfH, const basegfx::B2DRange& rGeoRange, SvgNode& rUser) const
210 double fTargetWidth(rGeoRange.getWidth());
211 double fTargetHeight(rGeoRange.getHeight());
213 if(fTargetWidth > 0.0 && fTargetHeight > 0.0)
215 const SvgUnits aPatternUnits(getPatternUnits() ? *getPatternUnits() : objectBoundingBox);
217 if(objectBoundingBox == aPatternUnits)
219 rfW = (getWidth().isSet()) ? getWidth().getNumber() : 0.0;
220 rfH = (getHeight().isSet()) ? getHeight().getNumber() : 0.0;
222 if(Unit_percent == getWidth().getUnit())
224 rfW *= 0.01;
227 if(Unit_percent == getHeight().getUnit())
229 rfH *= 0.01;
232 else
234 rfW = (getWidth().isSet()) ? getWidth().solve(rUser, xcoordinate) : 0.0;
235 rfH = (getHeight().isSet()) ? getHeight().solve(rUser, ycoordinate) : 0.0;
237 // make relative to rGeoRange
238 rfW /= fTargetWidth;
239 rfH /= fTargetHeight;
242 if(rfW > 0.0 && rfH > 0.0)
244 if(objectBoundingBox == aPatternUnits)
246 rfX = (getX().isSet()) ? getX().getNumber() : 0.0;
247 rfY = (getY().isSet()) ? getY().getNumber() : 0.0;
249 if(Unit_percent == getX().getUnit())
251 rfX *= 0.01;
254 if(Unit_percent == getY().getUnit())
256 rfY *= 0.01;
259 else
261 rfX = (getX().isSet()) ? getX().solve(rUser, xcoordinate) : 0.0;
262 rfY = (getY().isSet()) ? getY().solve(rUser, ycoordinate) : 0.0;
264 // make relative to rGeoRange
265 rfX = (rfX - rGeoRange.getMinX()) / fTargetWidth;
266 rfY = (rfY - rGeoRange.getMinY()) / fTargetHeight;
272 const drawinglayer::primitive2d::Primitive2DSequence& SvgPatternNode::getPatternPrimitives() const
274 if(!aPrimitives.hasElements())
276 decomposeSvgNode(const_cast< SvgPatternNode* >(this)->aPrimitives, true);
279 if(!aPrimitives.hasElements() && maXLink.getLength())
281 const_cast< SvgPatternNode* >(this)->tryToFindLink();
283 if(mpXLink)
285 return mpXLink->getPatternPrimitives();
289 return aPrimitives;
292 const basegfx::B2DRange* SvgPatternNode::getCurrentViewPort() const
294 if(getViewBox())
296 return getViewBox();
298 else
300 return SvgNode::getCurrentViewPort();
304 const basegfx::B2DRange* SvgPatternNode::getViewBox() const
306 if(mpViewBox)
308 return mpViewBox;
311 const_cast< SvgPatternNode* >(this)->tryToFindLink();
313 if(mpXLink)
315 return mpXLink->getViewBox();
318 return 0;
321 const SvgAspectRatio& SvgPatternNode::getSvgAspectRatio() const
323 if(maSvgAspectRatio.isSet())
325 return maSvgAspectRatio;
328 const_cast< SvgPatternNode* >(this)->tryToFindLink();
330 if(mpXLink)
332 return mpXLink->getSvgAspectRatio();
335 return maSvgAspectRatio;
338 const SvgNumber& SvgPatternNode::getX() const
340 if(maX.isSet())
342 return maX;
345 const_cast< SvgPatternNode* >(this)->tryToFindLink();
347 if(mpXLink)
349 return mpXLink->getX();
352 return maX;
355 const SvgNumber& SvgPatternNode::getY() const
357 if(maY.isSet())
359 return maY;
362 const_cast< SvgPatternNode* >(this)->tryToFindLink();
364 if(mpXLink)
366 return mpXLink->getY();
369 return maY;
372 const SvgNumber& SvgPatternNode::getWidth() const
374 if(maWidth.isSet())
376 return maWidth;
379 const_cast< SvgPatternNode* >(this)->tryToFindLink();
381 if(mpXLink)
383 return mpXLink->getWidth();
386 return maWidth;
389 const SvgNumber& SvgPatternNode::getHeight() const
391 if(maHeight.isSet())
393 return maHeight;
396 const_cast< SvgPatternNode* >(this)->tryToFindLink();
398 if(mpXLink)
400 return mpXLink->getHeight();
403 return maHeight;
406 const SvgUnits* SvgPatternNode::getPatternUnits() const
408 if(mpPatternUnits)
410 return mpPatternUnits;
413 const_cast< SvgPatternNode* >(this)->tryToFindLink();
415 if(mpXLink)
417 return mpXLink->getPatternUnits();
420 return 0;
423 const SvgUnits* SvgPatternNode::getPatternContentUnits() const
425 if(mpPatternContentUnits)
427 return mpPatternContentUnits;
430 const_cast< SvgPatternNode* >(this)->tryToFindLink();
432 if(mpXLink)
434 return mpXLink->getPatternContentUnits();
437 return 0;
440 const basegfx::B2DHomMatrix* SvgPatternNode::getPatternTransform() const
442 if(mpaPatternTransform)
444 return mpaPatternTransform;
447 const_cast< SvgPatternNode* >(this)->tryToFindLink();
449 if(mpXLink)
451 return mpXLink->getPatternTransform();
454 return 0;
457 } // end of namespace svgreader
458 } // end of namespace svgio
460 //////////////////////////////////////////////////////////////////////////////
461 // eof
463 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */