Updated core
[LibreOffice.git] / svgio / source / svgreader / svgpatternnode.cxx
blob229a7c6246a3c4bc478ac584cf5229580d658bdf
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 return checkForCssStyle(aClassStr, maSvgStyleAttributes);
71 void SvgPatternNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
73 // call parent
74 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
76 // read style attributes
77 maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
79 // parse own
80 switch(aSVGToken)
82 case SVGTokenStyle:
84 maSvgStyleAttributes.readStyle(aContent);
85 break;
87 case SVGTokenViewBox:
89 const basegfx::B2DRange aRange(readViewBox(aContent, *this));
91 if(!aRange.isEmpty())
93 setViewBox(&aRange);
95 break;
97 case SVGTokenPreserveAspectRatio:
99 setSvgAspectRatio(readSvgAspectRatio(aContent));
100 break;
102 case SVGTokenX:
104 SvgNumber aNum;
106 if(readSingleNumber(aContent, aNum))
108 setX(aNum);
110 break;
112 case SVGTokenY:
114 SvgNumber aNum;
116 if(readSingleNumber(aContent, aNum))
118 setY(aNum);
120 break;
122 case SVGTokenWidth:
124 SvgNumber aNum;
126 if(readSingleNumber(aContent, aNum))
128 if(aNum.isPositive())
130 setWidth(aNum);
133 break;
135 case SVGTokenHeight:
137 SvgNumber aNum;
139 if(readSingleNumber(aContent, aNum))
141 if(aNum.isPositive())
143 setHeight(aNum);
146 break;
148 case SVGTokenPatternUnits:
150 if(aContent.getLength())
152 if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
154 setPatternUnits(userSpaceOnUse);
156 else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
158 setPatternUnits(objectBoundingBox);
161 break;
163 case SVGTokenPatternContentUnits:
165 if(aContent.getLength())
167 if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
169 setPatternContentUnits(userSpaceOnUse);
171 else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
173 setPatternContentUnits(objectBoundingBox);
176 break;
178 case SVGTokenPatternTransform:
180 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
182 if(!aMatrix.isIdentity())
184 setPatternTransform(&aMatrix);
186 break;
188 case SVGTokenXlinkHref:
190 const sal_Int32 nLen(aContent.getLength());
192 if(nLen && sal_Unicode('#') == aContent[0])
194 maXLink = aContent.copy(1);
195 tryToFindLink();
197 break;
199 default:
201 break;
206 void SvgPatternNode::getValuesRelative(double& rfX, double& rfY, double& rfW, double& rfH, const basegfx::B2DRange& rGeoRange, SvgNode& rUser) const
208 double fTargetWidth(rGeoRange.getWidth());
209 double fTargetHeight(rGeoRange.getHeight());
211 if(fTargetWidth > 0.0 && fTargetHeight > 0.0)
213 const SvgUnits aPatternUnits(getPatternUnits() ? *getPatternUnits() : objectBoundingBox);
215 if(objectBoundingBox == aPatternUnits)
217 rfW = (getWidth().isSet()) ? getWidth().getNumber() : 0.0;
218 rfH = (getHeight().isSet()) ? getHeight().getNumber() : 0.0;
220 if(Unit_percent == getWidth().getUnit())
222 rfW *= 0.01;
225 if(Unit_percent == getHeight().getUnit())
227 rfH *= 0.01;
230 else
232 rfW = (getWidth().isSet()) ? getWidth().solve(rUser, xcoordinate) : 0.0;
233 rfH = (getHeight().isSet()) ? getHeight().solve(rUser, ycoordinate) : 0.0;
235 // make relative to rGeoRange
236 rfW /= fTargetWidth;
237 rfH /= fTargetHeight;
240 if(rfW > 0.0 && rfH > 0.0)
242 if(objectBoundingBox == aPatternUnits)
244 rfX = (getX().isSet()) ? getX().getNumber() : 0.0;
245 rfY = (getY().isSet()) ? getY().getNumber() : 0.0;
247 if(Unit_percent == getX().getUnit())
249 rfX *= 0.01;
252 if(Unit_percent == getY().getUnit())
254 rfY *= 0.01;
257 else
259 rfX = (getX().isSet()) ? getX().solve(rUser, xcoordinate) : 0.0;
260 rfY = (getY().isSet()) ? getY().solve(rUser, ycoordinate) : 0.0;
262 // make relative to rGeoRange
263 rfX = (rfX - rGeoRange.getMinX()) / fTargetWidth;
264 rfY = (rfY - rGeoRange.getMinY()) / fTargetHeight;
270 const drawinglayer::primitive2d::Primitive2DSequence& SvgPatternNode::getPatternPrimitives() const
272 if(!aPrimitives.hasElements())
274 decomposeSvgNode(const_cast< SvgPatternNode* >(this)->aPrimitives, true);
277 if(!aPrimitives.hasElements() && maXLink.getLength())
279 const_cast< SvgPatternNode* >(this)->tryToFindLink();
281 if(mpXLink)
283 return mpXLink->getPatternPrimitives();
287 return aPrimitives;
290 const basegfx::B2DRange* SvgPatternNode::getCurrentViewPort() const
292 if(getViewBox())
294 return getViewBox();
296 else
298 return SvgNode::getCurrentViewPort();
302 const basegfx::B2DRange* SvgPatternNode::getViewBox() const
304 if(mpViewBox)
306 return mpViewBox;
309 const_cast< SvgPatternNode* >(this)->tryToFindLink();
311 if(mpXLink)
313 return mpXLink->getViewBox();
316 return 0;
319 const SvgAspectRatio& SvgPatternNode::getSvgAspectRatio() const
321 if(maSvgAspectRatio.isSet())
323 return maSvgAspectRatio;
326 const_cast< SvgPatternNode* >(this)->tryToFindLink();
328 if(mpXLink)
330 return mpXLink->getSvgAspectRatio();
333 return maSvgAspectRatio;
336 const SvgNumber& SvgPatternNode::getX() const
338 if(maX.isSet())
340 return maX;
343 const_cast< SvgPatternNode* >(this)->tryToFindLink();
345 if(mpXLink)
347 return mpXLink->getX();
350 return maX;
353 const SvgNumber& SvgPatternNode::getY() const
355 if(maY.isSet())
357 return maY;
360 const_cast< SvgPatternNode* >(this)->tryToFindLink();
362 if(mpXLink)
364 return mpXLink->getY();
367 return maY;
370 const SvgNumber& SvgPatternNode::getWidth() const
372 if(maWidth.isSet())
374 return maWidth;
377 const_cast< SvgPatternNode* >(this)->tryToFindLink();
379 if(mpXLink)
381 return mpXLink->getWidth();
384 return maWidth;
387 const SvgNumber& SvgPatternNode::getHeight() const
389 if(maHeight.isSet())
391 return maHeight;
394 const_cast< SvgPatternNode* >(this)->tryToFindLink();
396 if(mpXLink)
398 return mpXLink->getHeight();
401 return maHeight;
404 const SvgUnits* SvgPatternNode::getPatternUnits() const
406 if(mpPatternUnits)
408 return mpPatternUnits;
411 const_cast< SvgPatternNode* >(this)->tryToFindLink();
413 if(mpXLink)
415 return mpXLink->getPatternUnits();
418 return 0;
421 const SvgUnits* SvgPatternNode::getPatternContentUnits() const
423 if(mpPatternContentUnits)
425 return mpPatternContentUnits;
428 const_cast< SvgPatternNode* >(this)->tryToFindLink();
430 if(mpXLink)
432 return mpXLink->getPatternContentUnits();
435 return 0;
438 const basegfx::B2DHomMatrix* SvgPatternNode::getPatternTransform() const
440 if(mpaPatternTransform)
442 return mpaPatternTransform;
445 const_cast< SvgPatternNode* >(this)->tryToFindLink();
447 if(mpXLink)
449 return mpXLink->getPatternTransform();
452 return 0;
455 } // end of namespace svgreader
456 } // end of namespace svgio
458 //////////////////////////////////////////////////////////////////////////////
459 // eof
461 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */